Merge "redo: don't die producing xml files if rev text export conversion fails"
[lhc/web/wiklou.git] / includes / specials / SpecialEditTags.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup SpecialPage
20 */
21
22 /**
23 * Special page for adding and removing change tags to individual revisions.
24 * A lot of this is copied out of SpecialRevisiondelete.
25 *
26 * @ingroup SpecialPage
27 * @since 1.25
28 */
29 class SpecialEditTags extends UnlistedSpecialPage {
30 /** @var bool Was the DB modified in this request */
31 protected $wasSaved = false;
32
33 /** @var bool True if the submit button was clicked, and the form was posted */
34 private $submitClicked;
35
36 /** @var array Target ID list */
37 private $ids;
38
39 /** @var Title Title object for target parameter */
40 private $targetObj;
41
42 /** @var string Deletion type, may be revision or logentry */
43 private $typeName;
44
45 /** @var ChangeTagsList Storing the list of items to be tagged */
46 private $revList;
47
48 /** @var bool Whether user is allowed to perform the action */
49 private $isAllowed;
50
51 /** @var string */
52 private $reason;
53
54 public function __construct() {
55 parent::__construct( 'EditTags', 'changetags' );
56 }
57
58 public function doesWrites() {
59 return true;
60 }
61
62 public function execute( $par ) {
63 $this->checkPermissions();
64 $this->checkReadOnly();
65
66 $output = $this->getOutput();
67 $user = $this->getUser();
68 $request = $this->getRequest();
69
70 // Check blocks
71 if ( $user->isBlocked() ) {
72 throw new UserBlockedError( $user->getBlock() );
73 }
74
75 $this->setHeaders();
76 $this->outputHeader();
77
78 $output->addModules( [ 'mediawiki.special.edittags' ] );
79 $output->addModuleStyles( [
80 'mediawiki.interface.helpers.styles',
81 'mediawiki.special'
82 ] );
83
84 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
85
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 );
91 } else {
92 // Array input
93 $this->ids = array_keys( $request->getArray( 'ids', [] ) );
94 }
95 $this->ids = array_unique( array_filter( $this->ids ) );
96
97 // No targets?
98 if ( count( $this->ids ) == 0 ) {
99 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
100 }
101
102 $this->typeName = $request->getVal( 'type' );
103 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
104
105 // sanity check of parameter
106 switch ( $this->typeName ) {
107 case 'logentry':
108 case 'logging':
109 $this->typeName = 'logentry';
110 break;
111 default:
112 $this->typeName = 'revision';
113 break;
114 }
115
116 // Allow the list type to adjust the passed target
117 // Yuck! Copied straight out of SpecialRevisiondelete, but it does exactly
118 // what we want
119 $this->targetObj = RevisionDeleter::suggestTarget(
120 $this->typeName === 'revision' ? 'revision' : 'logging',
121 $this->targetObj,
122 $this->ids
123 );
124
125 $this->isAllowed = $user->isAllowed( 'changetags' );
126
127 $this->reason = $request->getVal( 'wpReason' );
128 // We need a target page!
129 if ( is_null( $this->targetObj ) ) {
130 $output->addWikiMsg( 'undelete-header' );
131 return;
132 }
133 // Give a link to the logs/hist for this page
134 $this->showConvenienceLinks();
135
136 // Either submit or create our form
137 if ( $this->isAllowed && $this->submitClicked ) {
138 $this->submit();
139 } else {
140 $this->showForm();
141 }
142
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(
147 $output,
148 'tag',
149 $this->targetObj,
150 '', /* user */
151 [ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved ]
152 );
153 }
154
155 /**
156 * Show some useful links in the subtitle
157 */
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 );
163
164 $linkRenderer = $this->getLinkRenderer();
165 $links = [];
166 $links[] = $linkRenderer->makeKnownLink(
167 SpecialPage::getTitleFor( 'Log' ),
168 $this->msg( 'viewpagelogs' )->text(),
169 [],
170 [
171 'page' => $this->targetObj->getPrefixedText(),
172 'wpfilters' => [ 'tag' ],
173 ]
174 );
175 if ( !$this->targetObj->isSpecialPage() ) {
176 // Give a link to the page history
177 $links[] = $linkRenderer->makeKnownLink(
178 $this->targetObj,
179 $this->msg( 'pagehist' )->text(),
180 [],
181 [ 'action' => 'history' ]
182 );
183 }
184 // Link to Special:Tags
185 $links[] = $linkRenderer->makeKnownLink(
186 SpecialPage::getTitleFor( 'Tags' ),
187 $this->msg( 'tags-edit-manage-link' )->text()
188 );
189 // Logs themselves don't have histories or archived revisions
190 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
191 }
192 }
193
194 /**
195 * Get the list object for this request
196 * @return ChangeTagsList
197 */
198 protected function getList() {
199 if ( is_null( $this->revList ) ) {
200 $this->revList = ChangeTagsList::factory( $this->typeName, $this->getContext(),
201 $this->targetObj, $this->ids );
202 }
203
204 return $this->revList;
205 }
206
207 /**
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.
210 */
211 protected function showForm() {
212 $out = $this->getOutput();
213 // Messages: tags-edit-revision-selected, tags-edit-logentry-selected
214 $out->wrapWikiMsg( "<strong>$1</strong>", [
215 "tags-edit-{$this->typeName}-selected",
216 $this->getLanguage()->formatNum( count( $this->ids ) ),
217 $this->targetObj->getPrefixedText()
218 ] );
219
220 $this->addHelpLink( 'Help:Tags' );
221 $out->addHTML( "<ul>" );
222
223 $numRevisions = 0;
224 // Live revisions...
225 $list = $this->getList();
226 for ( $list->reset(); $list->current(); $list->next() ) {
227 $item = $list->current();
228 $numRevisions++;
229 $out->addHTML( $item->getHTML() );
230 }
231
232 if ( !$numRevisions ) {
233 throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' );
234 }
235
236 $out->addHTML( "</ul>" );
237 // Explanation text
238 $out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" );
239
240 // Show form if the user can submit
241 if ( $this->isAllowed ) {
242 $form = Xml::openElement( 'form', [ 'method' => 'post',
243 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
244 'id' => 'mw-revdel-form-revisions' ] ) .
245 Xml::fieldset( $this->msg( "tags-edit-{$this->typeName}-legend",
246 count( $this->ids ) )->text() ) .
247 $this->buildCheckBoxes() .
248 Xml::openElement( 'table' ) .
249 "<tr>\n" .
250 '<td class="mw-label">' .
251 Xml::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) .
252 '</td>' .
253 '<td class="mw-input">' .
254 Xml::input( 'wpReason', 60, $this->reason, [
255 'id' => 'wpReason',
256 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
257 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
258 // Unicode codepoints.
259 // "- 155" is to leave room for the auto-generated part of the log entry.
260 'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT - 155,
261 ] ) .
262 '</td>' .
263 "</tr><tr>\n" .
264 '<td></td>' .
265 '<td class="mw-submit">' .
266 Xml::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit",
267 $numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) .
268 '</td>' .
269 "</tr>\n" .
270 Xml::closeElement( 'table' ) .
271 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
272 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
273 Html::hidden( 'type', $this->typeName ) .
274 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
275 Xml::closeElement( 'fieldset' ) . "\n" .
276 Xml::closeElement( 'form' ) . "\n";
277 } else {
278 $form = '';
279 }
280 $out->addHTML( $form );
281 }
282
283 /**
284 * @return string HTML
285 */
286 protected function buildCheckBoxes() {
287 // If there is just one item, provide the user with a multi-select field
288 $list = $this->getList();
289 $tags = [];
290 if ( $list->length() == 1 ) {
291 $list->reset();
292 $tags = $list->current()->getTags();
293 if ( $tags ) {
294 $tags = explode( ',', $tags );
295 } else {
296 $tags = [];
297 }
298
299 $html = '<table id="mw-edittags-tags-selector">';
300 $html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() .
301 '</td><td>';
302 if ( $tags ) {
303 $html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) );
304 } else {
305 $html .= $this->msg( 'tags-edit-existing-tags-none' )->parse();
306 }
307 $html .= '</td></tr>';
308 $tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() );
309 $html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1];
310 } else {
311 // Otherwise, use a multi-select field for adding tags, and a list of
312 // checkboxes for removing them
313
314 for ( $list->reset(); $list->current(); $list->next() ) {
315 $currentTags = $list->current()->getTags();
316 if ( $currentTags ) {
317 $tags = array_merge( $tags, explode( ',', $currentTags ) );
318 }
319 }
320 $tags = array_unique( $tags );
321
322 $html = '<table id="mw-edittags-tags-selector-multi"><tr><td>';
323 $tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() );
324 $html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>';
325 $html .= Xml::element( 'p', null, $this->msg( 'tags-edit-remove' )->plain() );
326 $html .= Xml::checkLabel( $this->msg( 'tags-edit-remove-all-tags' )->plain(),
327 'wpRemoveAllTags', 'mw-edittags-remove-all' );
328 $i = 0; // used for generating checkbox IDs only
329 foreach ( $tags as $tag ) {
330 $html .= Xml::element( 'br' ) . "\n" . Xml::checkLabel( $tag,
331 'wpTagsToRemove[]', 'mw-edittags-remove-' . $i++, false, [
332 'value' => $tag,
333 'class' => 'mw-edittags-remove-checkbox',
334 ] );
335 }
336 }
337
338 // also output the tags currently applied as a hidden form field, so we
339 // know what to remove from the revision/log entry when the form is submitted
340 $html .= Html::hidden( 'wpExistingTags', implode( ',', $tags ) );
341 $html .= '</td></tr></table>';
342
343 return $html;
344 }
345
346 /**
347 * Returns a <select multiple> element with a list of change tags that can be
348 * applied by users.
349 *
350 * @param array $selectedTags The tags that should be preselected in the
351 * list. Any tags in this list, but not in the list returned by
352 * ChangeTags::listExplicitlyDefinedTags, will be appended to the <select>
353 * element.
354 * @param string $label The text of a <label> to precede the <select>
355 * @return array HTML <label> element at index 0, HTML <select> element at
356 * index 1
357 */
358 protected function getTagSelect( $selectedTags, $label ) {
359 $result = [];
360 $result[0] = Xml::label( $label, 'mw-edittags-tag-list' );
361
362 $select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags );
363 $select->setAttribute( 'multiple', 'multiple' );
364 $select->setAttribute( 'size', '8' );
365
366 $tags = ChangeTags::listExplicitlyDefinedTags();
367 $tags = array_unique( array_merge( $tags, $selectedTags ) );
368
369 // Values of $tags are also used as <option> labels
370 $select->addOptions( array_combine( $tags, $tags ) );
371
372 $result[1] = $select->getHTML();
373 return $result;
374 }
375
376 /**
377 * UI entry point for form submission.
378 * @return bool
379 */
380 protected function submit() {
381 // Check edit token on submission
382 $request = $this->getRequest();
383 $token = $request->getVal( 'wpEditToken' );
384 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
385 $this->getOutput()->addWikiMsg( 'sessionfailure' );
386 return false;
387 }
388
389 // Evaluate incoming request data
390 $tagList = $request->getArray( 'wpTagList' );
391 if ( is_null( $tagList ) ) {
392 $tagList = [];
393 }
394 $existingTags = $request->getVal( 'wpExistingTags' );
395 if ( is_null( $existingTags ) || $existingTags === '' ) {
396 $existingTags = [];
397 } else {
398 $existingTags = explode( ',', $existingTags );
399 }
400
401 if ( count( $this->ids ) > 1 ) {
402 // multiple revisions selected
403 $tagsToAdd = $tagList;
404 if ( $request->getBool( 'wpRemoveAllTags' ) ) {
405 $tagsToRemove = $existingTags;
406 } else {
407 $tagsToRemove = $request->getArray( 'wpTagsToRemove' );
408 }
409 } else {
410 // single revision selected
411 // The user tells us which tags they want associated to the revision.
412 // We have to figure out which ones to add, and which to remove.
413 $tagsToAdd = array_diff( $tagList, $existingTags );
414 $tagsToRemove = array_diff( $existingTags, $tagList );
415 }
416
417 if ( !$tagsToAdd && !$tagsToRemove ) {
418 $status = Status::newFatal( 'tags-edit-none-selected' );
419 } else {
420 $status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd,
421 $tagsToRemove, null, $this->reason, $this->getUser() );
422 }
423
424 if ( $status->isGood() ) {
425 $this->success();
426 return true;
427 } else {
428 $this->failure( $status );
429 return false;
430 }
431 }
432
433 /**
434 * Report that the submit operation succeeded
435 */
436 protected function success() {
437 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
438 $this->getOutput()->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>",
439 'tags-edit-success' );
440 $this->wasSaved = true;
441 $this->revList->reloadFromMaster();
442 $this->reason = ''; // no need to spew the reason back at the user
443 $this->showForm();
444 }
445
446 /**
447 * Report that the submit operation failed
448 * @param Status $status
449 */
450 protected function failure( $status ) {
451 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
452 $this->getOutput()->wrapWikiTextAsInterface(
453 'errorbox', $status->getWikiText( 'tags-edit-failure' )
454 );
455 $this->showForm();
456 }
457
458 public function getDescription() {
459 return $this->msg( 'tags-edit-title' )->text();
460 }
461
462 protected function getGroupName() {
463 return 'pagetools';
464 }
465 }