wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialEditTags.php
blob97b04c23f555d903928dce43925a458d7079bdc6
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.
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
18 * @file
19 * @ingroup SpecialPage
22 /**
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
27 * @since 1.25
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 */
37 private $ids;
39 /** @var Title Title object for target parameter */
40 private $targetObj;
42 /** @var string Deletion type, may be revision or logentry */
43 private $typeName;
45 /** @var ChangeTagsList Storing the list of items to be tagged */
46 private $revList;
48 /** @var bool Whether user is allowed to perform the action */
49 private $isAllowed;
51 /** @var string */
52 private $reason;
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();
66 // Check blocks
67 if ( $user->isBlocked() ) {
68 throw new UserBlockedError( $user->getBlock() );
71 $this->setHeaders();
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 );
84 } else {
85 // Array input
86 $this->ids = array_keys( $request->getArray( 'ids', array() ) );
88 $this->ids = array_unique( array_filter( $this->ids ) );
90 // No targets?
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 ) {
100 case 'logentry':
101 case 'logging':
102 $this->typeName = 'logentry';
103 break;
104 default:
105 $this->typeName = 'revision';
106 break;
109 // Allow the list type to adjust the passed target
110 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
111 // what we want
112 $this->targetObj = RevisionDeleter::suggestTarget(
113 $this->typeName === 'revision' ? 'revision' : 'logging',
114 $this->targetObj,
115 $this->ids
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' );
124 return;
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 ) {
131 $this->submit();
132 } else {
133 $this->showForm();
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(
140 $output,
141 'tag',
142 $this->targetObj,
143 '', /* user */
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 );
157 $links = array();
158 $links[] = Linker::linkKnown(
159 SpecialPage::getTitleFor( 'Log' ),
160 $this->msg( 'viewpagelogs' )->escaped(),
161 array(),
162 array(
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(
170 $this->targetObj,
171 $this->msg( 'pagehist' )->escaped(),
172 array(),
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() {
204 $userAllowed = true;
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()
212 ) );
214 $this->addHelpLink( 'Help:Tags' );
215 $out->addHTML( "<ul>" );
217 $numRevisions = 0;
218 // Live revisions...
219 $list = $this->getList();
220 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
221 for ( $list->reset(); $list->current(); $list->next() ) {
222 // @codingStandardsIgnoreEnd
223 $item = $list->current();
224 $numRevisions++;
225 $out->addHTML( $item->getHTML() );
228 if ( !$numRevisions ) {
229 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
232 $out->addHTML( "</ul>" );
233 // Explanation text
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' ) .
245 "<tr>\n" .
246 '<td class="mw-label">' .
247 Xml::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
248 '</td>' .
249 '<td class="mw-input">' .
250 Xml::input(
251 'wpReason',
253 $this->reason,
254 array( 'id' => 'wpReason', 'maxlength' => 100 )
256 '</td>' .
257 "</tr><tr>\n" .
258 '<td></td>' .
259 '<td class="mw-submit">' .
260 Xml::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
261 $numRevisions )->text(), array( 'name' => 'wpSubmit' ) ) .
262 '</td>' .
263 "</tr>\n" .
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";
271 } else {
272 $form = '';
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();
283 $tags = array();
284 if ( $list->length() == 1 ) {
285 $list->reset();
286 $tags = $list->current()->getTags();
287 if ( $tags ) {
288 $tags = explode( ',', $tags );
289 } else {
290 $tags = array();
293 $html = '<table id="mw-edittags-tags-selector">';
294 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
295 '</td><td>';
296 if ( $tags ) {
297 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
298 } else {
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];
304 } else {
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(
328 'value' => $tag,
329 'class' => 'mw-edittags-remove-checkbox',
330 ) );
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>';
339 return $html;
343 * Returns a <select multiple> element with a list of change tags that can be
344 * applied by users.
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>
349 * element.
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
352 * index 1
354 protected function getTagSelect( $selectedTags, $label ) {
355 $result = array();
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();
369 return $result;
373 * UI entry point for form submission.
374 * @throws PermissionsError
375 * @return bool
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' );
383 return false;
386 // Evaluate incoming request data
387 $tagList = $request->getArray( 'wpTagList' );
388 if ( is_null( $tagList ) ) {
389 $tagList = array();
391 $existingTags = $request->getVal( 'wpExistingTags' );
392 if ( is_null( $existingTags ) || $existingTags === '' ) {
393 $existingTags = array();
394 } else {
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;
403 } else {
404 $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
406 } else {
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' );
416 } else {
417 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
418 $tagsToRemove, null, $this->reason, $this->getUser() );
421 if ( $status->isGood() ) {
422 $this->success();
423 return true;
424 } else {
425 $this->failure( $status );
426 return false;
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
440 $this->showForm();
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' ) .
451 '</div>'
453 $this->showForm();
456 public function getDescription() {
457 return $this->msg( 'tags-edit-title' )->text();
460 protected function getGroupName() {
461 return 'pagetools';