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
74 * Calling this directly is deprecated in 1.34. Use MovePageFactory instead.
76 * @param Title $oldTitle
77 * @param Title $newTitle
78 * @param ServiceOptions|null $options
79 * @param LoadBalancer|null $loadBalancer
80 * @param NamespaceInfo|null $nsInfo
81 * @param WatchedItemStore|null $watchedItems
82 * @param PermissionManager|null $permMgr
84 public function __construct(
87 ServiceOptions
$options = null,
88 LoadBalancer
$loadBalancer = null,
89 NamespaceInfo
$nsInfo = null,
90 WatchedItemStore
$watchedItems = null,
91 PermissionManager
$permMgr = null
93 $this->oldTitle
= $oldTitle;
94 $this->newTitle
= $newTitle;
95 $this->options
= $options ??
96 new ServiceOptions( MovePageFactory
::$constructorOptions,
97 MediaWikiServices
::getInstance()->getMainConfig() );
99 $loadBalancer ?? MediaWikiServices
::getInstance()->getDBLoadBalancer();
100 $this->nsInfo
= $nsInfo ?? MediaWikiServices
::getInstance()->getNamespaceInfo();
101 $this->watchedItems
=
102 $watchedItems ?? MediaWikiServices
::getInstance()->getWatchedItemStore();
103 $this->permMgr
= $permMgr ?? MediaWikiServices
::getInstance()->getPermissionManager();
107 * Check if the user is allowed to perform the move.
110 * @param string|null $reason To check against summary spam regex. Set to null to skip the check,
111 * for instance to display errors preemptively before the user has filled in a summary.
114 public function checkPermissions( User
$user, $reason ) {
115 $status = new Status();
117 $errors = wfMergeErrorArrays(
118 $this->permMgr
->getPermissionErrors( 'move', $user, $this->oldTitle
),
119 $this->permMgr
->getPermissionErrors( 'edit', $user, $this->oldTitle
),
120 $this->permMgr
->getPermissionErrors( 'move-target', $user, $this->newTitle
),
121 $this->permMgr
->getPermissionErrors( 'edit', $user, $this->newTitle
)
124 // Convert into a Status object
126 foreach ( $errors as $error ) {
127 $status->fatal( ...$error );
131 if ( $reason !== null && EditPage
::matchSummarySpamRegex( $reason ) !== false ) {
132 // This is kind of lame, won't display nice
133 $status->fatal( 'spamprotectiontext' );
136 $tp = $this->newTitle
->getTitleProtection();
137 if ( $tp !== false && !$user->isAllowed( $tp['permission'] ) ) {
138 $status->fatal( 'cantmove-titleprotected' );
141 Hooks
::run( 'MovePageCheckPermissions',
142 [ $this->oldTitle
, $this->newTitle
, $user, $reason, $status ]
149 * Does various sanity checks that the move is
150 * valid. Only things based on the two titles
151 * should be checked here.
155 public function isValidMove() {
156 $status = new Status();
158 if ( $this->oldTitle
->equals( $this->newTitle
) ) {
159 $status->fatal( 'selfmove' );
161 if ( !$this->oldTitle
->isMovable() ) {
162 $status->fatal( 'immobile-source-namespace', $this->oldTitle
->getNsText() );
164 if ( $this->newTitle
->isExternal() ) {
165 $status->fatal( 'immobile-target-namespace-iw' );
167 if ( !$this->newTitle
->isMovable() ) {
168 $status->fatal( 'immobile-target-namespace', $this->newTitle
->getNsText() );
171 $oldid = $this->oldTitle
->getArticleID();
173 if ( $this->newTitle
->getDBkey() === '' ) {
174 $status->fatal( 'articleexists' );
177 ( $this->oldTitle
->getDBkey() == '' ) ||
179 ( $this->newTitle
->getDBkey() == '' )
181 $status->fatal( 'badarticleerror' );
184 # The move is allowed only if (1) the target doesn't exist, or
185 # (2) the target is a redirect to the source, and has no history
186 # (so we can undo bad moves right after they're done).
187 if ( $this->newTitle
->getArticleID() && !$this->isValidMoveTarget() ) {
188 $status->fatal( 'articleexists' );
191 // Content model checks
192 if ( !$this->options
->get( 'ContentHandlerUseDB' ) &&
193 $this->oldTitle
->getContentModel() !== $this->newTitle
->getContentModel() ) {
194 // can't move a page if that would change the page's content model
197 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
198 ContentHandler
::getLocalizedName( $this->newTitle
->getContentModel() )
201 !ContentHandler
::getForTitle( $this->oldTitle
)->canBeUsedOn( $this->newTitle
)
204 'content-not-allowed-here',
205 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
206 $this->newTitle
->getPrefixedText(),
211 // Image-specific checks
212 if ( $this->oldTitle
->inNamespace( NS_FILE
) ) {
213 $status->merge( $this->isValidFileMove() );
216 if ( $this->newTitle
->inNamespace( NS_FILE
) && !$this->oldTitle
->inNamespace( NS_FILE
) ) {
217 $status->fatal( 'nonfile-cannot-move-to-file' );
220 // Hook for extensions to say a title can't be moved for technical reasons
221 Hooks
::run( 'MovePageIsValidMove', [ $this->oldTitle
, $this->newTitle
, $status ] );
227 * Sanity checks for when a file is being moved
231 protected function isValidFileMove() {
232 $status = new Status();
233 $file = wfLocalFile( $this->oldTitle
);
234 $file->load( File
::READ_LATEST
);
235 if ( $file->exists() ) {
236 if ( $this->newTitle
->getText() != wfStripIllegalFilenameChars( $this->newTitle
->getText() ) ) {
237 $status->fatal( 'imageinvalidfilename' );
239 if ( !File
::checkExtensionCompatibility( $file, $this->newTitle
->getDBkey() ) ) {
240 $status->fatal( 'imagetypemismatch' );
244 if ( !$this->newTitle
->inNamespace( NS_FILE
) ) {
245 $status->fatal( 'imagenocrossnamespace' );
252 * Checks if $this can be moved to a given Title
253 * - Selects for update, so don't call it unless you mean business
258 protected function isValidMoveTarget() {
259 # Is it an existing file?
260 if ( $this->newTitle
->inNamespace( NS_FILE
) ) {
261 $file = wfLocalFile( $this->newTitle
);
262 $file->load( File
::READ_LATEST
);
263 if ( $file->exists() ) {
264 wfDebug( __METHOD__
. ": file exists\n" );
268 # Is it a redirect with no history?
269 if ( !$this->newTitle
->isSingleRevRedirect() ) {
270 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
273 # Get the article text
274 $rev = Revision
::newFromTitle( $this->newTitle
, false, Revision
::READ_LATEST
);
275 if ( !is_object( $rev ) ) {
278 $content = $rev->getContent();
279 # Does the redirect point to the source?
280 # Or is it a broken self-redirect, usually caused by namespace collisions?
281 $redirTitle = $content ?
$content->getRedirectTarget() : null;
284 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle
->getPrefixedDBkey() &&
285 $redirTitle->getPrefixedDBkey() !== $this->newTitle
->getPrefixedDBkey() ) {
286 wfDebug( __METHOD__
. ": redirect points to other page\n" );
292 # Fail safe (not a redirect after all. strange.)
293 wfDebug( __METHOD__
. ": failsafe: database says " . $this->newTitle
->getPrefixedDBkey() .
294 " is a redirect, but it doesn't contain a valid redirect.\n" );
300 * Move a page without taking user permissions into account. Only checks if the move is itself
301 * invalid, e.g., trying to move a special page or trying to move a page onto one that already
305 * @param string|null $reason
306 * @param bool|null $createRedirect
307 * @param string[] $changeTags Change tags to apply to the entry in the move log
310 public function move(
311 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
313 $status = $this->isValidMove();
314 if ( !$status->isOK() ) {
318 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
322 * Same as move(), but with permissions checks.
325 * @param string|null $reason
326 * @param bool|null $createRedirect Ignored if user doesn't have suppressredirect permission
327 * @param string[] $changeTags Change tags to apply to the entry in the move log
330 public function moveIfAllowed(
331 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
333 $status = $this->isValidMove();
334 $status->merge( $this->checkPermissions( $user, $reason ) );
336 $status->merge( ChangeTags
::canAddTagsAccompanyingChange( $changeTags, $user ) );
339 if ( !$status->isOK() ) {
340 // Auto-block user's IP if the account was "hard" blocked
341 $user->spreadAnyEditBlock();
345 // Check suppressredirect permission
346 if ( !$user->isAllowed( 'suppressredirect' ) ) {
347 $createRedirect = true;
350 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
354 * Move the source page's subpages to be subpages of the target page, without checking user
355 * permissions. The caller is responsible for moving the source page itself. We will still not
356 * do moves that are inherently not allowed, nor will we move more than $wgMaximumMovedPages.
359 * @param string|null $reason The reason for the move
360 * @param bool|null $createRedirect Whether to create redirects from the old subpages to
362 * @param string[] $changeTags Applied to entries in the move log and redirect page revision
363 * @return Status Good if no errors occurred. Ok if at least one page succeeded. The "value"
364 * of the top-level status is an array containing the per-title status for each page. For any
365 * move that succeeded, the "value" of the per-title status is the new page title.
367 public function moveSubpages(
368 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
370 return $this->moveSubpagesInternal( false, $user, $reason, $createRedirect, $changeTags );
374 * Move the source page's subpages to be subpages of the target page, with user permission
375 * checks. The caller is responsible for moving the source page itself.
378 * @param string|null $reason The reason for the move
379 * @param bool|null $createRedirect Whether to create redirects from the old subpages to
380 * the new ones. Ignored if the user doesn't have the 'suppressredirect' right.
381 * @param string[] $changeTags Applied to entries in the move log and redirect page revision
382 * @return Status Good if no errors occurred. Ok if at least one page succeeded. The "value"
383 * of the top-level status is an array containing the per-title status for each page. For any
384 * move that succeeded, the "value" of the per-title status is the new page title.
386 public function moveSubpagesIfAllowed(
387 User
$user, $reason = null, $createRedirect = true, array $changeTags = []
389 return $this->moveSubpagesInternal( true, $user, $reason, $createRedirect, $changeTags );
393 * @param bool $checkPermissions
395 * @param string $reason
396 * @param bool $createRedirect
397 * @param array $changeTags
400 private function moveSubpagesInternal(
401 $checkPermissions, User
$user, $reason, $createRedirect, array $changeTags
403 global $wgMaximumMovedPages;
404 $services = MediaWikiServices
::getInstance();
406 if ( $checkPermissions ) {
407 if ( !$services->getPermissionManager()->userCan(
408 'move-subpages', $user, $this->oldTitle
)
410 return Status
::newFatal( 'cant-move-subpages' );
414 $nsInfo = $services->getNamespaceInfo();
416 // Do the source and target namespaces support subpages?
417 if ( !$nsInfo->hasSubpages( $this->oldTitle
->getNamespace() ) ) {
418 return Status
::newFatal( 'namespace-nosubpages',
419 $nsInfo->getCanonicalName( $this->oldTitle
->getNamespace() ) );
421 if ( !$nsInfo->hasSubpages( $this->newTitle
->getNamespace() ) ) {
422 return Status
::newFatal( 'namespace-nosubpages',
423 $nsInfo->getCanonicalName( $this->newTitle
->getNamespace() ) );
426 // Return a status for the overall result. Its value will be an array with per-title
427 // status for each subpage. Merge any errors from the per-title statuses into the
428 // top-level status without resetting the overall result.
429 $topStatus = Status
::newGood();
430 $perTitleStatus = [];
431 $subpages = $this->oldTitle
->getSubpages( $wgMaximumMovedPages +
1 );
433 foreach ( $subpages as $oldSubpage ) {
435 if ( $count > $wgMaximumMovedPages ) {
436 $status = Status
::newFatal( 'movepage-max-pages', $wgMaximumMovedPages );
437 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
438 $topStatus->merge( $status );
439 $topStatus->setOk( true );
443 // We don't know whether this function was called before or after moving the root page,
444 // so check both titles
445 if ( $oldSubpage->getArticleID() == $this->oldTitle
->getArticleID() ||
446 $oldSubpage->getArticleID() == $this->newTitle
->getArticleID()
448 // When moving a page to a subpage of itself, don't move it twice
451 $newPageName = preg_replace(
452 '#^' . preg_quote( $this->oldTitle
->getDBkey(), '#' ) . '#',
453 StringUtils
::escapeRegexReplacement( $this->newTitle
->getDBkey() ), # T23234
454 $oldSubpage->getDBkey() );
455 if ( $oldSubpage->isTalkPage() ) {
456 $newNs = $this->newTitle
->getTalkPage()->getNamespace();
458 $newNs = $this->newTitle
->getSubjectPage()->getNamespace();
460 // T16385: we need makeTitleSafe because the new page names may be longer than 255
462 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
464 $mp = new MovePage( $oldSubpage, $newSubpage );
465 $method = $checkPermissions ?
'moveIfAllowed' : 'move';
466 $status = $mp->$method( $user, $reason, $createRedirect, $changeTags );
467 if ( $status->isOK() ) {
468 $status->setResult( true, $newSubpage->getPrefixedText() );
470 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
471 $topStatus->merge( $status );
472 $topStatus->setOk( true );
475 $topStatus->value
= $perTitleStatus;
480 * Moves *without* any sort of safety or sanity checks. Hooks can still fail the move, however.
483 * @param string $reason
484 * @param bool $createRedirect
485 * @param string[] $changeTags Change tags to apply to the entry in the move log
488 private function moveUnsafe( User
$user, $reason, $createRedirect, array $changeTags ) {
489 $status = Status
::newGood();
490 Hooks
::run( 'TitleMove', [ $this->oldTitle
, $this->newTitle
, $user, $reason, &$status ] );
491 if ( !$status->isOK() ) {
492 // Move was aborted by the hook
496 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
497 $dbw->startAtomic( __METHOD__
, IDatabase
::ATOMIC_CANCELABLE
);
499 Hooks
::run( 'TitleMoveStarting', [ $this->oldTitle
, $this->newTitle
, $user ] );
501 $pageid = $this->oldTitle
->getArticleID( Title
::GAID_FOR_UPDATE
);
502 $protected = $this->oldTitle
->isProtected();
504 // Do the actual move; if this fails, it will throw an MWException(!)
505 $nullRevision = $this->moveToInternal( $user, $this->newTitle
, $reason, $createRedirect,
508 // Refresh the sortkey for this row. Be careful to avoid resetting
509 // cl_timestamp, which may disturb time-based lists on some sites.
510 // @todo This block should be killed, it's duplicating code
511 // from LinksUpdate::getCategoryInsertions() and friends.
512 $prefixes = $dbw->select(
514 [ 'cl_sortkey_prefix', 'cl_to' ],
515 [ 'cl_from' => $pageid ],
518 $type = $this->nsInfo
->getCategoryLinkType( $this->newTitle
->getNamespace() );
519 foreach ( $prefixes as $prefixRow ) {
520 $prefix = $prefixRow->cl_sortkey_prefix
;
521 $catTo = $prefixRow->cl_to
;
522 $dbw->update( 'categorylinks',
524 'cl_sortkey' => Collation
::singleton()->getSortKey(
525 $this->newTitle
->getCategorySortkey( $prefix ) ),
526 'cl_collation' => $this->options
->get( 'CategoryCollation' ),
528 'cl_timestamp=cl_timestamp' ],
530 'cl_from' => $pageid,
536 $redirid = $this->oldTitle
->getArticleID();
539 # Protect the redirect title as the title used to be...
542 [ 'pr_type', 'pr_level', 'pr_cascade', 'pr_user', 'pr_expiry' ],
543 [ 'pr_page' => $pageid ],
548 foreach ( $res as $row ) {
550 'pr_page' => $redirid,
551 'pr_type' => $row->pr_type
,
552 'pr_level' => $row->pr_level
,
553 'pr_cascade' => $row->pr_cascade
,
554 'pr_user' => $row->pr_user
,
555 'pr_expiry' => $row->pr_expiry
558 $dbw->insert( 'page_restrictions', $rowsInsert, __METHOD__
, [ 'IGNORE' ] );
560 // Build comment for log
561 $comment = wfMessage(
563 $this->oldTitle
->getPrefixedText(),
564 $this->newTitle
->getPrefixedText()
565 )->inContentLanguage()->text();
567 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
570 // reread inserted pr_ids for log relation
571 $insertedPrIds = $dbw->select(
574 [ 'pr_page' => $redirid ],
577 $logRelationsValues = [];
578 foreach ( $insertedPrIds as $prid ) {
579 $logRelationsValues[] = $prid->pr_id
;
582 // Update the protection log
583 $logEntry = new ManualLogEntry( 'protect', 'move_prot' );
584 $logEntry->setTarget( $this->newTitle
);
585 $logEntry->setComment( $comment );
586 $logEntry->setPerformer( $user );
587 $logEntry->setParameters( [
588 '4::oldtitle' => $this->oldTitle
->getPrefixedText(),
590 $logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] );
591 $logEntry->setTags( $changeTags );
592 $logId = $logEntry->insert();
593 $logEntry->publish( $logId );
596 // Update *_from_namespace fields as needed
597 if ( $this->oldTitle
->getNamespace() != $this->newTitle
->getNamespace() ) {
598 $dbw->update( 'pagelinks',
599 [ 'pl_from_namespace' => $this->newTitle
->getNamespace() ],
600 [ 'pl_from' => $pageid ],
603 $dbw->update( 'templatelinks',
604 [ 'tl_from_namespace' => $this->newTitle
->getNamespace() ],
605 [ 'tl_from' => $pageid ],
608 $dbw->update( 'imagelinks',
609 [ 'il_from_namespace' => $this->newTitle
->getNamespace() ],
610 [ 'il_from' => $pageid ],
616 $oldtitle = $this->oldTitle
->getDBkey();
617 $newtitle = $this->newTitle
->getDBkey();
618 $oldsnamespace = $this->nsInfo
->getSubject( $this->oldTitle
->getNamespace() );
619 $newsnamespace = $this->nsInfo
->getSubject( $this->newTitle
->getNamespace() );
620 if ( $oldsnamespace != $newsnamespace ||
$oldtitle != $newtitle ) {
621 $this->watchedItems
->duplicateAllAssociatedEntries( $this->oldTitle
, $this->newTitle
);
624 // If it is a file then move it last.
625 // This is done after all database changes so that file system errors cancel the transaction.
626 if ( $this->oldTitle
->getNamespace() == NS_FILE
) {
627 $status = $this->moveFile( $this->oldTitle
, $this->newTitle
);
628 if ( !$status->isOK() ) {
629 $dbw->cancelAtomic( __METHOD__
);
635 'TitleMoveCompleting',
636 [ $this->oldTitle
, $this->newTitle
,
637 $user, $pageid, $redirid, $reason, $nullRevision ]
640 $dbw->endAtomic( __METHOD__
);
651 // Keep each single hook handler atomic
652 DeferredUpdates
::addUpdate(
653 new AtomicSectionUpdate(
656 // Hold onto $user to avoid HHVM bug where it no longer
657 // becomes a reference (T118683)
658 function () use ( $params, &$user ) {
659 Hooks
::run( 'TitleMoveComplete', $params );
664 return Status
::newGood();
668 * Move a file associated with a page to a new location.
669 * Can also be used to revert after a DB failure.
672 * @param Title $oldTitle Old location to move the file from.
673 * @param Title $newTitle New location to move the file to.
676 private function moveFile( $oldTitle, $newTitle ) {
677 $status = Status
::newFatal(
679 $oldTitle->getPrefixedText()
682 $file = wfLocalFile( $oldTitle );
683 $file->load( File
::READ_LATEST
);
684 if ( $file->exists() ) {
685 $status = $file->move( $newTitle );
688 // Clear RepoGroup process cache
689 RepoGroup
::singleton()->clearCache( $oldTitle );
690 RepoGroup
::singleton()->clearCache( $newTitle ); # clear false negative cache
695 * Move page to a title which is either a redirect to the
696 * source page or nonexistent
698 * @todo This was basically directly moved from Title, it should be split into
700 * @param User $user the User doing the move
701 * @param Title $nt The page to move to, which should be a redirect or non-existent
702 * @param string $reason The reason for the move
703 * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
704 * if the user has the suppressredirect right
705 * @param string[] $changeTags Change tags to apply to the entry in the move log
706 * @return Revision the revision created by the move
707 * @throws MWException
709 private function moveToInternal( User
$user, &$nt, $reason = '', $createRedirect = true,
710 array $changeTags = []
712 if ( $nt->exists() ) {
713 $moveOverRedirect = true;
714 $logType = 'move_redir';
716 $moveOverRedirect = false;
720 if ( $moveOverRedirect ) {
721 $overwriteMessage = wfMessage(
722 'delete_and_move_reason',
723 $this->oldTitle
->getPrefixedText()
724 )->inContentLanguage()->text();
725 $newpage = WikiPage
::factory( $nt );
727 $status = $newpage->doDeleteArticleReal(
729 /* $suppress */ false,
738 if ( !$status->isGood() ) {
739 throw new MWException( 'Failed to delete page-move revision: '
740 . $status->getWikiText( false, false, 'en' ) );
743 $nt->resetArticleID( false );
746 if ( $createRedirect ) {
747 if ( $this->oldTitle
->getNamespace() == NS_CATEGORY
748 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
750 $redirectContent = new WikitextContent(
751 wfMessage( 'category-move-redirect-override' )
752 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
754 $contentHandler = ContentHandler
::getForTitle( $this->oldTitle
);
755 $redirectContent = $contentHandler->makeRedirectContent( $nt,
756 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
759 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
761 $redirectContent = null;
764 // Figure out whether the content model is no longer the default
765 $oldDefault = ContentHandler
::getDefaultModelFor( $this->oldTitle
);
766 $contentModel = $this->oldTitle
->getContentModel();
767 $newDefault = ContentHandler
::getDefaultModelFor( $nt );
768 $defaultContentModelChanging = ( $oldDefault !== $newDefault
769 && $oldDefault === $contentModel );
771 // T59084: log_page should be the ID of the *moved* page
772 $oldid = $this->oldTitle
->getArticleID();
773 $logTitle = clone $this->oldTitle
;
775 $logEntry = new ManualLogEntry( 'move', $logType );
776 $logEntry->setPerformer( $user );
777 $logEntry->setTarget( $logTitle );
778 $logEntry->setComment( $reason );
779 $logEntry->setParameters( [
780 '4::target' => $nt->getPrefixedText(),
781 '5::noredir' => $redirectContent ?
'0' : '1',
784 $formatter = LogFormatter
::newFromEntry( $logEntry );
785 $formatter->setContext( RequestContext
::newExtraneousContext( $this->oldTitle
) );
786 $comment = $formatter->getPlainActionText();
788 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
791 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
793 $oldpage = WikiPage
::factory( $this->oldTitle
);
794 $oldcountable = $oldpage->isCountable();
796 $newpage = WikiPage
::factory( $nt );
798 # Change the name of the target page:
799 $dbw->update( 'page',
801 'page_namespace' => $nt->getNamespace(),
802 'page_title' => $nt->getDBkey(),
804 /* WHERE */ [ 'page_id' => $oldid ],
808 # Save a null revision in the page's history notifying of the move
809 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true, $user );
810 if ( !is_object( $nullRevision ) ) {
811 throw new MWException( 'Failed to create null revision while moving page ID '
812 . $oldid . ' to ' . $nt->getPrefixedDBkey() );
815 $nullRevId = $nullRevision->insertOn( $dbw );
816 $logEntry->setAssociatedRevId( $nullRevId );
820 * Increment user_editcount during page moves
821 * Moved from SpecialMovepage.php per T195550
823 $user->incEditCount();
825 if ( !$redirectContent ) {
826 // Clean up the old title *before* reset article id - T47348
827 WikiPage
::onArticleDelete( $this->oldTitle
);
830 $this->oldTitle
->resetArticleID( 0 ); // 0 == non existing
831 $nt->resetArticleID( $oldid );
832 $newpage->loadPageData( WikiPage
::READ_LOCKING
); // T48397
834 $newpage->updateRevisionOn( $dbw, $nullRevision );
836 Hooks
::run( 'NewRevisionFromEditComplete',
837 [ $newpage, $nullRevision, $nullRevision->getParentId(), $user ] );
839 $newpage->doEditUpdates( $nullRevision, $user,
840 [ 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ] );
842 // If the default content model changes, we need to populate rev_content_model
843 if ( $defaultContentModelChanging ) {
846 [ 'rev_content_model' => $contentModel ],
847 [ 'rev_page' => $nt->getArticleID(), 'rev_content_model IS NULL' ],
852 WikiPage
::onArticleCreate( $nt );
854 # Recreate the redirect, this time in the other direction.
855 if ( $redirectContent ) {
856 $redirectArticle = WikiPage
::factory( $this->oldTitle
);
857 $redirectArticle->loadFromRow( false, WikiPage
::READ_LOCKING
); // T48397
858 $newid = $redirectArticle->insertOn( $dbw );
859 if ( $newid ) { // sanity
860 $this->oldTitle
->resetArticleID( $newid );
861 $redirectRevision = new Revision( [
862 'title' => $this->oldTitle
, // for determining the default content model
864 'user_text' => $user->getName(),
865 'user' => $user->getId(),
866 'comment' => $comment,
867 'content' => $redirectContent ] );
868 $redirectRevId = $redirectRevision->insertOn( $dbw );
869 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
871 Hooks
::run( 'NewRevisionFromEditComplete',
872 [ $redirectArticle, $redirectRevision, false, $user ] );
874 $redirectArticle->doEditUpdates( $redirectRevision, $user, [ 'created' => true ] );
876 // make a copy because of log entry below
877 $redirectTags = $changeTags;
878 if ( in_array( 'mw-new-redirect', ChangeTags
::getSoftwareTags() ) ) {
879 $redirectTags[] = 'mw-new-redirect';
881 ChangeTags
::addTags( $redirectTags, null, $redirectRevId, null );
886 $logid = $logEntry->insert();
888 $logEntry->setTags( $changeTags );
889 $logEntry->publish( $logid );
891 return $nullRevision;