6807ed31da454613033967a8f8c0abeb696b512a
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 if ( $user->isBlocked() ) {
72 throw new UserBlockedError( $user->getBlock() );
76 $this->outputHeader();
78 $output->addModules( [ 'mediawiki.special.edittags' ] );
79 $output->addModuleStyles( [
80 'mediawiki.interface.helpers.styles',
84 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
86 // Handle our many different possible input types
87 $ids = $request->getVal( 'ids' );
88 if ( !is_null( $ids ) ) {
89 // Allow CSV from the form hidden field, or a single ID for show/hide links
90 $this->ids
= explode( ',', $ids );
93 $this->ids
= array_keys( $request->getArray( 'ids', [] ) );
95 $this->ids
= array_unique( array_filter( $this->ids
) );
98 if ( count( $this->ids
) == 0 ) {
99 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
102 $this->typeName
= $request->getVal( 'type' );
103 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
105 // sanity check of parameter
106 switch ( $this->typeName
) {
109 $this->typeName
= 'logentry';
112 $this->typeName
= 'revision';
116 // Allow the list type to adjust the passed target
117 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
119 $this->targetObj
= RevisionDeleter
::suggestTarget(
120 $this->typeName
=== 'revision' ?
'revision' : 'logging',
125 $this->isAllowed
= $user->isAllowed( 'changetags' );
127 $this->reason
= $request->getVal( 'wpReason' );
128 // We need a target page!
129 if ( is_null( $this->targetObj
) ) {
130 $output->addWikiMsg( 'undelete-header' );
133 // Give a link to the logs/hist for this page
134 $this->showConvenienceLinks();
136 // Either submit or create our form
137 if ( $this->isAllowed
&& $this->submitClicked
) {
143 // Show relevant lines from the tag log
144 $tagLogPage = new LogPage( 'tag' );
145 $output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" );
146 LogEventsList
::showLogExtract(
151 [ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved
]
156 * Show some useful links in the subtitle
158 protected function showConvenienceLinks() {
159 // Give a link to the logs/hist for this page
160 if ( $this->targetObj
) {
161 // Also set header tabs to be for the target.
162 $this->getSkin()->setRelevantTitle( $this->targetObj
);
164 $linkRenderer = $this->getLinkRenderer();
166 $links[] = $linkRenderer->makeKnownLink(
167 SpecialPage
::getTitleFor( 'Log' ),
168 $this->msg( 'viewpagelogs' )->text(),
171 'page' => $this->targetObj
->getPrefixedText(),
172 'wpfilters' => [ 'tag' ],
175 if ( !$this->targetObj
->isSpecialPage() ) {
176 // Give a link to the page history
177 $links[] = $linkRenderer->makeKnownLink(
179 $this->msg( 'pagehist' )->text(),
181 [ 'action' => 'history' ]
184 // Link to Special:Tags
185 $links[] = $linkRenderer->makeKnownLink(
186 SpecialPage
::getTitleFor( 'Tags' ),
187 $this->msg( 'tags-edit-manage-link' )->text()
189 // Logs themselves don't have histories or archived revisions
190 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
195 * Get the list object for this request
196 * @return ChangeTagsList
198 protected function getList() {
199 if ( is_null( $this->revList
) ) {
200 $this->revList
= ChangeTagsList
::factory( $this->typeName
, $this->getContext(),
201 $this->targetObj
, $this->ids
);
204 return $this->revList
;
208 * Show a list of items that we will operate on, and show a form which allows
209 * the user to modify the tags applied to those items.
211 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();
231 $out->addHTML( $item->getHTML() );
234 if ( !$numRevisions ) {
235 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
238 $out->addHTML( "</ul>" );
240 $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
242 // Show form if the user can submit
243 if ( $this->isAllowed
) {
244 $form = Xml
::openElement( 'form', [ 'method' => 'post',
245 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
246 'id' => 'mw-revdel-form-revisions' ] ) .
247 Xml
::fieldset( $this->msg( "tags-edit-{$this->typeName}-legend",
248 count( $this->ids
) )->text() ) .
249 $this->buildCheckBoxes() .
250 Xml
::openElement( 'table' ) .
252 '<td class="mw-label">' .
253 Xml
::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
255 '<td class="mw-input">' .
256 Xml
::input( 'wpReason', 60, $this->reason
, [
258 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
259 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
260 // Unicode codepoints.
261 // "- 155" is to leave room for the auto-generated part of the log entry.
262 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
- 155,
267 '<td class="mw-submit">' .
268 Xml
::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
269 $numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) .
272 Xml
::closeElement( 'table' ) .
273 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
274 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
275 Html
::hidden( 'type', $this->typeName
) .
276 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
277 Xml
::closeElement( 'fieldset' ) . "\n" .
278 Xml
::closeElement( 'form' ) . "\n";
282 $out->addHTML( $form );
286 * @return string HTML
288 protected function buildCheckBoxes() {
289 // If there is just one item, provide the user with a multi-select field
290 $list = $this->getList();
292 if ( $list->length() == 1 ) {
294 $tags = $list->current()->getTags();
296 $tags = explode( ',', $tags );
301 $html = '<table id="mw-edittags-tags-selector">';
302 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
305 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
307 $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
309 $html .= '</td></tr>';
310 $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
311 $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
313 // Otherwise, use a multi-select field for adding tags, and a list of
314 // checkboxes for removing them
316 for ( $list->reset(); $list->current(); $list->next() ) {
317 $currentTags = $list->current()->getTags();
318 if ( $currentTags ) {
319 $tags = array_merge( $tags, explode( ',', $currentTags ) );
322 $tags = array_unique( $tags );
324 $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
325 $tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() );
326 $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
327 $html .= Xml
::element( 'p', null, $this->msg( 'tags-edit-remove' )->plain() );
328 $html .= Xml
::checkLabel( $this->msg( 'tags-edit-remove-all-tags' )->plain(),
329 'wpRemoveAllTags', 'mw-edittags-remove-all' );
330 $i = 0; // used for generating checkbox IDs only
331 foreach ( $tags as $tag ) {
332 $html .= Xml
::element( 'br' ) . "\n" . Xml
::checkLabel( $tag,
333 'wpTagsToRemove[]', 'mw-edittags-remove-' . $i++
, false, [
335 'class' => 'mw-edittags-remove-checkbox',
340 // also output the tags currently applied as a hidden form field, so we
341 // know what to remove from the revision/log entry when the form is submitted
342 $html .= Html
::hidden( 'wpExistingTags', implode( ',', $tags ) );
343 $html .= '</td></tr></table>';
349 * Returns a <select multiple> element with a list of change tags that can be
352 * @param array $selectedTags The tags that should be preselected in the
353 * list. Any tags in this list, but not in the list returned by
354 * ChangeTags::listExplicitlyDefinedTags, will be appended to the <select>
356 * @param string $label The text of a <label> to precede the <select>
357 * @return array HTML <label> element at index 0, HTML <select> element at
360 protected function getTagSelect( $selectedTags, $label ) {
362 $result[0] = Xml
::label( $label, 'mw-edittags-tag-list' );
364 $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
365 $select->setAttribute( 'multiple', 'multiple' );
366 $select->setAttribute( 'size', '8' );
368 $tags = ChangeTags
::listExplicitlyDefinedTags();
369 $tags = array_unique( array_merge( $tags, $selectedTags ) );
371 // Values of $tags are also used as <option> labels
372 $select->addOptions( array_combine( $tags, $tags ) );
374 $result[1] = $select->getHTML();
379 * UI entry point for form submission.
382 protected function submit() {
383 // Check edit token on submission
384 $request = $this->getRequest();
385 $token = $request->getVal( 'wpEditToken' );
386 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
387 $this->getOutput()->addWikiMsg( 'sessionfailure' );
391 // Evaluate incoming request data
392 $tagList = $request->getArray( 'wpTagList' );
393 if ( is_null( $tagList ) ) {
396 $existingTags = $request->getVal( 'wpExistingTags' );
397 if ( is_null( $existingTags ) ||
$existingTags === '' ) {
400 $existingTags = explode( ',', $existingTags );
403 if ( count( $this->ids
) > 1 ) {
404 // multiple revisions selected
405 $tagsToAdd = $tagList;
406 if ( $request->getBool( 'wpRemoveAllTags' ) ) {
407 $tagsToRemove = $existingTags;
409 $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
412 // single revision selected
413 // The user tells us which tags they want associated to the revision.
414 // We have to figure out which ones to add, and which to remove.
415 $tagsToAdd = array_diff( $tagList, $existingTags );
416 $tagsToRemove = array_diff( $existingTags, $tagList );
419 if ( !$tagsToAdd && !$tagsToRemove ) {
420 $status = Status
::newFatal( 'tags-edit-none-selected' );
422 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
423 $tagsToRemove, null, $this->reason
, $this->getUser() );
426 if ( $status->isGood() ) {
430 $this->failure( $status );
436 * Report that the submit operation succeeded
438 protected function success() {
439 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
440 $this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
441 'tags-edit-success' );
442 $this->wasSaved
= true;
443 $this->revList
->reloadFromMaster();
444 $this->reason
= ''; // no need to spew the reason back at the user
449 * Report that the submit operation failed
450 * @param Status $status
452 protected function failure( $status ) {
453 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
454 $this->getOutput()->wrapWikiTextAsInterface(
455 'errorbox', $status->getWikiText( 'tags-edit-failure' )
460 public function getDescription() {
461 return $this->msg( 'tags-edit-title' )->text();
464 protected function getGroupName() {