*Use is_null() for namespace
[lhc/web/wiklou.git] / includes / SpecialProtectedpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 * @addtogroup SpecialPage
10 */
11 class ProtectedPagesForm {
12 function showList( $msg = '' ) {
13 global $wgOut, $wgRequest;
14
15 $wgOut->setPagetitle( wfMsg( "protectedpages" ) );
16 if ( "" != $msg ) {
17 $wgOut->setSubtitle( $msg );
18 }
19
20 // Purge expired entries on one in every 10 queries
21 if ( !mt_rand( 0, 10 ) ) {
22 Title::purgeExpiredRestrictions();
23 }
24
25 $type = $wgRequest->getVal( 'type' );
26 $level = $wgRequest->getVal( 'level' );
27 $NS = $wgRequest->getIntOrNull( 'namespace' );
28
29 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS );
30
31 $wgOut->addHTML( $this->showOptions( $NS, $type, $level ) );
32
33 if ( $pager->getNumRows() ) {
34 $s = $pager->getNavigationBar();
35 $s .= "<ul>" .
36 $pager->getBody() .
37 "</ul>";
38 $s .= $pager->getNavigationBar();
39 } else {
40 $s = '<p>' . wfMsgHTML( 'protectedpagesempty' ) . '</p>';
41 }
42 $wgOut->addHTML( $s );
43 }
44
45 /**
46 * Callback function to output a restriction
47 */
48 function formatRow( $row ) {
49 global $wgUser, $wgLang;
50
51 wfProfileIn( __METHOD__ );
52
53 static $skin=null;
54
55 if( is_null( $skin ) )
56 $skin = $wgUser->getSkin();
57
58 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
59 $link = $skin->makeLinkObj( $title );
60
61 $description_items = array ();
62
63 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
64
65 $description_items[] = $protType;
66
67 $expiry_description = '';
68
69 if ( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
70 $expiry = Block::decodeExpiry( $row->pr_expiry );
71
72 $expiry_description = wfMsgForContent( 'protect-expiring', $wgLang->timeanddate( $expiry ) );
73
74 $description_items[] = $expiry_description;
75 }
76
77 wfProfileOut( __METHOD__ );
78
79 return '<li>' . wfSpecialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
80 }
81
82 /**
83 * @param $namespace int
84 * @param $type string
85 * @param $level string
86 * @private
87 */
88 function showOptions( $namespace, $type, $level ) {
89 global $wgScript;
90 $action = htmlspecialchars( $wgScript );
91 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
92 $special = htmlspecialchars( $title->getPrefixedDBkey() );
93 return "<form action=\"$action\" method=\"get\">\n" .
94 '<fieldset>' .
95 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
96 Xml::hidden( 'title', $special ) . "\n" .
97 $this->getNamespaceMenu( $namespace ) . "\n" .
98 $this->getTypeMenu( $type ) . "\n" .
99 $this->getLevelMenu( $level ) . "\n" .
100 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
101 "</fieldset></form>";
102 }
103
104 function getNamespaceMenu( $namespace=NULL ) {
105 return "<label for='namespace'>" . wfMsgHtml('namespace') . "</label>" . HTMLnamespaceselector($namespace, '');
106 }
107
108 /**
109 * @return string Formatted HTML
110 * @private
111 */
112 function getTypeMenu( $pr_type ) {
113 global $wgRestrictionTypes, $wgUser;
114
115 $out = "<select name='type'>\n";
116 $m = array(); // Temporary array
117
118 // First pass to load the log names
119 foreach( $wgRestrictionTypes as $type ) {
120 $text = wfMsgHtml("restriction-$type");
121 $m[$text] = $type;
122 }
123
124 // Second pass to sort by name
125 ksort($m);
126
127 // Third pass generates sorted XHTML content
128 foreach( $m as $text => $type ) {
129 $selected = ($type == $pr_type );
130 $out .= Xml::option( $text, $type, $selected ) . "\n";
131 }
132
133 $out .= '</select>';
134 return "<label for='type'>" . wfMsgHtml('restriction-type') . "</label>: " . $out;
135 }
136
137 /**
138 * @return string Formatted HTML
139 * @private
140 */
141 function getLevelMenu( $pr_level ) {
142 global $wgRestrictionLevels, $wgUser;
143
144 $out = "<select name='level'>\n";
145 $m = array( wfMsgHtml('restriction-level-all') => 0 ); // Temporary array
146
147 // First pass to load the log names
148 foreach( $wgRestrictionLevels as $type ) {
149 if ( $type !='' && $type !='*') {
150 $text = wfMsgHtml("restriction-level-$type");
151 $m[$text] = $type;
152 }
153 }
154
155 // Second pass to sort by name
156 ksort($m);
157
158 // Third pass generates sorted XHTML content
159 foreach( $m as $text => $type ) {
160 $selected = ($type == $pr_level );
161 $out .= Xml::option( $text, $type, $selected ) . "\n";
162 }
163
164 $out .= '</select>';
165 return "<label for='level'>" . wfMsgHtml('restriction-level') . "</label>: " . $out;
166 }
167 }
168
169 /**
170 *
171 *
172 */
173 class ProtectedPagesPager extends ReverseChronologicalPager {
174 public $mForm, $mConds;
175
176 function __construct( $form, $conds = array(), $type, $level, $namespace ) {
177 $this->mForm = $form;
178 $this->mConds = $conds;
179 $this->type = ( $type ) ? $type : 'edit';
180 $this->level = $level;
181 $this->namespace = $namespace;
182 parent::__construct();
183 }
184
185 function getStartBody() {
186 wfProfileIn( __METHOD__ );
187 # Do a link batch query
188 $this->mResult->seek( 0 );
189 $lb = new LinkBatch;
190
191 while ( $row = $this->mResult->fetchObject() ) {
192 $name = str_replace( ' ', '_', $row->page_title );
193 $lb->add( $row->page_namespace, $name );
194 }
195
196 $lb->execute();
197 wfProfileOut( __METHOD__ );
198 return '';
199 }
200
201 function formatRow( $row ) {
202 $block = new Block;
203 return $this->mForm->formatRow( $row );
204 }
205
206 function getQueryInfo() {
207 $conds = $this->mConds;
208 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
209 $conds[] = 'page_id=pr_page';
210 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
211 if ( $this->level )
212 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
213 if ( !is_null($this->namespace) )
214 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
215 return array(
216 'tables' => array( 'page_restrictions', 'page' ),
217 'fields' => 'max(pr_id) AS pr_id,page_namespace,page_title,pr_type,pr_level,pr_expiry',
218 'conds' => $conds,
219 'options' => array( 'GROUP BY' => 'page_namespace,page_title,pr_level,pr_expiry' ),
220 );
221 }
222
223 function getIndexField() {
224 return 'pr_id';
225 }
226 }
227
228 /**
229 * Constructor
230 */
231 function wfSpecialProtectedpages() {
232
233 list( $limit, $offset ) = wfCheckLimits();
234
235 $ppForm = new ProtectedPagesForm();
236
237 $ppForm->showList();
238 }
239
240 ?>