Update/fix docs
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedtitles.php
1 <?php
2 /**
3 * Implements Special:Protectedtitles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that list protected titles from creation
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialProtectedtitles extends SpecialPage {
30
31 protected $IdLevel = 'level';
32 protected $IdType = 'type';
33
34 public function __construct() {
35 parent::__construct( 'Protectedtitles' );
36 }
37
38 function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41
42 // Purge expired entries on one in every 10 queries
43 if ( !mt_rand( 0, 10 ) ) {
44 Title::purgeExpiredRestrictions();
45 }
46
47 $request = $this->getRequest();
48 $type = $request->getVal( $this->IdType );
49 $level = $request->getVal( $this->IdLevel );
50 $sizetype = $request->getVal( 'sizetype' );
51 $size = $request->getIntOrNull( 'size' );
52 $NS = $request->getIntOrNull( 'namespace' );
53
54 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
55
56 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
57
58 if ( $pager->getNumRows() ) {
59 $this->getOutput()->addHTML(
60 $pager->getNavigationBar() .
61 '<ul>' . $pager->getBody() . '</ul>' .
62 $pager->getNavigationBar()
63 );
64 } else {
65 $this->getOutput()->addWikiMsg( 'protectedtitlesempty' );
66 }
67 }
68
69 /**
70 * Callback function to output a restriction
71 *
72 * @param Object $row Database row
73 * @return string
74 */
75 function formatRow( $row ) {
76 wfProfileIn( __METHOD__ );
77
78 static $infinity = null;
79
80 if( is_null( $infinity ) ) {
81 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
82 }
83
84 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
85 if( !$title ) {
86 return Html::rawElement( 'li', array(),
87 Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
88 Linker::getInvalidTitleDescription( $this->getContext(), $row->pt_namespace, $row->pt_title ) ) ) . "\n";
89 }
90
91 $link = Linker::link( $title );
92
93 $description_items = array ();
94
95 $protType = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
96
97 $description_items[] = $protType;
98
99 $lang = $this->getLanguage();
100 $expiry = strlen( $row->pt_expiry ) ? $lang->formatExpiry( $row->pt_expiry, TS_MW ) : $infinity;
101 if( $expiry != $infinity ) {
102 $user = $this->getUser();
103 $description_items[] = $this->msg(
104 'protect-expiring-local',
105 $lang->userTimeAndDate( $expiry, $user ),
106 $lang->userDate( $expiry, $user ),
107 $lang->userTime( $expiry, $user )
108 )->escaped();
109 }
110
111 wfProfileOut( __METHOD__ );
112
113 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
114 }
115
116 /**
117 * @param $namespace Integer:
118 * @param $type string
119 * @param $level string
120 * @return string
121 * @private
122 */
123 function showOptions( $namespace, $type = 'edit', $level ) {
124 global $wgScript;
125 $action = htmlspecialchars( $wgScript );
126 $title = $this->getTitle();
127 $special = htmlspecialchars( $title->getPrefixedDBkey() );
128 return "<form action=\"$action\" method=\"get\">\n" .
129 '<fieldset>' .
130 Xml::element( 'legend', array(), $this->msg( 'protectedtitles' )->text() ) .
131 Html::hidden( 'title', $special ) . "&#160;\n" .
132 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
133 $this->getLevelMenu( $level ) . "&#160;\n" .
134 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
135 "</fieldset></form>";
136 }
137
138 /**
139 * Prepare the namespace filter drop-down; standard namespace
140 * selector, sans the MediaWiki namespace
141 *
142 * @param $namespace Mixed: pre-select namespace
143 * @return string
144 */
145 function getNamespaceMenu( $namespace = null ) {
146 return Html::namespaceSelector(
147 array(
148 'selected' => $namespace,
149 'all' => '',
150 'label' => $this->msg( 'namespace' )->text()
151 ), array(
152 'name' => 'namespace',
153 'id' => 'namespace',
154 'class' => 'namespaceselector',
155 )
156 );
157 }
158
159 /**
160 * @param string $pr_level Determines which option is selected as default
161 * @return string Formatted HTML
162 * @private
163 */
164 function getLevelMenu( $pr_level ) {
165 global $wgRestrictionLevels;
166
167 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 ); // Temporary array
168 $options = array();
169
170 // First pass to load the log names
171 foreach( $wgRestrictionLevels as $type ) {
172 if ( $type != '' && $type != '*' ) {
173 $text = $this->msg( "restriction-level-$type" )->text();
174 $m[$text] = $type;
175 }
176 }
177 // Is there only one level (aside from "all")?
178 if( count( $m ) <= 2 ) {
179 return '';
180 }
181 // Third pass generates sorted XHTML content
182 foreach( $m as $text => $type ) {
183 $selected = ($type == $pr_level );
184 $options[] = Xml::option( $text, $type, $selected );
185 }
186
187 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
188 Xml::tags( 'select',
189 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
190 implode( "\n", $options ) );
191 }
192
193 protected function getGroupName() {
194 return 'maintenance';
195 }
196 }
197
198 /**
199 * @todo document
200 * @ingroup Pager
201 */
202 class ProtectedTitlesPager extends AlphabeticPager {
203 public $mForm, $mConds;
204
205 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype = '', $size = 0 ) {
206 $this->mForm = $form;
207 $this->mConds = $conds;
208 $this->level = $level;
209 $this->namespace = $namespace;
210 $this->size = intval( $size );
211 parent::__construct( $form->getContext() );
212 }
213
214 function getStartBody() {
215 wfProfileIn( __METHOD__ );
216 # Do a link batch query
217 $this->mResult->seek( 0 );
218 $lb = new LinkBatch;
219
220 foreach ( $this->mResult as $row ) {
221 $lb->add( $row->pt_namespace, $row->pt_title );
222 }
223
224 $lb->execute();
225 wfProfileOut( __METHOD__ );
226 return '';
227 }
228
229 /**
230 * @return Title
231 */
232 function getTitle() {
233 return $this->mForm->getTitle();
234 }
235
236 function formatRow( $row ) {
237 return $this->mForm->formatRow( $row );
238 }
239
240 /**
241 * @return array
242 */
243 function getQueryInfo() {
244 $conds = $this->mConds;
245 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
246 if( $this->level )
247 $conds['pt_create_perm'] = $this->level;
248 if( !is_null( $this->namespace ) )
249 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
250 return array(
251 'tables' => 'protected_titles',
252 'fields' => array( 'pt_namespace', 'pt_title', 'pt_create_perm',
253 'pt_expiry', 'pt_timestamp' ),
254 'conds' => $conds
255 );
256 }
257
258 function getIndexField() {
259 return 'pt_timestamp';
260 }
261 }