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