| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Page; |
| 22 |
|
| 23 |
use ChangeTags; |
| 24 |
use File; |
| 25 |
use LogFormatterFactory; |
| 26 |
use ManualLogEntry; |
| 27 |
use MediaWiki\Collation\CollationFactory; |
| 28 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 29 |
use MediaWiki\Config\ServiceOptions; |
| 30 |
use MediaWiki\Content\ContentHandler; |
| 31 |
use MediaWiki\Content\IContentHandlerFactory; |
| 32 |
use MediaWiki\Content\WikitextContent; |
| 33 |
use MediaWiki\Context\RequestContext; |
| 34 |
use MediaWiki\Deferred\AtomicSectionUpdate; |
| 35 |
use MediaWiki\Deferred\DeferredUpdates; |
| 36 |
use MediaWiki\EditPage\SpamChecker; |
| 37 |
use MediaWiki\HookContainer\HookContainer; |
| 38 |
use MediaWiki\HookContainer\HookRunner; |
| 39 |
use MediaWiki\MainConfigNames; |
| 40 |
use MediaWiki\Permissions\Authority; |
| 41 |
use MediaWiki\Permissions\PermissionStatus; |
| 42 |
use MediaWiki\Permissions\RestrictionStore; |
| 43 |
use MediaWiki\Revision\RevisionStore; |
| 44 |
use MediaWiki\Revision\SlotRecord; |
| 45 |
use MediaWiki\Status\Status; |
| 46 |
use MediaWiki\Storage\PageUpdaterFactory; |
| 47 |
use MediaWiki\Title\NamespaceInfo; |
| 48 |
use MediaWiki\Title\Title; |
| 49 |
use MediaWiki\User\UserEditTracker; |
| 50 |
use MediaWiki\User\UserFactory; |
| 51 |
use MediaWiki\User\UserIdentity; |
| 52 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 53 |
use RepoGroup; |
| 54 |
use StringUtils; |
| 55 |
use Wikimedia\NormalizedException\NormalizedException; |
| 56 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 57 |
use Wikimedia\Rdbms\IDatabase; |
| 58 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 59 |
use WikiPage; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class MovePage { |
| 68 |
|
| 69 |
protected Title $oldTitle; |
| 70 |
protected Title $newTitle; |
| 71 |
protected ServiceOptions $options; |
| 72 |
protected IConnectionProvider $dbProvider; |
| 73 |
protected NamespaceInfo $nsInfo; |
| 74 |
protected WatchedItemStoreInterface $watchedItems; |
| 75 |
protected RepoGroup $repoGroup; |
| 76 |
private IContentHandlerFactory $contentHandlerFactory; |
| 77 |
private RevisionStore $revisionStore; |
| 78 |
private SpamChecker $spamChecker; |
| 79 |
private HookRunner $hookRunner; |
| 80 |
private WikiPageFactory $wikiPageFactory; |
| 81 |
private UserFactory $userFactory; |
| 82 |
private UserEditTracker $userEditTracker; |
| 83 |
private MovePageFactory $movePageFactory; |
| 84 |
public CollationFactory $collationFactory; |
| 85 |
private PageUpdaterFactory $pageUpdaterFactory; |
| 86 |
private RestrictionStore $restrictionStore; |
| 87 |
private DeletePageFactory $deletePageFactory; |
| 88 |
private LogFormatterFactory $logFormatterFactory; |
| 89 |
|
| 90 |
|
| 91 |
private $maximumMovedPages; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public const CONSTRUCTOR_OPTIONS = [ |
| 97 |
MainConfigNames::CategoryCollation, |
| 98 |
MainConfigNames::MaximumMovedPages, |
| 99 |
]; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function __construct( |
| 105 |
Title $oldTitle, |
| 106 |
Title $newTitle, |
| 107 |
ServiceOptions $options, |
| 108 |
IConnectionProvider $dbProvider, |
| 109 |
NamespaceInfo $nsInfo, |
| 110 |
WatchedItemStoreInterface $watchedItems, |
| 111 |
RepoGroup $repoGroup, |
| 112 |
IContentHandlerFactory $contentHandlerFactory, |
| 113 |
RevisionStore $revisionStore, |
| 114 |
SpamChecker $spamChecker, |
| 115 |
HookContainer $hookContainer, |
| 116 |
WikiPageFactory $wikiPageFactory, |
| 117 |
UserFactory $userFactory, |
| 118 |
UserEditTracker $userEditTracker, |
| 119 |
MovePageFactory $movePageFactory, |
| 120 |
CollationFactory $collationFactory, |
| 121 |
PageUpdaterFactory $pageUpdaterFactory, |
| 122 |
RestrictionStore $restrictionStore, |
| 123 |
DeletePageFactory $deletePageFactory, |
| 124 |
LogFormatterFactory $logFormatterFactory |
| 125 |
) { |
| 126 |
$this->oldTitle = $oldTitle; |
| 127 |
$this->newTitle = $newTitle; |
| 128 |
|
| 129 |
$this->options = $options; |
| 130 |
$this->dbProvider = $dbProvider; |
| 131 |
$this->nsInfo = $nsInfo; |
| 132 |
$this->watchedItems = $watchedItems; |
| 133 |
$this->repoGroup = $repoGroup; |
| 134 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 135 |
$this->revisionStore = $revisionStore; |
| 136 |
$this->spamChecker = $spamChecker; |
| 137 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 138 |
$this->wikiPageFactory = $wikiPageFactory; |
| 139 |
$this->userFactory = $userFactory; |
| 140 |
$this->userEditTracker = $userEditTracker; |
| 141 |
$this->movePageFactory = $movePageFactory; |
| 142 |
$this->collationFactory = $collationFactory; |
| 143 |
$this->pageUpdaterFactory = $pageUpdaterFactory; |
| 144 |
$this->restrictionStore = $restrictionStore; |
| 145 |
$this->deletePageFactory = $deletePageFactory; |
| 146 |
$this->logFormatterFactory = $logFormatterFactory; |
| 147 |
|
| 148 |
$this->maximumMovedPages = $this->options->get( MainConfigNames::MaximumMovedPages ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function setMaximumMovedPages( $max ) { |
| 157 |
$this->maximumMovedPages = $max; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private function authorizeInternal( |
| 167 |
callable $authorizer, |
| 168 |
Authority $performer, |
| 169 |
?string $reason |
| 170 |
): PermissionStatus { |
| 171 |
$status = PermissionStatus::newEmpty(); |
| 172 |
|
| 173 |
$authorizer( 'move', $this->oldTitle, $status ); |
| 174 |
$authorizer( 'edit', $this->oldTitle, $status ); |
| 175 |
$authorizer( 'move-target', $this->newTitle, $status ); |
| 176 |
$authorizer( 'edit', $this->newTitle, $status ); |
| 177 |
|
| 178 |
if ( $reason !== null && $this->spamChecker->checkSummary( $reason ) !== false ) { |
| 179 |
|
| 180 |
$status->fatal( 'spamprotectiontext' ); |
| 181 |
} |
| 182 |
|
| 183 |
$tp = $this->restrictionStore->getCreateProtection( $this->newTitle ) ?: false; |
| 184 |
if ( $tp !== false && !$performer->isAllowed( $tp['permission'] ) ) { |
| 185 |
$status->fatal( 'cantmove-titleprotected' ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
$user = $this->userFactory->newFromAuthority( $performer ); |
| 190 |
$status = Status::wrap( $status ); |
| 191 |
$this->hookRunner->onMovePageCheckPermissions( |
| 192 |
$this->oldTitle, $this->newTitle, $user, $reason, $status ); |
| 193 |
|
| 194 |
$permissionStatus = PermissionStatus::newEmpty(); |
| 195 |
foreach ( $status->getMessages() as $msg ) { |
| 196 |
$permissionStatus->fatal( $msg ); |
| 197 |
} |
| 198 |
return $permissionStatus; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function probablyCanMove( Authority $performer, ?string $reason = null ): PermissionStatus { |
| 213 |
return $this->authorizeInternal( |
| 214 |
static function ( string $action, PageIdentity $target, PermissionStatus $status ) use ( $performer ) { |
| 215 |
return $performer->probablyCan( $action, $target, $status ); |
| 216 |
}, |
| 217 |
$performer, |
| 218 |
$reason |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function authorizeMove( Authority $performer, ?string $reason = null ): PermissionStatus { |
| 234 |
return $this->authorizeInternal( |
| 235 |
static function ( string $action, PageIdentity $target, PermissionStatus $status ) use ( $performer ) { |
| 236 |
return $performer->authorizeWrite( $action, $target, $status ); |
| 237 |
}, |
| 238 |
$performer, |
| 239 |
$reason |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function checkPermissions( Authority $performer, $reason ) { |
| 253 |
$permissionStatus = $this->authorizeInternal( |
| 254 |
static function ( string $action, PageIdentity $target, PermissionStatus $status ) use ( $performer ) { |
| 255 |
return $performer->definitelyCan( $action, $target, $status ); |
| 256 |
}, |
| 257 |
$performer, |
| 258 |
$reason |
| 259 |
); |
| 260 |
return Status::wrap( $permissionStatus ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function isValidMove() { |
| 271 |
$status = new Status(); |
| 272 |
|
| 273 |
if ( $this->oldTitle->equals( $this->newTitle ) ) { |
| 274 |
$status->fatal( 'selfmove' ); |
| 275 |
} elseif ( $this->newTitle->getArticleID( IDBAccessObject::READ_LATEST ) |
| 276 |
&& !$this->isValidMoveTarget() |
| 277 |
) { |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
$fatal = $this->newTitle->isSingleRevRedirect() ? 'redirectexists' : 'articleexists'; |
| 284 |
$status->fatal( $fatal, $this->newTitle->getPrefixedText() ); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if ( $this->oldTitle->getDBkey() == '' ) { |
| 291 |
$status->fatal( 'badarticleerror' ); |
| 292 |
} elseif ( $this->oldTitle->isExternal() ) { |
| 293 |
$status->fatal( 'immobile-source-namespace-iw' ); |
| 294 |
} elseif ( !$this->oldTitle->isMovable() ) { |
| 295 |
$nsText = $this->oldTitle->getNsText(); |
| 296 |
if ( $nsText === '' ) { |
| 297 |
$nsText = wfMessage( 'blanknamespace' )->text(); |
| 298 |
} |
| 299 |
$status->fatal( 'immobile-source-namespace', $nsText ); |
| 300 |
} elseif ( !$this->oldTitle->exists() ) { |
| 301 |
$status->fatal( 'movepage-source-doesnt-exist', $this->oldTitle->getPrefixedText() ); |
| 302 |
} |
| 303 |
|
| 304 |
if ( $this->newTitle->isExternal() ) { |
| 305 |
$status->fatal( 'immobile-target-namespace-iw' ); |
| 306 |
} elseif ( !$this->newTitle->isMovable() ) { |
| 307 |
$nsText = $this->newTitle->getNsText(); |
| 308 |
if ( $nsText === '' ) { |
| 309 |
$nsText = wfMessage( 'blanknamespace' )->text(); |
| 310 |
} |
| 311 |
$status->fatal( 'immobile-target-namespace', $nsText ); |
| 312 |
} |
| 313 |
if ( !$this->newTitle->isValid() ) { |
| 314 |
$status->fatal( 'movepage-invalid-target-title' ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
if ( !$this->contentHandlerFactory |
| 319 |
->getContentHandler( $this->oldTitle->getContentModel() ) |
| 320 |
->canBeUsedOn( $this->newTitle ) |
| 321 |
) { |
| 322 |
$status->fatal( |
| 323 |
'content-not-allowed-here', |
| 324 |
ContentHandler::getLocalizedName( $this->oldTitle->getContentModel() ), |
| 325 |
$this->newTitle->getPrefixedText(), |
| 326 |
SlotRecord::MAIN |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
if ( $this->oldTitle->inNamespace( NS_FILE ) ) { |
| 332 |
$status->merge( $this->isValidFileMove() ); |
| 333 |
} |
| 334 |
|
| 335 |
if ( $this->newTitle->inNamespace( NS_FILE ) && !$this->oldTitle->inNamespace( NS_FILE ) ) { |
| 336 |
$status->fatal( 'nonfile-cannot-move-to-file' ); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
$this->hookRunner->onMovePageIsValidMove( $this->oldTitle, $this->newTitle, $status ); |
| 341 |
|
| 342 |
return $status; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
protected function isValidFileMove() { |
| 352 |
$status = new Status(); |
| 353 |
|
| 354 |
if ( !$this->newTitle->inNamespace( NS_FILE ) ) { |
| 355 |
|
| 356 |
return $status->fatal( 'imagenocrossnamespace' ); |
| 357 |
} |
| 358 |
|
| 359 |
$file = $this->repoGroup->getLocalRepo()->newFile( $this->oldTitle ); |
| 360 |
$file->load( IDBAccessObject::READ_LATEST ); |
| 361 |
if ( $file->exists() ) { |
| 362 |
if ( $this->newTitle->getText() != wfStripIllegalFilenameChars( $this->newTitle->getText() ) ) { |
| 363 |
$status->fatal( 'imageinvalidfilename' ); |
| 364 |
} |
| 365 |
if ( strlen( $this->newTitle->getText() ) > 240 ) { |
| 366 |
$status->fatal( 'filename-toolong' ); |
| 367 |
} |
| 368 |
if ( |
| 369 |
!$this->repoGroup->getLocalRepo()->backendSupportsUnicodePaths() && |
| 370 |
!preg_match( '/^[\x0-\x7f]*$/', $this->newTitle->getText() ) |
| 371 |
) { |
| 372 |
$status->fatal( 'windows-nonascii-filename' ); |
| 373 |
} |
| 374 |
if ( strrpos( $this->newTitle->getText(), '.' ) === 0 ) { |
| 375 |
|
| 376 |
|
| 377 |
$status->fatal( 'filename-tooshort' ); |
| 378 |
} |
| 379 |
if ( !File::checkExtensionCompatibility( $file, $this->newTitle->getDBkey() ) ) { |
| 380 |
$status->fatal( 'imagetypemismatch' ); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return $status; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
protected function isValidMoveTarget() { |
| 395 |
|
| 396 |
if ( $this->newTitle->inNamespace( NS_FILE ) ) { |
| 397 |
$file = $this->repoGroup->getLocalRepo()->newFile( $this->newTitle ); |
| 398 |
$file->load( IDBAccessObject::READ_LATEST ); |
| 399 |
if ( $file->exists() ) { |
| 400 |
wfDebug( __METHOD__ . ": file exists" ); |
| 401 |
return false; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if ( !$this->newTitle->isSingleRevRedirect() ) { |
| 406 |
wfDebug( __METHOD__ . ": not a one-rev redirect" ); |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
$rev = $this->revisionStore->getRevisionByTitle( |
| 411 |
$this->newTitle, |
| 412 |
0, |
| 413 |
IDBAccessObject::READ_LATEST |
| 414 |
); |
| 415 |
if ( !is_object( $rev ) ) { |
| 416 |
return false; |
| 417 |
} |
| 418 |
$content = $rev->getContent( SlotRecord::MAIN ); |
| 419 |
|
| 420 |
|
| 421 |
$redirTitle = $content ? $content->getRedirectTarget() : null; |
| 422 |
|
| 423 |
if ( $redirTitle ) { |
| 424 |
if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle->getPrefixedDBkey() && |
| 425 |
$redirTitle->getPrefixedDBkey() !== $this->newTitle->getPrefixedDBkey() ) { |
| 426 |
wfDebug( __METHOD__ . ": redirect points to other page" ); |
| 427 |
return false; |
| 428 |
} else { |
| 429 |
return true; |
| 430 |
} |
| 431 |
} else { |
| 432 |
|
| 433 |
wfDebug( __METHOD__ . ": failsafe: database says " . $this->newTitle->getPrefixedDBkey() . |
| 434 |
" is a redirect, but it doesn't contain a valid redirect." ); |
| 435 |
return false; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public function move( |
| 451 |
UserIdentity $user, $reason = null, $createRedirect = true, array $changeTags = [] |
| 452 |
) { |
| 453 |
$status = $this->isValidMove(); |
| 454 |
if ( !$status->isOK() ) { |
| 455 |
return $status; |
| 456 |
} |
| 457 |
|
| 458 |
return $this->moveUnsafe( $user, $reason ?? '', $createRedirect, $changeTags ); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public function moveIfAllowed( |
| 471 |
Authority $performer, $reason = null, $createRedirect = true, array $changeTags = [] |
| 472 |
) { |
| 473 |
$status = $this->isValidMove(); |
| 474 |
$status->merge( $this->authorizeMove( $performer, $reason ) ); |
| 475 |
if ( $changeTags ) { |
| 476 |
$status->merge( ChangeTags::canAddTagsAccompanyingChange( $changeTags, $performer ) ); |
| 477 |
} |
| 478 |
|
| 479 |
if ( !$status->isOK() ) { |
| 480 |
|
| 481 |
$user = $this->userFactory->newFromAuthority( $performer ); |
| 482 |
|
| 483 |
$user->spreadAnyEditBlock(); |
| 484 |
return $status; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
if ( !$performer->isAllowed( 'suppressredirect' ) ) { |
| 489 |
$createRedirect = true; |
| 490 |
} |
| 491 |
|
| 492 |
return $this->moveUnsafe( $performer->getUser(), $reason ?? '', $createRedirect, $changeTags ); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
public function moveSubpages( |
| 510 |
UserIdentity $user, $reason = null, $createRedirect = true, array $changeTags = [] |
| 511 |
) { |
| 512 |
return $this->moveSubpagesInternal( |
| 513 |
function ( Title $oldSubpage, Title $newSubpage ) |
| 514 |
use ( $user, $reason, $createRedirect, $changeTags ) { |
| 515 |
$mp = $this->movePageFactory->newMovePage( $oldSubpage, $newSubpage ); |
| 516 |
return $mp->move( $user, $reason, $createRedirect, $changeTags ); |
| 517 |
} |
| 518 |
); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public function moveSubpagesIfAllowed( |
| 535 |
Authority $performer, $reason = null, $createRedirect = true, array $changeTags = [] |
| 536 |
) { |
| 537 |
if ( !$performer->authorizeWrite( 'move-subpages', $this->oldTitle ) ) { |
| 538 |
return Status::newFatal( 'cant-move-subpages' ); |
| 539 |
} |
| 540 |
return $this->moveSubpagesInternal( |
| 541 |
function ( Title $oldSubpage, Title $newSubpage ) |
| 542 |
use ( $performer, $reason, $createRedirect, $changeTags ) { |
| 543 |
$mp = $this->movePageFactory->newMovePage( $oldSubpage, $newSubpage ); |
| 544 |
return $mp->moveIfAllowed( $performer, $reason, $createRedirect, $changeTags ); |
| 545 |
} |
| 546 |
); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
private function moveSubpagesInternal( callable $subpageMoveCallback ) { |
| 554 |
|
| 555 |
if ( !$this->nsInfo->hasSubpages( $this->oldTitle->getNamespace() ) ) { |
| 556 |
return Status::newFatal( 'namespace-nosubpages', |
| 557 |
$this->nsInfo->getCanonicalName( $this->oldTitle->getNamespace() ) ); |
| 558 |
} |
| 559 |
if ( !$this->nsInfo->hasSubpages( $this->newTitle->getNamespace() ) ) { |
| 560 |
return Status::newFatal( 'namespace-nosubpages', |
| 561 |
$this->nsInfo->getCanonicalName( $this->newTitle->getNamespace() ) ); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
$max = $this->maximumMovedPages; |
| 568 |
$topStatus = Status::newGood(); |
| 569 |
$perTitleStatus = []; |
| 570 |
$subpages = $this->oldTitle->getSubpages( $max >= 0 ? $max + 1 : -1 ); |
| 571 |
$count = 0; |
| 572 |
foreach ( $subpages as $oldSubpage ) { |
| 573 |
$count++; |
| 574 |
if ( $max >= 0 && $count > $max ) { |
| 575 |
$status = Status::newFatal( 'movepage-max-pages', $max ); |
| 576 |
$perTitleStatus[$oldSubpage->getPrefixedText()] = $status; |
| 577 |
$topStatus->merge( $status ); |
| 578 |
$topStatus->setOK( true ); |
| 579 |
break; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
if ( $oldSubpage->getArticleID() == $this->oldTitle->getArticleID() || |
| 585 |
$oldSubpage->getArticleID() == $this->newTitle->getArticleID() |
| 586 |
) { |
| 587 |
|
| 588 |
continue; |
| 589 |
} |
| 590 |
$newPageName = preg_replace( |
| 591 |
'#^' . preg_quote( $this->oldTitle->getDBkey(), '#' ) . '#', |
| 592 |
StringUtils::escapeRegexReplacement( $this->newTitle->getDBkey() ), |
| 593 |
$oldSubpage->getDBkey() ); |
| 594 |
if ( $oldSubpage->isTalkPage() ) { |
| 595 |
$newNs = $this->nsInfo->getTalkPage( $this->newTitle )->getNamespace(); |
| 596 |
} else { |
| 597 |
$newNs = $this->nsInfo->getSubjectPage( $this->newTitle )->getNamespace(); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
$newSubpage = Title::makeTitleSafe( $newNs, $newPageName ); |
| 602 |
$status = $subpageMoveCallback( $oldSubpage, $newSubpage ); |
| 603 |
if ( $status->isOK() ) { |
| 604 |
$status->setResult( true, $newSubpage->getPrefixedText() ); |
| 605 |
} |
| 606 |
$perTitleStatus[$oldSubpage->getPrefixedText()] = $status; |
| 607 |
$topStatus->merge( $status ); |
| 608 |
$topStatus->setOK( true ); |
| 609 |
} |
| 610 |
|
| 611 |
$topStatus->value = $perTitleStatus; |
| 612 |
return $topStatus; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
private function moveUnsafe( UserIdentity $user, $reason, $createRedirect, array $changeTags ) { |
| 625 |
$status = Status::newGood(); |
| 626 |
|
| 627 |
|
| 628 |
$userObj = $this->userFactory->newFromUserIdentity( $user ); |
| 629 |
$this->hookRunner->onTitleMove( $this->oldTitle, $this->newTitle, $userObj, $reason, $status ); |
| 630 |
if ( !$status->isOK() ) { |
| 631 |
|
| 632 |
return $status; |
| 633 |
} |
| 634 |
|
| 635 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 636 |
$dbw->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE ); |
| 637 |
|
| 638 |
$this->hookRunner->onTitleMoveStarting( $this->oldTitle, $this->newTitle, $userObj ); |
| 639 |
|
| 640 |
$pageid = $this->oldTitle->getArticleID( IDBAccessObject::READ_LATEST ); |
| 641 |
$protected = $this->restrictionStore->isProtected( $this->oldTitle ); |
| 642 |
|
| 643 |
|
| 644 |
$moveAttemptResult = $this->moveToInternal( $user, $this->newTitle, $reason, $createRedirect, |
| 645 |
$changeTags ); |
| 646 |
|
| 647 |
if ( !$moveAttemptResult->isGood() ) { |
| 648 |
|
| 649 |
$dbw->cancelAtomic( __METHOD__ ); |
| 650 |
return $moveAttemptResult; |
| 651 |
} else { |
| 652 |
$nullRevision = $moveAttemptResult->getValue()['nullRevision']; |
| 653 |
'@phan-var \MediaWiki\Revision\RevisionRecord $nullRevision'; |
| 654 |
} |
| 655 |
|
| 656 |
$redirid = $this->oldTitle->getArticleID(); |
| 657 |
|
| 658 |
if ( $protected ) { |
| 659 |
|
| 660 |
$res = $dbw->newSelectQueryBuilder() |
| 661 |
->select( [ 'pr_type', 'pr_level', 'pr_cascade', 'pr_expiry' ] ) |
| 662 |
->from( 'page_restrictions' ) |
| 663 |
->where( [ 'pr_page' => $pageid ] ) |
| 664 |
->forUpdate() |
| 665 |
->caller( __METHOD__ ) |
| 666 |
->fetchResultSet(); |
| 667 |
$rowsInsert = []; |
| 668 |
foreach ( $res as $row ) { |
| 669 |
$rowsInsert[] = [ |
| 670 |
'pr_page' => $redirid, |
| 671 |
'pr_type' => $row->pr_type, |
| 672 |
'pr_level' => $row->pr_level, |
| 673 |
'pr_cascade' => $row->pr_cascade, |
| 674 |
'pr_expiry' => $row->pr_expiry |
| 675 |
]; |
| 676 |
} |
| 677 |
$dbw->newInsertQueryBuilder() |
| 678 |
->insertInto( 'page_restrictions' ) |
| 679 |
->ignore() |
| 680 |
->rows( $rowsInsert ) |
| 681 |
->caller( __METHOD__ )->execute(); |
| 682 |
|
| 683 |
|
| 684 |
$comment = wfMessage( |
| 685 |
'prot_1movedto2', |
| 686 |
$this->oldTitle->getPrefixedText(), |
| 687 |
$this->newTitle->getPrefixedText() |
| 688 |
)->inContentLanguage()->text(); |
| 689 |
if ( $reason ) { |
| 690 |
$comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
$logRelationsValues = $dbw->newSelectQueryBuilder() |
| 695 |
->select( 'pr_id' ) |
| 696 |
->from( 'page_restrictions' ) |
| 697 |
->where( [ 'pr_page' => $redirid ] ) |
| 698 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 699 |
|
| 700 |
|
| 701 |
$logEntry = new ManualLogEntry( 'protect', 'move_prot' ); |
| 702 |
$logEntry->setTarget( $this->newTitle ); |
| 703 |
$logEntry->setComment( $comment ); |
| 704 |
$logEntry->setPerformer( $user ); |
| 705 |
$logEntry->setParameters( [ |
| 706 |
'4::oldtitle' => $this->oldTitle->getPrefixedText(), |
| 707 |
] ); |
| 708 |
$logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] ); |
| 709 |
$logEntry->addTags( $changeTags ); |
| 710 |
$logId = $logEntry->insert(); |
| 711 |
$logEntry->publish( $logId ); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
$oldtitle = $this->oldTitle->getDBkey(); |
| 716 |
$newtitle = $this->newTitle->getDBkey(); |
| 717 |
$oldsnamespace = $this->nsInfo->getSubject( $this->oldTitle->getNamespace() ); |
| 718 |
$newsnamespace = $this->nsInfo->getSubject( $this->newTitle->getNamespace() ); |
| 719 |
if ( $oldsnamespace != $newsnamespace || $oldtitle != $newtitle ) { |
| 720 |
$this->watchedItems->duplicateAllAssociatedEntries( $this->oldTitle, $this->newTitle ); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
if ( $this->oldTitle->getNamespace() === NS_FILE ) { |
| 726 |
$status = $this->moveFile( $this->oldTitle, $this->newTitle ); |
| 727 |
if ( !$status->isOK() ) { |
| 728 |
$dbw->cancelAtomic( __METHOD__ ); |
| 729 |
return $status; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
$this->hookRunner->onPageMoveCompleting( |
| 734 |
$this->oldTitle, $this->newTitle, |
| 735 |
$user, $pageid, $redirid, $reason, $nullRevision |
| 736 |
); |
| 737 |
|
| 738 |
$dbw->endAtomic( __METHOD__ ); |
| 739 |
|
| 740 |
|
| 741 |
DeferredUpdates::addUpdate( |
| 742 |
new AtomicSectionUpdate( |
| 743 |
$dbw, |
| 744 |
__METHOD__, |
| 745 |
function () use ( $user, $pageid, $redirid, $reason, $nullRevision ) { |
| 746 |
$this->hookRunner->onPageMoveComplete( |
| 747 |
$this->oldTitle, |
| 748 |
$this->newTitle, |
| 749 |
$user, |
| 750 |
$pageid, |
| 751 |
$redirid, |
| 752 |
$reason, |
| 753 |
$nullRevision |
| 754 |
); |
| 755 |
} |
| 756 |
) |
| 757 |
); |
| 758 |
|
| 759 |
return $moveAttemptResult; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
private function moveFile( $oldTitle, $newTitle ) { |
| 772 |
$file = $this->repoGroup->getLocalRepo()->newFile( $oldTitle ); |
| 773 |
$file->load( IDBAccessObject::READ_LATEST ); |
| 774 |
if ( $file->exists() ) { |
| 775 |
$status = $file->move( $newTitle ); |
| 776 |
} else { |
| 777 |
$status = Status::newGood(); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
$this->repoGroup->clearCache( $oldTitle ); |
| 782 |
$this->repoGroup->clearCache( $newTitle ); |
| 783 |
return $status; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
private function moveToInternal( |
| 805 |
UserIdentity $user, |
| 806 |
&$nt, |
| 807 |
$reason = '', |
| 808 |
$createRedirect = true, |
| 809 |
array $changeTags = [] |
| 810 |
): Status { |
| 811 |
if ( $nt->getArticleID( IDBAccessObject::READ_LATEST ) ) { |
| 812 |
$moveOverRedirect = true; |
| 813 |
$logType = 'move_redir'; |
| 814 |
} else { |
| 815 |
$moveOverRedirect = false; |
| 816 |
$logType = 'move'; |
| 817 |
} |
| 818 |
|
| 819 |
if ( $moveOverRedirect ) { |
| 820 |
$overwriteMessage = wfMessage( |
| 821 |
'delete_and_move_reason', |
| 822 |
$this->oldTitle->getPrefixedText() |
| 823 |
)->inContentLanguage()->text(); |
| 824 |
$newpage = $this->wikiPageFactory->newFromTitle( $nt ); |
| 825 |
|
| 826 |
$moverAuthority = $this->userFactory->newFromUserIdentity( $user ); |
| 827 |
$deletePage = $this->deletePageFactory->newDeletePage( $newpage, $moverAuthority ); |
| 828 |
$status = $deletePage |
| 829 |
->setTags( $changeTags ) |
| 830 |
->setLogSubtype( 'delete_redir' ) |
| 831 |
->deleteUnsafe( $overwriteMessage ); |
| 832 |
if ( $status->isGood() && $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ) { |
| 833 |
|
| 834 |
|
| 835 |
$status->warning( 'delete-scheduled', wfEscapeWikiText( $nt->getPrefixedText() ) ); |
| 836 |
} |
| 837 |
|
| 838 |
if ( !$status->isGood() ) { |
| 839 |
return $status; |
| 840 |
} |
| 841 |
|
| 842 |
$nt->resetArticleID( false ); |
| 843 |
} |
| 844 |
|
| 845 |
if ( $createRedirect ) { |
| 846 |
if ( $this->oldTitle->getNamespace() === NS_CATEGORY |
| 847 |
&& !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled() |
| 848 |
) { |
| 849 |
$redirectContent = new WikitextContent( |
| 850 |
wfMessage( 'category-move-redirect-override' ) |
| 851 |
->params( $nt->getPrefixedText() )->inContentLanguage()->plain() ); |
| 852 |
} else { |
| 853 |
$redirectContent = $this->contentHandlerFactory |
| 854 |
->getContentHandler( $this->oldTitle->getContentModel() ) |
| 855 |
->makeRedirectContent( |
| 856 |
$nt, |
| 857 |
wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() |
| 858 |
); |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
} else { |
| 863 |
$redirectContent = null; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
$oldid = $this->oldTitle->getArticleID(); |
| 868 |
$logTitle = clone $this->oldTitle; |
| 869 |
|
| 870 |
$logEntry = new ManualLogEntry( 'move', $logType ); |
| 871 |
$logEntry->setPerformer( $user ); |
| 872 |
$logEntry->setTarget( $logTitle ); |
| 873 |
$logEntry->setComment( $reason ); |
| 874 |
$logEntry->setParameters( [ |
| 875 |
'4::target' => $nt->getPrefixedText(), |
| 876 |
'5::noredir' => $redirectContent ? '0' : '1', |
| 877 |
] ); |
| 878 |
|
| 879 |
$formatter = $this->logFormatterFactory->newFromEntry( $logEntry ); |
| 880 |
$formatter->setContext( RequestContext::newExtraneousContext( $this->oldTitle ) ); |
| 881 |
$comment = $formatter->getPlainActionText(); |
| 882 |
if ( $reason ) { |
| 883 |
$comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason; |
| 884 |
} |
| 885 |
|
| 886 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 887 |
|
| 888 |
$oldpage = $this->wikiPageFactory->newFromTitle( $this->oldTitle ); |
| 889 |
$oldcountable = $oldpage->isCountable(); |
| 890 |
|
| 891 |
$newpage = $this->wikiPageFactory->newFromTitle( $nt ); |
| 892 |
|
| 893 |
|
| 894 |
$dbw->newUpdateQueryBuilder() |
| 895 |
->update( 'page' ) |
| 896 |
->set( [ |
| 897 |
'page_namespace' => $nt->getNamespace(), |
| 898 |
'page_title' => $nt->getDBkey(), |
| 899 |
] ) |
| 900 |
->where( [ 'page_id' => $oldid ] ) |
| 901 |
->caller( __METHOD__ )->execute(); |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
$nt->resetArticleID( $oldid ); |
| 906 |
|
| 907 |
$commentObj = CommentStoreComment::newUnsavedComment( $comment ); |
| 908 |
|
| 909 |
$nullRevision = $this->revisionStore->newNullRevision( |
| 910 |
$dbw, |
| 911 |
$nt, |
| 912 |
$commentObj, |
| 913 |
true, |
| 914 |
$user |
| 915 |
); |
| 916 |
if ( $nullRevision === null ) { |
| 917 |
$id = $nt->getArticleID( IDBAccessObject::READ_EXCLUSIVE ); |
| 918 |
|
| 919 |
throw new NormalizedException( 'Failed to create null revision while ' . |
| 920 |
'moving page ID {oldId} to {prefixedDBkey} (page ID {id})', |
| 921 |
[ |
| 922 |
'oldId' => $oldid, |
| 923 |
'prefixedDBkey' => $nt->getPrefixedDBkey(), |
| 924 |
'id' => $id, |
| 925 |
] |
| 926 |
); |
| 927 |
} |
| 928 |
|
| 929 |
$nullRevision = $this->revisionStore->insertRevisionOn( $nullRevision, $dbw ); |
| 930 |
$logEntry->setAssociatedRevId( $nullRevision->getId() ); |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
$this->userEditTracker->incrementUserEditCount( $user ); |
| 938 |
|
| 939 |
|
| 940 |
$isRedirect = $this->oldTitle->isRedirect(); |
| 941 |
if ( !$redirectContent ) { |
| 942 |
|
| 943 |
WikiPage::onArticleDelete( $this->oldTitle ); |
| 944 |
} |
| 945 |
|
| 946 |
$this->oldTitle->resetArticleID( 0 ); |
| 947 |
$newpage->loadPageData( IDBAccessObject::READ_LOCKING ); |
| 948 |
|
| 949 |
$newpage->updateRevisionOn( $dbw, $nullRevision, null, $isRedirect ); |
| 950 |
|
| 951 |
$fakeTags = []; |
| 952 |
$this->hookRunner->onRevisionFromEditComplete( |
| 953 |
$newpage, $nullRevision, $nullRevision->getParentId(), $user, $fakeTags ); |
| 954 |
|
| 955 |
$options = [ |
| 956 |
'changed' => false, |
| 957 |
'moved' => true, |
| 958 |
'oldtitle' => $this->oldTitle, |
| 959 |
'oldcountable' => $oldcountable, |
| 960 |
'causeAction' => 'MovePage', |
| 961 |
'causeAgent' => $user->getName(), |
| 962 |
]; |
| 963 |
|
| 964 |
$updater = $this->pageUpdaterFactory->newDerivedPageDataUpdater( $newpage ); |
| 965 |
$updater->prepareUpdate( $nullRevision, $options ); |
| 966 |
$updater->doUpdates(); |
| 967 |
|
| 968 |
WikiPage::onArticleCreate( $nt ); |
| 969 |
|
| 970 |
|
| 971 |
$redirectRevision = null; |
| 972 |
if ( $redirectContent ) { |
| 973 |
$redirectArticle = $this->wikiPageFactory->newFromTitle( $this->oldTitle ); |
| 974 |
$redirectArticle->loadFromRow( false, IDBAccessObject::READ_LOCKING ); |
| 975 |
$redirectRevision = $redirectArticle->newPageUpdater( $user ) |
| 976 |
->setContent( SlotRecord::MAIN, $redirectContent ) |
| 977 |
->addTags( $changeTags ) |
| 978 |
->addSoftwareTag( 'mw-new-redirect' ) |
| 979 |
->setUsePageCreationLog( false ) |
| 980 |
->setFlags( EDIT_SUPPRESS_RC ) |
| 981 |
->saveRevision( $commentObj ); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
$logid = $logEntry->insert(); |
| 986 |
|
| 987 |
$logEntry->addTags( $changeTags ); |
| 988 |
$logEntry->publish( $logid ); |
| 989 |
|
| 990 |
return Status::newGood( [ |
| 991 |
'nullRevision' => $nullRevision, |
| 992 |
'redirectRevision' => $redirectRevision, |
| 993 |
] ); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
class_alias( MovePage::class, 'MovePage' ); |