Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/87544


Change subject: Refactor LinkTitles api module execute method
......................................................................

Refactor LinkTitles api module execute method

Change-Id: I22f0a65ae7f38e2e11be465665cc4c9a08a7a328
---
M repo/includes/api/LinkTitles.php
1 file changed, 47 insertions(+), 24 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/44/87544/1

diff --git a/repo/includes/api/LinkTitles.php b/repo/includes/api/LinkTitles.php
index 397ea31..58b5947 100644
--- a/repo/includes/api/LinkTitles.php
+++ b/repo/includes/api/LinkTitles.php
@@ -18,7 +18,6 @@
  * Requires API write mode to be enabled.
  *
  * @since 0.1
- *
  * @licence GNU GPL v2+
  */
 class LinkTitles extends ApiWikibase {
@@ -28,10 +27,10 @@
         */
        protected function getRequiredPermissions( EntityContent 
$entityContent, array $params ) {
                $permissions = parent::getRequiredPermissions( $entityContent, 
$params );
-
                $permissions[] = 'linktitles-update';
                return $permissions;
        }
+
 
        /**
         * Main method. Does the actual work and sets the result.
@@ -44,20 +43,18 @@
                $params = $this->extractRequestParams();
                $this->validateParameters( $params );
 
+               // Sites are already tested through allowed params ;)
                $sites = $this->getSiteLinkTargetSites();
-
-               // Site is already tested through allowed params ;)
                $fromSite = $sites->getSite( $params['fromsite'] );
-               $fromPage = $fromSite->normalizePageName( $params['fromtitle'] 
);
-               $this->validatePage( $fromPage, 'from' );
-               $fromId = 
StoreFactory::getStore()->newSiteLinkCache()->getItemIdForLink( 
$params['fromsite'], $fromPage );
-
-               // Site is already tested through allowed params ;)
                $toSite = $sites->getSite( $params['tosite'] );
-               // This must be tested now
+
+               $fromPage = $fromSite->normalizePageName( $params['fromtitle'] 
);
                $toPage = $toSite->normalizePageName( $params['totitle'] );
-               $this->validatePage( $toPage, 'to' );
-               $toId = 
StoreFactory::getStore()->newSiteLinkCache()->getItemIdForLink( 
$params['tosite'], $toPage );
+               $this->validatePages( $fromPage, $toPage );
+
+               $siteLinkCache = StoreFactory::getStore()->newSiteLinkCache();
+               $fromId = $siteLinkCache->getItemIdForLink( 
$params['fromsite'], $fromPage );
+               $toId = $siteLinkCache->getItemIdForLink( $params['tosite'], 
$toPage );
 
                $return = array();
                $flags = 0;
@@ -112,21 +109,55 @@
                }
 
                $this->addSiteLinksToResult( $return, 'entity' );
+               $status = $this->getAttemptSaveStatus( $itemContent, $summary, 
$flags );
+               $this->buildResult( $itemContent, $status );
+               wfProfileOut( __METHOD__ );
+       }
 
+       /**
+        * @param string $fromPage
+        * @param string $toPage
+        */
+       private function validatePages( $fromPage, $toPage ){
+               $this->validatePage( $fromPage, 'from' );
+               $this->validatePage( $toPage, 'to' );
+       }
+
+       /**
+        * @param string $page
+        * @param string $label
+        */
+       private function validatePage( $page, $label ) {
+               if ( $page === false ) {
+                       $this->dieUsage(
+                               "The external client site did not provide page 
information for the {$label} page" ,
+                               'no-external-page'
+                       );
+               }
+       }
+
+       /**
+        * @param ItemContent $itemContent
+        * @param Summary $summary
+        * @param int $flags
+        * @return Status
+        */
+       private function getAttemptSaveStatus( ItemContent $itemContent, 
Summary $summary, $flags ) {
                if ( $itemContent === null ) {
                        // to not have an ItemContent isn't really bad at this 
point
-                       $status = Status::newGood( true );
+                       return Status::newGood( true );
                }
                else {
                        // Do the actual save, or if it don't exist yet create 
it.
-                       $status = $this->attemptSaveEntity( $itemContent,
+                       return $this->attemptSaveEntity( $itemContent,
                                $summary->toString(),
                                $flags );
-
-                       $this->addRevisionIdFromStatusToResult( 'entity', 
'lastrevid', $status );
                }
+       }
 
+       private function buildResult( ItemContent $itemContent, Status $status 
) {
                if ( $itemContent !== null ) {
+                       $this->addRevisionIdFromStatusToResult( 'entity', 
'lastrevid', $status );
                        $this->getResult()->addValue(
                                'entity',
                                'id', 
$itemContent->getItem()->getId()->getNumericId()
@@ -142,8 +173,6 @@
                        'success',
                        (int)$status->isOK()
                );
-
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -256,12 +285,6 @@
                        
'api.php?action=wblinktitles&fromsite=enwiki&fromtitle=Hydrogen&tosite=dewiki&totitle=Wasserstoff'
                        => 'Add a link "Hydrogen" from the English page to 
"Wasserstoff" at the German page',
                );
-       }
-
-       private function validatePage( $page, $label ) {
-               if ( $page === false ) {
-                       $this->dieUsage( "The external client site did not 
provide page information for the {$label} page" , 'no-external-page' );
-               }
        }
 
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/87544
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I22f0a65ae7f38e2e11be465665cc4c9a08a7a328
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to