4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\Config\ServiceOptions
;
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Page\MovePageFactory
;
25 use MediaWiki\Permissions\PermissionManager
;
26 use MediaWiki\Revision\SlotRecord
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use Wikimedia\Rdbms\LoadBalancer
;
31 * Handles the backend logic of moving a page from one title
56 protected $loadBalancer;
64 * @var WatchedItemStore
66 protected $watchedItems;
69 * @var PermissionManager
79 * Calling this directly is deprecated in 1.34. Use MovePageFactory instead.
81 * @param Title $oldTitle
82 * @param Title $newTitle
83 * @param ServiceOptions|null $options
84 * @param LoadBalancer|null $loadBalancer
85 * @param NamespaceInfo|null $nsInfo
86 * @param WatchedItemStore|null $watchedItems
87 * @param PermissionManager|null $permMgr
89 public function __construct(
92 ServiceOptions
$options = null,
93 LoadBalancer
$loadBalancer = null,
94 NamespaceInfo
$nsInfo = null,
95 WatchedItemStore
$watchedItems = null,
96 PermissionManager
$permMgr = null,
97 RepoGroup
$repoGroup = null
99 $this->oldTitle
= $oldTitle;
100 $this->newTitle
= $newTitle;
101 $this->options
= $options ??
102 new ServiceOptions( MovePageFactory
::$constructorOptions,
103 MediaWikiServices
::getInstance()->getMainConfig() );
104 $this->loadBalancer
=
105 $loadBalancer ?? MediaWikiServices
::getInstance()->getDBLoadBalancer();
106 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
107 $this->watchedItems
=
108 $watchedItems ?? MediaWikiServices
::getInstance()->getWatchedItemStore();
109 $this->permMgr
= $permMgr ?? MediaWikiServices
::getInstance()->getPermissionManager();
110 $this->repoGroup
= $repoGroup ?? MediaWikiServices
::getInstance()->getRepoGroup();
114 * Check if the user is allowed to perform the move.
117 * @param string|null $reason To check against summary spam regex. Set to null to skip the check,
118 * for instance to display errors preemptively before the user has filled in a summary.
121 public function checkPermissions( User
$user, $reason ) {
122 $status = new Status();
124 $errors = wfMergeErrorArrays(
125 $this->permMgr
->getPermissionErrors( 'move', $user, $this->oldTitle
),
126 $this->permMgr
->getPermissionErrors( 'edit', $user, $this->oldTitle
),
127 $this->permMgr
->getPermissionErrors( 'move-target', $user, $this->newTitle
),
128 $this->permMgr
->getPermissionErrors( 'edit', $user, $this->newTitle
)
131 // Convert into a Status object
133 foreach ( $errors as $error ) {
134 $status->fatal( ...$error );
138 if ( $reason !== null && EditPage
::matchSummarySpamRegex( $reason ) !== false ) {
139 // This is kind of lame, won't display nice
140 $status->fatal( 'spamprotectiontext' );
143 $tp = $this->newTitle
->getTitleProtection();
144 if ( $tp !== false && !$user->isAllowed( $tp['permission'] ) ) {
145 $status->fatal( 'cantmove-titleprotected' );
148 Hooks
::run( 'MovePageCheckPermissions',
149 [ $this->oldTitle
, $this->newTitle
, $user, $reason, $status ]
156 * Does various sanity checks that the move is
157 * valid. Only things based on the two titles
158 * should be checked here.
162 public function isValidMove() {
163 $status = new Status();
165 if ( $this->oldTitle
->equals( $this->newTitle
) ) {
166 $status->fatal( 'selfmove' );
168 if ( !$this->oldTitle
->isMovable() ) {
169 $status->fatal( 'immobile-source-namespace', $this->oldTitle
->getNsText() );
171 if ( $this->newTitle
->isExternal() ) {
172 $status->fatal( 'immobile-target-namespace-iw' );
174 if ( !$this->newTitle
->isMovable() ) {
175 $status->fatal( 'immobile-target-namespace', $this->newTitle
->getNsText() );
178 $oldid = $this->oldTitle
->getArticleID();
180 if ( $this->newTitle
->getDBkey() === '' ) {
181 $status->fatal( 'articleexists' );
184 ( $this->oldTitle
->getDBkey() == '' ) ||
186 ( $this->newTitle
->getDBkey() == '' )
188 $status->fatal( 'badarticleerror' );
191 # The move is allowed only if (1) the target doesn't exist, or
192 # (2) the target is a redirect to the source, and has no history
193 # (so we can undo bad moves right after they're done).
194 if ( $this->newTitle
->getArticleID() && !$this->isValidMoveTarget() ) {
195 $status->fatal( 'articleexists' );
198 // Content model checks
199 if ( !$this->options
->get( 'ContentHandlerUseDB' ) &&
200 $this->oldTitle
->getContentModel() !== $this->newTitle
->getContentModel() ) {
201 // can't move a page if that would change the page's content model
204 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
205 ContentHandler
::getLocalizedName( $this->newTitle
->getContentModel() )
208 !ContentHandler
::getForTitle( $this->oldTitle
)->canBeUsedOn( $this->newTitle
)
211 'content-not-allowed-here',
212 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
213 $this->newTitle
->getPrefixedText(),
218 // Image-specific checks
219 if ( $this->oldTitle
->inNamespace( NS_FILE
) ) {
220 $status->merge( $this->isValidFileMove() );
223 if ( $this->newTitle
->inNamespace( NS_FILE
) && !$this->oldTitle
->inNamespace( NS_FILE
) ) {
224 $status->fatal( 'nonfile-cannot-move-to-file' );
227 // Hook for extensions to say a title can't be moved for technical reasons
228 Hooks
::run( 'MovePageIsValidMove', [ $this->oldTitle
, $this->newTitle
, $status ] );
234 * Sanity checks for when a file is being moved
238 protected function isValidFileMove() {
239 $status = new Status();
240 $file = $this->repoGroup
->getLocalRepo()->newFile( $this->oldTitle
);
241 $file->load( File
::READ_LATEST
);
242 if ( $file->exists() ) {
243 if ( $this->newTitle
->getText() != wfStripIllegalFilenameChars( $this->newTitle
->getText() ) ) {
244 $status->fatal( 'imageinvalidfilename' );
246 if ( !File
::checkExtensionCompatibility( $file, $this->newTitle
->getDBkey() ) ) {
247 $status->fatal( 'imagetypemismatch' );
251 if ( !$this->newTitle
->inNamespace( NS_FILE
) ) {
252 $status->fatal( 'imagenocrossnamespace' );
259 * Checks if $this can be moved to a given Title
260 * - Selects for update, so don't call it unless you mean business
265 protected function isValidMoveTarget() {
266 # Is it an existing file?
267 if ( $this->newTitle
->inNamespace( NS_FILE
) ) {
268 $file = $this->repoGroup
->getLocalRepo()->newFile( $this->newTitle
);
269 $file->load( File
::READ_LATEST
);
270 if ( $file->exists() ) {
271 wfDebug( __METHOD__
. ": file exists\n" );
275 # Is it a redirect with no history?
276 if ( !$this->newTitle
->isSingleRevRedirect() ) {
277 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
280 # Get the article text
281 $rev = Revision
::newFromTitle( $this->newTitle
, false, Revision
::READ_LATEST
);
282 if ( !is_object( $rev ) ) {
285 $content = $rev->getContent();
286 # Does the redirect point to the source?
287 # Or is it a broken self-redirect, usually caused by namespace collisions?
288 $redirTitle = $content ?
$content->getRedirectTarget() : null;
291 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle
->getPrefixedDBkey() &&
292 $redirTitle->getPrefixedDBkey() !== $this->newTitle
->getPrefixedDBkey() ) {
293 wfDebug( __METHOD__
. ": redirect points to other page\n" );
299 # Fail safe (not a redirect after all. strange.)
300 wfDebug( __METHOD__
. ": failsafe: database says " . $this->newTitle
->getPrefixedDBkey() .
301 " is a redirect, but it doesn't contain a valid redirect.\n" );
307 * Move a page without taking user permissions into account. Only checks if the move is itself
308 * invalid, e.g., trying to move a special page or trying to move a page onto one that already
312 * @param string|null $reason
313 * @param bool|null $createRedirect
314 * @param string[] $changeTags Change tags to apply to the entry in the move log
317 public function move(
318 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
320 $status = $this->isValidMove();
321 if ( !$status->isOK() ) {
325 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
329 * Same as move(), but with permissions checks.
332 * @param string|null $reason
333 * @param bool|null $createRedirect Ignored if user doesn't have suppressredirect permission
334 * @param string[] $changeTags Change tags to apply to the entry in the move log
337 public function moveIfAllowed(
338 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
340 $status = $this->isValidMove();
341 $status->merge( $this->checkPermissions( $user, $reason ) );
343 $status->merge( ChangeTags
::canAddTagsAccompanyingChange( $changeTags, $user ) );
346 if ( !$status->isOK() ) {
347 // Auto-block user's IP if the account was "hard" blocked
348 $user->spreadAnyEditBlock();
352 // Check suppressredirect permission
353 if ( !$user->isAllowed( 'suppressredirect' ) ) {
354 $createRedirect = true;
357 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
361 * Move the source page's subpages to be subpages of the target page, without checking user
362 * permissions. The caller is responsible for moving the source page itself. We will still not
363 * do moves that are inherently not allowed, nor will we move more than $wgMaximumMovedPages.
366 * @param string|null $reason The reason for the move
367 * @param bool|null $createRedirect Whether to create redirects from the old subpages to
369 * @param string[] $changeTags Applied to entries in the move log and redirect page revision
370 * @return Status Good if no errors occurred. Ok if at least one page succeeded. The "value"
371 * of the top-level status is an array containing the per-title status for each page. For any
372 * move that succeeded, the "value" of the per-title status is the new page title.
374 public function moveSubpages(
375 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
377 return $this->moveSubpagesInternal( false, $user, $reason, $createRedirect, $changeTags );
381 * Move the source page's subpages to be subpages of the target page, with user permission
382 * checks. The caller is responsible for moving the source page itself.
385 * @param string|null $reason The reason for the move
386 * @param bool|null $createRedirect Whether to create redirects from the old subpages to
387 * the new ones. Ignored if the user doesn't have the 'suppressredirect' right.
388 * @param string[] $changeTags Applied to entries in the move log and redirect page revision
389 * @return Status Good if no errors occurred. Ok if at least one page succeeded. The "value"
390 * of the top-level status is an array containing the per-title status for each page. For any
391 * move that succeeded, the "value" of the per-title status is the new page title.
393 public function moveSubpagesIfAllowed(
394 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
396 return $this->moveSubpagesInternal( true, $user, $reason, $createRedirect, $changeTags );
400 * @param bool $checkPermissions
402 * @param string $reason
403 * @param bool $createRedirect
404 * @param array $changeTags
407 private function moveSubpagesInternal(
408 $checkPermissions, User
$user, $reason, $createRedirect, array $changeTags
410 global $wgMaximumMovedPages;
411 $services = MediaWikiServices
::getInstance();
413 if ( $checkPermissions ) {
414 if ( !$services->getPermissionManager()->userCan(
415 'move-subpages', $user, $this->oldTitle
)
417 return Status
::newFatal( 'cant-move-subpages' );
421 $nsInfo = $services->getNamespaceInfo();
423 // Do the source and target namespaces support subpages?
424 if ( !$nsInfo->hasSubpages( $this->oldTitle
->getNamespace() ) ) {
425 return Status
::newFatal( 'namespace-nosubpages',
426 $nsInfo->getCanonicalName( $this->oldTitle
->getNamespace() ) );
428 if ( !$nsInfo->hasSubpages( $this->newTitle
->getNamespace() ) ) {
429 return Status
::newFatal( 'namespace-nosubpages',
430 $nsInfo->getCanonicalName( $this->newTitle
->getNamespace() ) );
433 // Return a status for the overall result. Its value will be an array with per-title
434 // status for each subpage. Merge any errors from the per-title statuses into the
435 // top-level status without resetting the overall result.
436 $topStatus = Status
::newGood();
437 $perTitleStatus = [];
438 $subpages = $this->oldTitle
->getSubpages( $wgMaximumMovedPages +
1 );
440 foreach ( $subpages as $oldSubpage ) {
442 if ( $count > $wgMaximumMovedPages ) {
443 $status = Status
::newFatal( 'movepage-max-pages', $wgMaximumMovedPages );
444 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
445 $topStatus->merge( $status );
446 $topStatus->setOk( true );
450 // We don't know whether this function was called before or after moving the root page,
451 // so check both titles
452 if ( $oldSubpage->getArticleID() == $this->oldTitle
->getArticleID() ||
453 $oldSubpage->getArticleID() == $this->newTitle
->getArticleID()
455 // When moving a page to a subpage of itself, don't move it twice
458 $newPageName = preg_replace(
459 '#^' . preg_quote( $this->oldTitle
->getDBkey(), '#' ) . '#',
460 StringUtils
::escapeRegexReplacement( $this->newTitle
->getDBkey() ), # T23234
461 $oldSubpage->getDBkey() );
462 if ( $oldSubpage->isTalkPage() ) {
463 $newNs = $this->newTitle
->getTalkPage()->getNamespace();
465 $newNs = $this->newTitle
->getSubjectPage()->getNamespace();
467 // T16385: we need makeTitleSafe because the new page names may be longer than 255
469 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
471 $mp = new MovePage( $oldSubpage, $newSubpage );
472 $method = $checkPermissions ?
'moveIfAllowed' : 'move';
473 $status = $mp->$method( $user, $reason, $createRedirect, $changeTags );
474 if ( $status->isOK() ) {
475 $status->setResult( true, $newSubpage->getPrefixedText() );
477 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
478 $topStatus->merge( $status );
479 $topStatus->setOk( true );
482 $topStatus->value
= $perTitleStatus;
487 * Moves *without* any sort of safety or sanity checks. Hooks can still fail the move, however.
490 * @param string $reason
491 * @param bool $createRedirect
492 * @param string[] $changeTags Change tags to apply to the entry in the move log
495 private function moveUnsafe( User
$user, $reason, $createRedirect, array $changeTags ) {
496 $status = Status
::newGood();
497 Hooks
::run( 'TitleMove', [ $this->oldTitle
, $this->newTitle
, $user, $reason, &$status ] );
498 if ( !$status->isOK() ) {
499 // Move was aborted by the hook
503 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
504 $dbw->startAtomic( __METHOD__
, IDatabase
::ATOMIC_CANCELABLE
);
506 Hooks
::run( 'TitleMoveStarting', [ $this->oldTitle
, $this->newTitle
, $user ] );
508 $pageid = $this->oldTitle
->getArticleID( Title
::GAID_FOR_UPDATE
);
509 $protected = $this->oldTitle
->isProtected();
511 // Do the actual move; if this fails, it will throw an MWException(!)
512 $nullRevision = $this->moveToInternal( $user, $this->newTitle
, $reason, $createRedirect,
515 // Refresh the sortkey for this row. Be careful to avoid resetting
516 // cl_timestamp, which may disturb time-based lists on some sites.
517 // @todo This block should be killed, it's duplicating code
518 // from LinksUpdate::getCategoryInsertions() and friends.
519 $prefixes = $dbw->select(
521 [ 'cl_sortkey_prefix', 'cl_to' ],
522 [ 'cl_from' => $pageid ],
525 $type = $this->nsInfo
->getCategoryLinkType( $this->newTitle
->getNamespace() );
526 foreach ( $prefixes as $prefixRow ) {
527 $prefix = $prefixRow->cl_sortkey_prefix
;
528 $catTo = $prefixRow->cl_to
;
529 $dbw->update( 'categorylinks',
531 'cl_sortkey' => Collation
::singleton()->getSortKey(
532 $this->newTitle
->getCategorySortkey( $prefix ) ),
533 'cl_collation' => $this->options
->get( 'CategoryCollation' ),
535 'cl_timestamp=cl_timestamp' ],
537 'cl_from' => $pageid,
543 $redirid = $this->oldTitle
->getArticleID();
546 # Protect the redirect title as the title used to be...
549 [ 'pr_type', 'pr_level', 'pr_cascade', 'pr_user', 'pr_expiry' ],
550 [ 'pr_page' => $pageid ],
555 foreach ( $res as $row ) {
557 'pr_page' => $redirid,
558 'pr_type' => $row->pr_type
,
559 'pr_level' => $row->pr_level
,
560 'pr_cascade' => $row->pr_cascade
,
561 'pr_user' => $row->pr_user
,
562 'pr_expiry' => $row->pr_expiry
565 $dbw->insert( 'page_restrictions', $rowsInsert, __METHOD__
, [ 'IGNORE' ] );
567 // Build comment for log
568 $comment = wfMessage(
570 $this->oldTitle
->getPrefixedText(),
571 $this->newTitle
->getPrefixedText()
572 )->inContentLanguage()->text();
574 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
577 // reread inserted pr_ids for log relation
578 $insertedPrIds = $dbw->select(
581 [ 'pr_page' => $redirid ],
584 $logRelationsValues = [];
585 foreach ( $insertedPrIds as $prid ) {
586 $logRelationsValues[] = $prid->pr_id
;
589 // Update the protection log
590 $logEntry = new ManualLogEntry( 'protect', 'move_prot' );
591 $logEntry->setTarget( $this->newTitle
);
592 $logEntry->setComment( $comment );
593 $logEntry->setPerformer( $user );
594 $logEntry->setParameters( [
595 '4::oldtitle' => $this->oldTitle
->getPrefixedText(),
597 $logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] );
598 $logEntry->setTags( $changeTags );
599 $logId = $logEntry->insert();
600 $logEntry->publish( $logId );
603 // Update *_from_namespace fields as needed
604 if ( $this->oldTitle
->getNamespace() != $this->newTitle
->getNamespace() ) {
605 $dbw->update( 'pagelinks',
606 [ 'pl_from_namespace' => $this->newTitle
->getNamespace() ],
607 [ 'pl_from' => $pageid ],
610 $dbw->update( 'templatelinks',
611 [ 'tl_from_namespace' => $this->newTitle
->getNamespace() ],
612 [ 'tl_from' => $pageid ],
615 $dbw->update( 'imagelinks',
616 [ 'il_from_namespace' => $this->newTitle
->getNamespace() ],
617 [ 'il_from' => $pageid ],
623 $oldtitle = $this->oldTitle
->getDBkey();
624 $newtitle = $this->newTitle
->getDBkey();
625 $oldsnamespace = $this->nsInfo
->getSubject( $this->oldTitle
->getNamespace() );
626 $newsnamespace = $this->nsInfo
->getSubject( $this->newTitle
->getNamespace() );
627 if ( $oldsnamespace != $newsnamespace ||
$oldtitle != $newtitle ) {
628 $this->watchedItems
->duplicateAllAssociatedEntries( $this->oldTitle
, $this->newTitle
);
631 // If it is a file then move it last.
632 // This is done after all database changes so that file system errors cancel the transaction.
633 if ( $this->oldTitle
->getNamespace() == NS_FILE
) {
634 $status = $this->moveFile( $this->oldTitle
, $this->newTitle
);
635 if ( !$status->isOK() ) {
636 $dbw->cancelAtomic( __METHOD__
);
642 'TitleMoveCompleting',
643 [ $this->oldTitle
, $this->newTitle
,
644 $user, $pageid, $redirid, $reason, $nullRevision ]
647 $dbw->endAtomic( __METHOD__
);
658 // Keep each single hook handler atomic
659 DeferredUpdates
::addUpdate(
660 new AtomicSectionUpdate(
663 // Hold onto $user to avoid HHVM bug where it no longer
664 // becomes a reference (T118683)
665 function () use ( $params, &$user ) {
666 Hooks
::run( 'TitleMoveComplete', $params );
671 return Status
::newGood();
675 * Move a file associated with a page to a new location.
676 * Can also be used to revert after a DB failure.
679 * @param Title $oldTitle Old location to move the file from.
680 * @param Title $newTitle New location to move the file to.
683 private function moveFile( $oldTitle, $newTitle ) {
684 $status = Status
::newFatal(
686 $oldTitle->getPrefixedText()
689 $file = $this->repoGroup
->getLocalRepo()->newFile( $oldTitle );
690 $file->load( File
::READ_LATEST
);
691 if ( $file->exists() ) {
692 $status = $file->move( $newTitle );
695 // Clear RepoGroup process cache
696 $this->repoGroup
->clearCache( $oldTitle );
697 $this->repoGroup
->clearCache( $newTitle ); # clear false negative cache
702 * Move page to a title which is either a redirect to the
703 * source page or nonexistent
705 * @todo This was basically directly moved from Title, it should be split into
707 * @param User $user the User doing the move
708 * @param Title $nt The page to move to, which should be a redirect or non-existent
709 * @param string $reason The reason for the move
710 * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
711 * if the user has the suppressredirect right
712 * @param string[] $changeTags Change tags to apply to the entry in the move log
713 * @return Revision the revision created by the move
714 * @throws MWException
716 private function moveToInternal( User
$user, &$nt, $reason = '', $createRedirect = true,
717 array $changeTags = []
719 if ( $nt->exists() ) {
720 $moveOverRedirect = true;
721 $logType = 'move_redir';
723 $moveOverRedirect = false;
727 if ( $moveOverRedirect ) {
728 $overwriteMessage = wfMessage(
729 'delete_and_move_reason',
730 $this->oldTitle
->getPrefixedText()
731 )->inContentLanguage()->text();
732 $newpage = WikiPage
::factory( $nt );
734 $status = $newpage->doDeleteArticleReal(
736 /* $suppress */ false,
745 if ( !$status->isGood() ) {
746 throw new MWException( 'Failed to delete page-move revision: '
747 . $status->getWikiText( false, false, 'en' ) );
750 $nt->resetArticleID( false );
753 if ( $createRedirect ) {
754 if ( $this->oldTitle
->getNamespace() == NS_CATEGORY
755 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
757 $redirectContent = new WikitextContent(
758 wfMessage( 'category-move-redirect-override' )
759 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
761 $contentHandler = ContentHandler
::getForTitle( $this->oldTitle
);
762 $redirectContent = $contentHandler->makeRedirectContent( $nt,
763 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
766 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
768 $redirectContent = null;
771 // Figure out whether the content model is no longer the default
772 $oldDefault = ContentHandler
::getDefaultModelFor( $this->oldTitle
);
773 $contentModel = $this->oldTitle
->getContentModel();
774 $newDefault = ContentHandler
::getDefaultModelFor( $nt );
775 $defaultContentModelChanging = ( $oldDefault !== $newDefault
776 && $oldDefault === $contentModel );
778 // T59084: log_page should be the ID of the *moved* page
779 $oldid = $this->oldTitle
->getArticleID();
780 $logTitle = clone $this->oldTitle
;
782 $logEntry = new ManualLogEntry( 'move', $logType );
783 $logEntry->setPerformer( $user );
784 $logEntry->setTarget( $logTitle );
785 $logEntry->setComment( $reason );
786 $logEntry->setParameters( [
787 '4::target' => $nt->getPrefixedText(),
788 '5::noredir' => $redirectContent ?
'0' : '1',
791 $formatter = LogFormatter
::newFromEntry( $logEntry );
792 $formatter->setContext( RequestContext
::newExtraneousContext( $this->oldTitle
) );
793 $comment = $formatter->getPlainActionText();
795 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
798 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
800 $oldpage = WikiPage
::factory( $this->oldTitle
);
801 $oldcountable = $oldpage->isCountable();
803 $newpage = WikiPage
::factory( $nt );
805 # Change the name of the target page:
806 $dbw->update( 'page',
808 'page_namespace' => $nt->getNamespace(),
809 'page_title' => $nt->getDBkey(),
811 /* WHERE */ [ 'page_id' => $oldid ],
815 # Save a null revision in the page's history notifying of the move
816 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true, $user );
817 if ( !is_object( $nullRevision ) ) {
818 throw new MWException( 'Failed to create null revision while moving page ID '
819 . $oldid . ' to ' . $nt->getPrefixedDBkey() );
822 $nullRevId = $nullRevision->insertOn( $dbw );
823 $logEntry->setAssociatedRevId( $nullRevId );
827 * Increment user_editcount during page moves
828 * Moved from SpecialMovepage.php per T195550
830 $user->incEditCount();
832 if ( !$redirectContent ) {
833 // Clean up the old title *before* reset article id - T47348
834 WikiPage
::onArticleDelete( $this->oldTitle
);
837 $this->oldTitle
->resetArticleID( 0 ); // 0 == non existing
838 $nt->resetArticleID( $oldid );
839 $newpage->loadPageData( WikiPage
::READ_LOCKING
); // T48397
841 $newpage->updateRevisionOn( $dbw, $nullRevision );
843 Hooks
::run( 'NewRevisionFromEditComplete',
844 [ $newpage, $nullRevision, $nullRevision->getParentId(), $user ] );
846 $newpage->doEditUpdates( $nullRevision, $user,
847 [ 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ] );
849 // If the default content model changes, we need to populate rev_content_model
850 if ( $defaultContentModelChanging ) {
853 [ 'rev_content_model' => $contentModel ],
854 [ 'rev_page' => $nt->getArticleID(), 'rev_content_model IS NULL' ],
859 WikiPage
::onArticleCreate( $nt );
861 # Recreate the redirect, this time in the other direction.
862 if ( $redirectContent ) {
863 $redirectArticle = WikiPage
::factory( $this->oldTitle
);
864 $redirectArticle->loadFromRow( false, WikiPage
::READ_LOCKING
); // T48397
865 $newid = $redirectArticle->insertOn( $dbw );
866 if ( $newid ) { // sanity
867 $this->oldTitle
->resetArticleID( $newid );
868 $redirectRevision = new Revision( [
869 'title' => $this->oldTitle
, // for determining the default content model
871 'user_text' => $user->getName(),
872 'user' => $user->getId(),
873 'comment' => $comment,
874 'content' => $redirectContent ] );
875 $redirectRevId = $redirectRevision->insertOn( $dbw );
876 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
878 Hooks
::run( 'NewRevisionFromEditComplete',
879 [ $redirectArticle, $redirectRevision, false, $user ] );
881 $redirectArticle->doEditUpdates( $redirectRevision, $user, [ 'created' => true ] );
883 // make a copy because of log entry below
884 $redirectTags = $changeTags;
885 if ( in_array( 'mw-new-redirect', ChangeTags
::getSoftwareTags() ) ) {
886 $redirectTags[] = 'mw-new-redirect';
888 ChangeTags
::addTags( $redirectTags, null, $redirectRevId, null );
893 $logid = $logEntry->insert();
895 $logEntry->setTags( $changeTags );
896 $logEntry->publish( $logid );
898 return $nullRevision;