summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialListUserRestrictions.php
blob: 27b242982298621dc6bdaea90657dba79c48cdff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

function wfSpecialListUserRestrictions() {
	global $wgOut, $wgRequest;
	
	$wgOut->addWikiMsg( 'listuserrestrictions-intro' );
	$f = new SpecialListUserRestrictionsForm();
	$wgOut->addHTML( $f->getHTML() );

	if( !mt_rand( 0, 10 ) )
		UserRestriction::purgeExpired();
	$pager = new UserRestrictionsPager( $f->getConds() );
	if( $pager->getNumRows() ) 
		$wgOut->addHTML( $pager->getNavigationBar() .
			Xml::tags( 'ul', null, $pager->getBody() ) .
			$pager->getNavigationBar()
		);
	elseif( $f->getConds() )
		$wgOut->addWikiMsg( 'listuserrestrictions-notfound' );
	else 
		$wgOut->addWikiMsg( 'listuserrestrictions-empty' );
}

class SpecialListUserRestrictionsForm {
	public function getHTML() {
		global $wgRequest, $wgScript, $wgTitle;
		$s = '';
		$s .= Xml::fieldset( wfMsg( 'listuserrestrictions-legend' ) );
		$s .= "<form action=\"{$wgScript}\">";
		$s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
		$s .= Xml::label( wfMsgHtml( 'listuserrestrictions-type' ), 'type' ) . '&nbsp;' .
			self::typeSelector( 'type', $wgRequest->getVal( 'type' ), 'type' );
		$s .= '&nbsp;';
		$s .= Xml::inputLabel( wfMsgHtml( 'listuserrestrictions-user' ), 'user', 'user',
			false, $wgRequest->getVal( 'user' ) );
		$s .= '<p>';
		$s .= Xml::label( wfMsgHtml( 'listuserrestrictions-namespace' ), 'namespace' ) . '&nbsp;' .
			Xml::namespaceSelector( $wgRequest->getVal( 'namespace' ), '', 'namespace' );
		$s .= '&nbsp;';
		$s .= Xml::inputLabel( wfMsgHtml( 'listuserrestrictions-page' ), 'page', 'page',
			false, $wgRequest->getVal( 'page' ) );
		$s .= Xml::submitButton( wfMsg( 'listuserrestrictions-submit' ) );
		$s .= "</p></form></fieldset>";
		return $s;
	}

	public static function typeSelector( $name = 'type', $value = '', $id = false ) {
		$s = new XmlSelect( $name, $id, $value );
		$s->addOption( wfMsg( 'userrestrictiontype-none' ), '' );
		$s->addOption( wfMsg( 'userrestrictiontype-page' ), UserRestriction::PAGE );
		$s->addOption( wfMsg( 'userrestrictiontype-namespace' ), UserRestriction::NAMESPACE );
		return $s->getHTML();
	}

	public function getConds() {
		global $wgRequest;
		$conds = array();

		$type = $wgRequest->getVal( 'type' );
		if( in_array( $type, array( UserRestriction::PAGE, UserRestriction::NAMESPACE ) ) )
			$conds['ur_type'] = $type;

		$user = $wgRequest->getVal( 'user' );
		if( $user )
			$conds['ur_user_text'] = $user;

		$namespace = $wgRequest->getVal( 'namespace' );
		if( $namespace || $namespace === '0' )
			$conds['ur_namespace'] = $namespace;

		$page = $wgRequest->getVal( 'page' );
		$title = Title::newFromText( $page );
		if( $title ) {
			$conds['ur_page_namespace'] = $title->getNamespace();
			$conds['ur_page_title'] = $title->getDBKey();
		}

		return $conds;
	}
}

class UserRestrictionsPager extends ReverseChronologicalPager {
	public $mConds;

	public function __construct( $conds = array() ) {
		$this->mConds = $conds;
		parent::__construct();
	}

	public function getStartBody() {
		# Copied from Special:Ipblocklist
		wfProfileIn( __METHOD__ );
		# Do a link batch query
		$this->mResult->seek( 0 );
		$lb = new LinkBatch;

		# Faster way
		# Usernames and titles are in fact related by a simple substitution of space -> underscore
		# The last few lines of Title::secureAndSplit() tell the story.
		foreach( $this->mResult as $row ) {
			$name = str_replace( ' ', '_', $row->ur_by_text );
			$lb->add( NS_USER, $name );
			$lb->add( NS_USER_TALK, $name );
			$name = str_replace( ' ', '_', $row->ur_user_text );
			$lb->add( NS_USER, $name );
			$lb->add( NS_USER_TALK, $name );
			if( $row->ur_type == UserRestriction::PAGE )
				$lb->add( $row->ur_page_namespace, $row->ur_page_title );
		}
		$lb->execute();
		wfProfileOut( __METHOD__ );
		return '';
	}

	public function getQueryInfo() {
		return array(
			'tables' => 'user_restrictions',
			'fields' => '*',
			'conds' => $this->mConds,
		);
	}

	public function formatRow( $row ) {
		return self::formatRestriction( UserRestriction::newFromRow( $row )  );
	}

	// Split off for use on Special:RestrictUser
	public static function formatRestriction( $r ) {
		global $wgUser, $wgLang;
		$sk = $wgUser->getSkin();
		$timestamp = $wgLang->timeanddate( $r->getTimestamp(), true );
		$blockerlink = $sk->userLink( $r->getBlockerId(), $r->getBlockerText() ) .
			$sk->userToolLinks( $r->getBlockerId(), $r->getBlockerText() );
		$subjlink = $sk->userLink( $r->getSubjectId(), $r->getSubjectText() ) .
			$sk->userToolLinks( $r->getSubjectId(), $r->getSubjectText() );
		$expiry = is_numeric( $r->getExpiry() ) ?
			wfMsg( 'listuserrestrictions-row-expiry', $wgLang->timeanddate( $r->getExpiry() ) ) :
			wfMsg( 'ipbinfinite' );
		$msg = '';
		if( $r->isNamespace() ) {
			$msg = wfMsgHtml( 'listuserrestrictions-row-ns', $subjlink,
				$wgLang->getDisplayNsText( $r->getNamespace() ), $expiry );
		}
		if( $r->isPage() ) {
			$pagelink = $sk->link( $r->getPage() );
			$msg = wfMsgHtml( 'listuserrestrictions-row-page', $subjlink,
				$pagelink, $expiry );
		}
		$reason = $sk->commentBlock( $r->getReason() );
		$removelink = '';
		if( $wgUser->isAllowed( 'restrict' ) ) {
			$removelink = '(' . $sk->link( SpecialPage::getTitleFor( 'RemoveRestrictions' ),
				wfMsgHtml( 'listuserrestrictions-remove' ), array(), array( 'id' => $r->getId() ) ) . ')';
		}
		return "<li>{$timestamp}, {$blockerlink} {$msg} {$reason} {$removelink}</li>\n";
	}

	public function getIndexField() {
		return 'ur_timestamp';
	}
}