MediaWiki fundraising/REL1_35
MovePage.php
Go to the documentation of this file.
1<?php
2
35
42class MovePage {
43
47 protected $oldTitle;
48
52 protected $newTitle;
53
57 protected $options;
58
62 protected $loadBalancer;
63
67 protected $nsInfo;
68
72 protected $watchedItems;
73
77 protected $permMgr;
78
82 protected $repoGroup;
83
88
93
97 private $spamChecker;
98
103
107 private $hookRunner;
108
109 public const CONSTRUCTOR_OPTIONS = [
110 'CategoryCollation'
111 ];
112
129 public function __construct(
130 Title $oldTitle,
131 Title $newTitle,
132 ServiceOptions $options = null,
133 ILoadBalancer $loadBalancer = null,
134 NamespaceInfo $nsInfo = null,
135 WatchedItemStoreInterface $watchedItems = null,
136 PermissionManager $permMgr = null,
137 RepoGroup $repoGroup = null,
138 IContentHandlerFactory $contentHandlerFactory = null,
139 RevisionStore $revisionStore = null,
140 SpamChecker $spamChecker = null,
141 HookContainer $hookContainer = null
142 ) {
143 $this->oldTitle = $oldTitle;
144 $this->newTitle = $newTitle;
145
146 $services = MediaWikiServices::getInstance();
147 $this->options = $options ??
148 new ServiceOptions(
149 self::CONSTRUCTOR_OPTIONS,
150 $services->getMainConfig()
151 );
152 $this->loadBalancer = $loadBalancer ?? $services->getDBLoadBalancer();
153 $this->nsInfo = $nsInfo ?? $services->getNamespaceInfo();
154 $this->watchedItems = $watchedItems ?? $services->getWatchedItemStore();
155 $this->permMgr = $permMgr ?? $services->getPermissionManager();
156 $this->repoGroup = $repoGroup ?? $services->getRepoGroup();
157 $this->contentHandlerFactory =
158 $contentHandlerFactory ?? $services->getContentHandlerFactory();
159
160 $this->revisionStore = $revisionStore ?? $services->getRevisionStore();
161 $this->spamChecker = $spamChecker ?? $services->getSpamChecker();
162 $this->hookContainer = $hookContainer ?? $services->getHookContainer();
163 $this->hookRunner = new HookRunner( $this->hookContainer );
164 }
165
174 public function checkPermissions( User $user, $reason ) {
175 $status = new Status();
176
177 $errors = wfMergeErrorArrays(
178 $this->permMgr->getPermissionErrors( 'move', $user, $this->oldTitle ),
179 $this->permMgr->getPermissionErrors( 'edit', $user, $this->oldTitle ),
180 $this->permMgr->getPermissionErrors( 'move-target', $user, $this->newTitle ),
181 $this->permMgr->getPermissionErrors( 'edit', $user, $this->newTitle )
182 );
183
184 // Convert into a Status object
185 if ( $errors ) {
186 foreach ( $errors as $error ) {
187 $status->fatal( ...$error );
188 }
189 }
190
191 if ( $reason !== null && $this->spamChecker->checkSummary( $reason ) !== false ) {
192 // This is kind of lame, won't display nice
193 $status->fatal( 'spamprotectiontext' );
194 }
195
196 $tp = $this->newTitle->getTitleProtection();
197 if ( $tp !== false && !$this->permMgr->userHasRight( $user, $tp['permission'] ) ) {
198 $status->fatal( 'cantmove-titleprotected' );
199 }
200
201 $this->hookRunner->onMovePageCheckPermissions(
202 $this->oldTitle, $this->newTitle, $user, $reason, $status );
203
204 return $status;
205 }
206
214 public function isValidMove() {
215 $status = new Status();
216
217 if ( $this->oldTitle->equals( $this->newTitle ) ) {
218 $status->fatal( 'selfmove' );
219 } elseif ( $this->newTitle->getArticleID( Title::READ_LATEST /* T272386 */ )
220 && !$this->isValidMoveTarget()
221 ) {
222 // The move is allowed only if (1) the target doesn't exist, or (2) the target is a
223 // redirect to the source, and has no history (so we can undo bad moves right after
224 // they're done).
225 $status->fatal( 'articleexists', $this->newTitle->getPrefixedText() );
226 }
227
228 // @todo If the old title is invalid, maybe we should check if it somehow exists in the
229 // database and allow moving it to a valid name? Why prohibit the move from an empty name
230 // without checking in the database?
231 if ( $this->oldTitle->getDBkey() == '' ) {
232 $status->fatal( 'badarticleerror' );
233 } elseif ( $this->oldTitle->isExternal() ) {
234 $status->fatal( 'immobile-source-namespace-iw' );
235 } elseif ( !$this->oldTitle->isMovable() ) {
236 $nsText = $this->oldTitle->getNsText();
237 if ( $nsText === '' ) {
238 $nsText = wfMessage( 'blanknamespace' )->text();
239 }
240 $status->fatal( 'immobile-source-namespace', $nsText );
241 } elseif ( !$this->oldTitle->exists() ) {
242 $status->fatal( 'movepage-source-doesnt-exist' );
243 }
244
245 if ( $this->newTitle->isExternal() ) {
246 $status->fatal( 'immobile-target-namespace-iw' );
247 } elseif ( !$this->newTitle->isMovable() ) {
248 $nsText = $this->newTitle->getNsText();
249 if ( $nsText === '' ) {
250 $nsText = wfMessage( 'blanknamespace' )->text();
251 }
252 $status->fatal( 'immobile-target-namespace', $nsText );
253 }
254 if ( !$this->newTitle->isValid() ) {
255 $status->fatal( 'movepage-invalid-target-title' );
256 }
257
258 // Content model checks
259 if ( !$this->contentHandlerFactory
260 ->getContentHandler( $this->oldTitle->getContentModel() )
261 ->canBeUsedOn( $this->newTitle )
262 ) {
263 $status->fatal(
264 'content-not-allowed-here',
265 ContentHandler::getLocalizedName( $this->oldTitle->getContentModel() ),
266 $this->newTitle->getPrefixedText(),
267 SlotRecord::MAIN
268 );
269 }
270
271 // Image-specific checks
272 if ( $this->oldTitle->inNamespace( NS_FILE ) ) {
273 $status->merge( $this->isValidFileMove() );
274 }
275
276 if ( $this->newTitle->inNamespace( NS_FILE ) && !$this->oldTitle->inNamespace( NS_FILE ) ) {
277 $status->fatal( 'nonfile-cannot-move-to-file' );
278 }
279
280 // Hook for extensions to say a title can't be moved for technical reasons
281 $this->hookRunner->onMovePageIsValidMove( $this->oldTitle, $this->newTitle, $status );
282
283 return $status;
284 }
285
291 protected function isValidFileMove() {
292 $status = new Status();
293
294 if ( !$this->newTitle->inNamespace( NS_FILE ) ) {
295 $status->fatal( 'imagenocrossnamespace' );
296 // No need for further errors about the target filename being wrong
297 return $status;
298 }
299
300 $file = $this->repoGroup->getLocalRepo()->newFile( $this->oldTitle );
301 $file->load( File::READ_LATEST );
302 if ( $file->exists() ) {
303 if ( $this->newTitle->getText() != wfStripIllegalFilenameChars( $this->newTitle->getText() ) ) {
304 $status->fatal( 'imageinvalidfilename' );
305 }
306 if ( !File::checkExtensionCompatibility( $file, $this->newTitle->getDBkey() ) ) {
307 $status->fatal( 'imagetypemismatch' );
308 }
309 }
310
311 return $status;
312 }
313
321 protected function isValidMoveTarget() {
322 # Is it an existing file?
323 if ( $this->newTitle->inNamespace( NS_FILE ) ) {
324 $file = $this->repoGroup->getLocalRepo()->newFile( $this->newTitle );
325 $file->load( File::READ_LATEST );
326 if ( $file->exists() ) {
327 wfDebug( __METHOD__ . ": file exists" );
328 return false;
329 }
330 }
331 # Is it a redirect with no history?
332 if ( !$this->newTitle->isSingleRevRedirect() ) {
333 wfDebug( __METHOD__ . ": not a one-rev redirect" );
334 return false;
335 }
336 # Get the article text
337 $rev = $this->revisionStore->getRevisionByTitle(
338 $this->newTitle,
339 0,
340 RevisionStore::READ_LATEST
341 );
342 if ( !is_object( $rev ) ) {
343 return false;
344 }
345 $content = $rev->getContent( SlotRecord::MAIN );
346 # Does the redirect point to the source?
347 # Or is it a broken self-redirect, usually caused by namespace collisions?
348 $redirTitle = $content ? $content->getRedirectTarget() : null;
349
350 if ( $redirTitle ) {
351 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle->getPrefixedDBkey() &&
352 $redirTitle->getPrefixedDBkey() !== $this->newTitle->getPrefixedDBkey() ) {
353 wfDebug( __METHOD__ . ": redirect points to other page" );
354 return false;
355 } else {
356 return true;
357 }
358 } else {
359 # Fail safe (not a redirect after all. strange.)
360 wfDebug( __METHOD__ . ": failsafe: database says " . $this->newTitle->getPrefixedDBkey() .
361 " is a redirect, but it doesn't contain a valid redirect." );
362 return false;
363 }
364 }
365
377 public function move(
378 User $user, $reason = null, $createRedirect = true, array $changeTags = []
379 ) {
380 $status = $this->isValidMove();
381 if ( !$status->isOK() ) {
382 return $status;
383 }
384
385 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
386 }
387
397 public function moveIfAllowed(
398 User $user, $reason = null, $createRedirect = true, array $changeTags = []
399 ) {
400 $status = $this->isValidMove();
401 $status->merge( $this->checkPermissions( $user, $reason ) );
402 if ( $changeTags ) {
403 $status->merge( ChangeTags::canAddTagsAccompanyingChange( $changeTags, $user ) );
404 }
405
406 if ( !$status->isOK() ) {
407 // Auto-block user's IP if the account was "hard" blocked
408 $user->spreadAnyEditBlock();
409 return $status;
410 }
411
412 // Check suppressredirect permission
413 if ( !$this->permMgr->userHasRight( $user, 'suppressredirect' ) ) {
414 $createRedirect = true;
415 }
416
417 return $this->moveUnsafe( $user, $reason, $createRedirect, $changeTags );
418 }
419
434 public function moveSubpages(
435 User $user, $reason = null, $createRedirect = true, array $changeTags = []
436 ) {
437 return $this->moveSubpagesInternal( false, $user, $reason, $createRedirect, $changeTags );
438 }
439
453 public function moveSubpagesIfAllowed(
454 User $user, $reason = null, $createRedirect = true, array $changeTags = []
455 ) {
456 return $this->moveSubpagesInternal( true, $user, $reason, $createRedirect, $changeTags );
457 }
458
467 private function moveSubpagesInternal(
468 $checkPermissions, User $user, $reason, $createRedirect, array $changeTags
469 ) {
471
472 if ( $checkPermissions ) {
473 if ( !$this->permMgr->userCan(
474 'move-subpages', $user, $this->oldTitle )
475 ) {
476 return Status::newFatal( 'cant-move-subpages' );
477 }
478 }
479
480 // Do the source and target namespaces support subpages?
481 if ( !$this->nsInfo->hasSubpages( $this->oldTitle->getNamespace() ) ) {
482 return Status::newFatal( 'namespace-nosubpages',
483 $this->nsInfo->getCanonicalName( $this->oldTitle->getNamespace() ) );
484 }
485 if ( !$this->nsInfo->hasSubpages( $this->newTitle->getNamespace() ) ) {
486 return Status::newFatal( 'namespace-nosubpages',
487 $this->nsInfo->getCanonicalName( $this->newTitle->getNamespace() ) );
488 }
489
490 // Return a status for the overall result. Its value will be an array with per-title
491 // status for each subpage. Merge any errors from the per-title statuses into the
492 // top-level status without resetting the overall result.
493 $topStatus = Status::newGood();
494 $perTitleStatus = [];
495 $subpages = $this->oldTitle->getSubpages( $wgMaximumMovedPages + 1 );
496 $count = 0;
497 foreach ( $subpages as $oldSubpage ) {
498 $count++;
499 if ( $count > $wgMaximumMovedPages ) {
500 $status = Status::newFatal( 'movepage-max-pages', $wgMaximumMovedPages );
501 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
502 $topStatus->merge( $status );
503 $topStatus->setOK( true );
504 break;
505 }
506
507 // We don't know whether this function was called before or after moving the root page,
508 // so check both titles
509 if ( $oldSubpage->getArticleID() == $this->oldTitle->getArticleID() ||
510 $oldSubpage->getArticleID() == $this->newTitle->getArticleID()
511 ) {
512 // When moving a page to a subpage of itself, don't move it twice
513 continue;
514 }
515 $newPageName = preg_replace(
516 '#^' . preg_quote( $this->oldTitle->getDBkey(), '#' ) . '#',
517 StringUtils::escapeRegexReplacement( $this->newTitle->getDBkey() ), # T23234
518 $oldSubpage->getDBkey() );
519 if ( $oldSubpage->isTalkPage() ) {
520 $newNs = $this->newTitle->getTalkPage()->getNamespace();
521 } else {
522 $newNs = $this->newTitle->getSubjectPage()->getNamespace();
523 }
524 // T16385: we need makeTitleSafe because the new page names may be longer than 255
525 // characters.
526 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
527
528 $mp = new MovePage( $oldSubpage, $newSubpage );
529 $method = $checkPermissions ? 'moveIfAllowed' : 'move';
531 $status = $mp->$method( $user, $reason, $createRedirect, $changeTags );
532 if ( $status->isOK() ) {
533 $status->setResult( true, $newSubpage->getPrefixedText() );
534 }
535 $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
536 $topStatus->merge( $status );
537 $topStatus->setOK( true );
538 }
539
540 $topStatus->value = $perTitleStatus;
541 return $topStatus;
542 }
543
553 private function moveUnsafe( User $user, $reason, $createRedirect, array $changeTags ) {
554 $status = Status::newGood();
555 $this->hookRunner->onTitleMove( $this->oldTitle, $this->newTitle, $user, $reason, $status );
556 if ( !$status->isOK() ) {
557 // Move was aborted by the hook
558 return $status;
559 }
560
561 $dbw = $this->loadBalancer->getConnection( DB_MASTER );
562 $dbw->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
563
564 $this->hookRunner->onTitleMoveStarting( $this->oldTitle, $this->newTitle, $user );
565
566 $pageid = $this->oldTitle->getArticleID( Title::READ_LATEST );
567 $protected = $this->oldTitle->isProtected();
568
569 // Do the actual move; if this fails, it will throw an MWException(!)
570 $nullRevision = $this->moveToInternal( $user, $this->newTitle, $reason, $createRedirect,
571 $changeTags );
572
573 // Refresh the sortkey for this row. Be careful to avoid resetting
574 // cl_timestamp, which may disturb time-based lists on some sites.
575 // @todo This block should be killed, it's duplicating code
576 // from LinksUpdate::getCategoryInsertions() and friends.
577 $prefixes = $dbw->select(
578 'categorylinks',
579 [ 'cl_sortkey_prefix', 'cl_to' ],
580 [ 'cl_from' => $pageid ],
581 __METHOD__
582 );
583 $type = $this->nsInfo->getCategoryLinkType( $this->newTitle->getNamespace() );
584 foreach ( $prefixes as $prefixRow ) {
585 $prefix = $prefixRow->cl_sortkey_prefix;
586 $catTo = $prefixRow->cl_to;
587 $dbw->update( 'categorylinks',
588 [
589 'cl_sortkey' => Collation::singleton()->getSortKey(
590 $this->newTitle->getCategorySortkey( $prefix ) ),
591 'cl_collation' => $this->options->get( 'CategoryCollation' ),
592 'cl_type' => $type,
593 'cl_timestamp=cl_timestamp' ],
594 [
595 'cl_from' => $pageid,
596 'cl_to' => $catTo ],
597 __METHOD__
598 );
599 }
600
601 $redirid = $this->oldTitle->getArticleID();
602
603 if ( $protected ) {
604 # Protect the redirect title as the title used to be...
605 $res = $dbw->select(
606 'page_restrictions',
607 [ 'pr_type', 'pr_level', 'pr_cascade', 'pr_user', 'pr_expiry' ],
608 [ 'pr_page' => $pageid ],
609 __METHOD__,
610 'FOR UPDATE'
611 );
612 $rowsInsert = [];
613 foreach ( $res as $row ) {
614 $rowsInsert[] = [
615 'pr_page' => $redirid,
616 'pr_type' => $row->pr_type,
617 'pr_level' => $row->pr_level,
618 'pr_cascade' => $row->pr_cascade,
619 'pr_user' => $row->pr_user,
620 'pr_expiry' => $row->pr_expiry
621 ];
622 }
623 $dbw->insert( 'page_restrictions', $rowsInsert, __METHOD__, [ 'IGNORE' ] );
624
625 // Build comment for log
626 $comment = wfMessage(
627 'prot_1movedto2',
628 $this->oldTitle->getPrefixedText(),
629 $this->newTitle->getPrefixedText()
630 )->inContentLanguage()->text();
631 if ( $reason ) {
632 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
633 }
634
635 // reread inserted pr_ids for log relation
636 $insertedPrIds = $dbw->select(
637 'page_restrictions',
638 'pr_id',
639 [ 'pr_page' => $redirid ],
640 __METHOD__
641 );
642 $logRelationsValues = [];
643 foreach ( $insertedPrIds as $prid ) {
644 $logRelationsValues[] = $prid->pr_id;
645 }
646
647 // Update the protection log
648 $logEntry = new ManualLogEntry( 'protect', 'move_prot' );
649 $logEntry->setTarget( $this->newTitle );
650 $logEntry->setComment( $comment );
651 $logEntry->setPerformer( $user );
652 $logEntry->setParameters( [
653 '4::oldtitle' => $this->oldTitle->getPrefixedText(),
654 ] );
655 $logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] );
656 $logEntry->addTags( $changeTags );
657 $logId = $logEntry->insert();
658 $logEntry->publish( $logId );
659 }
660
661 // Update *_from_namespace fields as needed
662 if ( $this->oldTitle->getNamespace() != $this->newTitle->getNamespace() ) {
663 $dbw->update( 'pagelinks',
664 [ 'pl_from_namespace' => $this->newTitle->getNamespace() ],
665 [ 'pl_from' => $pageid ],
666 __METHOD__
667 );
668 $dbw->update( 'templatelinks',
669 [ 'tl_from_namespace' => $this->newTitle->getNamespace() ],
670 [ 'tl_from' => $pageid ],
671 __METHOD__
672 );
673 $dbw->update( 'imagelinks',
674 [ 'il_from_namespace' => $this->newTitle->getNamespace() ],
675 [ 'il_from' => $pageid ],
676 __METHOD__
677 );
678 }
679
680 # Update watchlists
681 $oldtitle = $this->oldTitle->getDBkey();
682 $newtitle = $this->newTitle->getDBkey();
683 $oldsnamespace = $this->nsInfo->getSubject( $this->oldTitle->getNamespace() );
684 $newsnamespace = $this->nsInfo->getSubject( $this->newTitle->getNamespace() );
685 if ( $oldsnamespace != $newsnamespace || $oldtitle != $newtitle ) {
686 $this->watchedItems->duplicateAllAssociatedEntries( $this->oldTitle, $this->newTitle );
687 }
688
689 // If it is a file then move it last.
690 // This is done after all database changes so that file system errors cancel the transaction.
691 if ( $this->oldTitle->getNamespace() == NS_FILE ) {
692 $status = $this->moveFile( $this->oldTitle, $this->newTitle );
693 if ( !$status->isOK() ) {
694 $dbw->cancelAtomic( __METHOD__ );
695 return $status;
696 }
697 }
698
699 $this->hookRunner->onPageMoveCompleting(
700 $this->oldTitle, $this->newTitle,
701 $user, $pageid, $redirid, $reason, $nullRevision
702 );
703
704 // Deprecated since 1.35, use PageMoveCompleting
705 if ( $this->hookContainer->isRegistered( 'TitleMoveCompleting' ) ) {
706 // Only create the Revision object if needed
707 $nullRevisionObj = new Revision( $nullRevision );
708 $this->hookRunner->onTitleMoveCompleting(
709 $this->oldTitle,
710 $this->newTitle,
711 $user,
712 $pageid,
713 $redirid,
714 $reason,
715 $nullRevisionObj
716 );
717 }
718
719 $dbw->endAtomic( __METHOD__ );
720
721 // Keep each single hook handler atomic
722 DeferredUpdates::addUpdate(
724 $dbw,
725 __METHOD__,
726 function () use ( $user, $pageid, $redirid, $reason, $nullRevision ) {
727 $this->hookRunner->onPageMoveComplete(
728 $this->oldTitle,
729 $this->newTitle,
730 $user,
731 $pageid,
732 $redirid,
733 $reason,
734 $nullRevision
735 );
736
737 if ( !$this->hookContainer->isRegistered( 'TitleMoveComplete' ) ) {
738 // Don't go on to create a Revision unless its needed
739 return;
740 }
741
742 $nullRevisionObj = new Revision( $nullRevision );
743 // Deprecated since 1.35, use PageMoveComplete
744 $this->hookRunner->onTitleMoveComplete(
745 $this->oldTitle,
746 $this->newTitle,
747 $user, $pageid,
748 $redirid,
749 $reason,
750 $nullRevisionObj
751 );
752 }
753 )
754 );
755
756 return Status::newGood();
757 }
758
768 private function moveFile( $oldTitle, $newTitle ) {
769 $file = $this->repoGroup->getLocalRepo()->newFile( $oldTitle );
770 $file->load( File::READ_LATEST );
771 if ( $file->exists() ) {
772 $status = $file->move( $newTitle );
773 } else {
774 $status = Status::newGood();
775 }
776
777 // Clear RepoGroup process cache
778 $this->repoGroup->clearCache( $oldTitle );
779 $this->repoGroup->clearCache( $newTitle ); # clear false negative cache
780 return $status;
781 }
782
798 private function moveToInternal( User $user, &$nt, $reason = '', $createRedirect = true,
799 array $changeTags = []
800 ) {
801 if ( $nt->exists() ) {
802 $moveOverRedirect = true;
803 $logType = 'move_redir';
804 } else {
805 $moveOverRedirect = false;
806 $logType = 'move';
807 }
808
809 if ( $moveOverRedirect ) {
810 $overwriteMessage = wfMessage(
811 'delete_and_move_reason',
812 $this->oldTitle->getPrefixedText()
813 )->inContentLanguage()->text();
814 $newpage = WikiPage::factory( $nt );
815 $errs = [];
816 $status = $newpage->doDeleteArticleReal(
817 $overwriteMessage,
818 $user,
819 /* $suppress */ false,
820 /* unused */ null,
821 $errs,
822 /* unused */ null,
823 $changeTags,
824 'delete_redir'
825 );
826
827 if ( !$status->isGood() ) {
828 throw new MWException( 'Failed to delete page-move revision: '
829 . $status->getWikiText( false, false, 'en' ) );
830 }
831
832 $nt->resetArticleID( false );
833 }
834
835 if ( $createRedirect ) {
836 if ( $this->oldTitle->getNamespace() == NS_CATEGORY
837 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
838 ) {
839 $redirectContent = new WikitextContent(
840 wfMessage( 'category-move-redirect-override' )
841 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
842 } else {
843 $redirectContent = $this->contentHandlerFactory
844 ->getContentHandler( $this->oldTitle->getContentModel() )
845 ->makeRedirectContent(
846 $nt,
847 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain()
848 );
849 }
850
851 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
852 } else {
853 $redirectContent = null;
854 }
855
856 // T59084: log_page should be the ID of the *moved* page
857 $oldid = $this->oldTitle->getArticleID();
858 $logTitle = clone $this->oldTitle;
859
860 $logEntry = new ManualLogEntry( 'move', $logType );
861 $logEntry->setPerformer( $user );
862 $logEntry->setTarget( $logTitle );
863 $logEntry->setComment( $reason );
864 $logEntry->setParameters( [
865 '4::target' => $nt->getPrefixedText(),
866 '5::noredir' => $redirectContent ? '0' : '1',
867 ] );
868
869 $formatter = LogFormatter::newFromEntry( $logEntry );
870 $formatter->setContext( RequestContext::newExtraneousContext( $this->oldTitle ) );
871 $comment = $formatter->getPlainActionText();
872 if ( $reason ) {
873 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
874 }
875
876 $dbw = $this->loadBalancer->getConnection( DB_MASTER );
877
878 $oldpage = WikiPage::factory( $this->oldTitle );
879 $oldcountable = $oldpage->isCountable();
880
881 $newpage = WikiPage::factory( $nt );
882
883 # Change the name of the target page:
884 $dbw->update( 'page',
885 /* SET */ [
886 'page_namespace' => $nt->getNamespace(),
887 'page_title' => $nt->getDBkey(),
888 ],
889 /* WHERE */ [ 'page_id' => $oldid ],
890 __METHOD__
891 );
892
893 // Reset $nt before using it to create the null revision (T248789).
894 // But not $this->oldTitle yet, see below (T47348).
895 $nt->resetArticleID( $oldid );
896
897 $commentObj = CommentStoreComment::newUnsavedComment( $comment );
898 # Save a null revision in the page's history notifying of the move
899 $nullRevision = $this->revisionStore->newNullRevision(
900 $dbw,
901 $nt,
902 $commentObj,
903 true,
904 $user
905 );
906 if ( !is_object( $nullRevision ) ) {
907 throw new MWException( 'Failed to create null revision while moving page ID '
908 . $oldid . ' to ' . $nt->getPrefixedDBkey() );
909 }
910
911 $nullRevision = $this->revisionStore->insertRevisionOn( $nullRevision, $dbw );
912 $logEntry->setAssociatedRevId( $nullRevision->getId() );
913
919 $user->incEditCount();
920
921 if ( !$redirectContent ) {
922 // Clean up the old title *before* reset article id - T47348
923 WikiPage::onArticleDelete( $this->oldTitle );
924 }
925
926 $this->oldTitle->resetArticleID( 0 ); // 0 == non existing
927 $newpage->loadPageData( WikiPage::READ_LOCKING ); // T48397
928
929 $newpage->updateRevisionOn( $dbw, $nullRevision );
930
931 $fakeTags = [];
932 $this->hookRunner->onRevisionFromEditComplete(
933 $newpage, $nullRevision, $nullRevision->getParentId(), $user, $fakeTags );
934
935 // Hook is hard deprecated since 1.35
936 if ( $this->hookContainer->isRegistered( 'NewRevisionFromEditComplete' ) ) {
937 // Only create the Revision object if needed
938 $nullRevisionObj = new Revision( $nullRevision );
939 $this->hookRunner->onNewRevisionFromEditComplete(
940 $newpage,
941 $nullRevisionObj,
942 $nullRevision->getParentId(),
943 $user,
944 $fakeTags
945 );
946 }
947
948 $newpage->doEditUpdates( $nullRevision, $user,
949 [ 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ] );
950
951 WikiPage::onArticleCreate( $nt );
952
953 # Recreate the redirect, this time in the other direction.
954 if ( $redirectContent ) {
955 $redirectArticle = WikiPage::factory( $this->oldTitle );
956 $redirectArticle->loadFromRow( false, WikiPage::READ_LOCKING ); // T48397
957 $newid = $redirectArticle->insertOn( $dbw );
958 if ( $newid ) { // sanity
959 $this->oldTitle->resetArticleID( $newid );
960 $redirectRevisionRecord = new MutableRevisionRecord( $this->oldTitle );
961 $redirectRevisionRecord->setPageId( $newid );
962 $redirectRevisionRecord->setUser( $user );
963 $redirectRevisionRecord->setComment( $commentObj );
964 $redirectRevisionRecord->setContent( SlotRecord::MAIN, $redirectContent );
965 $redirectRevisionRecord->setTimestamp( MWTimestamp::now( TS_MW ) );
966
967 $inserted = $this->revisionStore->insertRevisionOn(
968 $redirectRevisionRecord,
969 $dbw
970 );
971 $redirectRevId = $inserted->getId();
972 $redirectArticle->updateRevisionOn( $dbw, $inserted, 0 );
973
974 $fakeTags = [];
975 $this->hookRunner->onRevisionFromEditComplete(
976 $redirectArticle,
977 $inserted,
978 false,
979 $user,
980 $fakeTags
981 );
982
983 // Hook is hard deprecated since 1.35
984 if ( $this->hookContainer->isRegistered( 'NewRevisionFromEditComplete' ) ) {
985 // Only create the Revision object if needed
986 $redirectRevisionObj = new Revision( $inserted );
987 $this->hookRunner->onNewRevisionFromEditComplete(
988 $redirectArticle,
989 $redirectRevisionObj,
990 false,
991 $user,
992 $fakeTags
993 );
994 }
995
996 $redirectArticle->doEditUpdates(
997 $inserted,
998 $user,
999 [ 'created' => true ]
1000 );
1001
1002 // make a copy because of log entry below
1003 $redirectTags = $changeTags;
1004 if ( in_array( 'mw-new-redirect', ChangeTags::getSoftwareTags() ) ) {
1005 $redirectTags[] = 'mw-new-redirect';
1006 }
1007 ChangeTags::addTags( $redirectTags, null, $redirectRevId, null );
1008 }
1009 }
1010
1011 # Log the move
1012 $logid = $logEntry->insert();
1013
1014 $logEntry->addTags( $changeTags );
1015 $logEntry->publish( $logid );
1016
1017 return $nullRevision;
1018 }
1019}
$wgMaximumMovedPages
Maximum number of pages to move at once when moving subpages with a page.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMergeErrorArrays(... $args)
Merge arrays in the style of PermissionManager::getPermissionErrors, with duplicate removal e....
wfStripIllegalFilenameChars( $name)
Replace all invalid characters with '-'.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
static getSoftwareTags( $all=false)
Loads defined core tags, checks for invalid types (if not array), and filters for supported and enabl...
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null, RecentChange $rc=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
MediaWiki exception.
Class for creating new log entries and inserting them into the database.
A class for passing options to services.
Service to check if text (either content or a summary) qualifies as spam.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
MediaWikiServices is the service locator for the application scope of MediaWiki.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Page revision base class.
Service for looking up page revisions.
Value object representing a content slot associated with a page revision.
Handles the backend logic of moving a page from one title to another.
Definition MovePage.php:42
checkPermissions(User $user, $reason)
Check if the user is allowed to perform the move.
Definition MovePage.php:174
moveSubpagesInternal( $checkPermissions, User $user, $reason, $createRedirect, array $changeTags)
Definition MovePage.php:467
moveIfAllowed(User $user, $reason=null, $createRedirect=true, array $changeTags=[])
Same as move(), but with permissions checks.
Definition MovePage.php:397
isValidFileMove()
Sanity checks for when a file is being moved.
Definition MovePage.php:291
NamespaceInfo $nsInfo
Definition MovePage.php:67
moveUnsafe(User $user, $reason, $createRedirect, array $changeTags)
Moves without any sort of safety or sanity checks.
Definition MovePage.php:553
HookContainer $hookContainer
Definition MovePage.php:102
ServiceOptions $options
Definition MovePage.php:57
Title $oldTitle
Definition MovePage.php:47
WatchedItemStoreInterface $watchedItems
Definition MovePage.php:72
moveSubpagesIfAllowed(User $user, $reason=null, $createRedirect=true, array $changeTags=[])
Move the source page's subpages to be subpages of the target page, with user permission checks.
Definition MovePage.php:453
moveToInternal(User $user, &$nt, $reason='', $createRedirect=true, array $changeTags=[])
Move page to a title which is either a redirect to the source page or nonexistent.
Definition MovePage.php:798
ILoadBalancer $loadBalancer
Definition MovePage.php:62
HookRunner $hookRunner
Definition MovePage.php:107
isValidMove()
Does various sanity checks that the move is valid.
Definition MovePage.php:214
isValidMoveTarget()
Checks if $this can be moved to a given Title.
Definition MovePage.php:321
move(User $user, $reason=null, $createRedirect=true, array $changeTags=[])
Move a page without taking user permissions into account.
Definition MovePage.php:377
moveSubpages(User $user, $reason=null, $createRedirect=true, array $changeTags=[])
Move the source page's subpages to be subpages of the target page, without checking user permissions.
Definition MovePage.php:434
PermissionManager $permMgr
Definition MovePage.php:77
SpamChecker $spamChecker
Definition MovePage.php:97
IContentHandlerFactory $contentHandlerFactory
Definition MovePage.php:87
moveFile( $oldTitle, $newTitle)
Move a file associated with a page to a new location.
Definition MovePage.php:768
RevisionStore $revisionStore
Definition MovePage.php:92
Title $newTitle
Definition MovePage.php:52
__construct(Title $oldTitle, Title $newTitle, ServiceOptions $options=null, ILoadBalancer $loadBalancer=null, NamespaceInfo $nsInfo=null, WatchedItemStoreInterface $watchedItems=null, PermissionManager $permMgr=null, RepoGroup $repoGroup=null, IContentHandlerFactory $contentHandlerFactory=null, RevisionStore $revisionStore=null, SpamChecker $spamChecker=null, HookContainer $hookContainer=null)
Calling this directly is deprecated in 1.34.
Definition MovePage.php:129
RepoGroup $repoGroup
Definition MovePage.php:82
const CONSTRUCTOR_OPTIONS
Definition MovePage.php:109
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Prioritized list of file repositories.
Definition RepoGroup.php:31
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
spreadAnyEditBlock()
If this user is logged-in and blocked, block any IP address they've successfully logged in from.
Definition User.php:3717
incEditCount()
Schedule a deferred update to update the user's edit count.
Definition User.php:4413
Content object for wiki text pages.
const NS_FILE
Definition Defines.php:76
const NS_CATEGORY
Definition Defines.php:84
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_MASTER
Definition defines.php:29
$content
Definition router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42