MediaWiki  master
SpecialEditTags.php
Go to the documentation of this file.
1 <?php
23 
33  protected $wasSaved = false;
34 
36  private $submitClicked;
37 
39  private $ids;
40 
42  private $targetObj;
43 
45  private $typeName;
46 
48  private $revList;
49 
51  private $isAllowed;
52 
54  private $reason;
55 
58 
65  parent::__construct( 'EditTags', 'changetags' );
66 
67  $this->permissionManager = $permissionManager;
68  }
69 
70  public function doesWrites() {
71  return true;
72  }
73 
74  public function execute( $par ) {
75  $this->checkPermissions();
76  $this->checkReadOnly();
77 
78  $output = $this->getOutput();
79  $user = $this->getUser();
80  $request = $this->getRequest();
81 
82  $this->setHeaders();
83  $this->outputHeader();
84 
85  $output->addModules( [ 'mediawiki.special.edittags' ] );
86  $output->addModuleStyles( [
87  'mediawiki.interface.helpers.styles',
88  'mediawiki.special'
89  ] );
90 
91  $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
92 
93  // Handle our many different possible input types
94  $ids = $request->getVal( 'ids' );
95  if ( !is_null( $ids ) ) {
96  // Allow CSV from the form hidden field, or a single ID for show/hide links
97  $this->ids = explode( ',', $ids );
98  } else {
99  // Array input
100  $this->ids = array_keys( $request->getArray( 'ids', [] ) );
101  }
102  $this->ids = array_unique( array_filter( $this->ids ) );
103 
104  // No targets?
105  if ( count( $this->ids ) == 0 ) {
106  throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
107  }
108 
109  $this->typeName = $request->getVal( 'type' );
110  $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
111 
112  // sanity check of parameter
113  switch ( $this->typeName ) {
114  case 'logentry':
115  case 'logging':
116  $this->typeName = 'logentry';
117  break;
118  default:
119  $this->typeName = 'revision';
120  break;
121  }
122 
123  // Allow the list type to adjust the passed target
124  // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
125  // what we want
126  $this->targetObj = RevisionDeleter::suggestTarget(
127  $this->typeName === 'revision' ? 'revision' : 'logging',
128  $this->targetObj,
129  $this->ids
130  );
131 
132  $this->isAllowed = $this->permissionManager->userHasRight( $user, 'changetags' );
133 
134  $this->reason = $request->getVal( 'wpReason' );
135  // We need a target page!
136  if ( is_null( $this->targetObj ) ) {
137  $output->addWikiMsg( 'undelete-header' );
138  return;
139  }
140 
141  // Check blocks
142  if ( $this->permissionManager->isBlockedFrom( $user, $this->targetObj ) ) {
143  throw new UserBlockedError(
144  $user->getBlock(),
145  $user,
146  $this->getLanguage(),
147  $request->getIP()
148  );
149  }
150 
151  // Give a link to the logs/hist for this page
152  $this->showConvenienceLinks();
153 
154  // Either submit or create our form
155  if ( $this->isAllowed && $this->submitClicked ) {
156  $this->submit();
157  } else {
158  $this->showForm();
159  }
160 
161  // Show relevant lines from the tag log
162  $tagLogPage = new LogPage( 'tag' );
163  $output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" );
165  $output,
166  'tag',
167  $this->targetObj,
168  '', /* user */
169  [ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved ]
170  );
171  }
172 
176  protected function showConvenienceLinks() {
177  // Give a link to the logs/hist for this page
178  if ( $this->targetObj ) {
179  // Also set header tabs to be for the target.
180  $this->getSkin()->setRelevantTitle( $this->targetObj );
181 
182  $linkRenderer = $this->getLinkRenderer();
183  $links = [];
184  $links[] = $linkRenderer->makeKnownLink(
185  SpecialPage::getTitleFor( 'Log' ),
186  $this->msg( 'viewpagelogs' )->text(),
187  [],
188  [
189  'page' => $this->targetObj->getPrefixedText(),
190  'wpfilters' => [ 'tag' ],
191  ]
192  );
193  if ( !$this->targetObj->isSpecialPage() ) {
194  // Give a link to the page history
195  $links[] = $linkRenderer->makeKnownLink(
196  $this->targetObj,
197  $this->msg( 'pagehist' )->text(),
198  [],
199  [ 'action' => 'history' ]
200  );
201  }
202  // Link to Special:Tags
203  $links[] = $linkRenderer->makeKnownLink(
204  SpecialPage::getTitleFor( 'Tags' ),
205  $this->msg( 'tags-edit-manage-link' )->text()
206  );
207  // Logs themselves don't have histories or archived revisions
208  $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
209  }
210  }
211 
216  protected function getList() {
217  if ( is_null( $this->revList ) ) {
218  $this->revList = ChangeTagsList::factory( $this->typeName, $this->getContext(),
219  $this->targetObj, $this->ids );
220  }
221 
222  return $this->revList;
223  }
224 
229  protected function showForm() {
230  $out = $this->getOutput();
231  // Messages: tags-edit-revision-selected, tags-edit-logentry-selected
232  $out->wrapWikiMsg( "<strong>$1</strong>", [
233  "tags-edit-{$this->typeName}-selected",
234  $this->getLanguage()->formatNum( count( $this->ids ) ),
235  $this->targetObj->getPrefixedText()
236  ] );
237 
238  $this->addHelpLink( 'Help:Tags' );
239  $out->addHTML( "<ul>" );
240 
241  $numRevisions = 0;
242  // Live revisions...
243  $list = $this->getList();
244  for ( $list->reset(); $list->current(); $list->next() ) {
245  $item = $list->current();
246  if ( !$item->canView() ) {
247  throw new ErrorPageError( 'permissionserrors', 'tags-update-no-permission' );
248  }
249  $numRevisions++;
250  $out->addHTML( $item->getHTML() );
251  }
252 
253  if ( !$numRevisions ) {
254  throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
255  }
256 
257  $out->addHTML( "</ul>" );
258  // Explanation text
259  $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
260 
261  // Show form if the user can submit
262  if ( $this->isAllowed ) {
263  $form = Xml::openElement( 'form', [ 'method' => 'post',
264  'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
265  'id' => 'mw-revdel-form-revisions' ] ) .
266  Xml::fieldset( $this->msg( "tags-edit-{$this->typeName}-legend",
267  count( $this->ids ) )->text() ) .
268  $this->buildCheckBoxes() .
269  Xml::openElement( 'table' ) .
270  "<tr>\n" .
271  '<td class="mw-label">' .
272  Xml::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
273  '</td>' .
274  '<td class="mw-input">' .
275  Xml::input( 'wpReason', 60, $this->reason, [
276  'id' => 'wpReason',
277  // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
278  // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
279  // Unicode codepoints.
281  ] ) .
282  '</td>' .
283  "</tr><tr>\n" .
284  '<td></td>' .
285  '<td class="mw-submit">' .
286  Xml::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
287  $numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) .
288  '</td>' .
289  "</tr>\n" .
290  Xml::closeElement( 'table' ) .
291  Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
292  Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
293  Html::hidden( 'type', $this->typeName ) .
294  Html::hidden( 'ids', implode( ',', $this->ids ) ) .
295  Xml::closeElement( 'fieldset' ) . "\n" .
296  Xml::closeElement( 'form' ) . "\n";
297  } else {
298  $form = '';
299  }
300  $out->addHTML( $form );
301  }
302 
306  protected function buildCheckBoxes() {
307  // If there is just one item, provide the user with a multi-select field
308  $list = $this->getList();
309  $tags = [];
310  if ( $list->length() == 1 ) {
311  $list->reset();
312  $tags = $list->current()->getTags();
313  if ( $tags ) {
314  $tags = explode( ',', $tags );
315  } else {
316  $tags = [];
317  }
318 
319  $html = '<table id="mw-edittags-tags-selector">';
320  $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
321  '</td><td>';
322  if ( $tags ) {
323  $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
324  } else {
325  $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
326  }
327  $html .= '</td></tr>';
328  $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
329  $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
330  } else {
331  // Otherwise, use a multi-select field for adding tags, and a list of
332  // checkboxes for removing them
333 
334  for ( $list->reset(); $list->current(); $list->next() ) {
335  $currentTags = $list->current()->getTags();
336  if ( $currentTags ) {
337  $tags = array_merge( $tags, explode( ',', $currentTags ) );
338  }
339  }
340  $tags = array_unique( $tags );
341 
342  $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
343  $tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() );
344  $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
345  $html .= Xml::element( 'p', null, $this->msg( 'tags-edit-remove' )->plain() );
346  $html .= Xml::checkLabel( $this->msg( 'tags-edit-remove-all-tags' )->plain(),
347  'wpRemoveAllTags', 'mw-edittags-remove-all' );
348  $i = 0; // used for generating checkbox IDs only
349  foreach ( $tags as $tag ) {
350  $html .= Xml::element( 'br' ) . "\n" . Xml::checkLabel( $tag,
351  'wpTagsToRemove[]', 'mw-edittags-remove-' . $i++, false, [
352  'value' => $tag,
353  'class' => 'mw-edittags-remove-checkbox',
354  ] );
355  }
356  }
357 
358  // also output the tags currently applied as a hidden form field, so we
359  // know what to remove from the revision/log entry when the form is submitted
360  $html .= Html::hidden( 'wpExistingTags', implode( ',', $tags ) );
361  $html .= '</td></tr></table>';
362 
363  return $html;
364  }
365 
378  protected function getTagSelect( $selectedTags, $label ) {
379  $result = [];
380  $result[0] = Xml::label( $label, 'mw-edittags-tag-list' );
381 
382  $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
383  $select->setAttribute( 'multiple', 'multiple' );
384  $select->setAttribute( 'size', '8' );
385 
387  $tags = array_unique( array_merge( $tags, $selectedTags ) );
388 
389  // Values of $tags are also used as <option> labels
390  $select->addOptions( array_combine( $tags, $tags ) );
391 
392  $result[1] = $select->getHTML();
393  return $result;
394  }
395 
400  protected function submit() {
401  // Check edit token on submission
402  $request = $this->getRequest();
403  $token = $request->getVal( 'wpEditToken' );
404  if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
405  $this->getOutput()->addWikiMsg( 'sessionfailure' );
406  return false;
407  }
408 
409  // Evaluate incoming request data
410  $tagList = $request->getArray( 'wpTagList' );
411  if ( is_null( $tagList ) ) {
412  $tagList = [];
413  }
414  $existingTags = $request->getVal( 'wpExistingTags' );
415  if ( is_null( $existingTags ) || $existingTags === '' ) {
416  $existingTags = [];
417  } else {
418  $existingTags = explode( ',', $existingTags );
419  }
420 
421  if ( count( $this->ids ) > 1 ) {
422  // multiple revisions selected
423  $tagsToAdd = $tagList;
424  if ( $request->getBool( 'wpRemoveAllTags' ) ) {
425  $tagsToRemove = $existingTags;
426  } else {
427  $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
428  }
429  } else {
430  // single revision selected
431  // The user tells us which tags they want associated to the revision.
432  // We have to figure out which ones to add, and which to remove.
433  $tagsToAdd = array_diff( $tagList, $existingTags );
434  $tagsToRemove = array_diff( $existingTags, $tagList );
435  }
436 
437  if ( !$tagsToAdd && !$tagsToRemove ) {
438  $status = Status::newFatal( 'tags-edit-none-selected' );
439  } else {
440  $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
441  $tagsToRemove, null, $this->reason, $this->getUser() );
442  }
443 
444  if ( $status->isGood() ) {
445  $this->success();
446  return true;
447  } else {
448  $this->failure( $status );
449  return false;
450  }
451  }
452 
456  protected function success() {
457  $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
458  $this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
459  'tags-edit-success' );
460  $this->wasSaved = true;
461  $this->revList->reloadFromMaster();
462  $this->reason = ''; // no need to spew the reason back at the user
463  $this->showForm();
464  }
465 
470  protected function failure( $status ) {
471  $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
472  $this->getOutput()->wrapWikiTextAsInterface(
473  'errorbox', $status->getWikiText( 'tags-edit-failure', false, $this->getLanguage() )
474  );
475  $this->showForm();
476  }
477 
478  public function getDescription() {
479  return $this->msg( 'tags-edit-title' )->text();
480  }
481 
482  protected function getGroupName() {
483  return 'pagetools';
484  }
485 }
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
failure( $status)
Report that the submit operation failed.
Shortcut to construct a special page which is unlisted by default.
string $typeName
Deletion type, may be revision or logentry.
getContext()
Gets the context this SpecialPage is executed in.
getList()
Get the list object for this request.
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition: Xml.php:274
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
getOutput()
Get the OutputPage being used for this instance.
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
Class to simplify the use of log pages.
Definition: LogPage.php:33
success()
Report that the submit operation succeeded.
Special page for adding and removing change tags to individual revisions.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
array $ids
Target ID list.
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
An error page which can definitely be safely rendered using the OutputPage.
showForm()
Show a list of items that we will operate on, and show a form which allows the user to modify the tag...
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition: Xml.php:609
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition: Xml.php:459
getSkin()
Shortcut to get the skin being used for this instance.
submit()
UI entry point for form submission.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
bool $wasSaved
Was the DB modified in this request.
showConvenienceLinks()
Show some useful links in the subtitle.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
bool $submitClicked
True if the submit button was clicked, and the form was posted.
Show an error when the user tries to do something whilst blocked.
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition: Xml.php:358
PermissionManager $permissionManager
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
static suggestTarget( $typeName, $target, array $ids)
Suggest a target for the revision deletion.
getUser()
Shortcut to get the User executing this instance.
Title $targetObj
Title object for target parameter.
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:802
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
getLanguage()
Shortcut to get user&#39;s language.
getTagSelect( $selectedTags, $label)
Returns a <select multiple>=""> element with a list of change tags that can be applied by users...
__construct(PermissionManager $permissionManager)
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:419
getRequest()
Get the WebRequest being used for this instance.
static factory( $typeName, IContextSource $context, Title $title, array $ids)
Creates a ChangeTags*List of the requested type.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
getPageTitle( $subpage=false)
Get a self-referential title object.
bool $isAllowed
Whether user is allowed to perform the action.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
ChangeTagsList $revList
Storing the list of items to be tagged.
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:67
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319