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