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
{
31 * @var array List of defined tags
35 * @var array List of active tags
39 function __construct() {
40 parent
::__construct( 'Tags' );
43 function execute( $par ) {
45 $this->outputHeader();
47 $request = $this->getRequest();
50 $this->showDeleteTagForm( $request->getVal( 'tag' ) );
53 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), true );
56 $this->showActivateDeactivateForm( $request->getVal( 'tag' ), false );
59 // fall through, thanks to HTMLForm's logic
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' );
71 $user = $this->getUser();
73 // Show form to create a tag
74 if ( $user->isAllowed( 'managechangetags' ) ) {
78 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
83 'label' => $this->msg( 'tags-create-reason' )->plain(),
86 'IgnoreWarnings' => array(
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' );
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() !== '' ) {
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
114 $showActions = $user->isAllowed( 'managechangetags' );
117 $tagUsageStatistics = ChangeTags
::tagUsageStatistics();
119 // Show header only if there exists atleast one tag
120 if ( !$tagUsageStatistics ) {
123 $html = Xml
::tags( 'tr', null, Xml
::tags( 'th', null, $this->msg( 'tags-tag' )->parse() ) .
124 Xml
::tags( 'th', null, $this->msg( 'tags-display-header' )->parse() ) .
125 Xml
::tags( 'th', null, $this->msg( 'tags-description-header' )->parse() ) .
126 Xml
::tags( 'th', null, $this->msg( 'tags-source-header' )->parse() ) .
127 Xml
::tags( 'th', null, $this->msg( 'tags-active-header' )->parse() ) .
128 Xml
::tags( 'th', null, $this->msg( 'tags-hitcount-header' )->parse() ) .
130 Xml
::tags( 'th', array( 'class' => 'unsortable' ),
131 $this->msg( 'tags-actions-header' )->parse() ) :
135 // Used in #doTagRow()
136 $this->explicitlyDefinedTags
= array_fill_keys(
137 ChangeTags
::listExplicitlyDefinedTags(), true );
138 $this->extensionDefinedTags
= array_fill_keys(
139 ChangeTags
::listExtensionDefinedTags(), true );
140 $this->extensionActivatedTags
= array_fill_keys(
141 ChangeTags
::listExtensionActivatedTags(), true );
143 foreach ( $tagUsageStatistics as $tag => $hitcount ) {
144 $html .= $this->doTagRow( $tag, $hitcount, $showActions );
147 $out->addHTML( Xml
::tags(
149 array( 'class' => 'mw-datatable sortable mw-tags-table' ),
154 function doTagRow( $tag, $hitcount, $showActions ) {
155 $user = $this->getUser();
157 $newRow .= Xml
::tags( 'td', null, Xml
::element( 'code', null, $tag ) );
159 $disp = ChangeTags
::tagDescription( $tag );
160 if ( $user->isAllowed( 'editinterface' ) ) {
162 $editLink = Linker
::link(
163 $this->msg( "tag-$tag" )->inContentLanguage()->getTitle(),
164 $this->msg( 'tags-edit' )->escaped()
166 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
168 $newRow .= Xml
::tags( 'td', null, $disp );
170 $msg = $this->msg( "tag-$tag-description" );
171 $desc = !$msg->exists() ?
'' : $msg->parse();
172 if ( $user->isAllowed( 'editinterface' ) ) {
174 $editDescLink = Linker
::link(
175 $this->msg( "tag-$tag-description" )->inContentLanguage()->getTitle(),
176 $this->msg( 'tags-edit' )->escaped()
178 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
180 $newRow .= Xml
::tags( 'td', null, $desc );
182 $sourceMsgs = array();
183 $isExtension = isset( $this->extensionDefinedTags
[$tag] );
184 $isExplicit = isset( $this->explicitlyDefinedTags
[$tag] );
185 if ( $isExtension ) {
186 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
189 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
191 if ( !$sourceMsgs ) {
192 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
194 $newRow .= Xml
::tags( 'td', null, implode( Xml
::element( 'br' ), $sourceMsgs ) );
196 $isActive = $isExplicit ||
isset( $this->extensionActivatedTags
[$tag] );
197 $activeMsg = ( $isActive ?
'tags-active-yes' : 'tags-active-no' );
198 $newRow .= Xml
::tags( 'td', null, $this->msg( $activeMsg )->escaped() );
200 $hitcountLabel = $this->msg( 'tags-hitcount' )->numParams( $hitcount )->escaped();
201 $hitcountLink = Linker
::link(
202 SpecialPage
::getTitleFor( 'Recentchanges' ),
205 array( 'tagfilter' => $tag )
208 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
209 $newRow .= Xml
::tags( 'td', array( 'data-sort-value' => $hitcount ), $hitcountLink );
212 $actionLinks = array();
213 if ( $showActions ) {
215 if ( ChangeTags
::canDeleteTag( $tag, $user )->isOK() ) {
216 $actionLinks[] = Linker
::linkKnown( $this->getPageTitle( 'delete' ),
217 $this->msg( 'tags-delete' )->escaped(),
219 array( 'tag' => $tag ) );
223 if ( ChangeTags
::canActivateTag( $tag, $user )->isOK() ) {
224 $actionLinks[] = Linker
::linkKnown( $this->getPageTitle( 'activate' ),
225 $this->msg( 'tags-activate' )->escaped(),
227 array( 'tag' => $tag ) );
231 if ( ChangeTags
::canDeactivateTag( $tag, $user )->isOK() ) {
232 $actionLinks[] = Linker
::linkKnown( $this->getPageTitle( 'deactivate' ),
233 $this->msg( 'tags-deactivate' )->escaped(),
235 array( 'tag' => $tag ) );
238 $newRow .= Xml
::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
241 return Xml
::tags( 'tr', null, $newRow ) . "\n";
244 public function processCreateTagForm( array $data, HTMLForm
$form ) {
245 $context = $form->getContext();
246 $out = $context->getOutput();
248 $tag = trim( strval( $data['Tag'] ) );
249 $ignoreWarnings = isset( $data['IgnoreWarnings'] ) && $data['IgnoreWarnings'] === '1';
250 $status = ChangeTags
::createTagWithChecks( $tag, $data['Reason'],
251 $context->getUser(), $ignoreWarnings );
253 if ( $status->isGood() ) {
254 $out->redirect( $this->getPageTitle()->getLocalURL() );
256 } elseif ( $status->isOK() ) {
257 // we have some warnings, so we show a confirmation form
261 'default' => $data['Tag'],
265 'default' => $data['Reason'],
267 'IgnoreWarnings' => array(
273 // fool HTMLForm into thinking the form hasn't been submitted yet. Otherwise
274 // we get into an infinite loop!
275 $context->getRequest()->unsetVal( 'wpEditToken' );
277 $headerText = $this->msg( 'tags-create-warnings-above', $tag,
278 count( $status->getWarningsArray() ) )->parseAsBlock() .
279 $out->parse( $status->getWikitext() ) .
280 $this->msg( 'tags-create-warnings-below' )->parseAsBlock();
282 $subform = new HTMLForm( $fields, $this->getContext() );
283 $subform->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
284 $subform->setWrapperLegendMsg( 'tags-create-heading' );
285 $subform->setHeaderText( $headerText );
286 $subform->setSubmitCallback( array( $this, 'processCreateTagForm' ) );
287 $subform->setSubmitTextMsg( 'htmlform-yes' );
290 $out->addBacklinkSubtitle( $this->getPageTitle() );
293 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
299 protected function showDeleteTagForm( $tag ) {
300 $user = $this->getUser();
301 if ( !$user->isAllowed( 'managechangetags' ) ) {
302 throw new PermissionsError( 'managechangetags' );
305 $out = $this->getOutput();
306 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
307 $out->addBacklinkSubtitle( $this->getPageTitle() );
309 // is the tag actually able to be deleted?
310 $canDeleteResult = ChangeTags
::canDeleteTag( $tag, $user );
311 if ( !$canDeleteResult->isGood() ) {
312 $out->addWikiText( "<div class=\"error\">\n" . $canDeleteResult->getWikiText() .
314 if ( !$canDeleteResult->isOK() ) {
319 $preText = $this->msg( 'tags-delete-explanation-initial', $tag )->parseAsBlock();
320 $tagUsage = ChangeTags
::tagUsageStatistics();
321 if ( $tagUsage[$tag] > 0 ) {
322 $preText .= $this->msg( 'tags-delete-explanation-in-use', $tag,
323 $tagUsage[$tag] )->parseAsBlock();
325 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
327 // see if the tag is in use
328 $this->extensionActivatedTags
= array_fill_keys(
329 ChangeTags
::listExtensionActivatedTags(), true );
330 if ( isset( $this->extensionActivatedTags
[$tag] ) ) {
331 $preText .= $this->msg( 'tags-delete-explanation-active', $tag )->parseAsBlock();
335 $fields['Reason'] = array(
337 'label' => $this->msg( 'tags-delete-reason' )->plain(),
340 $fields['HiddenTag'] = array(
347 $form = new HTMLForm( $fields, $this->getContext() );
348 $form->setAction( $this->getPageTitle( 'delete' )->getLocalURL() );
349 $form->tagAction
= 'delete'; // custom property on HTMLForm object
350 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
351 $form->setSubmitTextMsg( 'tags-delete-submit' );
352 $form->setSubmitDestructive(); // nasty!
353 $form->addPreText( $preText );
357 protected function showActivateDeactivateForm( $tag, $activate ) {
358 $actionStr = $activate ?
'activate' : 'deactivate';
360 $user = $this->getUser();
361 if ( !$user->isAllowed( 'managechangetags' ) ) {
362 throw new PermissionsError( 'managechangetags' );
365 $out = $this->getOutput();
366 // tags-activate-title, tags-deactivate-title
367 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
368 $out->addBacklinkSubtitle( $this->getPageTitle() );
370 // is it possible to do this?
371 $func = $activate ?
'canActivateTag' : 'canDeactivateTag';
372 $result = ChangeTags
::$func( $tag, $user );
373 if ( !$result->isGood() ) {
374 $out->wrapWikiMsg( "<div class=\"error\">\n$1" . $result->getWikiText() .
376 if ( !$result->isOK() ) {
381 // tags-activate-question, tags-deactivate-question
382 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
385 // tags-activate-reason, tags-deactivate-reason
386 $fields['Reason'] = array(
388 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
391 $fields['HiddenTag'] = array(
398 $form = new HTMLForm( $fields, $this->getContext() );
399 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
400 $form->tagAction
= $actionStr;
401 $form->setSubmitCallback( array( $this, 'processTagForm' ) );
402 // tags-activate-submit, tags-deactivate-submit
403 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
404 $form->addPreText( $preText );
408 public function processTagForm( array $data, HTMLForm
$form ) {
409 $context = $form->getContext();
410 $out = $context->getOutput();
412 $tag = $data['HiddenTag'];
413 $status = call_user_func( array( 'ChangeTags', "{$form->tagAction}TagWithChecks" ),
414 $tag, $data['Reason'], $context->getUser(), true );
416 if ( $status->isGood() ) {
417 $out->redirect( $this->getPageTitle()->getLocalURL() );
419 } elseif ( $status->isOK() && $form->tagAction
=== 'delete' ) {
420 // deletion succeeded, but hooks raised a warning
421 $out->addWikiText( $this->msg( 'tags-delete-warnings-after-delete', $tag,
422 count( $status->getWarningsArray() ) )->text() . "\n" .
423 $status->getWikitext() );
424 $out->addReturnTo( $this->getPageTitle() );
427 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
433 protected function getGroupName() {