Merge "Do not call purgeExpiredRestrictions on simple page views"
[lhc/web/wiklou.git] / includes / specials / SpecialTags.php
1 <?php
2 /**
3 * Implements Special:Tags
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 lists tags for edits
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialTags extends SpecialPage {
30 /**
31 * @var array List of defined tags
32 */
33 public $definedTags;
34 /**
35 * @var array List of active tags
36 */
37 public $activeTags;
38
39 function __construct() {
40 parent::__construct( 'Tags' );
41 }
42
43 function execute( $par ) {
44 $this->setHeaders();
45 $this->outputHeader();
46
47 $request = $this->getRequest();
48 switch ( $par ) {
49 case 'delete':
50 $this->showDeleteTagForm( $request->getVal( 'tag' ) );
51 break;
52 case 'activate':
53 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), true );
54 break;
55 case 'deactivate':
56 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), false );
57 break;
58 case 'create':
59 // fall through, thanks to HTMLForm's logic
60 default:
61 $this->showTagList();
62 break;
63 }
64 }
65
66 function showTagList() {
67 $out = $this->getOutput();
68 $out->setPageTitle( $this->msg( 'tags-title' ) );
69 $out->wrapWikiMsg( "<div class='mw-tags-intro'>\n$1\n</div>", 'tags-intro' );
70
71 $user = $this->getUser();
72
73 // Show form to create a tag
74 if ( $user->isAllowed( 'managechangetags' ) ) {
75 $fields = array(
76 'Tag' => array(
77 'type' => 'text',
78 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
79 'required' => true,
80 ),
81 'Reason' => array(
82 'type' => 'text',
83 'label' => $this->msg( 'tags-create-reason' )->plain(),
84 'size' => 50,
85 ),
86 'IgnoreWarnings' => array(
87 'type' => 'hidden',
88 ),
89 );
90
91 $form = new HTMLForm( $fields, $this->getContext() );
92 $form->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
93 $form->setWrapperLegendMsg( 'tags-create-heading' );
94 $form->setHeaderText( $this->msg( 'tags-create-explanation' )->plain() );
95 $form->setSubmitCallback( array( $this, 'processCreateTagForm' ) );
96 $form->setSubmitTextMsg( 'tags-create-submit' );
97 $form->show();
98
99 // If processCreateTagForm generated a redirect, there's no point
100 // continuing with this, as the user is just going to end up getting sent
101 // somewhere else. Additionally, if we keep going here, we end up
102 // populating the memcache of tag data (see ChangeTags::listDefinedTags)
103 // with out-of-date data from the slave, because the slave hasn't caught
104 // up to the fact that a new tag has been created as part of an implicit,
105 // as yet uncommitted transaction on master.
106 if ( $out->getRedirect() !== '' ) {
107 return;
108 }
109 }
110
111 // Whether to show the "Actions" column in the tag list
112 // If any actions added in the future require other user rights, add those
113 // rights here
114 $showActions = $user->isAllowed( 'managechangetags' );
115
116 // Write the headers
117 $html = Xml::tags( 'tr', null, Xml::tags( 'th', null, $this->msg( 'tags-tag' )->parse() ) .
118 Xml::tags( 'th', null, $this->msg( 'tags-display-header' )->parse() ) .
119 Xml::tags( 'th', null, $this->msg( 'tags-description-header' )->parse() ) .
120 Xml::tags( 'th', null, $this->msg( 'tags-source-header' )->parse() ) .
121 Xml::tags( 'th', null, $this->msg( 'tags-active-header' )->parse() ) .
122 Xml::tags( 'th', null, $this->msg( 'tags-hitcount-header' )->parse() ) .
123 ( $showActions ?
124 Xml::tags( 'th', array( 'class' => 'unsortable' ),
125 $this->msg( 'tags-actions-header' )->parse() ) :
126 '' )
127 );
128
129 // Used in #doTagRow()
130 $this->explicitlyDefinedTags = array_fill_keys(
131 ChangeTags::listExplicitlyDefinedTags(), true );
132 $this->extensionDefinedTags = array_fill_keys(
133 ChangeTags::listExtensionDefinedTags(), true );
134 $this->extensionActivatedTags = array_fill_keys(
135 ChangeTags::listExtensionActivatedTags(), true );
136
137 foreach ( ChangeTags::tagUsageStatistics() as $tag => $hitcount ) {
138 $html .= $this->doTagRow( $tag, $hitcount, $showActions );
139 }
140
141 $out->addHTML( Xml::tags(
142 'table',
143 array( 'class' => 'mw-datatable sortable mw-tags-table' ),
144 $html
145 ) );
146 }
147
148 function doTagRow( $tag, $hitcount, $showActions ) {
149 $user = $this->getUser();
150 $newRow = '';
151 $newRow .= Xml::tags( 'td', null, Xml::element( 'code', null, $tag ) );
152
153 $disp = ChangeTags::tagDescription( $tag );
154 if ( $user->isAllowed( 'editinterface' ) ) {
155 $disp .= ' ';
156 $editLink = Linker::link(
157 Title::makeTitle( NS_MEDIAWIKI, "Tag-$tag" ),
158 $this->msg( 'tags-edit' )->escaped()
159 );
160 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
161 }
162 $newRow .= Xml::tags( 'td', null, $disp );
163
164 $msg = $this->msg( "tag-$tag-description" );
165 $desc = !$msg->exists() ? '' : $msg->parse();
166 if ( $user->isAllowed( 'editinterface' ) ) {
167 $desc .= ' ';
168 $editDescLink = Linker::link(
169 Title::makeTitle( NS_MEDIAWIKI, "Tag-$tag-description" ),
170 $this->msg( 'tags-edit' )->escaped()
171 );
172 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
173 }
174 $newRow .= Xml::tags( 'td', null, $desc );
175
176 $sourceMsgs = array();
177 $isExtension = isset( $this->extensionDefinedTags[$tag] );
178 $isExplicit = isset( $this->explicitlyDefinedTags[$tag] );
179 if ( $isExtension ) {
180 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
181 }
182 if ( $isExplicit ) {
183 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
184 }
185 if ( !$sourceMsgs ) {
186 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
187 }
188 $newRow .= Xml::tags( 'td', null, implode( Xml::element( 'br' ), $sourceMsgs ) );
189
190 $isActive = $isExplicit || isset( $this->extensionActivatedTags[$tag] );
191 $activeMsg = ( $isActive ? 'tags-active-yes' : 'tags-active-no' );
192 $newRow .= Xml::tags( 'td', null, $this->msg( $activeMsg )->escaped() );
193
194 $hitcountLabel = $this->msg( 'tags-hitcount' )->numParams( $hitcount )->escaped();
195 $hitcountLink = Linker::link(
196 SpecialPage::getTitleFor( 'Recentchanges' ),
197 $hitcountLabel,
198 array(),
199 array( 'tagfilter' => $tag )
200 );
201
202 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
203 $newRow .= Xml::tags( 'td', array( 'data-sort-value' => $hitcount ), $hitcountLink );
204
205 // actions
206 $actionLinks = array();
207 if ( $showActions ) {
208 // delete
209 if ( ChangeTags::canDeleteTag( $tag, $user )->isOK() ) {
210 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'delete' ),
211 $this->msg( 'tags-delete' )->escaped(),
212 array(),
213 array( 'tag' => $tag ) );
214 }
215
216 // activate
217 if ( ChangeTags::canActivateTag( $tag, $user )->isOK() ) {
218 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'activate' ),
219 $this->msg( 'tags-activate' )->escaped(),
220 array(),
221 array( 'tag' => $tag ) );
222 }
223
224 // deactivate
225 if ( ChangeTags::canDeactivateTag( $tag, $user )->isOK() ) {
226 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'deactivate' ),
227 $this->msg( 'tags-deactivate' )->escaped(),
228 array(),
229 array( 'tag' => $tag ) );
230 }
231
232 $newRow .= Xml::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
233 }
234
235 return Xml::tags( 'tr', null, $newRow ) . "\n";
236 }
237
238 public function processCreateTagForm( array $data, HTMLForm $form ) {
239 $context = $form->getContext();
240 $out = $context->getOutput();
241
242 $tag = trim( strval( $data['Tag'] ) );
243 $ignoreWarnings = isset( $data['IgnoreWarnings'] ) && $data['IgnoreWarnings'] === '1';
244 $status = ChangeTags::createTagWithChecks( $tag, $data['Reason'],
245 $context->getUser(), $ignoreWarnings );
246
247 if ( $status->isGood() ) {
248 $out->redirect( $this->getPageTitle()->getLocalURL() );
249 return true;
250 } elseif ( $status->isOK() ) {
251 // we have some warnings, so we show a confirmation form
252 $fields = array(
253 'Tag' => array(
254 'type' => 'hidden',
255 'default' => $data['Tag'],
256 ),
257 'Reason' => array(
258 'type' => 'hidden',
259 'default' => $data['Reason'],
260 ),
261 'IgnoreWarnings' => array(
262 'type' => 'hidden',
263 'default' => '1',
264 ),
265 );
266
267 // fool HTMLForm into thinking the form hasn't been submitted yet. Otherwise
268 // we get into an infinite loop!
269 $context->getRequest()->unsetVal( 'wpEditToken' );
270
271 $headerText = $this->msg( 'tags-create-warnings-above', $tag,
272 count( $status->getWarningsArray() ) )->parseAsBlock() .
273 $out->parse( $status->getWikitext() ) .
274 $this->msg( 'tags-create-warnings-below' )->parseAsBlock();
275
276 $subform = new HTMLForm( $fields, $this->getContext() );
277 $subform->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
278 $subform->setWrapperLegendMsg( 'tags-create-heading' );
279 $subform->setHeaderText( $headerText );
280 $subform->setSubmitCallback( array( $this, 'processCreateTagForm' ) );
281 $subform->setSubmitTextMsg( 'htmlform-yes' );
282 $subform->show();
283
284 $out->addBacklinkSubtitle( $this->getPageTitle() );
285 return true;
286 } else {
287 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
288 "\n</div>" );
289 return false;
290 }
291 }
292
293 protected function showDeleteTagForm( $tag ) {
294 $user = $this->getUser();
295 if ( !$user->isAllowed( 'managechangetags' ) ) {
296 throw new PermissionsError( 'managechangetags' );
297 }
298
299 $out = $this->getOutput();
300 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
301 $out->addBacklinkSubtitle( $this->getPageTitle() );
302
303 // is the tag actually able to be deleted?
304 $canDeleteResult = ChangeTags::canDeleteTag( $tag, $user );
305 if ( !$canDeleteResult->isGood() ) {
306 $out->addWikiText( "<div class=\"error\">\n" . $canDeleteResult->getWikiText() .
307 "\n</div>" );
308 if ( !$canDeleteResult->isOK() ) {
309 return;
310 }
311 }
312
313 $preText = $this->msg( 'tags-delete-explanation-initial', $tag )->parseAsBlock();
314 $tagUsage = ChangeTags::tagUsageStatistics();
315 if ( $tagUsage[$tag] > 0 ) {
316 $preText .= $this->msg( 'tags-delete-explanation-in-use', $tag,
317 $tagUsage[$tag] )->parseAsBlock();
318 }
319 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
320
321 // see if the tag is in use
322 $this->extensionActivatedTags = array_fill_keys(
323 ChangeTags::listExtensionActivatedTags(), true );
324 if ( isset( $this->extensionActivatedTags[$tag] ) ) {
325 $preText .= $this->msg( 'tags-delete-explanation-active', $tag )->parseAsBlock();
326 }
327
328 $fields = array();
329 $fields['Reason'] = array(
330 'type' => 'text',
331 'label' => $this->msg( 'tags-delete-reason' )->plain(),
332 'size' => 50,
333 );
334 $fields['HiddenTag'] = array(
335 'type' => 'hidden',
336 'name' => 'tag',
337 'default' => $tag,
338 'required' => true,
339 );
340
341 $form = new HTMLForm( $fields, $this->getContext() );
342 $form->setAction( $this->getPageTitle( 'delete' )->getLocalURL() );
343 $form->tagAction = 'delete'; // custom property on HTMLForm object
344 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
345 $form->setSubmitTextMsg( 'tags-delete-submit' );
346 $form->setSubmitDestructive(); // nasty!
347 $form->addPreText( $preText );
348 $form->show();
349 }
350
351 protected function showActivateDeactivateForm( $tag, $activate ) {
352 $actionStr = $activate ? 'activate' : 'deactivate';
353
354 $user = $this->getUser();
355 if ( !$user->isAllowed( 'managechangetags' ) ) {
356 throw new PermissionsError( 'managechangetags' );
357 }
358
359 $out = $this->getOutput();
360 // tags-activate-title, tags-deactivate-title
361 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
362 $out->addBacklinkSubtitle( $this->getPageTitle() );
363
364 // is it possible to do this?
365 $func = $activate ? 'canActivateTag' : 'canDeactivateTag';
366 $result = ChangeTags::$func( $tag, $user );
367 if ( !$result->isGood() ) {
368 $out->wrapWikiMsg( "<div class=\"error\">\n$1" . $result->getWikiText() .
369 "\n</div>" );
370 if ( !$result->isOK() ) {
371 return;
372 }
373 }
374
375 // tags-activate-question, tags-deactivate-question
376 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
377
378 $fields = array();
379 // tags-activate-reason, tags-deactivate-reason
380 $fields['Reason'] = array(
381 'type' => 'text',
382 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
383 'size' => 50,
384 );
385 $fields['HiddenTag'] = array(
386 'type' => 'hidden',
387 'name' => 'tag',
388 'default' => $tag,
389 'required' => true,
390 );
391
392 $form = new HTMLForm( $fields, $this->getContext() );
393 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
394 $form->tagAction = $actionStr;
395 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
396 // tags-activate-submit, tags-deactivate-submit
397 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
398 $form->addPreText( $preText );
399 $form->show();
400 }
401
402 public function processTagForm( array $data, HTMLForm $form ) {
403 $context = $form->getContext();
404 $out = $context->getOutput();
405
406 $tag = $data['HiddenTag'];
407 $status = call_user_func( array( 'ChangeTags', "{$form->tagAction}TagWithChecks" ),
408 $tag, $data['Reason'], $context->getUser(), true );
409
410 if ( $status->isGood() ) {
411 $out->redirect( $this->getPageTitle()->getLocalURL() );
412 return true;
413 } elseif ( $status->isOK() && $form->tagAction === 'delete' ) {
414 // deletion succeeded, but hooks raised a warning
415 $out->addWikiText( $this->msg( 'tags-delete-warnings-after-delete', $tag,
416 count( $status->getWarningsArray() ) )->text() . "\n" .
417 $status->getWikitext() );
418 $out->addReturnTo( $this->getPageTitle() );
419 return true;
420 } else {
421 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
422 "\n</div>" );
423 return false;
424 }
425 }
426
427 protected function getGroupName() {
428 return 'changes';
429 }
430 }