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 execute( $par ) {
59 $this->checkPermissions();
60 $this->checkReadOnly();
62 $output = $this->getOutput();
63 $user = $this->getUser();
64 $request = $this->getRequest();
67 if ( $user->isBlocked() ) {
68 throw new UserBlockedError( $user->getBlock() );
72 $this->outputHeader();
74 $this->getOutput()->addModules( array( 'mediawiki.special.edittags',
75 'mediawiki.special.edittags.styles' ) );
77 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
79 // Handle our many different possible input types
80 $ids = $request->getVal( 'ids' );
81 if ( !is_null( $ids ) ) {
82 // Allow CSV from the form hidden field, or a single ID for show/hide links
83 $this->ids
= explode( ',', $ids );
86 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
88 $this->ids
= array_unique( array_filter( $this->ids
) );
91 if ( count( $this->ids
) == 0 ) {
92 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
95 $this->typeName
= $request->getVal( 'type' );
96 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
98 // sanity check of parameter
99 switch ( $this->typeName
) {
102 $this->typeName
= 'logentry';
105 $this->typeName
= 'revision';
109 // Allow the list type to adjust the passed target
110 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
112 $this->targetObj
= RevisionDeleter
::suggestTarget(
113 $this->typeName
=== 'revision' ?
'revision' : 'logging',
118 $this->isAllowed
= $user->isAllowed( 'changetags' );
120 $this->reason
= $request->getVal( 'wpReason' );
121 // We need a target page!
122 if ( is_null( $this->targetObj
) ) {
123 $output->addWikiMsg( 'undelete-header' );
126 // Give a link to the logs/hist for this page
127 $this->showConvenienceLinks();
129 // Either submit or create our form
130 if ( $this->isAllowed
&& $this->submitClicked
) {
136 // Show relevant lines from the tag log
137 $tagLogPage = new LogPage( 'tag' );
138 $output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" );
139 LogEventsList
::showLogExtract(
144 array( 'lim' => 25, 'conds' => array(), 'useMaster' => $this->wasSaved
)
149 * Show some useful links in the subtitle
151 protected function showConvenienceLinks() {
152 // Give a link to the logs/hist for this page
153 if ( $this->targetObj
) {
154 // Also set header tabs to be for the target.
155 $this->getSkin()->setRelevantTitle( $this->targetObj
);
158 $links[] = Linker
::linkKnown(
159 SpecialPage
::getTitleFor( 'Log' ),
160 $this->msg( 'viewpagelogs' )->escaped(),
163 'page' => $this->targetObj
->getPrefixedText(),
164 'hide_tag_log' => '0',
167 if ( !$this->targetObj
->isSpecialPage() ) {
168 // Give a link to the page history
169 $links[] = Linker
::linkKnown(
171 $this->msg( 'pagehist' )->escaped(),
173 array( 'action' => 'history' )
176 // Link to Special:Tags
177 $links[] = Linker
::linkKnown(
178 SpecialPage
::getTitleFor( 'Tags' ),
179 $this->msg( 'tags-edit-manage-link' )->escaped()
181 // Logs themselves don't have histories or archived revisions
182 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
187 * Get the list object for this request
188 * @return ChangeTagsList
190 protected function getList() {
191 if ( is_null( $this->revList
) ) {
192 $this->revList
= ChangeTagsList
::factory( $this->typeName
, $this->getContext(),
193 $this->targetObj
, $this->ids
);
196 return $this->revList
;
200 * Show a list of items that we will operate on, and show a form which allows
201 * the user to modify the tags applied to those items.
203 protected function showForm() {
206 $out = $this->getOutput();
207 // Messages: tags-edit-revision-selected, tags-edit-logentry-selected
208 $out->wrapWikiMsg( "<strong>$1</strong>", array(
209 "tags-edit-{$this->typeName}-selected",
210 $this->getLanguage()->formatNum( count( $this->ids
) ),
211 $this->targetObj
->getPrefixedText()
214 $this->addHelpLink( 'Help:Tags' );
215 $out->addHTML( "<ul>" );
219 $list = $this->getList();
220 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
221 for ( $list->reset(); $list->current(); $list->next() ) {
222 // @codingStandardsIgnoreEnd
223 $item = $list->current();
225 $out->addHTML( $item->getHTML() );
228 if ( !$numRevisions ) {
229 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
232 $out->addHTML( "</ul>" );
234 $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
236 // Show form if the user can submit
237 if ( $this->isAllowed
) {
238 $form = Xml
::openElement( 'form', array( 'method' => 'post',
239 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
240 'id' => 'mw-revdel-form-revisions' ) ) .
241 Xml
::fieldset( $this->msg( "tags-edit-{$this->typeName}-legend",
242 count( $this->ids
) )->text() ) .
243 $this->buildCheckBoxes() .
244 Xml
::openElement( 'table' ) .
246 '<td class="mw-label">' .
247 Xml
::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
249 '<td class="mw-input">' .
254 array( 'id' => 'wpReason', 'maxlength' => 100 )
259 '<td class="mw-submit">' .
260 Xml
::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
261 $numRevisions )->text(), array( 'name' => 'wpSubmit' ) ) .
264 Xml
::closeElement( 'table' ) .
265 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
266 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
267 Html
::hidden( 'type', $this->typeName
) .
268 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
269 Xml
::closeElement( 'fieldset' ) . "\n" .
270 Xml
::closeElement( 'form' ) . "\n";
274 $out->addHTML( $form );
278 * @return string HTML
280 protected function buildCheckBoxes() {
281 // If there is just one item, provide the user with a multi-select field
282 $list = $this->getList();
284 if ( $list->length() == 1 ) {
286 $tags = $list->current()->getTags();
288 $tags = explode( ',', $tags );
293 $html = '<table id="mw-edittags-tags-selector">';
294 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
297 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
299 $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
301 $html .= '</td></tr>';
302 $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
303 $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
305 // Otherwise, use a multi-select field for adding tags, and a list of
306 // checkboxes for removing them
308 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
309 for ( $list->reset(); $list->current(); $list->next() ) {
310 // @codingStandardsIgnoreEnd
311 $currentTags = $list->current()->getTags();
312 if ( $currentTags ) {
313 $tags = array_merge( $tags, explode( ',', $currentTags ) );
316 $tags = array_unique( $tags );
318 $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
319 $tagSelect = $this->getTagSelect( array(), $this->msg( 'tags-edit-add' )->plain() );
320 $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
321 $html .= Xml
::element( 'p', null, $this->msg( 'tags-edit-remove' )->plain() );
322 $html .= Xml
::checkLabel( $this->msg( 'tags-edit-remove-all-tags' )->plain(),
323 'wpRemoveAllTags', 'mw-edittags-remove-all' );
324 $i = 0; // used for generating checkbox IDs only
325 foreach ( $tags as $tag ) {
326 $html .= Xml
::element( 'br' ) . "\n" . Xml
::checkLabel( $tag,
327 'wpTagsToRemove[]', 'mw-edittags-remove-' . $i++
, false, array(
329 'class' => 'mw-edittags-remove-checkbox',
334 // also output the tags currently applied as a hidden form field, so we
335 // know what to remove from the revision/log entry when the form is submitted
336 $html .= Html
::hidden( 'wpExistingTags', implode( ',', $tags ) );
337 $html .= '</td></tr></table>';
343 * Returns a <select multiple> element with a list of change tags that can be
346 * @param array $selectedTags The tags that should be preselected in the
347 * list. Any tags in this list, but not in the list returned by
348 * ChangeTags::listExplicitlyDefinedTags, will be appended to the <select>
350 * @param string $label The text of a <label> to precede the <select>
351 * @return array HTML <label> element at index 0, HTML <select> element at
354 protected function getTagSelect( $selectedTags, $label ) {
356 $result[0] = Xml
::label( $label, 'mw-edittags-tag-list' );
358 $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
359 $select->setAttribute( 'multiple', 'multiple' );
360 $select->setAttribute( 'size', '8' );
362 $tags = ChangeTags
::listExplicitlyDefinedTags();
363 $tags = array_unique( array_merge( $tags, $selectedTags ) );
365 // Values of $tags are also used as <option> labels
366 $select->addOptions( array_combine( $tags, $tags ) );
368 $result[1] = $select->getHTML();
373 * UI entry point for form submission.
374 * @throws PermissionsError
377 protected function submit() {
378 // Check edit token on submission
379 $request = $this->getRequest();
380 $token = $request->getVal( 'wpEditToken' );
381 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
382 $this->getOutput()->addWikiMsg( 'sessionfailure' );
386 // Evaluate incoming request data
387 $tagList = $request->getArray( 'wpTagList' );
388 if ( is_null( $tagList ) ) {
391 $existingTags = $request->getVal( 'wpExistingTags' );
392 if ( is_null( $existingTags ) ||
$existingTags === '' ) {
393 $existingTags = array();
395 $existingTags = explode( ',', $existingTags );
398 if ( count( $this->ids
) > 1 ) {
399 // multiple revisions selected
400 $tagsToAdd = $tagList;
401 if ( $request->getBool( 'wpRemoveAllTags' ) ) {
402 $tagsToRemove = $existingTags;
404 $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
407 // single revision selected
408 // The user tells us which tags they want associated to the revision.
409 // We have to figure out which ones to add, and which to remove.
410 $tagsToAdd = array_diff( $tagList, $existingTags );
411 $tagsToRemove = array_diff( $existingTags, $tagList );
414 if ( !$tagsToAdd && !$tagsToRemove ) {
415 $status = Status
::newFatal( 'tags-edit-none-selected' );
417 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
418 $tagsToRemove, null, $this->reason
, $this->getUser() );
421 if ( $status->isGood() ) {
425 $this->failure( $status );
431 * Report that the submit operation succeeded
433 protected function success() {
434 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
435 $this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
436 'tags-edit-success' );
437 $this->wasSaved
= true;
438 $this->revList
->reloadFromMaster();
439 $this->reason
= ''; // no need to spew the reason back at the user
444 * Report that the submit operation failed
445 * @param Status $status
447 protected function failure( $status ) {
448 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
449 $this->getOutput()->addWikiText( '<div class="errorbox">' .
450 $status->getWikiText( 'tags-edit-failure' ) .
456 public function getDescription() {
457 return $this->msg( 'tags-edit-title' )->text();
460 protected function getGroupName() {