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.
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.
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
19 * @ingroup SpecialPage
23 * Special page for adding and removing change tags to individual revisions.
24 * A lot of this is copied out of SpecialRevisiondelete.
26 * @ingroup SpecialPage
29 class SpecialEditTags
extends UnlistedSpecialPage
{
30 /** @var bool Was the DB modified in this request */
31 protected $wasSaved = false;
33 /** @var bool True if the submit button was clicked, and the form was posted */
34 private $submitClicked;
36 /** @var array Target ID list */
39 /** @var Title Title object for target parameter */
42 /** @var string Deletion type, may be revision or logentry */
45 /** @var ChangeTagsList Storing the list of items to be tagged */
48 /** @var bool Whether user is allowed to perform the action */
54 public function __construct() {
55 parent
::__construct( 'EditTags', 'changetags' );
58 public function doesWrites() {
62 public function execute( $par ) {
63 $this->checkPermissions();
64 $this->checkReadOnly();
66 $output = $this->getOutput();
67 $user = $this->getUser();
68 $request = $this->getRequest();
71 // @TODO Use PermissionManager::isBlockedFrom() instead.
72 $block = $user->getBlock();
74 throw new UserBlockedError( $block );
78 $this->outputHeader();
80 $output->addModules( [ 'mediawiki.special.edittags' ] );
81 $output->addModuleStyles( [
82 'mediawiki.interface.helpers.styles',
86 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
88 // Handle our many different possible input types
89 $ids = $request->getVal( 'ids' );
90 if ( !is_null( $ids ) ) {
91 // Allow CSV from the form hidden field, or a single ID for show/hide links
92 $this->ids
= explode( ',', $ids );
95 $this->ids
= array_keys( $request->getArray( 'ids', [] ) );
97 $this->ids
= array_unique( array_filter( $this->ids
) );
100 if ( count( $this->ids
) == 0 ) {
101 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
104 $this->typeName
= $request->getVal( 'type' );
105 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
107 // sanity check of parameter
108 switch ( $this->typeName
) {
111 $this->typeName
= 'logentry';
114 $this->typeName
= 'revision';
118 // Allow the list type to adjust the passed target
119 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
121 $this->targetObj
= RevisionDeleter
::suggestTarget(
122 $this->typeName
=== 'revision' ?
'revision' : 'logging',
127 $this->isAllowed
= $user->isAllowed( 'changetags' );
129 $this->reason
= $request->getVal( 'wpReason' );
130 // We need a target page!
131 if ( is_null( $this->targetObj
) ) {
132 $output->addWikiMsg( 'undelete-header' );
135 // Give a link to the logs/hist for this page
136 $this->showConvenienceLinks();
138 // Either submit or create our form
139 if ( $this->isAllowed
&& $this->submitClicked
) {
145 // Show relevant lines from the tag log
146 $tagLogPage = new LogPage( 'tag' );
147 $output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" );
148 LogEventsList
::showLogExtract(
153 [ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved
]
158 * Show some useful links in the subtitle
160 protected function showConvenienceLinks() {
161 // Give a link to the logs/hist for this page
162 if ( $this->targetObj
) {
163 // Also set header tabs to be for the target.
164 $this->getSkin()->setRelevantTitle( $this->targetObj
);
166 $linkRenderer = $this->getLinkRenderer();
168 $links[] = $linkRenderer->makeKnownLink(
169 SpecialPage
::getTitleFor( 'Log' ),
170 $this->msg( 'viewpagelogs' )->text(),
173 'page' => $this->targetObj
->getPrefixedText(),
174 'wpfilters' => [ 'tag' ],
177 if ( !$this->targetObj
->isSpecialPage() ) {
178 // Give a link to the page history
179 $links[] = $linkRenderer->makeKnownLink(
181 $this->msg( 'pagehist' )->text(),
183 [ 'action' => 'history' ]
186 // Link to Special:Tags
187 $links[] = $linkRenderer->makeKnownLink(
188 SpecialPage
::getTitleFor( 'Tags' ),
189 $this->msg( 'tags-edit-manage-link' )->text()
191 // Logs themselves don't have histories or archived revisions
192 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
197 * Get the list object for this request
198 * @return ChangeTagsList
200 protected function getList() {
201 if ( is_null( $this->revList
) ) {
202 $this->revList
= ChangeTagsList
::factory( $this->typeName
, $this->getContext(),
203 $this->targetObj
, $this->ids
);
206 return $this->revList
;
210 * Show a list of items that we will operate on, and show a form which allows
211 * the user to modify the tags applied to those items.
213 protected function showForm() {
214 $out = $this->getOutput();
215 // Messages: tags-edit-revision-selected, tags-edit-logentry-selected
216 $out->wrapWikiMsg( "<strong>$1</strong>", [
217 "tags-edit-{$this->typeName}-selected",
218 $this->getLanguage()->formatNum( count( $this->ids
) ),
219 $this->targetObj
->getPrefixedText()
222 $this->addHelpLink( 'Help:Tags' );
223 $out->addHTML( "<ul>" );
227 $list = $this->getList();
228 for ( $list->reset(); $list->current(); $list->next() ) {
229 $item = $list->current();
230 if ( !$item->canView() ) {
231 throw new ErrorPageError( 'permissionserrors', 'tags-update-no-permission' );
234 $out->addHTML( $item->getHTML() );
237 if ( !$numRevisions ) {
238 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
241 $out->addHTML( "</ul>" );
243 $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
245 // Show form if the user can submit
246 if ( $this->isAllowed
) {
247 $form = Xml
::openElement( 'form', [ 'method' => 'post',
248 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
249 'id' => 'mw-revdel-form-revisions' ] ) .
250 Xml
::fieldset( $this->msg( "tags-edit-{$this->typeName}-legend",
251 count( $this->ids
) )->text() ) .
252 $this->buildCheckBoxes() .
253 Xml
::openElement( 'table' ) .
255 '<td class="mw-label">' .
256 Xml
::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
258 '<td class="mw-input">' .
259 Xml
::input( 'wpReason', 60, $this->reason
, [
261 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
262 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
263 // Unicode codepoints.
264 // "- 155" is to leave room for the auto-generated part of the log entry.
265 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
- 155,
270 '<td class="mw-submit">' .
271 Xml
::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
272 $numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) .
275 Xml
::closeElement( 'table' ) .
276 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
277 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
278 Html
::hidden( 'type', $this->typeName
) .
279 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
280 Xml
::closeElement( 'fieldset' ) . "\n" .
281 Xml
::closeElement( 'form' ) . "\n";
285 $out->addHTML( $form );
289 * @return string HTML
291 protected function buildCheckBoxes() {
292 // If there is just one item, provide the user with a multi-select field
293 $list = $this->getList();
295 if ( $list->length() == 1 ) {
297 $tags = $list->current()->getTags();
299 $tags = explode( ',', $tags );
304 $html = '<table id="mw-edittags-tags-selector">';
305 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
308 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
310 $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
312 $html .= '</td></tr>';
313 $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
314 $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
316 // Otherwise, use a multi-select field for adding tags, and a list of
317 // checkboxes for removing them
319 for ( $list->reset(); $list->current(); $list->next() ) {
320 $currentTags = $list->current()->getTags();
321 if ( $currentTags ) {
322 $tags = array_merge( $tags, explode( ',', $currentTags ) );
325 $tags = array_unique( $tags );
327 $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
328 $tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() );
329 $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
330 $html .= Xml
::element( 'p', null, $this->msg( 'tags-edit-remove' )->plain() );
331 $html .= Xml
::checkLabel( $this->msg( 'tags-edit-remove-all-tags' )->plain(),
332 'wpRemoveAllTags', 'mw-edittags-remove-all' );
333 $i = 0; // used for generating checkbox IDs only
334 foreach ( $tags as $tag ) {
335 $html .= Xml
::element( 'br' ) . "\n" . Xml
::checkLabel( $tag,
336 'wpTagsToRemove[]', 'mw-edittags-remove-' . $i++
, false, [
338 'class' => 'mw-edittags-remove-checkbox',
343 // also output the tags currently applied as a hidden form field, so we
344 // know what to remove from the revision/log entry when the form is submitted
345 $html .= Html
::hidden( 'wpExistingTags', implode( ',', $tags ) );
346 $html .= '</td></tr></table>';
352 * Returns a <select multiple> element with a list of change tags that can be
355 * @param array $selectedTags The tags that should be preselected in the
356 * list. Any tags in this list, but not in the list returned by
357 * ChangeTags::listExplicitlyDefinedTags, will be appended to the <select>
359 * @param string $label The text of a <label> to precede the <select>
360 * @return array HTML <label> element at index 0, HTML <select> element at
363 protected function getTagSelect( $selectedTags, $label ) {
365 $result[0] = Xml
::label( $label, 'mw-edittags-tag-list' );
367 $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
368 $select->setAttribute( 'multiple', 'multiple' );
369 $select->setAttribute( 'size', '8' );
371 $tags = ChangeTags
::listExplicitlyDefinedTags();
372 $tags = array_unique( array_merge( $tags, $selectedTags ) );
374 // Values of $tags are also used as <option> labels
375 $select->addOptions( array_combine( $tags, $tags ) );
377 $result[1] = $select->getHTML();
382 * UI entry point for form submission.
385 protected function submit() {
386 // Check edit token on submission
387 $request = $this->getRequest();
388 $token = $request->getVal( 'wpEditToken' );
389 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
390 $this->getOutput()->addWikiMsg( 'sessionfailure' );
394 // Evaluate incoming request data
395 $tagList = $request->getArray( 'wpTagList' );
396 if ( is_null( $tagList ) ) {
399 $existingTags = $request->getVal( 'wpExistingTags' );
400 if ( is_null( $existingTags ) ||
$existingTags === '' ) {
403 $existingTags = explode( ',', $existingTags );
406 if ( count( $this->ids
) > 1 ) {
407 // multiple revisions selected
408 $tagsToAdd = $tagList;
409 if ( $request->getBool( 'wpRemoveAllTags' ) ) {
410 $tagsToRemove = $existingTags;
412 $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
415 // single revision selected
416 // The user tells us which tags they want associated to the revision.
417 // We have to figure out which ones to add, and which to remove.
418 $tagsToAdd = array_diff( $tagList, $existingTags );
419 $tagsToRemove = array_diff( $existingTags, $tagList );
422 if ( !$tagsToAdd && !$tagsToRemove ) {
423 $status = Status
::newFatal( 'tags-edit-none-selected' );
425 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
426 $tagsToRemove, null, $this->reason
, $this->getUser() );
429 if ( $status->isGood() ) {
433 $this->failure( $status );
439 * Report that the submit operation succeeded
441 protected function success() {
442 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
443 $this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
444 'tags-edit-success' );
445 $this->wasSaved
= true;
446 $this->revList
->reloadFromMaster();
447 $this->reason
= ''; // no need to spew the reason back at the user
452 * Report that the submit operation failed
453 * @param Status $status
455 protected function failure( $status ) {
456 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
457 $this->getOutput()->wrapWikiTextAsInterface(
458 'errorbox', $status->getWikiText( 'tags-edit-failure' )
463 public function getDescription() {
464 return $this->msg( 'tags-edit-title' )->text();
467 protected function getGroupName() {