Update RELEASE-NOTES-1.34 for various backports
[lhc/web/wiklou.git] / includes / MovePage.php
index f48d92e..59baae7 100644 (file)
@@ -25,7 +25,7 @@ use MediaWiki\Page\MovePageFactory;
 use MediaWiki\Permissions\PermissionManager;
 use MediaWiki\Revision\SlotRecord;
 use Wikimedia\Rdbms\IDatabase;
-use Wikimedia\Rdbms\LoadBalancer;
+use Wikimedia\Rdbms\ILoadBalancer;
 
 /**
  * Handles the backend logic of moving a page from one title
@@ -51,7 +51,7 @@ class MovePage {
        protected $options;
 
        /**
-        * @var LoadBalancer
+        * @var ILoadBalancer
         */
        protected $loadBalancer;
 
@@ -61,7 +61,7 @@ class MovePage {
        protected $nsInfo;
 
        /**
-        * @var WatchedItemStore
+        * @var WatchedItemStoreInterface
         */
        protected $watchedItems;
 
@@ -70,25 +70,31 @@ class MovePage {
         */
        protected $permMgr;
 
+       /**
+        * @var RepoGroup
+        */
+       protected $repoGroup;
+
        /**
         * Calling this directly is deprecated in 1.34. Use MovePageFactory instead.
         *
         * @param Title $oldTitle
         * @param Title $newTitle
         * @param ServiceOptions|null $options
-        * @param LoadBalancer|null $loadBalancer
+        * @param ILoadBalancer|null $loadBalancer
         * @param NamespaceInfo|null $nsInfo
-        * @param WatchedItemStore|null $watchedItems
+        * @param WatchedItemStoreInterface|null $watchedItems
         * @param PermissionManager|null $permMgr
         */
        public function __construct(
                Title $oldTitle,
                Title $newTitle,
                ServiceOptions $options = null,
-               LoadBalancer $loadBalancer = null,
+               ILoadBalancer $loadBalancer = null,
                NamespaceInfo $nsInfo = null,
-               WatchedItemStore $watchedItems = null,
-               PermissionManager $permMgr = null
+               WatchedItemStoreInterface $watchedItems = null,
+               PermissionManager $permMgr = null,
+               RepoGroup $repoGroup = null
        ) {
                $this->oldTitle = $oldTitle;
                $this->newTitle = $newTitle;
@@ -101,6 +107,7 @@ class MovePage {
                $this->watchedItems =
                        $watchedItems ?? MediaWikiServices::getInstance()->getWatchedItemStore();
                $this->permMgr = $permMgr ?? MediaWikiServices::getInstance()->getPermissionManager();
+               $this->repoGroup = $repoGroup ?? MediaWikiServices::getInstance()->getRepoGroup();
        }
 
        /**
@@ -134,8 +141,9 @@ class MovePage {
                }
 
                $tp = $this->newTitle->getTitleProtection();
-               if ( $tp !== false && !$user->isAllowed( $tp['permission'] ) ) {
-                               $status->fatal( 'cantmove-titleprotected' );
+               $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+               if ( $tp !== false && !$permissionManager->userHasRight( $user, $tp['permission'] ) ) {
+                       $status->fatal( 'cantmove-titleprotected' );
                }
 
                Hooks::run( 'MovePageCheckPermissions',
@@ -157,35 +165,33 @@ class MovePage {
 
                if ( $this->oldTitle->equals( $this->newTitle ) ) {
                        $status->fatal( 'selfmove' );
+               } elseif ( $this->newTitle->getArticleID() && !$this->isValidMoveTarget() ) {
+                       // The move is allowed only if (1) the target doesn't exist, or (2) the target is a
+                       // redirect to the source, and has no history (so we can undo bad moves right after
+                       // they're done).
+                       $status->fatal( 'articleexists' );
                }
-               if ( !$this->oldTitle->isMovable() ) {
+
+               // @todo If the old title is invalid, maybe we should check if it somehow exists in the
+               // database and allow moving it to a valid name? Why prohibit the move from an empty name
+               // without checking in the database?
+               if ( $this->oldTitle->getDBkey() == '' ) {
+                       $status->fatal( 'badarticleerror' );
+               } elseif ( $this->oldTitle->isExternal() ) {
+                       $status->fatal( 'immobile-source-namespace-iw' );
+               } elseif ( !$this->oldTitle->isMovable() ) {
                        $status->fatal( 'immobile-source-namespace', $this->oldTitle->getNsText() );
+               } elseif ( !$this->oldTitle->exists() ) {
+                       $status->fatal( 'movepage-source-doesnt-exist' );
                }
+
                if ( $this->newTitle->isExternal() ) {
                        $status->fatal( 'immobile-target-namespace-iw' );
-               }
-               if ( !$this->newTitle->isMovable() ) {
+               } elseif ( !$this->newTitle->isMovable() ) {
                        $status->fatal( 'immobile-target-namespace', $this->newTitle->getNsText() );
                }
-
-               $oldid = $this->oldTitle->getArticleID();
-
-               if ( $this->newTitle->getDBkey() === '' ) {
-                       $status->fatal( 'articleexists' );
-               }
-               if (
-                       ( $this->oldTitle->getDBkey() == '' ) ||
-                       ( !$oldid ) ||
-                       ( $this->newTitle->getDBkey() == '' )
-               ) {
-                       $status->fatal( 'badarticleerror' );
-               }
-
-               # The move is allowed only if (1) the target doesn't exist, or
-               # (2) the target is a redirect to the source, and has no history
-               # (so we can undo bad moves right after they're done).
-               if ( $this->newTitle->getArticleID() && !$this->isValidMoveTarget() ) {
-                       $status->fatal( 'articleexists' );
+               if ( !$this->newTitle->isValid() ) {
+                       $status->fatal( 'movepage-invalid-target-title' );
                }
 
                // Content model checks
@@ -230,7 +236,14 @@ class MovePage {
         */
        protected function isValidFileMove() {
                $status = new Status();
-               $file = wfLocalFile( $this->oldTitle );
+
+               if ( !$this->newTitle->inNamespace( NS_FILE ) ) {
+                       $status->fatal( 'imagenocrossnamespace' );
+                       // No need for further errors about the target filename being wrong
+                       return $status;
+               }
+
+               $file = $this->repoGroup->getLocalRepo()->newFile( $this->oldTitle );
                $file->load( File::READ_LATEST );
                if ( $file->exists() ) {
                        if ( $this->newTitle->getText() != wfStripIllegalFilenameChars( $this->newTitle->getText() ) ) {
@@ -241,10 +254,6 @@ class MovePage {
                        }
                }
 
-               if ( !$this->newTitle->inNamespace( NS_FILE ) ) {
-                       $status->fatal( 'imagenocrossnamespace' );
-               }
-
                return $status;
        }
 
@@ -258,7 +267,7 @@ class MovePage {
        protected function isValidMoveTarget() {
                # Is it an existing file?
                if ( $this->newTitle->inNamespace( NS_FILE ) ) {
-                       $file = wfLocalFile( $this->newTitle );
+                       $file = $this->repoGroup->getLocalRepo()->newFile( $this->newTitle );
                        $file->load( File::READ_LATEST );
                        if ( $file->exists() ) {
                                wfDebug( __METHOD__ . ": file exists\n" );
@@ -343,7 +352,8 @@ class MovePage {
                }
 
                // Check suppressredirect permission
-               if ( !$user->isAllowed( 'suppressredirect' ) ) {
+               $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+               if ( !$permissionManager->userHasRight( $user, 'suppressredirect' ) ) {
                        $createRedirect = true;
                }
 
@@ -436,7 +446,7 @@ class MovePage {
                                $status = Status::newFatal( 'movepage-max-pages', $wgMaximumMovedPages );
                                $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
                                $topStatus->merge( $status );
-                               $topStatus->setOk( true );
+                               $topStatus->setOK( true );
                                break;
                        }
 
@@ -463,13 +473,14 @@ class MovePage {
 
                        $mp = new MovePage( $oldSubpage, $newSubpage );
                        $method = $checkPermissions ? 'moveIfAllowed' : 'move';
+                       /** @var Status $status */
                        $status = $mp->$method( $user, $reason, $createRedirect, $changeTags );
                        if ( $status->isOK() ) {
                                $status->setResult( true, $newSubpage->getPrefixedText() );
                        }
                        $perTitleStatus[$oldSubpage->getPrefixedText()] = $status;
                        $topStatus->merge( $status );
-                       $topStatus->setOk( true );
+                       $topStatus->setOK( true );
                }
 
                $topStatus->value = $perTitleStatus;
@@ -498,7 +509,7 @@ class MovePage {
 
                Hooks::run( 'TitleMoveStarting', [ $this->oldTitle, $this->newTitle, $user ] );
 
-               $pageid = $this->oldTitle->getArticleID( Title::GAID_FOR_UPDATE );
+               $pageid = $this->oldTitle->getArticleID( Title::READ_LATEST );
                $protected = $this->oldTitle->isProtected();
 
                // Do the actual move; if this fails, it will throw an MWException(!)
@@ -588,7 +599,7 @@ class MovePage {
                                '4::oldtitle' => $this->oldTitle->getPrefixedText(),
                        ] );
                        $logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] );
-                       $logEntry->setTags( $changeTags );
+                       $logEntry->addTags( $changeTags );
                        $logId = $logEntry->insert();
                        $logEntry->publish( $logId );
                }
@@ -679,15 +690,15 @@ class MovePage {
                        $oldTitle->getPrefixedText()
                );
 
-               $file = wfLocalFile( $oldTitle );
+               $file = $this->repoGroup->getLocalRepo()->newFile( $oldTitle );
                $file->load( File::READ_LATEST );
                if ( $file->exists() ) {
                        $status = $file->move( $newTitle );
                }
 
                // Clear RepoGroup process cache
-               RepoGroup::singleton()->clearCache( $oldTitle );
-               RepoGroup::singleton()->clearCache( $newTitle ); # clear false negative cache
+               $this->repoGroup->clearCache( $oldTitle );
+               $this->repoGroup->clearCache( $newTitle ); # clear false negative cache
                return $status;
        }
 
@@ -885,7 +896,7 @@ class MovePage {
                # Log the move
                $logid = $logEntry->insert();
 
-               $logEntry->setTags( $changeTags );
+               $logEntry->addTags( $changeTags );
                $logEntry->publish( $logid );
 
                return $nullRevision;