Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / specials / SpecialTags.php
blobc03f81184f8c335eae776c88f85b8a5f15796635
1 <?php
2 /**
3 * Implements Special:Tags
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists tags for edits
27 * @ingroup SpecialPage
29 class SpecialTags extends SpecialPage {
31 /**
32 * @var array List of explicitly defined tags
34 protected $explicitlyDefinedTags;
36 /**
37 * @var array List of extension defined tags
39 protected $extensionDefinedTags;
41 /**
42 * @var array List of extension activated tags
44 protected $extensionActivatedTags;
46 function __construct() {
47 parent::__construct( 'Tags' );
50 function execute( $par ) {
51 $this->setHeaders();
52 $this->outputHeader();
54 $request = $this->getRequest();
55 switch ( $par ) {
56 case 'delete':
57 $this->showDeleteTagForm( $request->getVal( 'tag' ) );
58 break;
59 case 'activate':
60 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), true );
61 break;
62 case 'deactivate':
63 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), false );
64 break;
65 case 'create':
66 // fall through, thanks to HTMLForm's logic
67 default:
68 $this->showTagList();
69 break;
73 function showTagList() {
74 $out = $this->getOutput();
75 $out->setPageTitle( $this->msg( 'tags-title' ) );
76 $out->wrapWikiMsg( "<div class='mw-tags-intro'>\n$1\n</div>", 'tags-intro' );
78 $user = $this->getUser();
79 $userCanManage = $user->isAllowed( 'managechangetags' );
80 $userCanEditInterface = $user->isAllowed( 'editinterface' );
82 // Show form to create a tag
83 if ( $userCanManage ) {
84 $fields = array(
85 'Tag' => array(
86 'type' => 'text',
87 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
88 'required' => true,
90 'Reason' => array(
91 'type' => 'text',
92 'label' => $this->msg( 'tags-create-reason' )->plain(),
93 'size' => 50,
95 'IgnoreWarnings' => array(
96 'type' => 'hidden',
100 $form = new HTMLForm( $fields, $this->getContext() );
101 $form->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
102 $form->setWrapperLegendMsg( 'tags-create-heading' );
103 $form->setHeaderText( $this->msg( 'tags-create-explanation' )->parseAsBlock() );
104 $form->setSubmitCallback( array( $this, 'processCreateTagForm' ) );
105 $form->setSubmitTextMsg( 'tags-create-submit' );
106 $form->show();
108 // If processCreateTagForm generated a redirect, there's no point
109 // continuing with this, as the user is just going to end up getting sent
110 // somewhere else. Additionally, if we keep going here, we end up
111 // populating the memcache of tag data (see ChangeTags::listDefinedTags)
112 // with out-of-date data from the slave, because the slave hasn't caught
113 // up to the fact that a new tag has been created as part of an implicit,
114 // as yet uncommitted transaction on master.
115 if ( $out->getRedirect() !== '' ) {
116 return;
120 // Used to get hitcounts for #doTagRow()
121 $tagStats = ChangeTags::tagUsageStatistics();
123 // Used in #doTagRow()
124 $this->explicitlyDefinedTags = array_fill_keys(
125 ChangeTags::listExplicitlyDefinedTags(), true );
126 $this->extensionDefinedTags = array_fill_keys(
127 ChangeTags::listExtensionDefinedTags(), true );
129 // List all defined tags, even if they were never applied
130 $definedTags = array_keys( array_merge(
131 $this->explicitlyDefinedTags, $this->extensionDefinedTags ) );
133 // Show header only if there exists atleast one tag
134 if ( !$tagStats && !$definedTags ) {
135 return;
138 // Write the headers
139 $html = Xml::tags( 'tr', null, Xml::tags( 'th', null, $this->msg( 'tags-tag' )->parse() ) .
140 Xml::tags( 'th', null, $this->msg( 'tags-display-header' )->parse() ) .
141 Xml::tags( 'th', null, $this->msg( 'tags-description-header' )->parse() ) .
142 Xml::tags( 'th', null, $this->msg( 'tags-source-header' )->parse() ) .
143 Xml::tags( 'th', null, $this->msg( 'tags-active-header' )->parse() ) .
144 Xml::tags( 'th', null, $this->msg( 'tags-hitcount-header' )->parse() ) .
145 ( $userCanManage ?
146 Xml::tags( 'th', array( 'class' => 'unsortable' ),
147 $this->msg( 'tags-actions-header' )->parse() ) :
148 '' )
151 // Used in #doTagRow()
152 $this->extensionActivatedTags = array_fill_keys(
153 ChangeTags::listExtensionActivatedTags(), true );
155 // Insert tags that have been applied at least once
156 foreach ( $tagStats as $tag => $hitcount ) {
157 $html .= $this->doTagRow( $tag, $hitcount, $userCanManage, $userCanEditInterface );
159 // Insert tags defined somewhere but never applied
160 foreach ( $definedTags as $tag ) {
161 if ( !isset( $tagStats[$tag] ) ) {
162 $html .= $this->doTagRow( $tag, 0, $userCanManage, $userCanEditInterface );
166 $out->addHTML( Xml::tags(
167 'table',
168 array( 'class' => 'mw-datatable sortable mw-tags-table' ),
169 $html
170 ) );
173 function doTagRow( $tag, $hitcount, $showActions, $showEditLinks ) {
174 $newRow = '';
175 $newRow .= Xml::tags( 'td', null, Xml::element( 'code', null, $tag ) );
177 $disp = ChangeTags::tagDescription( $tag );
178 if ( $showEditLinks ) {
179 $disp .= ' ';
180 $editLink = Linker::link(
181 $this->msg( "tag-$tag" )->inContentLanguage()->getTitle(),
182 $this->msg( 'tags-edit' )->escaped()
184 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
186 $newRow .= Xml::tags( 'td', null, $disp );
188 $msg = $this->msg( "tag-$tag-description" );
189 $desc = !$msg->exists() ? '' : $msg->parse();
190 if ( $showEditLinks ) {
191 $desc .= ' ';
192 $editDescLink = Linker::link(
193 $this->msg( "tag-$tag-description" )->inContentLanguage()->getTitle(),
194 $this->msg( 'tags-edit' )->escaped()
196 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
198 $newRow .= Xml::tags( 'td', null, $desc );
200 $sourceMsgs = array();
201 $isExtension = isset( $this->extensionDefinedTags[$tag] );
202 $isExplicit = isset( $this->explicitlyDefinedTags[$tag] );
203 if ( $isExtension ) {
204 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
206 if ( $isExplicit ) {
207 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
209 if ( !$sourceMsgs ) {
210 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
212 $newRow .= Xml::tags( 'td', null, implode( Xml::element( 'br' ), $sourceMsgs ) );
214 $isActive = $isExplicit || isset( $this->extensionActivatedTags[$tag] );
215 $activeMsg = ( $isActive ? 'tags-active-yes' : 'tags-active-no' );
216 $newRow .= Xml::tags( 'td', null, $this->msg( $activeMsg )->escaped() );
218 $hitcountLabel = $this->msg( 'tags-hitcount' )->numParams( $hitcount )->escaped();
219 if ( $this->getConfig()->get( 'UseTagFilter' ) ) {
220 $hitcountLabel = Linker::link(
221 SpecialPage::getTitleFor( 'Recentchanges' ),
222 $hitcountLabel,
223 array(),
224 array( 'tagfilter' => $tag )
228 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
229 $newRow .= Xml::tags( 'td', array( 'data-sort-value' => $hitcount ), $hitcountLabel );
231 // actions
232 if ( $showActions ) { // we've already checked that the user had the requisite userright
233 $actionLinks = array();
235 // delete
236 if ( ChangeTags::canDeleteTag( $tag )->isOK() ) {
237 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'delete' ),
238 $this->msg( 'tags-delete' )->escaped(),
239 array(),
240 array( 'tag' => $tag ) );
243 // activate
244 if ( ChangeTags::canActivateTag( $tag )->isOK() ) {
245 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'activate' ),
246 $this->msg( 'tags-activate' )->escaped(),
247 array(),
248 array( 'tag' => $tag ) );
251 // deactivate
252 if ( ChangeTags::canDeactivateTag( $tag )->isOK() ) {
253 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'deactivate' ),
254 $this->msg( 'tags-deactivate' )->escaped(),
255 array(),
256 array( 'tag' => $tag ) );
259 $newRow .= Xml::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
262 return Xml::tags( 'tr', null, $newRow ) . "\n";
265 public function processCreateTagForm( array $data, HTMLForm $form ) {
266 $context = $form->getContext();
267 $out = $context->getOutput();
269 $tag = trim( strval( $data['Tag'] ) );
270 $ignoreWarnings = isset( $data['IgnoreWarnings'] ) && $data['IgnoreWarnings'] === '1';
271 $status = ChangeTags::createTagWithChecks( $tag, $data['Reason'],
272 $context->getUser(), $ignoreWarnings );
274 if ( $status->isGood() ) {
275 $out->redirect( $this->getPageTitle()->getLocalURL() );
276 return true;
277 } elseif ( $status->isOK() ) {
278 // we have some warnings, so we show a confirmation form
279 $fields = array(
280 'Tag' => array(
281 'type' => 'hidden',
282 'default' => $data['Tag'],
284 'Reason' => array(
285 'type' => 'hidden',
286 'default' => $data['Reason'],
288 'IgnoreWarnings' => array(
289 'type' => 'hidden',
290 'default' => '1',
294 // fool HTMLForm into thinking the form hasn't been submitted yet. Otherwise
295 // we get into an infinite loop!
296 $context->getRequest()->unsetVal( 'wpEditToken' );
298 $headerText = $this->msg( 'tags-create-warnings-above', $tag,
299 count( $status->getWarningsArray() ) )->parseAsBlock() .
300 $out->parse( $status->getWikitext() ) .
301 $this->msg( 'tags-create-warnings-below' )->parseAsBlock();
303 $subform = new HTMLForm( $fields, $this->getContext() );
304 $subform->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
305 $subform->setWrapperLegendMsg( 'tags-create-heading' );
306 $subform->setHeaderText( $headerText );
307 $subform->setSubmitCallback( array( $this, 'processCreateTagForm' ) );
308 $subform->setSubmitTextMsg( 'htmlform-yes' );
309 $subform->show();
311 $out->addBacklinkSubtitle( $this->getPageTitle() );
312 return true;
313 } else {
314 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
315 "\n</div>" );
316 return false;
320 protected function showDeleteTagForm( $tag ) {
321 $user = $this->getUser();
322 if ( !$user->isAllowed( 'managechangetags' ) ) {
323 throw new PermissionsError( 'managechangetags' );
326 $out = $this->getOutput();
327 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
328 $out->addBacklinkSubtitle( $this->getPageTitle() );
330 // is the tag actually able to be deleted?
331 $canDeleteResult = ChangeTags::canDeleteTag( $tag, $user );
332 if ( !$canDeleteResult->isGood() ) {
333 $out->addWikiText( "<div class=\"error\">\n" . $canDeleteResult->getWikiText() .
334 "\n</div>" );
335 if ( !$canDeleteResult->isOK() ) {
336 return;
340 $preText = $this->msg( 'tags-delete-explanation-initial', $tag )->parseAsBlock();
341 $tagUsage = ChangeTags::tagUsageStatistics();
342 if ( isset( $tagUsage[$tag] ) && $tagUsage[$tag] > 0 ) {
343 $preText .= $this->msg( 'tags-delete-explanation-in-use', $tag,
344 $tagUsage[$tag] )->parseAsBlock();
346 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
348 // see if the tag is in use
349 $this->extensionActivatedTags = array_fill_keys(
350 ChangeTags::listExtensionActivatedTags(), true );
351 if ( isset( $this->extensionActivatedTags[$tag] ) ) {
352 $preText .= $this->msg( 'tags-delete-explanation-active', $tag )->parseAsBlock();
355 $fields = array();
356 $fields['Reason'] = array(
357 'type' => 'text',
358 'label' => $this->msg( 'tags-delete-reason' )->plain(),
359 'size' => 50,
361 $fields['HiddenTag'] = array(
362 'type' => 'hidden',
363 'name' => 'tag',
364 'default' => $tag,
365 'required' => true,
368 $form = new HTMLForm( $fields, $this->getContext() );
369 $form->setAction( $this->getPageTitle( 'delete' )->getLocalURL() );
370 $form->tagAction = 'delete'; // custom property on HTMLForm object
371 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
372 $form->setSubmitTextMsg( 'tags-delete-submit' );
373 $form->setSubmitDestructive(); // nasty!
374 $form->addPreText( $preText );
375 $form->show();
378 protected function showActivateDeactivateForm( $tag, $activate ) {
379 $actionStr = $activate ? 'activate' : 'deactivate';
381 $user = $this->getUser();
382 if ( !$user->isAllowed( 'managechangetags' ) ) {
383 throw new PermissionsError( 'managechangetags' );
386 $out = $this->getOutput();
387 // tags-activate-title, tags-deactivate-title
388 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
389 $out->addBacklinkSubtitle( $this->getPageTitle() );
391 // is it possible to do this?
392 $func = $activate ? 'canActivateTag' : 'canDeactivateTag';
393 $result = ChangeTags::$func( $tag, $user );
394 if ( !$result->isGood() ) {
395 $out->addWikiText( "<div class=\"error\">\n" . $result->getWikiText() .
396 "\n</div>" );
397 if ( !$result->isOK() ) {
398 return;
402 // tags-activate-question, tags-deactivate-question
403 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
405 $fields = array();
406 // tags-activate-reason, tags-deactivate-reason
407 $fields['Reason'] = array(
408 'type' => 'text',
409 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
410 'size' => 50,
412 $fields['HiddenTag'] = array(
413 'type' => 'hidden',
414 'name' => 'tag',
415 'default' => $tag,
416 'required' => true,
419 $form = new HTMLForm( $fields, $this->getContext() );
420 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
421 $form->tagAction = $actionStr;
422 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
423 // tags-activate-submit, tags-deactivate-submit
424 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
425 $form->addPreText( $preText );
426 $form->show();
429 public function processTagForm( array $data, HTMLForm $form ) {
430 $context = $form->getContext();
431 $out = $context->getOutput();
433 $tag = $data['HiddenTag'];
434 $status = call_user_func( array( 'ChangeTags', "{$form->tagAction}TagWithChecks" ),
435 $tag, $data['Reason'], $context->getUser(), true );
437 if ( $status->isGood() ) {
438 $out->redirect( $this->getPageTitle()->getLocalURL() );
439 return true;
440 } elseif ( $status->isOK() && $form->tagAction === 'delete' ) {
441 // deletion succeeded, but hooks raised a warning
442 $out->addWikiText( $this->msg( 'tags-delete-warnings-after-delete', $tag,
443 count( $status->getWarningsArray() ) )->text() . "\n" .
444 $status->getWikitext() );
445 $out->addReturnTo( $this->getPageTitle() );
446 return true;
447 } else {
448 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
449 "\n</div>" );
450 return false;
455 * Return an array of subpages that this special page will accept.
457 * @return string[] subpages
459 public function getSubpagesForPrefixSearch() {
460 // The subpages does not have an own form, so not listing it at the moment
461 return array(
462 // 'delete',
463 // 'activate',
464 // 'deactivate',
465 // 'create',
469 protected function getGroupName() {
470 return 'changes';