/**
  * @see SearchIndexField::getFieldData
  *
  * @param EntityDocument $entity
  *
  * @return int
  */
 public function getFieldData(EntityDocument $entity)
 {
     if ($entity instanceof Item) {
         return $entity->getSiteLinkList()->count();
     }
     return 0;
 }
 /**
  * @param EntityDocument $entity
  */
 public function processEntity(EntityDocument $entity)
 {
     if ($entity instanceof StatementListProvider && $this->statementDataUpdaters) {
         $this->processStatements($entity->getStatements());
     }
     if ($entity instanceof Item && $this->siteLinkDataUpdaters) {
         $this->processSiteLinks($entity->getSiteLinkList());
     }
 }
 /**
  * Calculate a weight the given entity to be used for ranking. Should be normalized
  * between 0 and 1, but that's not a strong constraint.
  * This implementation uses the max of the number of labels and the number of sitelinks.
  *
  * TODO Should be moved to its own object and be added via dependency injection
  *
  * @param EntityDocument $entity
  *
  * @return float weight
  */
 private function getWeight(EntityDocument $entity)
 {
     // FIXME: OCP violation. No support for new types of entities can be registered
     $weight = 0.0;
     if ($entity instanceof FingerprintProvider) {
         $weight = max($weight, $entity->getFingerprint()->getLabels()->count() / 1000.0);
     }
     if ($entity instanceof Item) {
         $weight = max($weight, $entity->getSiteLinkList()->count() / 1000.0);
     }
     return $weight;
 }
 /**
  * @param EntityDocument $entity
  */
 private function buildResult(EntityDocument $entity)
 {
     $builder = $this->getResultBuilder();
     if ($entity instanceof FingerprintProvider) {
         $fingerprint = $entity->getFingerprint();
         $builder->addLabels($fingerprint->getLabels(), 'entity');
         $builder->addDescriptions($fingerprint->getDescriptions(), 'entity');
         $builder->addAliasGroupList($fingerprint->getAliasGroups(), 'entity');
     }
     if ($entity instanceof Item) {
         $builder->addSiteLinkList($entity->getSiteLinkList(), 'entity');
     }
     if ($entity instanceof StatementListProvider) {
         $builder->addStatements($entity->getStatements(), 'entity');
     }
 }
 /**
  * @see ChangeOp::apply
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof Item) {
         throw new InvalidArgumentException('ChangeOpSiteLink can only be applied to Item instances');
     }
     $siteLinks = $entity->getSiteLinkList();
     if ($this->pageName === null && $this->badges === null || $this->pageName === '') {
         if ($siteLinks->hasLinkWithSiteId($this->siteId)) {
             $this->updateSummary($summary, 'remove', $this->siteId, $siteLinks->getBySiteId($this->siteId)->getPageName());
             $siteLinks->removeLinkWithSiteId($this->siteId);
         } else {
             //TODO: throw error, or ignore silently?
         }
     } else {
         $commentArgs = array();
         if ($this->pageName === null) {
             if (!$siteLinks->hasLinkWithSiteId($this->siteId)) {
                 throw new InvalidArgumentException('The sitelink does not exist');
             }
             // If page name is not set (but badges are) make sure that it remains intact
             $pageName = $siteLinks->getBySiteId($this->siteId)->getPageName();
         } else {
             $pageName = $this->pageName;
             $commentArgs[] = $pageName;
         }
         $action = $siteLinks->hasLinkWithSiteId($this->siteId) ? 'set' : 'add';
         $badges = $this->applyBadges($siteLinks, $action, $commentArgs);
         $this->updateSummary($summary, $action, $this->siteId, $commentArgs);
         // FIXME: Use SiteLinkList::setNewSiteLink.
         $siteLinks->removeLinkWithSiteId($this->siteId);
         $siteLinks->addNewSiteLink($this->siteId, $pageName, $badges);
     }
 }