| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use ChangeTagsList; |
| 24 |
use ErrorPageError; |
| 25 |
use LogEventsList; |
| 26 |
use LogPage; |
| 27 |
use MediaWiki\ChangeTags\ChangeTagsStore; |
| 28 |
use MediaWiki\CommentStore\CommentStore; |
| 29 |
use MediaWiki\Html\Html; |
| 30 |
use MediaWiki\Permissions\PermissionManager; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 33 |
use MediaWiki\Status\Status; |
| 34 |
use MediaWiki\Title\Title; |
| 35 |
use MediaWiki\Xml\XmlSelect; |
| 36 |
use RevisionDeleter; |
| 37 |
use UserBlockedError; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class SpecialEditTags extends UnlistedSpecialPage { |
| 48 |
|
| 49 |
protected $wasSaved = false; |
| 50 |
|
| 51 |
|
| 52 |
private $submitClicked; |
| 53 |
|
| 54 |
|
| 55 |
private $ids; |
| 56 |
|
| 57 |
|
| 58 |
private $targetObj; |
| 59 |
|
| 60 |
|
| 61 |
private $typeName; |
| 62 |
|
| 63 |
|
| 64 |
private $revList; |
| 65 |
|
| 66 |
|
| 67 |
private $reason; |
| 68 |
|
| 69 |
private PermissionManager $permissionManager; |
| 70 |
private ChangeTagsStore $changeTagsStore; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function __construct( PermissionManager $permissionManager, ChangeTagsStore $changeTagsStore ) { |
| 78 |
parent::__construct( 'EditTags', 'changetags' ); |
| 79 |
|
| 80 |
$this->permissionManager = $permissionManager; |
| 81 |
$this->changeTagsStore = $changeTagsStore; |
| 82 |
} |
| 83 |
|
| 84 |
public function doesWrites() { |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
public function execute( $par ) { |
| 89 |
$this->checkPermissions(); |
| 90 |
$this->checkReadOnly(); |
| 91 |
|
| 92 |
$output = $this->getOutput(); |
| 93 |
$user = $this->getUser(); |
| 94 |
$request = $this->getRequest(); |
| 95 |
|
| 96 |
$this->setHeaders(); |
| 97 |
$this->outputHeader(); |
| 98 |
|
| 99 |
$output->addModules( [ 'mediawiki.misc-authed-curate' ] ); |
| 100 |
$output->addModuleStyles( [ |
| 101 |
'mediawiki.interface.helpers.styles', |
| 102 |
'mediawiki.special' |
| 103 |
] ); |
| 104 |
|
| 105 |
$this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' ); |
| 106 |
|
| 107 |
|
| 108 |
$ids = $request->getVal( 'ids' ); |
| 109 |
if ( $ids !== null ) { |
| 110 |
|
| 111 |
$this->ids = explode( ',', $ids ); |
| 112 |
} else { |
| 113 |
|
| 114 |
$this->ids = array_keys( $request->getArray( 'ids', [] ) ); |
| 115 |
} |
| 116 |
$this->ids = array_unique( array_filter( $this->ids ) ); |
| 117 |
|
| 118 |
|
| 119 |
if ( count( $this->ids ) == 0 ) { |
| 120 |
throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' ); |
| 121 |
} |
| 122 |
|
| 123 |
$this->typeName = $request->getVal( 'type' ); |
| 124 |
$this->targetObj = Title::newFromText( $request->getText( 'target' ) ); |
| 125 |
|
| 126 |
switch ( $this->typeName ) { |
| 127 |
case 'logentry': |
| 128 |
case 'logging': |
| 129 |
$this->typeName = 'logentry'; |
| 130 |
break; |
| 131 |
default: |
| 132 |
$this->typeName = 'revision'; |
| 133 |
break; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
$this->targetObj = RevisionDeleter::suggestTarget( |
| 140 |
$this->typeName === 'revision' ? 'revision' : 'logging', |
| 141 |
$this->targetObj, |
| 142 |
$this->ids |
| 143 |
); |
| 144 |
|
| 145 |
$this->reason = $request->getVal( 'wpReason', '' ); |
| 146 |
|
| 147 |
if ( $this->targetObj === null ) { |
| 148 |
$output->addWikiMsg( 'undelete-header' ); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
$checkReplica = !$this->submitClicked; |
| 154 |
if ( |
| 155 |
$this->permissionManager->isBlockedFrom( |
| 156 |
$user, |
| 157 |
$this->targetObj, |
| 158 |
$checkReplica |
| 159 |
) |
| 160 |
) { |
| 161 |
throw new UserBlockedError( |
| 162 |
|
| 163 |
$user->getBlock(), |
| 164 |
$user, |
| 165 |
$this->getLanguage(), |
| 166 |
$request->getIP() |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
$this->showConvenienceLinks(); |
| 172 |
|
| 173 |
|
| 174 |
if ( $this->submitClicked ) { |
| 175 |
$this->submit(); |
| 176 |
} else { |
| 177 |
$this->showForm(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
$tagLogPage = new LogPage( 'tag' ); |
| 182 |
$output->addHTML( "<h2>" . $tagLogPage->getName()->escaped() . "</h2>\n" ); |
| 183 |
LogEventsList::showLogExtract( |
| 184 |
$output, |
| 185 |
'tag', |
| 186 |
$this->targetObj, |
| 187 |
'', |
| 188 |
[ 'lim' => 25, 'conds' => [], 'useMaster' => $this->wasSaved ] |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected function showConvenienceLinks() { |
| 196 |
|
| 197 |
if ( $this->targetObj ) { |
| 198 |
|
| 199 |
$this->getSkin()->setRelevantTitle( $this->targetObj ); |
| 200 |
|
| 201 |
$linkRenderer = $this->getLinkRenderer(); |
| 202 |
$links = []; |
| 203 |
$links[] = $linkRenderer->makeKnownLink( |
| 204 |
SpecialPage::getTitleFor( 'Log' ), |
| 205 |
$this->msg( 'viewpagelogs' )->text(), |
| 206 |
[], |
| 207 |
[ |
| 208 |
'page' => $this->targetObj->getPrefixedText(), |
| 209 |
'wpfilters' => [ 'tag' ], |
| 210 |
] |
| 211 |
); |
| 212 |
if ( !$this->targetObj->isSpecialPage() ) { |
| 213 |
|
| 214 |
$links[] = $linkRenderer->makeKnownLink( |
| 215 |
$this->targetObj, |
| 216 |
$this->msg( 'pagehist' )->text(), |
| 217 |
[], |
| 218 |
[ 'action' => 'history' ] |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
$links[] = $linkRenderer->makeKnownLink( |
| 223 |
SpecialPage::getTitleFor( 'Tags' ), |
| 224 |
$this->msg( 'tags-edit-manage-link' )->text() |
| 225 |
); |
| 226 |
|
| 227 |
$this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
protected function getList() { |
| 236 |
if ( $this->revList === null ) { |
| 237 |
$this->revList = ChangeTagsList::factory( $this->typeName, $this->getContext(), |
| 238 |
$this->targetObj, $this->ids ); |
| 239 |
} |
| 240 |
|
| 241 |
return $this->revList; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
protected function showForm() { |
| 249 |
$out = $this->getOutput(); |
| 250 |
|
| 251 |
$out->wrapWikiMsg( "<strong>$1</strong>", [ |
| 252 |
"tags-edit-{$this->typeName}-selected", |
| 253 |
$this->getLanguage()->formatNum( count( $this->ids ) ), |
| 254 |
$this->targetObj->getPrefixedText() |
| 255 |
] ); |
| 256 |
|
| 257 |
$this->addHelpLink( 'Help:Tags' ); |
| 258 |
$out->addHTML( "<ul>" ); |
| 259 |
|
| 260 |
$numRevisions = 0; |
| 261 |
|
| 262 |
$list = $this->getList(); |
| 263 |
for ( $list->reset(); $list->current(); $list->next() ) { |
| 264 |
$item = $list->current(); |
| 265 |
if ( !$item->canView() ) { |
| 266 |
throw new ErrorPageError( 'permissionserrors', 'tags-update-no-permission' ); |
| 267 |
} |
| 268 |
$numRevisions++; |
| 269 |
$out->addHTML( $item->getHTML() ); |
| 270 |
} |
| 271 |
|
| 272 |
if ( !$numRevisions ) { |
| 273 |
throw new ErrorPageError( 'tags-edit-nooldid-title', 'tags-edit-nooldid-text' ); |
| 274 |
} |
| 275 |
|
| 276 |
$out->addHTML( "</ul>" ); |
| 277 |
|
| 278 |
$out->wrapWikiMsg( '<p>$1</p>', "tags-edit-{$this->typeName}-explanation" ); |
| 279 |
|
| 280 |
|
| 281 |
$form = Html::openElement( 'form', [ 'method' => 'post', |
| 282 |
'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ), |
| 283 |
'id' => 'mw-revdel-form-revisions' ] ) . |
| 284 |
Html::openElement( 'fieldset' ) . |
| 285 |
Html::element( |
| 286 |
'legend', [], |
| 287 |
$this->msg( "tags-edit-{$this->typeName}-legend", count( $this->ids ) )->text() |
| 288 |
) . |
| 289 |
$this->buildCheckBoxes() . |
| 290 |
Html::openElement( 'table' ) . |
| 291 |
"<tr>\n" . |
| 292 |
'<td class="mw-label">' . |
| 293 |
Html::label( $this->msg( 'tags-edit-reason' )->text(), 'wpReason' ) . |
| 294 |
'</td>' . |
| 295 |
'<td class="mw-input">' . |
| 296 |
Html::element( 'input', [ 'name' => 'wpReason', 'size' => 60, 'value' => $this->reason, |
| 297 |
'id' => 'wpReason', |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT, |
| 302 |
] ) . |
| 303 |
'</td>' . |
| 304 |
"</tr><tr>\n" . |
| 305 |
'<td></td>' . |
| 306 |
'<td class="mw-submit">' . |
| 307 |
Html::submitButton( $this->msg( "tags-edit-{$this->typeName}-submit", |
| 308 |
$numRevisions )->text(), [ 'name' => 'wpSubmit' ] ) . |
| 309 |
'</td>' . |
| 310 |
"</tr>\n" . |
| 311 |
Html::closeElement( 'table' ) . |
| 312 |
Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) . |
| 313 |
Html::hidden( 'target', $this->targetObj->getPrefixedText() ) . |
| 314 |
Html::hidden( 'type', $this->typeName ) . |
| 315 |
Html::hidden( 'ids', implode( ',', $this->ids ) ) . |
| 316 |
Html::closeElement( 'fieldset' ) . "\n" . |
| 317 |
Html::closeElement( 'form' ) . "\n"; |
| 318 |
|
| 319 |
$out->addHTML( $form ); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
protected function buildCheckBoxes() { |
| 326 |
|
| 327 |
$list = $this->getList(); |
| 328 |
$tags = []; |
| 329 |
if ( $list->length() == 1 ) { |
| 330 |
$list->reset(); |
| 331 |
$tags = $list->current()->getTags(); |
| 332 |
if ( $tags ) { |
| 333 |
$tags = explode( ',', $tags ); |
| 334 |
} else { |
| 335 |
$tags = []; |
| 336 |
} |
| 337 |
|
| 338 |
$html = '<table id="mw-edittags-tags-selector">'; |
| 339 |
$html .= '<tr><td>' . $this->msg( 'tags-edit-existing-tags' )->escaped() . |
| 340 |
'</td><td>'; |
| 341 |
if ( $tags ) { |
| 342 |
$html .= $this->getLanguage()->commaList( array_map( 'htmlspecialchars', $tags ) ); |
| 343 |
} else { |
| 344 |
$html .= $this->msg( 'tags-edit-existing-tags-none' )->parse(); |
| 345 |
} |
| 346 |
$html .= '</td></tr>'; |
| 347 |
$tagSelect = $this->getTagSelect( $tags, $this->msg( 'tags-edit-new-tags' )->plain() ); |
| 348 |
$html .= '<tr><td>' . $tagSelect[0] . '</td><td>' . $tagSelect[1]; |
| 349 |
} else { |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
for ( $list->reset(); $list->current(); $list->next() ) { |
| 354 |
$currentTags = $list->current()->getTags(); |
| 355 |
if ( $currentTags ) { |
| 356 |
$tags = array_merge( $tags, explode( ',', $currentTags ) ); |
| 357 |
} |
| 358 |
} |
| 359 |
$tags = array_unique( $tags ); |
| 360 |
|
| 361 |
$html = '<table id="mw-edittags-tags-selector-multi"><tr><td>'; |
| 362 |
$tagSelect = $this->getTagSelect( [], $this->msg( 'tags-edit-add' )->plain() ); |
| 363 |
$html .= '<p>' . $tagSelect[0] . '</p>' . $tagSelect[1] . '</td><td>'; |
| 364 |
$html .= Html::element( 'p', [], $this->msg( 'tags-edit-remove' )->plain() ); |
| 365 |
$html .= Html::element( 'input', [ |
| 366 |
'type' => 'checkbox', 'name' => 'wpRemoveAllTags', 'value' => '1', |
| 367 |
'id' => 'mw-edittags-remove-all' |
| 368 |
] ) . ' ' |
| 369 |
. Html::label( $this->msg( 'tags-edit-remove-all-tags' )->plain(), 'mw-edittags-remove-all' ); |
| 370 |
$i = 0; |
| 371 |
foreach ( $tags as $tag ) { |
| 372 |
$id = 'mw-edittags-remove-' . $i++; |
| 373 |
$html .= Html::element( 'br' ) . "\n" . Html::element( 'input', [ |
| 374 |
'type' => 'checkbox', 'name' => 'wpTagsToRemove[]', 'value' => $tag, |
| 375 |
'class' => 'mw-edittags-remove-checkbox', 'id' => $id, |
| 376 |
] ) . ' ' . Html::label( $tag, $id ); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
$html .= Html::hidden( 'wpExistingTags', implode( ',', $tags ) ); |
| 383 |
$html .= '</td></tr></table>'; |
| 384 |
|
| 385 |
return $html; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
protected function getTagSelect( $selectedTags, $label ) { |
| 401 |
$result = []; |
| 402 |
$result[0] = Html::label( $label, 'mw-edittags-tag-list' ); |
| 403 |
|
| 404 |
$select = new XmlSelect( 'wpTagList[]', 'mw-edittags-tag-list', $selectedTags ); |
| 405 |
$select->setAttribute( 'multiple', 'multiple' ); |
| 406 |
$select->setAttribute( 'size', '8' ); |
| 407 |
|
| 408 |
$tags = $this->changeTagsStore->listExplicitlyDefinedTags(); |
| 409 |
$tags = array_unique( array_merge( $tags, $selectedTags ) ); |
| 410 |
|
| 411 |
|
| 412 |
$select->addOptions( array_combine( $tags, $tags ) ); |
| 413 |
|
| 414 |
$result[1] = $select->getHTML(); |
| 415 |
return $result; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
protected function submit() { |
| 423 |
|
| 424 |
$request = $this->getRequest(); |
| 425 |
$token = $request->getVal( 'wpEditToken' ); |
| 426 |
if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) { |
| 427 |
$this->getOutput()->addWikiMsg( 'sessionfailure' ); |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
$tagList = $request->getArray( 'wpTagList' ) ?? []; |
| 433 |
$existingTags = $request->getVal( 'wpExistingTags' ); |
| 434 |
if ( $existingTags === null || $existingTags === '' ) { |
| 435 |
$existingTags = []; |
| 436 |
} else { |
| 437 |
$existingTags = explode( ',', $existingTags ); |
| 438 |
} |
| 439 |
|
| 440 |
if ( count( $this->ids ) > 1 ) { |
| 441 |
|
| 442 |
$tagsToAdd = $tagList; |
| 443 |
if ( $request->getBool( 'wpRemoveAllTags' ) ) { |
| 444 |
$tagsToRemove = $existingTags; |
| 445 |
} else { |
| 446 |
$tagsToRemove = $request->getArray( 'wpTagsToRemove', [] ); |
| 447 |
} |
| 448 |
} else { |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
$tagsToAdd = array_diff( $tagList, $existingTags ); |
| 453 |
$tagsToRemove = array_diff( $existingTags, $tagList ); |
| 454 |
} |
| 455 |
|
| 456 |
if ( !$tagsToAdd && !$tagsToRemove ) { |
| 457 |
$status = Status::newFatal( 'tags-edit-none-selected' ); |
| 458 |
} else { |
| 459 |
$status = $this->getList()->updateChangeTagsOnAll( $tagsToAdd, |
| 460 |
$tagsToRemove, null, $this->reason, $this->getAuthority() ); |
| 461 |
} |
| 462 |
|
| 463 |
if ( $status->isGood() ) { |
| 464 |
$this->success(); |
| 465 |
return true; |
| 466 |
} else { |
| 467 |
$this->failure( $status ); |
| 468 |
return false; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
protected function success() { |
| 476 |
$out = $this->getOutput(); |
| 477 |
$out->setPageTitleMsg( $this->msg( 'actioncomplete' ) ); |
| 478 |
$out->addHTML( |
| 479 |
Html::successBox( $out->msg( 'tags-edit-success' )->parse() ) |
| 480 |
); |
| 481 |
$this->wasSaved = true; |
| 482 |
$this->revList->reloadFromPrimary(); |
| 483 |
$this->reason = ''; |
| 484 |
$this->showForm(); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
protected function failure( $status ) { |
| 492 |
$out = $this->getOutput(); |
| 493 |
$out->setPageTitleMsg( $this->msg( 'actionfailed' ) ); |
| 494 |
$out->addHTML( |
| 495 |
Html::errorBox( |
| 496 |
$out->parseAsContent( |
| 497 |
$status->getWikiText( 'tags-edit-failure', false, $this->getLanguage() ) |
| 498 |
) |
| 499 |
) |
| 500 |
); |
| 501 |
$this->showForm(); |
| 502 |
} |
| 503 |
|
| 504 |
public function getDescription() { |
| 505 |
return $this->msg( 'tags-edit-title' ); |
| 506 |
} |
| 507 |
|
| 508 |
protected function getGroupName() { |
| 509 |
return 'pagetools'; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
class_alias( SpecialEditTags::class, 'SpecialEditTags' ); |