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
21 * @ingroup SpecialPage
25 * A special page that lists tags for edits
27 * @ingroup SpecialPage
29 class SpecialTags
extends SpecialPage
{
32 * @var array List of explicitly defined tags
34 protected $explicitlyDefinedTags;
37 * @var array List of software defined tags
39 protected $softwareDefinedTags;
42 * @var array List of software activated tags
44 protected $softwareActivatedTags;
46 function __construct() {
47 parent
::__construct( 'Tags' );
50 function execute( $par ) {
52 $this->outputHeader();
53 $this->addHelpLink( 'Manual:Tags' );
55 $request = $this->getRequest();
58 $this->showDeleteTagForm( $request->getVal( 'tag' ) );
61 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), true );
64 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), false );
67 // fall through, thanks to HTMLForm's logic
74 function showTagList() {
75 $out = $this->getOutput();
76 $out->setPageTitle( $this->msg( 'tags-title' ) );
77 $out->wrapWikiMsg( "<div class='mw-tags-intro'>\n$1\n</div>", 'tags-intro' );
79 $user = $this->getUser();
80 $userCanManage = $user->isAllowed( 'managechangetags' );
81 $userCanDelete = $user->isAllowed( 'deletechangetags' );
82 $userCanEditInterface = $user->isAllowed( 'editinterface' );
84 // Show form to create a tag
85 if ( $userCanManage ) {
89 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
94 'label' => $this->msg( 'tags-create-reason' )->plain(),
102 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
103 $form->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
104 $form->setWrapperLegendMsg( 'tags-create-heading' );
105 $form->setHeaderText( $this->msg( 'tags-create-explanation' )->parseAsBlock() );
106 $form->setSubmitCallback( [ $this, 'processCreateTagForm' ] );
107 $form->setSubmitTextMsg( 'tags-create-submit' );
110 // If processCreateTagForm generated a redirect, there's no point
111 // continuing with this, as the user is just going to end up getting sent
112 // somewhere else. Additionally, if we keep going here, we end up
113 // populating the memcache of tag data (see ChangeTags::listDefinedTags)
114 // with out-of-date data from the replica DB, because the replica DB hasn't caught
115 // up to the fact that a new tag has been created as part of an implicit,
116 // as yet uncommitted transaction on master.
117 if ( $out->getRedirect() !== '' ) {
122 // Used to get hitcounts for #doTagRow()
123 $tagStats = ChangeTags
::tagUsageStatistics();
125 // Used in #doTagRow()
126 $this->explicitlyDefinedTags
= array_fill_keys(
127 ChangeTags
::listExplicitlyDefinedTags(), true );
128 $this->softwareDefinedTags
= array_fill_keys(
129 ChangeTags
::listSoftwareDefinedTags(), true );
131 // List all defined tags, even if they were never applied
132 $definedTags = array_keys( $this->explicitlyDefinedTags +
$this->softwareDefinedTags
);
134 // Show header only if there exists atleast one tag
135 if ( !$tagStats && !$definedTags ) {
140 $thead = Xml
::tags( 'tr', null, Xml
::tags( 'th', null, $this->msg( 'tags-tag' )->parse() ) .
141 Xml
::tags( 'th', null, $this->msg( 'tags-display-header' )->parse() ) .
142 Xml
::tags( 'th', null, $this->msg( 'tags-description-header' )->parse() ) .
143 Xml
::tags( 'th', null, $this->msg( 'tags-source-header' )->parse() ) .
144 Xml
::tags( 'th', null, $this->msg( 'tags-active-header' )->parse() ) .
145 Xml
::tags( 'th', null, $this->msg( 'tags-hitcount-header' )->parse() ) .
146 ( ( $userCanManage ||
$userCanDelete ) ?
147 Xml
::tags( 'th', [ 'class' => 'unsortable' ],
148 $this->msg( 'tags-actions-header' )->parse() ) :
153 // Used in #doTagRow()
154 $this->softwareActivatedTags
= array_fill_keys(
155 ChangeTags
::listSoftwareActivatedTags(), true );
157 // Insert tags that have been applied at least once
158 foreach ( $tagStats as $tag => $hitcount ) {
159 $tbody .= $this->doTagRow( $tag, $hitcount, $userCanManage,
160 $userCanDelete, $userCanEditInterface );
162 // Insert tags defined somewhere but never applied
163 foreach ( $definedTags as $tag ) {
164 if ( !isset( $tagStats[$tag] ) ) {
165 $tbody .= $this->doTagRow( $tag, 0, $userCanManage, $userCanDelete, $userCanEditInterface );
169 $out->addModuleStyles( 'jquery.tablesorter.styles' );
170 $out->addModules( 'jquery.tablesorter' );
171 $out->addHTML( Xml
::tags(
173 [ 'class' => 'mw-datatable sortable mw-tags-table' ],
174 Xml
::tags( 'thead', null, $thead ) .
175 Xml
::tags( 'tbody', null, $tbody )
179 function doTagRow( $tag, $hitcount, $showManageActions, $showDeleteActions, $showEditLinks ) {
181 $newRow .= Xml
::tags( 'td', null, Xml
::element( 'code', null, $tag ) );
183 $linkRenderer = $this->getLinkRenderer();
184 $disp = ChangeTags
::tagDescription( $tag, $this->getContext() );
185 if ( $showEditLinks ) {
187 $editLink = $linkRenderer->makeLink(
188 $this->msg( "tag-$tag" )->inContentLanguage()->getTitle(),
189 $this->msg( 'tags-edit' )->text()
191 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
193 $newRow .= Xml
::tags( 'td', null, $disp );
195 $msg = $this->msg( "tag-$tag-description" );
196 $desc = !$msg->exists() ?
'' : $msg->parse();
197 if ( $showEditLinks ) {
199 $editDescLink = $linkRenderer->makeLink(
200 $this->msg( "tag-$tag-description" )->inContentLanguage()->getTitle(),
201 $this->msg( 'tags-edit' )->text()
203 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
205 $newRow .= Xml
::tags( 'td', null, $desc );
208 $isSoftware = isset( $this->softwareDefinedTags
[$tag] );
209 $isExplicit = isset( $this->explicitlyDefinedTags
[$tag] );
211 // TODO: Rename this message
212 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
215 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
217 if ( !$sourceMsgs ) {
218 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
220 $newRow .= Xml
::tags( 'td', null, implode( Xml
::element( 'br' ), $sourceMsgs ) );
222 $isActive = $isExplicit ||
isset( $this->softwareActivatedTags
[$tag] );
223 $activeMsg = ( $isActive ?
'tags-active-yes' : 'tags-active-no' );
224 $newRow .= Xml
::tags( 'td', null, $this->msg( $activeMsg )->escaped() );
226 $hitcountLabelMsg = $this->msg( 'tags-hitcount' )->numParams( $hitcount );
227 if ( $this->getConfig()->get( 'UseTagFilter' ) ) {
228 $hitcountLabel = $linkRenderer->makeLink(
229 SpecialPage
::getTitleFor( 'Recentchanges' ),
230 $hitcountLabelMsg->text(),
232 [ 'tagfilter' => $tag ]
235 $hitcountLabel = $hitcountLabelMsg->escaped();
238 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
239 $newRow .= Xml
::tags( 'td', [ 'data-sort-value' => $hitcount ], $hitcountLabel );
243 if ( $showDeleteActions && ChangeTags
::canDeleteTag( $tag )->isOK() ) {
244 $actionLinks[] = $linkRenderer->makeKnownLink(
245 $this->getPageTitle( 'delete' ),
246 $this->msg( 'tags-delete' )->text(),
251 if ( $showManageActions ) { // we've already checked that the user had the requisite userright
252 if ( ChangeTags
::canActivateTag( $tag )->isOK() ) {
253 $actionLinks[] = $linkRenderer->makeKnownLink(
254 $this->getPageTitle( 'activate' ),
255 $this->msg( 'tags-activate' )->text(),
260 if ( ChangeTags
::canDeactivateTag( $tag )->isOK() ) {
261 $actionLinks[] = $linkRenderer->makeKnownLink(
262 $this->getPageTitle( 'deactivate' ),
263 $this->msg( 'tags-deactivate' )->text(),
269 if ( $showDeleteActions ||
$showManageActions ) {
270 $newRow .= Xml
::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
273 return Xml
::tags( 'tr', null, $newRow ) . "\n";
276 public function processCreateTagForm( array $data, HTMLForm
$form ) {
277 $context = $form->getContext();
278 $out = $context->getOutput();
280 $tag = trim( strval( $data['Tag'] ) );
281 $ignoreWarnings = isset( $data['IgnoreWarnings'] ) && $data['IgnoreWarnings'] === '1';
282 $status = ChangeTags
::createTagWithChecks( $tag, $data['Reason'],
283 $context->getUser(), $ignoreWarnings );
285 if ( $status->isGood() ) {
286 $out->redirect( $this->getPageTitle()->getLocalURL() );
288 } elseif ( $status->isOK() ) {
289 // we have some warnings, so we show a confirmation form
293 'default' => $data['Tag'],
297 'default' => $data['Reason'],
299 'IgnoreWarnings' => [
305 // fool HTMLForm into thinking the form hasn't been submitted yet. Otherwise
306 // we get into an infinite loop!
307 $context->getRequest()->unsetVal( 'wpEditToken' );
309 $headerText = $this->msg( 'tags-create-warnings-above', $tag,
310 count( $status->getWarningsArray() ) )->parseAsBlock() .
311 $out->parseAsInterface( $status->getWikiText() ) .
312 $this->msg( 'tags-create-warnings-below' )->parseAsBlock();
314 $subform = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
315 $subform->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
316 $subform->setWrapperLegendMsg( 'tags-create-heading' );
317 $subform->setHeaderText( $headerText );
318 $subform->setSubmitCallback( [ $this, 'processCreateTagForm' ] );
319 $subform->setSubmitTextMsg( 'htmlform-yes' );
322 $out->addBacklinkSubtitle( $this->getPageTitle() );
325 $out->wrapWikiTextAsInterface( 'error', $status->getWikiText() );
330 protected function showDeleteTagForm( $tag ) {
331 $user = $this->getUser();
332 if ( !$user->isAllowed( 'deletechangetags' ) ) {
333 throw new PermissionsError( 'deletechangetags' );
336 $out = $this->getOutput();
337 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
338 $out->addBacklinkSubtitle( $this->getPageTitle() );
340 // is the tag actually able to be deleted?
341 $canDeleteResult = ChangeTags
::canDeleteTag( $tag, $user );
342 if ( !$canDeleteResult->isGood() ) {
343 $out->wrapWikiTextAsInterface( 'error', $canDeleteResult->getWikiText() );
344 if ( !$canDeleteResult->isOK() ) {
349 $preText = $this->msg( 'tags-delete-explanation-initial', $tag )->parseAsBlock();
350 $tagUsage = ChangeTags
::tagUsageStatistics();
351 if ( isset( $tagUsage[$tag] ) && $tagUsage[$tag] > 0 ) {
352 $preText .= $this->msg( 'tags-delete-explanation-in-use', $tag,
353 $tagUsage[$tag] )->parseAsBlock();
355 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
357 // see if the tag is in use
358 $this->softwareActivatedTags
= array_fill_keys(
359 ChangeTags
::listSoftwareActivatedTags(), true );
360 if ( isset( $this->softwareActivatedTags
[$tag] ) ) {
361 $preText .= $this->msg( 'tags-delete-explanation-active', $tag )->parseAsBlock();
365 $fields['Reason'] = [
367 'label' => $this->msg( 'tags-delete-reason' )->plain(),
370 $fields['HiddenTag'] = [
377 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
378 $form->setAction( $this->getPageTitle( 'delete' )->getLocalURL() );
379 $form->tagAction
= 'delete'; // custom property on HTMLForm object
380 $form->setSubmitCallback( [ $this, 'processTagForm' ] );
381 $form->setSubmitTextMsg( 'tags-delete-submit' );
382 $form->setSubmitDestructive(); // nasty!
383 $form->addPreText( $preText );
387 protected function showActivateDeactivateForm( $tag, $activate ) {
388 $actionStr = $activate ?
'activate' : 'deactivate';
390 $user = $this->getUser();
391 if ( !$user->isAllowed( 'managechangetags' ) ) {
392 throw new PermissionsError( 'managechangetags' );
395 $out = $this->getOutput();
396 // tags-activate-title, tags-deactivate-title
397 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
398 $out->addBacklinkSubtitle( $this->getPageTitle() );
400 // is it possible to do this?
401 $func = $activate ?
'canActivateTag' : 'canDeactivateTag';
402 $result = ChangeTags
::$func( $tag, $user );
403 if ( !$result->isGood() ) {
404 $out->wrapWikiTextAsInterface( 'error', $result->getWikiText() );
405 if ( !$result->isOK() ) {
410 // tags-activate-question, tags-deactivate-question
411 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
414 // tags-activate-reason, tags-deactivate-reason
415 $fields['Reason'] = [
417 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
420 $fields['HiddenTag'] = [
427 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
428 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
429 $form->tagAction
= $actionStr;
430 $form->setSubmitCallback( [ $this, 'processTagForm' ] );
431 // tags-activate-submit, tags-deactivate-submit
432 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
433 $form->addPreText( $preText );
437 public function processTagForm( array $data, HTMLForm
$form ) {
438 $context = $form->getContext();
439 $out = $context->getOutput();
441 $tag = $data['HiddenTag'];
442 $status = call_user_func( [ ChangeTags
::class, "{$form->tagAction}TagWithChecks" ],
443 $tag, $data['Reason'], $context->getUser(), true );
445 if ( $status->isGood() ) {
446 $out->redirect( $this->getPageTitle()->getLocalURL() );
448 } elseif ( $status->isOK() && $form->tagAction
=== 'delete' ) {
449 // deletion succeeded, but hooks raised a warning
450 $out->addWikiTextAsInterface( $this->msg( 'tags-delete-warnings-after-delete', $tag,
451 count( $status->getWarningsArray() ) )->text() . "\n" .
452 $status->getWikitext() );
453 $out->addReturnTo( $this->getPageTitle() );
456 $out->wrapWikiTextAsInterface( 'error', $status->getWikitext() );
462 * Return an array of subpages that this special page will accept.
464 * @return string[] subpages
466 public function getSubpagesForPrefixSearch() {
467 // The subpages does not have an own form, so not listing it at the moment
476 protected function getGroupName() {