Merge "Log to DBPerformance log when many LB connections are acquired"
[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 protected $IdLevel = 'level';
31 protected $IdType = 'type';
32
33 public function __construct() {
34 parent::__construct( 'Protectedtitles' );
35 }
36
37 function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40
41 // Purge expired entries on one in every 10 queries
42 if ( !mt_rand( 0, 10 ) ) {
43 Title::purgeExpiredRestrictions();
44 }
45
46 $request = $this->getRequest();
47 $type = $request->getVal( $this->IdType );
48 $level = $request->getVal( $this->IdLevel );
49 $sizetype = $request->getVal( 'sizetype' );
50 $size = $request->getIntOrNull( 'size' );
51 $NS = $request->getIntOrNull( 'namespace' );
52
53 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
54
55 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
56
57 if ( $pager->getNumRows() ) {
58 $this->getOutput()->addHTML(
59 $pager->getNavigationBar() .
60 '<ul>' . $pager->getBody() . '</ul>' .
61 $pager->getNavigationBar()
62 );
63 } else {
64 $this->getOutput()->addWikiMsg( 'protectedtitlesempty' );
65 }
66 }
67
68 /**
69 * Callback function to output a restriction
70 *
71 * @param object $row Database row
72 * @return string
73 */
74 function formatRow( $row ) {
75
76 static $infinity = null;
77
78 if ( is_null( $infinity ) ) {
79 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
80 }
81
82 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
83 if ( !$title ) {
84
85 return Html::rawElement(
86 'li',
87 array(),
88 Html::element(
89 'span',
90 array( 'class' => 'mw-invalidtitle' ),
91 Linker::getInvalidTitleDescription(
92 $this->getContext(),
93 $row->pt_namespace,
94 $row->pt_title
95 )
96 )
97 ) . "\n";
98 }
99
100 $link = Linker::link( $title );
101 $description_items = array();
102 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
103 $protType = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
104 $description_items[] = $protType;
105 $lang = $this->getLanguage();
106 $expiry = strlen( $row->pt_expiry ) ?
107 $lang->formatExpiry( $row->pt_expiry, TS_MW ) :
108 $infinity;
109
110 if ( $expiry != $infinity ) {
111 $user = $this->getUser();
112 $description_items[] = $this->msg(
113 'protect-expiring-local',
114 $lang->userTimeAndDate( $expiry, $user ),
115 $lang->userDate( $expiry, $user ),
116 $lang->userTime( $expiry, $user )
117 )->escaped();
118 }
119
120 // @todo i18n: This should use a comma separator instead of a hard coded comma, right?
121 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
122 }
123
124 /**
125 * @param int $namespace
126 * @param string $type
127 * @param string $level
128 * @return string
129 * @private
130 */
131 function showOptions( $namespace, $type = 'edit', $level ) {
132 $action = htmlspecialchars( wfScript() );
133 $title = $this->getPageTitle();
134 $special = htmlspecialchars( $title->getPrefixedDBkey() );
135
136 return "<form action=\"$action\" method=\"get\">\n" .
137 '<fieldset>' .
138 Xml::element( 'legend', array(), $this->msg( 'protectedtitles' )->text() ) .
139 Html::hidden( 'title', $special ) . "&#160;\n" .
140 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
141 $this->getLevelMenu( $level ) . "&#160;\n" .
142 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
143 "</fieldset></form>";
144 }
145
146 /**
147 * Prepare the namespace filter drop-down; standard namespace
148 * selector, sans the MediaWiki namespace
149 *
150 * @param string|null $namespace Pre-select namespace
151 * @return string
152 */
153 function getNamespaceMenu( $namespace = null ) {
154 return Html::namespaceSelector(
155 array(
156 'selected' => $namespace,
157 'all' => '',
158 'label' => $this->msg( 'namespace' )->text()
159 ), array(
160 'name' => 'namespace',
161 'id' => 'namespace',
162 'class' => 'namespaceselector',
163 )
164 );
165 }
166
167 /**
168 * @param string $pr_level Determines which option is selected as default
169 * @return string Formatted HTML
170 * @private
171 */
172 function getLevelMenu( $pr_level ) {
173 // Temporary array
174 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
175 $options = array();
176
177 // First pass to load the log names
178 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
179 if ( $type != '' && $type != '*' ) {
180 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
181 $text = $this->msg( "restriction-level-$type" )->text();
182 $m[$text] = $type;
183 }
184 }
185
186 // Is there only one level (aside from "all")?
187 if ( count( $m ) <= 2 ) {
188 return '';
189 }
190 // Third pass generates sorted XHTML content
191 foreach ( $m as $text => $type ) {
192 $selected = ( $type == $pr_level );
193 $options[] = Xml::option( $text, $type, $selected );
194 }
195
196 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
197 Xml::tags( 'select',
198 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
199 implode( "\n", $options ) );
200 }
201
202 protected function getGroupName() {
203 return 'maintenance';
204 }
205 }
206
207 /**
208 * @todo document
209 * @ingroup Pager
210 */
211 class ProtectedTitlesPager extends AlphabeticPager {
212 public $mForm, $mConds;
213
214 function __construct( $form, $conds = array(), $type, $level, $namespace,
215 $sizetype = '', $size = 0
216 ) {
217 $this->mForm = $form;
218 $this->mConds = $conds;
219 $this->level = $level;
220 $this->namespace = $namespace;
221 $this->size = intval( $size );
222 parent::__construct( $form->getContext() );
223 }
224
225 function getStartBody() {
226 # Do a link batch query
227 $this->mResult->seek( 0 );
228 $lb = new LinkBatch;
229
230 foreach ( $this->mResult as $row ) {
231 $lb->add( $row->pt_namespace, $row->pt_title );
232 }
233
234 $lb->execute();
235
236 return '';
237 }
238
239 /**
240 * @return Title
241 */
242 function getTitle() {
243 return $this->mForm->getTitle();
244 }
245
246 function formatRow( $row ) {
247 return $this->mForm->formatRow( $row );
248 }
249
250 /**
251 * @return array
252 */
253 function getQueryInfo() {
254 $conds = $this->mConds;
255 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
256 if ( $this->level ) {
257 $conds['pt_create_perm'] = $this->level;
258 }
259
260 if ( !is_null( $this->namespace ) ) {
261 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
262 }
263
264 return array(
265 'tables' => 'protected_titles',
266 'fields' => array( 'pt_namespace', 'pt_title', 'pt_create_perm',
267 'pt_expiry', 'pt_timestamp' ),
268 'conds' => $conds
269 );
270 }
271
272 function getIndexField() {
273 return 'pt_timestamp';
274 }
275 }