Debian Codegraph @

includes/specials/SpecialEditTags.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials;
22
23 use ChangeTagsList;
24 use ErrorPageError;
25 use LogEventsList;
26 use LogPage;
27 use MediaWiki\ChangeTags\ChangeTagsStore;
28 use MediaWiki\CommentStore\CommentStore;
29 use MediaWiki\Html\Html;
30 use MediaWiki\Permissions\PermissionManager;
31 use MediaWiki\SpecialPage\SpecialPage;
32 use MediaWiki\SpecialPage\UnlistedSpecialPage;
33 use MediaWiki\Status\Status;
34 use MediaWiki\Title\Title;
35 use MediaWiki\Xml\XmlSelect;
36 use RevisionDeleter;
37 use UserBlockedError;
38
39 /**
40 * Add or remove change tags to individual revisions.
41 *
42 * A lot of this was copied out of SpecialRevisiondelete.
43 *
44 * @ingroup SpecialPage
45 * @since 1.25
46 */
47 class SpecialEditTags extends UnlistedSpecialPage {
48 /** @var bool Was the DB modified in this request */
49 protected $wasSaved = false;
50
51 /** @var bool True if the submit button was clicked, and the form was posted */
52 private $submitClicked;
53
54 /** @var array Target ID list */
55 private $ids;
56
57 /** @var Title Title object for target parameter */
58 private $targetObj;
59
60 /** @var string Deletion type, may be revision or logentry */
61 private $typeName;
62
63 /** @var ChangeTagsList Storing the list of items to be tagged */
64 private $revList;
65
66 /** @var string */
67 private $reason;
68
69 private PermissionManager $permissionManager;
70 private ChangeTagsStore $changeTagsStore;
71
72 /**
73 * @inheritDoc
74 *
75 * @param PermissionManager $permissionManager
76 */
77 public function __construct( PermissionManager $permissionManager, ChangeTagsStore $changeTagsStore ) {
78 parent::__construct( 'EditTags', 'changetags' );
79
80 $this->permissionManager = $permissionManager;
81 $this->changeTagsStore = $changeTagsStore;
82 }
83
84 public function doesWrites() {
85 return true;
86 }
87
88 public function execute( $par ) {
89 $this->checkPermissions();
90 $this->checkReadOnly();
91
92 $output = $this->getOutput();
93 $user = $this->getUser();
94 $request = $this->getRequest();
95
96 $this->setHeaders();
97 $this->outputHeader();
98
99 $output->addModules( [ 'mediawiki.misc-authed-curate' ] );
100 $output->addModuleStyles( [
101 'mediawiki.interface.helpers.styles',
102 'mediawiki.special'
103 ] );
104
105 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
106
107 // Handle our many different possible input types
108 $ids = $request->getVal( 'ids' );
109 if ( $ids !== null ) {
110 // Allow CSV from the form hidden field, or a single ID for show/hide links
111 $this->ids = explode( ',', $ids );
112 } else {
113 // Array input
114 $this->ids = array_keys( $request->getArray( 'ids', [] ) );
115 }
116 $this->ids = array_unique( array_filter( $this->ids ) );
117
118 // No targets?
119 if ( count( $this->ids ) == 0 ) {
120 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
121 }
122
123 $this->typeName = $request->getVal( 'type' );
124 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
125
126 switch ( $this->typeName ) {
127 case 'logentry':
128 case 'logging':
129 $this->typeName = 'logentry';
130 break;
131 default:
132 $this->typeName = 'revision';
133 break;
134 }
135
136 // Allow the list type to adjust the passed target
137 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
138 // what we want
139 $this->targetObj = RevisionDeleter::suggestTarget(
140 $this->typeName === 'revision' ? 'revision' : 'logging',
141 $this->targetObj,
142 $this->ids
143 );
144
145 $this->reason = $request->getVal( 'wpReason', '' );
146 // We need a target page!
147 if ( $this->targetObj === null ) {
148 $output->addWikiMsg( 'undelete-header' );
149 return;
150 }
151
152 // Check blocks
153 $checkReplica = !$this->submitClicked;
154 if (
155 $this->permissionManager->isBlockedFrom(
156 $user,
157 $this->targetObj,
158 $checkReplica
159 )
160 ) {
161 throw new UserBlockedError(
162 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
163 $user->getBlock(),
164 $user,
165 $this->getLanguage(),
166 $request->getIP()
167 );
168 }
169
170 // Give a link to the logs/hist for this page
171 $this->showConvenienceLinks();
172
173 // Either submit or create our form
174 if ( $this->submitClicked ) {
175 $this->submit();
176 } else {
177 $this->showForm();
178 }
179
180 // Show relevant lines from the tag log
181 $tagLogPage = new LogPage( 'tag' );
182 $output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" );
183 LogEventsList::showLogExtract(
184 $output,
185 'tag',
186 $this->targetObj,
187 '', /* user */
188 [ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved ]
189 );
190 }
191
192 /**
193 * Show some useful links in the subtitle
194 */
195 protected function showConvenienceLinks() {
196 // Give a link to the logs/hist for this page
197 if ( $this->targetObj ) {
198 // Also set header tabs to be for the target.
199 $this->getSkin()->setRelevantTitle( $this->targetObj );
200
201 $linkRenderer = $this->getLinkRenderer();
202 $links = [];
203 $links[] = $linkRenderer->makeKnownLink(
204 SpecialPage::getTitleFor( 'Log' ),
205 $this->msg( 'viewpagelogs' )->text(),
206 [],
207 [
208 'page' => $this->targetObj->getPrefixedText(),
209 'wpfilters' => [ 'tag' ],
210 ]
211 );
212 if ( !$this->targetObj->isSpecialPage() ) {
213 // Give a link to the page history
214 $links[] = $linkRenderer->makeKnownLink(
215 $this->targetObj,
216 $this->msg( 'pagehist' )->text(),
217 [],
218 [ 'action' => 'history' ]
219 );
220 }
221 // Link to Special:Tags
222 $links[] = $linkRenderer->makeKnownLink(
223 SpecialPage::getTitleFor( 'Tags' ),
224 $this->msg( 'tags-edit-manage-link' )->text()
225 );
226 // Logs themselves don't have histories or archived revisions
227 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
228 }
229 }
230
231 /**
232 * Get the list object for this request
233 * @return ChangeTagsList
234 */
235 protected function getList() {
236 if ( $this->revList === null ) {
237 $this->revList = ChangeTagsList::factory( $this->typeName, $this->getContext(),
238 $this->targetObj, $this->ids );
239 }
240
241 return $this->revList;
242 }
243
244 /**
245 * Show a list of items that we will operate on, and show a form which allows
246 * the user to modify the tags applied to those items.
247 */
248 protected function showForm() {
249 $out = $this->getOutput();
250 // Messages: tags-edit-revision-selected, tags-edit-logentry-selected
251 $out->wrapWikiMsg( "<strong>$1</strong>", [
252 "tags-edit-{$this->typeName}-selected",
253 $this->getLanguage()->formatNum( count( $this->ids ) ),
254 $this->targetObj->getPrefixedText()
255 ] );
256
257 $this->addHelpLink( 'Help:Tags' );
258 $out->addHTML( "<ul>" );
259
260 $numRevisions = 0;
261 // Live revisions...
262 $list = $this->getList();
263 for ( $list->reset(); $list->current(); $list->next() ) {
264 $item = $list->current();
265 if ( !$item->canView() ) {
266 throw new ErrorPageError( 'permissionserrors', 'tags-update-no-permission' );
267 }
268 $numRevisions++;
269 $out->addHTML( $item->getHTML() );
270 }
271
272 if ( !$numRevisions ) {
273 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
274 }
275
276 $out->addHTML( "</ul>" );
277 // Explanation text
278 $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
279
280 // Show form
281 $form = Html::openElement( 'form', [ 'method' => 'post',
282 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
283 'id' => 'mw-revdel-form-revisions' ] ) .
284 Html::openElement( 'fieldset' ) .
285 Html::element(
286 'legend', [],
287 $this->msg( "tags-edit-{$this->typeName}-legend", count( $this->ids ) )->text()
288 ) .
289 $this->buildCheckBoxes() .
290 Html::openElement( 'table' ) .
291 "<tr>\n" .
292 '<td class="mw-label">' .
293 Html::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
294 '</td>' .
295 '<td class="mw-input">' .
296 Html::element( 'input', [ 'name' => 'wpReason', 'size' => 60, 'value' => $this->reason,
297 'id' => 'wpReason',
298 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
299 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
300 // Unicode codepoints.
301 'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
302 ] ) .
303 '</td>' .
304 "</tr><tr>\n" .
305 '<td></td>' .
306 '<td class="mw-submit">' .
307 Html::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
308 $numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) .
309 '</td>' .
310 "</tr>\n" .
311 Html::closeElement( 'table' ) .
312 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
313 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
314 Html::hidden( 'type', $this->typeName ) .
315 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
316 Html::closeElement( 'fieldset' ) . "\n" .
317 Html::closeElement( 'form' ) . "\n";
318
319 $out->addHTML( $form );
320 }
321
322 /**
323 * @return string HTML
324 */
325 protected function buildCheckBoxes() {
326 // If there is just one item, provide the user with a multi-select field
327 $list = $this->getList();
328 $tags = [];
329 if ( $list->length() == 1 ) {
330 $list->reset();
331 $tags = $list->current()->getTags();
332 if ( $tags ) {
333 $tags = explode( ',', $tags );
334 } else {
335 $tags = [];
336 }
337
338 $html = '<table id="mw-edittags-tags-selector">';
339 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
340 '</td><td>';
341 if ( $tags ) {
342 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
343 } else {
344 $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
345 }
346 $html .= '</td></tr>';
347 $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
348 $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
349 } else {
350 // Otherwise, use a multi-select field for adding tags, and a list of
351 // checkboxes for removing them
352
353 for ( $list->reset(); $list->current(); $list->next() ) {
354 $currentTags = $list->current()->getTags();
355 if ( $currentTags ) {
356 $tags = array_merge( $tags, explode( ',', $currentTags ) );
357 }
358 }
359 $tags = array_unique( $tags );
360
361 $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
362 $tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() );
363 $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
364 $html .= Html::element( 'p', [], $this->msg( 'tags-edit-remove' )->plain() );
365 $html .= Html::element( 'input', [
366 'type' => 'checkbox', 'name' => 'wpRemoveAllTags', 'value' => '1',
367 'id' => 'mw-edittags-remove-all'
368 ] ) . '&nbsp;'
369 . Html::label( $this->msg( 'tags-edit-remove-all-tags' )->plain(), 'mw-edittags-remove-all' );
370 $i = 0; // used for generating checkbox IDs only
371 foreach ( $tags as $tag ) {
372 $id = 'mw-edittags-remove-' . $i++;
373 $html .= Html::element( 'br' ) . "\n" . Html::element( 'input', [
374 'type' => 'checkbox', 'name' => 'wpTagsToRemove[]', 'value' => $tag,
375 'class' => 'mw-edittags-remove-checkbox', 'id' => $id,
376 ] ) . '&nbsp;' . Html::label( $tag, $id );
377 }
378 }
379
380 // also output the tags currently applied as a hidden form field, so we
381 // know what to remove from the revision/log entry when the form is submitted
382 $html .= Html::hidden( 'wpExistingTags', implode( ',', $tags ) );
383 $html .= '</td></tr></table>';
384
385 return $html;
386 }
387
388 /**
389 * Returns a <select multiple> element with a list of change tags that can be
390 * applied by users.
391 *
392 * @param array $selectedTags The tags that should be preselected in the
393 * list. Any tags in this list, but not in the list returned by
394 * ChangeTagsStore::listExplicitlyDefinedTags, will be appended to the <select>
395 * element.
396 * @param string $label The text of a <label> to precede the <select>
397 * @return array HTML <label> element at index 0, HTML <select> element at
398 * index 1
399 */
400 protected function getTagSelect( $selectedTags, $label ) {
401 $result = [];
402 $result[0] = Html::label( $label, 'mw-edittags-tag-list' );
403
404 $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
405 $select->setAttribute( 'multiple', 'multiple' );
406 $select->setAttribute( 'size', '8' );
407
408 $tags = $this->changeTagsStore->listExplicitlyDefinedTags();
409 $tags = array_unique( array_merge( $tags, $selectedTags ) );
410
411 // Values of $tags are also used as <option> labels
412 $select->addOptions( array_combine( $tags, $tags ) );
413
414 $result[1] = $select->getHTML();
415 return $result;
416 }
417
418 /**
419 * UI entry point for form submission.
420 * @return bool
421 */
422 protected function submit() {
423 // Check edit token on submission
424 $request = $this->getRequest();
425 $token = $request->getVal( 'wpEditToken' );
426 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
427 $this->getOutput()->addWikiMsg( 'sessionfailure' );
428 return false;
429 }
430
431 // Evaluate incoming request data
432 $tagList = $request->getArray( 'wpTagList' ) ?? [];
433 $existingTags = $request->getVal( 'wpExistingTags' );
434 if ( $existingTags === null || $existingTags === '' ) {
435 $existingTags = [];
436 } else {
437 $existingTags = explode( ',', $existingTags );
438 }
439
440 if ( count( $this->ids ) > 1 ) {
441 // multiple revisions selected
442 $tagsToAdd = $tagList;
443 if ( $request->getBool( 'wpRemoveAllTags' ) ) {
444 $tagsToRemove = $existingTags;
445 } else {
446 $tagsToRemove = $request->getArray( 'wpTagsToRemove', [] );
447 }
448 } else {
449 // single revision selected
450 // The user tells us which tags they want associated to the revision.
451 // We have to figure out which ones to add, and which to remove.
452 $tagsToAdd = array_diff( $tagList, $existingTags );
453 $tagsToRemove = array_diff( $existingTags, $tagList );
454 }
455
456 if ( !$tagsToAdd && !$tagsToRemove ) {
457 $status = Status::newFatal( 'tags-edit-none-selected' );
458 } else {
459 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
460 $tagsToRemove, null, $this->reason, $this->getAuthority() );
461 }
462
463 if ( $status->isGood() ) {
464 $this->success();
465 return true;
466 } else {
467 $this->failure( $status );
468 return false;
469 }
470 }
471
472 /**
473 * Report that the submit operation succeeded
474 */
475 protected function success() {
476 $out = $this->getOutput();
477 $out->setPageTitleMsg( $this->msg( 'actioncomplete' ) );
478 $out->addHTML(
479 Html::successBox( $out->msg( 'tags-edit-success' )->parse() )
480 );
481 $this->wasSaved = true;
482 $this->revList->reloadFromPrimary();
483 $this->reason = ''; // no need to spew the reason back at the user
484 $this->showForm();
485 }
486
487 /**
488 * Report that the submit operation failed
489 * @param Status $status
490 */
491 protected function failure( $status ) {
492 $out = $this->getOutput();
493 $out->setPageTitleMsg( $this->msg( 'actionfailed' ) );
494 $out->addHTML(
495 Html::errorBox(
496 $out->parseAsContent(
497 $status->getWikiText( 'tags-edit-failure', false, $this->getLanguage() )
498 )
499 )
500 );
501 $this->showForm();
502 }
503
504 public function getDescription() {
505 return $this->msg( 'tags-edit-title' );
506 }
507
508 protected function getGroupName() {
509 return 'pagetools';
510 }
511 }
512
513 /** @deprecated class alias since 1.41 */
514 class_alias( SpecialEditTags::class, 'SpecialEditTags' );