Merge "backport release notes from Ia6efdf931b0b6be7556fc16c040aa7493845cdfc"
[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 * @return string
73 */
74 function formatRow( $row ) {
75 wfProfileIn( __METHOD__ );
76
77 static $infinity = null;
78
79 if( is_null( $infinity ) ) {
80 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
81 }
82
83 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
84 if( !$title ) {
85 return Html::rawElement( 'li', array(),
86 Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
87 Linker::getInvalidTitleDescription( $this->getContext(), $row->pt_namespace, $row->pt_title ) ) ) . "\n";
88 }
89
90 $link = Linker::link( $title );
91
92 $description_items = array ();
93
94 $protType = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
95
96 $description_items[] = $protType;
97
98 $lang = $this->getLanguage();
99 $expiry = strlen( $row->pt_expiry ) ? $lang->formatExpiry( $row->pt_expiry, TS_MW ) : $infinity;
100 if( $expiry != $infinity ) {
101 $user = $this->getUser();
102 $description_items[] = $this->msg(
103 'protect-expiring-local',
104 $lang->userTimeAndDate( $expiry, $user ),
105 $lang->userDate( $expiry, $user ),
106 $lang->userTime( $expiry, $user )
107 )->escaped();
108 }
109
110 wfProfileOut( __METHOD__ );
111
112 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
113 }
114
115 /**
116 * @param $namespace Integer:
117 * @param $type string
118 * @param $level string
119 * @return string
120 * @private
121 */
122 function showOptions( $namespace, $type = 'edit', $level ) {
123 global $wgScript;
124 $action = htmlspecialchars( $wgScript );
125 $title = $this->getTitle();
126 $special = htmlspecialchars( $title->getPrefixedDBkey() );
127 return "<form action=\"$action\" method=\"get\">\n" .
128 '<fieldset>' .
129 Xml::element( 'legend', array(), $this->msg( 'protectedtitles' )->text() ) .
130 Html::hidden( 'title', $special ) . "&#160;\n" .
131 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
132 $this->getLevelMenu( $level ) . "&#160;\n" .
133 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
134 "</fieldset></form>";
135 }
136
137 /**
138 * Prepare the namespace filter drop-down; standard namespace
139 * selector, sans the MediaWiki namespace
140 *
141 * @param $namespace Mixed: pre-select namespace
142 * @return string
143 */
144 function getNamespaceMenu( $namespace = null ) {
145 return Html::namespaceSelector(
146 array(
147 'selected' => $namespace,
148 'all' => '',
149 'label' => $this->msg( 'namespace' )->text()
150 ), array(
151 'name' => 'namespace',
152 'id' => 'namespace',
153 'class' => 'namespaceselector',
154 )
155 );
156 }
157
158 /**
159 * @return string Formatted HTML
160 * @private
161 */
162 function getLevelMenu( $pr_level ) {
163 global $wgRestrictionLevels;
164
165 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 ); // Temporary array
166 $options = array();
167
168 // First pass to load the log names
169 foreach( $wgRestrictionLevels as $type ) {
170 if ( $type != '' && $type != '*' ) {
171 $text = $this->msg( "restriction-level-$type" )->text();
172 $m[$text] = $type;
173 }
174 }
175 // Is there only one level (aside from "all")?
176 if( count( $m ) <= 2 ) {
177 return '';
178 }
179 // Third pass generates sorted XHTML content
180 foreach( $m as $text => $type ) {
181 $selected = ($type == $pr_level );
182 $options[] = Xml::option( $text, $type, $selected );
183 }
184
185 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
186 Xml::tags( 'select',
187 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
188 implode( "\n", $options ) );
189 }
190
191 protected function getGroupName() {
192 return 'maintenance';
193 }
194 }
195
196 /**
197 * @todo document
198 * @ingroup Pager
199 */
200 class ProtectedTitlesPager extends AlphabeticPager {
201 public $mForm, $mConds;
202
203 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype = '', $size = 0 ) {
204 $this->mForm = $form;
205 $this->mConds = $conds;
206 $this->level = $level;
207 $this->namespace = $namespace;
208 $this->size = intval( $size );
209 parent::__construct( $form->getContext() );
210 }
211
212 function getStartBody() {
213 wfProfileIn( __METHOD__ );
214 # Do a link batch query
215 $this->mResult->seek( 0 );
216 $lb = new LinkBatch;
217
218 foreach ( $this->mResult as $row ) {
219 $lb->add( $row->pt_namespace, $row->pt_title );
220 }
221
222 $lb->execute();
223 wfProfileOut( __METHOD__ );
224 return '';
225 }
226
227 /**
228 * @return Title
229 */
230 function getTitle() {
231 return $this->mForm->getTitle();
232 }
233
234 function formatRow( $row ) {
235 return $this->mForm->formatRow( $row );
236 }
237
238 /**
239 * @return array
240 */
241 function getQueryInfo() {
242 $conds = $this->mConds;
243 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
244 if( $this->level )
245 $conds['pt_create_perm'] = $this->level;
246 if( !is_null( $this->namespace ) )
247 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
248 return array(
249 'tables' => 'protected_titles',
250 'fields' => array( 'pt_namespace', 'pt_title', 'pt_create_perm',
251 'pt_expiry', 'pt_timestamp' ),
252 'conds' => $conds
253 );
254 }
255
256 function getIndexField() {
257 return 'pt_timestamp';
258 }
259 }