Avoid multiply calls to MediaWikiServices::getInstance() in one function
authorUmherirrender <umherirrender_de.wp@web.de>
Mon, 19 Aug 2019 20:45:43 +0000 (22:45 +0200)
committerUmherirrender <umherirrender_de.wp@web.de>
Sun, 1 Sep 2019 18:49:19 +0000 (20:49 +0200)
RepoGroup::singleton() results in
MediaWikiServices::getInstance()->getRepoGroup()

Also avoid getting the service in loops, doing it once before the loop
is cheaper

Change-Id: I29e0c7487e3e498559ff16a567e2fad0c0f8bb69

includes/Linker.php
includes/api/ApiMove.php
includes/specials/SpecialMovepage.php

index e748b3a..1a5058d 100644 (file)
@@ -688,13 +688,14 @@ class Linker {
                if ( $label == '' ) {
                        $label = $title->getPrefixedText();
                }
+               $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
                $currentExists = $time
-                       && MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title ) !== false;
+                       && $repoGroup->findFile( $title ) !== false;
 
                if ( ( $wgUploadMissingFileUrl || $wgUploadNavigationUrl || $wgEnableUploads )
                        && !$currentExists
                ) {
-                       if ( RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title ) ) {
+                       if ( $repoGroup->getLocalRepo()->checkRedirect( $title ) ) {
                                // We already know it's a redirect, so mark it accordingly
                                return self::link(
                                        $title,
index 4cbed21..74c6f8f 100644 (file)
@@ -59,9 +59,10 @@ class ApiMove extends ApiBase {
                }
                $toTalk = $toTitle->getTalkPageIfDefined();
 
+               $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
                if ( $toTitle->getNamespace() == NS_FILE
-                       && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
-                       && MediaWikiServices::getInstance()->getRepoGroup()->findFile( $toTitle )
+                       && !$repoGroup->getLocalRepo()->findFile( $toTitle )
+                       && $repoGroup->findFile( $toTitle )
                ) {
                        if ( !$params['ignorewarnings'] &&
                                 $this->getPermissionManager()->userHasRight( $user, 'reupload-shared' ) ) {
index 941d108..6da362d 100644 (file)
@@ -305,8 +305,9 @@ class MovePageForm extends UnlistedSpecialPage {
                // mediawiki.special.movePage module
 
                $immovableNamespaces = [];
+               $namespaceInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
                foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) {
-                       if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->isMovable( $nsId ) ) {
+                       if ( !$namespaceInfo->isMovable( $nsId ) ) {
                                $immovableNamespaces[] = $nsId;
                        }
                }
@@ -534,11 +535,14 @@ class MovePageForm extends UnlistedSpecialPage {
                        return;
                }
 
+               $services = MediaWikiServices::getInstance();
+
                # Show a warning if the target file exists on a shared repo
+               $repoGroup = $services->getRepoGroup();
                if ( $nt->getNamespace() == NS_FILE
                        && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) )
-                       && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
-                       && MediaWikiServices::getInstance()->getRepoGroup()->findFile( $nt )
+                       && !$repoGroup->getLocalRepo()->findFile( $nt )
+                       && $repoGroup->findFile( $nt )
                ) {
                        $this->showForm( [ [ 'file-exists-sharedrepo' ] ] );
 
@@ -568,8 +572,7 @@ class MovePageForm extends UnlistedSpecialPage {
 
                        // Delete an associated image if there is
                        if ( $nt->getNamespace() == NS_FILE ) {
-                               $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
-                                       ->newFile( $nt );
+                               $file = $repoGroup->getLocalRepo()->newFile( $nt );
                                $file->load( File::READ_LATEST );
                                if ( $file->exists() ) {
                                        $file->delete( $reason, false, $user );
@@ -604,7 +607,7 @@ class MovePageForm extends UnlistedSpecialPage {
                        $this->moveTalk = false;
                }
                if ( $this->moveSubpages ) {
-                       $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+                       $permissionManager = $services->getPermissionManager();
                        $this->moveSubpages = $permissionManager->userCan( 'move-subpages', $user, $ot );
                }
 
@@ -670,7 +673,7 @@ class MovePageForm extends UnlistedSpecialPage {
                 */
 
                // @todo FIXME: Use Title::moveSubpages() here
-               $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
+               $nsInfo = $services->getNamespaceInfo();
                $dbr = wfGetDB( DB_MASTER );
                if ( $this->moveSubpages && (
                        $nsInfo->hasSubpages( $nt->getNamespace() ) || (