/**
  * Setting the sitepage of the entity.
  *
  * @param Item $item
  * @param string $siteId
  * @param string $pageName
  * @param string[] $badgeIds
  * @param Summary &$summary The summary for this edit will be saved here.
  *
  * @return Status
  */
 private function setSiteLink(Item $item, $siteId, $pageName, $badgeIds, &$summary)
 {
     $status = Status::newGood();
     $site = $this->siteStore->getSite($siteId);
     if ($site === null) {
         $status->fatal('wikibase-setsitelink-invalid-site', $siteId);
         return $status;
     }
     $summary = new Summary('wbsetsitelink');
     // when $pageName is an empty string, we want to remove the site link
     if ($pageName === '') {
         if (!$item->hasLinkToSite($siteId)) {
             $status->fatal('wikibase-setsitelink-remove-failed');
             return $status;
         }
     } else {
         $pageName = $site->normalizePageName($pageName);
         if ($pageName === false) {
             $status->fatal('wikibase-error-ui-no-external-page');
             return $status;
         }
     }
     $badges = $this->parseBadges($badgeIds, $status);
     if (!$status->isGood()) {
         return $status;
     }
     $changeOp = $this->siteLinkChangeOpFactory->newSetSiteLinkOp($siteId, $pageName, $badges);
     $this->applyChangeOp($changeOp, $item, $summary);
     return $status;
 }
 /**
  * @param array[] $siteLinks
  * @param Item $item
  *
  * @return ChangeOp[]
  */
 private function getSiteLinksChangeOps(array $siteLinks, Item $item)
 {
     $siteLinksChangeOps = array();
     $sites = $this->siteLinkTargetProvider->getSiteList($this->siteLinkGroups);
     foreach ($siteLinks as $siteId => $arg) {
         $this->checkSiteLinks($arg, $siteId, $sites);
         $globalSiteId = $arg['site'];
         if (!$sites->hasSite($globalSiteId)) {
             $this->errorReporter->dieError("There is no site for global site id '{$globalSiteId}'", 'no-such-site');
         }
         $linkSite = $sites->getSite($globalSiteId);
         $shouldRemove = array_key_exists('remove', $arg) || !isset($arg['title']) && !isset($arg['badges']) || isset($arg['title']) && $arg['title'] === '';
         if ($shouldRemove) {
             $siteLinksChangeOps[] = $this->siteLinkChangeOpFactory->newRemoveSiteLinkOp($globalSiteId);
         } else {
             $badges = isset($arg['badges']) ? $this->parseSiteLinkBadges($arg['badges']) : null;
             if (isset($arg['title'])) {
                 $linkPage = $linkSite->normalizePageName($this->stringNormalizer->trimWhitespace($arg['title']));
                 if ($linkPage === false) {
                     $this->errorReporter->dieMessage('no-external-page', $globalSiteId, $arg['title']);
                 }
             } else {
                 $linkPage = null;
                 if (!$item->getSiteLinkList()->hasLinkWithSiteId($globalSiteId)) {
                     $this->errorReporter->dieMessage('no-such-sitelink', $globalSiteId);
                 }
             }
             $siteLinksChangeOps[] = $this->siteLinkChangeOpFactory->newSetSiteLinkOp($globalSiteId, $linkPage, $badges);
         }
     }
     return $siteLinksChangeOps;
 }
 /**
  * @param array $params
  *
  * @return ChangeOpSiteLink
  */
 private function getChangeOp(array $params)
 {
     if ($this->shouldRemove($params)) {
         $linksite = $this->stringNormalizer->trimToNFC($params['linksite']);
         return $this->siteLinkChangeOpFactory->newRemoveSiteLinkOp($linksite);
     } else {
         $linksite = $this->stringNormalizer->trimToNFC($params['linksite']);
         $sites = $this->siteLinkTargetProvider->getSiteList($this->siteLinkGroups);
         $site = $sites->getSite($linksite);
         if ($site === false) {
             $this->errorReporter->dieError('The supplied site identifier was not recognized', 'not-recognized-siteid');
         }
         if (isset($params['linktitle'])) {
             $page = $site->normalizePageName($this->stringNormalizer->trimWhitespace($params['linktitle']));
             if ($page === false) {
                 $this->errorReporter->dieMessage('no-external-page', $linksite, $params['linktitle']);
             }
         } else {
             $page = null;
         }
         $badges = isset($params['badges']) ? $this->parseSiteLinkBadges($params['badges']) : null;
         return $this->siteLinkChangeOpFactory->newSetSiteLinkOp($linksite, $page, $badges);
     }
 }