Fix display of numeric tag names on Special:Tags
[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 /**
32 * @var array List of explicitly defined tags
33 */
34 protected $explicitlyDefinedTags;
35
36 /**
37 * @var array List of extension defined tags
38 */
39 protected $extensionDefinedTags;
40
41 /**
42 * @var array List of extension activated tags
43 */
44 protected $extensionActivatedTags;
45
46 function __construct() {
47 parent::__construct( 'Tags' );
48 }
49
50 function execute( $par ) {
51 $this->setHeaders();
52 $this->outputHeader();
53
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;
70 }
71 }
72
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' );
77
78 $user = $this->getUser();
79 $userCanManage = $user->isAllowed( 'managechangetags' );
80 $userCanDelete = $user->isAllowed( 'deletechangetags' );
81 $userCanEditInterface = $user->isAllowed( 'editinterface' );
82
83 // Show form to create a tag
84 if ( $userCanManage ) {
85 $fields = [
86 'Tag' => [
87 'type' => 'text',
88 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
89 'required' => true,
90 ],
91 'Reason' => [
92 'type' => 'text',
93 'label' => $this->msg( 'tags-create-reason' )->plain(),
94 'size' => 50,
95 ],
96 'IgnoreWarnings' => [
97 'type' => 'hidden',
98 ],
99 ];
100
101 $form = new HTMLForm( $fields, $this->getContext() );
102 $form->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
103 $form->setWrapperLegendMsg( 'tags-create-heading' );
104 $form->setHeaderText( $this->msg( 'tags-create-explanation' )->parseAsBlock() );
105 $form->setSubmitCallback( [ $this, 'processCreateTagForm' ] );
106 $form->setSubmitTextMsg( 'tags-create-submit' );
107 $form->show();
108
109 // If processCreateTagForm generated a redirect, there's no point
110 // continuing with this, as the user is just going to end up getting sent
111 // somewhere else. Additionally, if we keep going here, we end up
112 // populating the memcache of tag data (see ChangeTags::listDefinedTags)
113 // with out-of-date data from the slave, because the slave hasn't caught
114 // up to the fact that a new tag has been created as part of an implicit,
115 // as yet uncommitted transaction on master.
116 if ( $out->getRedirect() !== '' ) {
117 return;
118 }
119 }
120
121 // Used to get hitcounts for #doTagRow()
122 $tagStats = ChangeTags::tagUsageStatistics();
123
124 // Used in #doTagRow()
125 $this->explicitlyDefinedTags = array_fill_keys(
126 ChangeTags::listExplicitlyDefinedTags(), true );
127 $this->extensionDefinedTags = array_fill_keys(
128 ChangeTags::listExtensionDefinedTags(), true );
129
130 // List all defined tags, even if they were never applied
131 $definedTags = array_keys( $this->explicitlyDefinedTags + $this->extensionDefinedTags );
132
133 // Show header only if there exists atleast one tag
134 if ( !$tagStats && !$definedTags ) {
135 return;
136 }
137
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', [ 'class' => 'unsortable' ],
147 $this->msg( 'tags-actions-header' )->parse() ) :
148 '' )
149 );
150
151 // Used in #doTagRow()
152 $this->extensionActivatedTags = array_fill_keys(
153 ChangeTags::listExtensionActivatedTags(), true );
154
155 // Insert tags that have been applied at least once
156 foreach ( $tagStats as $tag => $hitcount ) {
157 $html .= $this->doTagRow( $tag, $hitcount, $userCanManage,
158 $userCanDelete, $userCanEditInterface );
159 }
160 // Insert tags defined somewhere but never applied
161 foreach ( $definedTags as $tag ) {
162 if ( !isset( $tagStats[$tag] ) ) {
163 $html .= $this->doTagRow( $tag, 0, $userCanManage, $userCanDelete, $userCanEditInterface );
164 }
165 }
166
167 $out->addHTML( Xml::tags(
168 'table',
169 [ 'class' => 'mw-datatable sortable mw-tags-table' ],
170 $html
171 ) );
172 }
173
174 function doTagRow( $tag, $hitcount, $showManageActions, $showDeleteActions, $showEditLinks ) {
175 $newRow = '';
176 $newRow .= Xml::tags( 'td', null, Xml::element( 'code', null, $tag ) );
177
178 $disp = ChangeTags::tagDescription( $tag );
179 if ( $showEditLinks ) {
180 $disp .= ' ';
181 $editLink = Linker::link(
182 $this->msg( "tag-$tag" )->inContentLanguage()->getTitle(),
183 $this->msg( 'tags-edit' )->escaped()
184 );
185 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
186 }
187 $newRow .= Xml::tags( 'td', null, $disp );
188
189 $msg = $this->msg( "tag-$tag-description" );
190 $desc = !$msg->exists() ? '' : $msg->parse();
191 if ( $showEditLinks ) {
192 $desc .= ' ';
193 $editDescLink = Linker::link(
194 $this->msg( "tag-$tag-description" )->inContentLanguage()->getTitle(),
195 $this->msg( 'tags-edit' )->escaped()
196 );
197 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
198 }
199 $newRow .= Xml::tags( 'td', null, $desc );
200
201 $sourceMsgs = [];
202 $isExtension = isset( $this->extensionDefinedTags[$tag] );
203 $isExplicit = isset( $this->explicitlyDefinedTags[$tag] );
204 if ( $isExtension ) {
205 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
206 }
207 if ( $isExplicit ) {
208 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
209 }
210 if ( !$sourceMsgs ) {
211 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
212 }
213 $newRow .= Xml::tags( 'td', null, implode( Xml::element( 'br' ), $sourceMsgs ) );
214
215 $isActive = $isExplicit || isset( $this->extensionActivatedTags[$tag] );
216 $activeMsg = ( $isActive ? 'tags-active-yes' : 'tags-active-no' );
217 $newRow .= Xml::tags( 'td', null, $this->msg( $activeMsg )->escaped() );
218
219 $hitcountLabel = $this->msg( 'tags-hitcount' )->numParams( $hitcount )->escaped();
220 if ( $this->getConfig()->get( 'UseTagFilter' ) ) {
221 $hitcountLabel = Linker::link(
222 SpecialPage::getTitleFor( 'Recentchanges' ),
223 $hitcountLabel,
224 [],
225 [ 'tagfilter' => $tag ]
226 );
227 }
228
229 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
230 $newRow .= Xml::tags( 'td', [ 'data-sort-value' => $hitcount ], $hitcountLabel );
231
232 // actions
233 $actionLinks = [];
234
235 // delete
236 if ( $showDeleteActions && ChangeTags::canDeleteTag( $tag )->isOK() ) {
237 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'delete' ),
238 $this->msg( 'tags-delete' )->escaped(),
239 [],
240 [ 'tag' => $tag ] );
241 }
242
243 if ( $showManageActions ) { // we've already checked that the user had the requisite userright
244
245 // activate
246 if ( ChangeTags::canActivateTag( $tag )->isOK() ) {
247 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'activate' ),
248 $this->msg( 'tags-activate' )->escaped(),
249 [],
250 [ 'tag' => $tag ] );
251 }
252
253 // deactivate
254 if ( ChangeTags::canDeactivateTag( $tag )->isOK() ) {
255 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'deactivate' ),
256 $this->msg( 'tags-deactivate' )->escaped(),
257 [],
258 [ 'tag' => $tag ] );
259 }
260
261 }
262
263 if ( $actionLinks ) {
264 $newRow .= Xml::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
265 }
266
267 return Xml::tags( 'tr', null, $newRow ) . "\n";
268 }
269
270 public function processCreateTagForm( array $data, HTMLForm $form ) {
271 $context = $form->getContext();
272 $out = $context->getOutput();
273
274 $tag = trim( strval( $data['Tag'] ) );
275 $ignoreWarnings = isset( $data['IgnoreWarnings'] ) && $data['IgnoreWarnings'] === '1';
276 $status = ChangeTags::createTagWithChecks( $tag, $data['Reason'],
277 $context->getUser(), $ignoreWarnings );
278
279 if ( $status->isGood() ) {
280 $out->redirect( $this->getPageTitle()->getLocalURL() );
281 return true;
282 } elseif ( $status->isOK() ) {
283 // we have some warnings, so we show a confirmation form
284 $fields = [
285 'Tag' => [
286 'type' => 'hidden',
287 'default' => $data['Tag'],
288 ],
289 'Reason' => [
290 'type' => 'hidden',
291 'default' => $data['Reason'],
292 ],
293 'IgnoreWarnings' => [
294 'type' => 'hidden',
295 'default' => '1',
296 ],
297 ];
298
299 // fool HTMLForm into thinking the form hasn't been submitted yet. Otherwise
300 // we get into an infinite loop!
301 $context->getRequest()->unsetVal( 'wpEditToken' );
302
303 $headerText = $this->msg( 'tags-create-warnings-above', $tag,
304 count( $status->getWarningsArray() ) )->parseAsBlock() .
305 $out->parse( $status->getWikiText() ) .
306 $this->msg( 'tags-create-warnings-below' )->parseAsBlock();
307
308 $subform = new HTMLForm( $fields, $this->getContext() );
309 $subform->setAction( $this->getPageTitle( 'create' )->getLocalURL() );
310 $subform->setWrapperLegendMsg( 'tags-create-heading' );
311 $subform->setHeaderText( $headerText );
312 $subform->setSubmitCallback( [ $this, 'processCreateTagForm' ] );
313 $subform->setSubmitTextMsg( 'htmlform-yes' );
314 $subform->show();
315
316 $out->addBacklinkSubtitle( $this->getPageTitle() );
317 return true;
318 } else {
319 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikiText() .
320 "\n</div>" );
321 return false;
322 }
323 }
324
325 protected function showDeleteTagForm( $tag ) {
326 $user = $this->getUser();
327 if ( !$user->isAllowed( 'deletechangetags' ) ) {
328 throw new PermissionsError( 'deletechangetags' );
329 }
330
331 $out = $this->getOutput();
332 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
333 $out->addBacklinkSubtitle( $this->getPageTitle() );
334
335 // is the tag actually able to be deleted?
336 $canDeleteResult = ChangeTags::canDeleteTag( $tag, $user );
337 if ( !$canDeleteResult->isGood() ) {
338 $out->addWikiText( "<div class=\"error\">\n" . $canDeleteResult->getWikiText() .
339 "\n</div>" );
340 if ( !$canDeleteResult->isOK() ) {
341 return;
342 }
343 }
344
345 $preText = $this->msg( 'tags-delete-explanation-initial', $tag )->parseAsBlock();
346 $tagUsage = ChangeTags::tagUsageStatistics();
347 if ( isset( $tagUsage[$tag] ) && $tagUsage[$tag] > 0 ) {
348 $preText .= $this->msg( 'tags-delete-explanation-in-use', $tag,
349 $tagUsage[$tag] )->parseAsBlock();
350 }
351 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
352
353 // see if the tag is in use
354 $this->extensionActivatedTags = array_fill_keys(
355 ChangeTags::listExtensionActivatedTags(), true );
356 if ( isset( $this->extensionActivatedTags[$tag] ) ) {
357 $preText .= $this->msg( 'tags-delete-explanation-active', $tag )->parseAsBlock();
358 }
359
360 $fields = [];
361 $fields['Reason'] = [
362 'type' => 'text',
363 'label' => $this->msg( 'tags-delete-reason' )->plain(),
364 'size' => 50,
365 ];
366 $fields['HiddenTag'] = [
367 'type' => 'hidden',
368 'name' => 'tag',
369 'default' => $tag,
370 'required' => true,
371 ];
372
373 $form = new HTMLForm( $fields, $this->getContext() );
374 $form->setAction( $this->getPageTitle( 'delete' )->getLocalURL() );
375 $form->tagAction = 'delete'; // custom property on HTMLForm object
376 $form->setSubmitCallback( [ $this, 'processTagForm' ] );
377 $form->setSubmitTextMsg( 'tags-delete-submit' );
378 $form->setSubmitDestructive(); // nasty!
379 $form->addPreText( $preText );
380 $form->show();
381 }
382
383 protected function showActivateDeactivateForm( $tag, $activate ) {
384 $actionStr = $activate ? 'activate' : 'deactivate';
385
386 $user = $this->getUser();
387 if ( !$user->isAllowed( 'managechangetags' ) ) {
388 throw new PermissionsError( 'managechangetags' );
389 }
390
391 $out = $this->getOutput();
392 // tags-activate-title, tags-deactivate-title
393 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
394 $out->addBacklinkSubtitle( $this->getPageTitle() );
395
396 // is it possible to do this?
397 $func = $activate ? 'canActivateTag' : 'canDeactivateTag';
398 $result = ChangeTags::$func( $tag, $user );
399 if ( !$result->isGood() ) {
400 $out->addWikiText( "<div class=\"error\">\n" . $result->getWikiText() .
401 "\n</div>" );
402 if ( !$result->isOK() ) {
403 return;
404 }
405 }
406
407 // tags-activate-question, tags-deactivate-question
408 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
409
410 $fields = [];
411 // tags-activate-reason, tags-deactivate-reason
412 $fields['Reason'] = [
413 'type' => 'text',
414 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
415 'size' => 50,
416 ];
417 $fields['HiddenTag'] = [
418 'type' => 'hidden',
419 'name' => 'tag',
420 'default' => $tag,
421 'required' => true,
422 ];
423
424 $form = new HTMLForm( $fields, $this->getContext() );
425 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
426 $form->tagAction = $actionStr;
427 $form->setSubmitCallback( [ $this, 'processTagForm' ] );
428 // tags-activate-submit, tags-deactivate-submit
429 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
430 $form->addPreText( $preText );
431 $form->show();
432 }
433
434 public function processTagForm( array $data, HTMLForm $form ) {
435 $context = $form->getContext();
436 $out = $context->getOutput();
437
438 $tag = $data['HiddenTag'];
439 $status = call_user_func( [ 'ChangeTags', "{$form->tagAction}TagWithChecks" ],
440 $tag, $data['Reason'], $context->getUser(), true );
441
442 if ( $status->isGood() ) {
443 $out->redirect( $this->getPageTitle()->getLocalURL() );
444 return true;
445 } elseif ( $status->isOK() && $form->tagAction === 'delete' ) {
446 // deletion succeeded, but hooks raised a warning
447 $out->addWikiText( $this->msg( 'tags-delete-warnings-after-delete', $tag,
448 count( $status->getWarningsArray() ) )->text() . "\n" .
449 $status->getWikitext() );
450 $out->addReturnTo( $this->getPageTitle() );
451 return true;
452 } else {
453 $out->addWikiText( "<div class=\"error\">\n" . $status->getWikitext() .
454 "\n</div>" );
455 return false;
456 }
457 }
458
459 /**
460 * Return an array of subpages that this special page will accept.
461 *
462 * @return string[] subpages
463 */
464 public function getSubpagesForPrefixSearch() {
465 // The subpages does not have an own form, so not listing it at the moment
466 return [
467 // 'delete',
468 // 'activate',
469 // 'deactivate',
470 // 'create',
471 ];
472 }
473
474 protected function getGroupName() {
475 return 'changes';
476 }
477 }