Merge "Http::getProxy() method to get proxy configuration"
[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 $userCanEditInterface = $user->isAllowed( 'editinterface' );
81
82 // Show form to create a tag
83 if ( $userCanManage ) {
84 $fields = [
85 'Tag' => [
86 'type' => 'text',
87 'label' => $this->msg( 'tags-create-tag-name' )->plain(),
88 'required' => true,
89 ],
90 'Reason' => [
91 'type' => 'text',
92 'label' => $this->msg( 'tags-create-reason' )->plain(),
93 'size' => 50,
94 ],
95 'IgnoreWarnings' => [
96 'type' => 'hidden',
97 ],
98 ];
99
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( [ $this, 'processCreateTagForm' ] );
105 $form->setSubmitTextMsg( 'tags-create-submit' );
106 $form->show();
107
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;
117 }
118 }
119
120 // Used to get hitcounts for #doTagRow()
121 $tagStats = ChangeTags::tagUsageStatistics();
122
123 // Used in #doTagRow()
124 $this->explicitlyDefinedTags = array_fill_keys(
125 ChangeTags::listExplicitlyDefinedTags(), true );
126 $this->extensionDefinedTags = array_fill_keys(
127 ChangeTags::listExtensionDefinedTags(), true );
128
129 // List all defined tags, even if they were never applied
130 $definedTags = array_keys( array_merge(
131 $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, $userCanEditInterface );
158 }
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 );
163 }
164 }
165
166 $out->addHTML( Xml::tags(
167 'table',
168 [ 'class' => 'mw-datatable sortable mw-tags-table' ],
169 $html
170 ) );
171 }
172
173 function doTagRow( $tag, $hitcount, $showActions, $showEditLinks ) {
174 $newRow = '';
175 $newRow .= Xml::tags( 'td', null, Xml::element( 'code', null, $tag ) );
176
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()
183 );
184 $disp .= $this->msg( 'parentheses' )->rawParams( $editLink )->escaped();
185 }
186 $newRow .= Xml::tags( 'td', null, $disp );
187
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()
195 );
196 $desc .= $this->msg( 'parentheses' )->rawParams( $editDescLink )->escaped();
197 }
198 $newRow .= Xml::tags( 'td', null, $desc );
199
200 $sourceMsgs = [];
201 $isExtension = isset( $this->extensionDefinedTags[$tag] );
202 $isExplicit = isset( $this->explicitlyDefinedTags[$tag] );
203 if ( $isExtension ) {
204 $sourceMsgs[] = $this->msg( 'tags-source-extension' )->escaped();
205 }
206 if ( $isExplicit ) {
207 $sourceMsgs[] = $this->msg( 'tags-source-manual' )->escaped();
208 }
209 if ( !$sourceMsgs ) {
210 $sourceMsgs[] = $this->msg( 'tags-source-none' )->escaped();
211 }
212 $newRow .= Xml::tags( 'td', null, implode( Xml::element( 'br' ), $sourceMsgs ) );
213
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() );
217
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 [],
224 [ 'tagfilter' => $tag ]
225 );
226 }
227
228 // add raw $hitcount for sorting, because tags-hitcount contains numbers and letters
229 $newRow .= Xml::tags( 'td', [ 'data-sort-value' => $hitcount ], $hitcountLabel );
230
231 // actions
232 if ( $showActions ) { // we've already checked that the user had the requisite userright
233 $actionLinks = [];
234
235 // delete
236 if ( ChangeTags::canDeleteTag( $tag )->isOK() ) {
237 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'delete' ),
238 $this->msg( 'tags-delete' )->escaped(),
239 [],
240 [ 'tag' => $tag ] );
241 }
242
243 // activate
244 if ( ChangeTags::canActivateTag( $tag )->isOK() ) {
245 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'activate' ),
246 $this->msg( 'tags-activate' )->escaped(),
247 [],
248 [ 'tag' => $tag ] );
249 }
250
251 // deactivate
252 if ( ChangeTags::canDeactivateTag( $tag )->isOK() ) {
253 $actionLinks[] = Linker::linkKnown( $this->getPageTitle( 'deactivate' ),
254 $this->msg( 'tags-deactivate' )->escaped(),
255 [],
256 [ 'tag' => $tag ] );
257 }
258
259 $newRow .= Xml::tags( 'td', null, $this->getLanguage()->pipeList( $actionLinks ) );
260 }
261
262 return Xml::tags( 'tr', null, $newRow ) . "\n";
263 }
264
265 public function processCreateTagForm( array $data, HTMLForm $form ) {
266 $context = $form->getContext();
267 $out = $context->getOutput();
268
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 );
273
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 = [
280 'Tag' => [
281 'type' => 'hidden',
282 'default' => $data['Tag'],
283 ],
284 'Reason' => [
285 'type' => 'hidden',
286 'default' => $data['Reason'],
287 ],
288 'IgnoreWarnings' => [
289 'type' => 'hidden',
290 'default' => '1',
291 ],
292 ];
293
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' );
297
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();
302
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( [ $this, 'processCreateTagForm' ] );
308 $subform->setSubmitTextMsg( 'htmlform-yes' );
309 $subform->show();
310
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;
317 }
318 }
319
320 protected function showDeleteTagForm( $tag ) {
321 $user = $this->getUser();
322 if ( !$user->isAllowed( 'managechangetags' ) ) {
323 throw new PermissionsError( 'managechangetags' );
324 }
325
326 $out = $this->getOutput();
327 $out->setPageTitle( $this->msg( 'tags-delete-title' ) );
328 $out->addBacklinkSubtitle( $this->getPageTitle() );
329
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;
337 }
338 }
339
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();
345 }
346 $preText .= $this->msg( 'tags-delete-explanation-warning', $tag )->parseAsBlock();
347
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();
353 }
354
355 $fields = [];
356 $fields['Reason'] = [
357 'type' => 'text',
358 'label' => $this->msg( 'tags-delete-reason' )->plain(),
359 'size' => 50,
360 ];
361 $fields['HiddenTag'] = [
362 'type' => 'hidden',
363 'name' => 'tag',
364 'default' => $tag,
365 'required' => true,
366 ];
367
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( [ $this, 'processTagForm' ] );
372 $form->setSubmitTextMsg( 'tags-delete-submit' );
373 $form->setSubmitDestructive(); // nasty!
374 $form->addPreText( $preText );
375 $form->show();
376 }
377
378 protected function showActivateDeactivateForm( $tag, $activate ) {
379 $actionStr = $activate ? 'activate' : 'deactivate';
380
381 $user = $this->getUser();
382 if ( !$user->isAllowed( 'managechangetags' ) ) {
383 throw new PermissionsError( 'managechangetags' );
384 }
385
386 $out = $this->getOutput();
387 // tags-activate-title, tags-deactivate-title
388 $out->setPageTitle( $this->msg( "tags-$actionStr-title" ) );
389 $out->addBacklinkSubtitle( $this->getPageTitle() );
390
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;
399 }
400 }
401
402 // tags-activate-question, tags-deactivate-question
403 $preText = $this->msg( "tags-$actionStr-question", $tag )->parseAsBlock();
404
405 $fields = [];
406 // tags-activate-reason, tags-deactivate-reason
407 $fields['Reason'] = [
408 'type' => 'text',
409 'label' => $this->msg( "tags-$actionStr-reason" )->plain(),
410 'size' => 50,
411 ];
412 $fields['HiddenTag'] = [
413 'type' => 'hidden',
414 'name' => 'tag',
415 'default' => $tag,
416 'required' => true,
417 ];
418
419 $form = new HTMLForm( $fields, $this->getContext() );
420 $form->setAction( $this->getPageTitle( $actionStr )->getLocalURL() );
421 $form->tagAction = $actionStr;
422 $form->setSubmitCallback( [ $this, 'processTagForm' ] );
423 // tags-activate-submit, tags-deactivate-submit
424 $form->setSubmitTextMsg( "tags-$actionStr-submit" );
425 $form->addPreText( $preText );
426 $form->show();
427 }
428
429 public function processTagForm( array $data, HTMLForm $form ) {
430 $context = $form->getContext();
431 $out = $context->getOutput();
432
433 $tag = $data['HiddenTag'];
434 $status = call_user_func( [ 'ChangeTags', "{$form->tagAction}TagWithChecks" ],
435 $tag, $data['Reason'], $context->getUser(), true );
436
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;
451 }
452 }
453
454 /**
455 * Return an array of subpages that this special page will accept.
456 *
457 * @return string[] subpages
458 */
459 public function getSubpagesForPrefixSearch() {
460 // The subpages does not have an own form, so not listing it at the moment
461 return [
462 // 'delete',
463 // 'activate',
464 // 'deactivate',
465 // 'create',
466 ];
467 }
468
469 protected function getGroupName() {
470 return 'changes';
471 }
472 }