private function getTermRows(EntityId $id, $termType, $terms)
 {
     $rows = array();
     foreach ($terms as $lang => $langTerms) {
         $langTerms = (array) $langTerms;
         foreach ($langTerms as $term) {
             $rows[] = array($id->getEntityType(), $id->getNumericId(), $termType, $lang, $term, $term);
         }
     }
     return $rows;
 }
 /**
  * @see TermIndex::deleteTermsOfEntity
  *
  * @since 0.5
  *
  * @param EntityId $entityId
  *
  * @return bool Success indicator
  */
 public function deleteTermsOfEntity(EntityId $entityId)
 {
     $dbw = $this->getConnection(DB_MASTER);
     $success = $dbw->delete($this->tableName, array('term_entity_id' => $entityId->getNumericId(), 'term_entity_type' => $entityId->getEntityType()), __METHOD__);
     // NOTE: if we fail to delete some labels, it may not be possible to use those labels
     // for other entities, without any way to remove them from the database.
     // We probably want some extra handling here.
     return $success;
 }
 /**
  * @param string $text
  * @param string $languageCode
  * @param string $termType
  * @param EntityId|ItemId|PropertyId $entityId
  *
  * @return TermIndexEntry
  */
 private function getTermIndexEntry($text, $languageCode, $termType, EntityId $entityId)
 {
     return new TermIndexEntry(array('termText' => $text, 'termLanguage' => $languageCode, 'termType' => $termType, 'entityId' => $entityId->getNumericId(), 'entityType' => $entityId->getEntityType()));
 }
 private function getPageId(EntityId $entityId)
 {
     $dbr = wfGetDB(DB_SLAVE);
     $row = $dbr->selectRow('wb_entity_per_page', array('epp_page_id'), array('epp_entity_type' => $entityId->getEntityType(), 'epp_entity_id' => $entityId->getNumericId()), __METHOD__);
     if (!$row) {
         return false;
     }
     return $pageId = (int) $row->epp_page_id;
 }
 /**
  * @see EntityPerPage::listEntities
  *
  * @param null|string $entityType The entity type to look for.
  * @param int $limit The maximum number of IDs to return.
  * @param EntityId $after Only return entities with IDs greater than this.
  * @param mixed $redirects A XXX_REDIRECTS constant (default is NO_REDIRECTS).
  *
  * @throws InvalidArgumentException
  * @return EntityId[]
  */
 public function listEntities($entityType, $limit, EntityId $after = null, $redirects = self::NO_REDIRECTS)
 {
     if ($entityType === null) {
         $where = array();
         //NOTE: needs to be id/type, not type/id, according to the definition of the relevant
         //      index in wikibase.sql: wb_entity_per_page (epp_entity_id, epp_entity_type);
         $orderBy = array('epp_entity_id', 'epp_entity_type');
     } elseif (!is_string($entityType)) {
         throw new InvalidArgumentException('$entityType must be a string (or null)');
     } else {
         $where = array('epp_entity_type' => $entityType);
         // NOTE: If the type is fixed, don't use the type in the order;
         // before changing this, check index usage.
         $orderBy = array('epp_entity_id');
     }
     if ($redirects === self::NO_REDIRECTS) {
         $where[] = 'epp_redirect_target IS NULL';
     } elseif ($redirects === self::ONLY_REDIRECTS) {
         $where[] = 'epp_redirect_target IS NOT NULL';
     }
     if (!is_int($limit) || $limit < 1) {
         throw new InvalidArgumentException('$limit must be a positive integer');
     }
     $dbr = wfGetDB(DB_SLAVE);
     if ($after) {
         $numericId = (int) $after->getNumericId();
         if ($entityType === null) {
             // Ugly. About time we switch to qualified, string based IDs!
             // NOTE: this must be consistent with the sort order, see above!
             $where[] = '( ( epp_entity_type > ' . $dbr->addQuotes($after->getEntityType()) . ' AND epp_entity_id = ' . $numericId . ' )' . ' OR epp_entity_id > ' . $numericId . ' )';
         } else {
             $where[] = 'epp_entity_id > ' . $numericId;
         }
     }
     $rows = $dbr->select('wb_entity_per_page', array('entity_type' => 'epp_entity_type', 'entity_id' => 'epp_entity_id'), $where, __METHOD__, array('ORDER BY' => $orderBy, 'USE INDEX' => 'wb_epp_entity', 'LIMIT' => $limit));
     $ids = $this->getEntityIdsFromRows($rows);
     return $ids;
 }
 /**
  * @param EntityId $entityId
  * @param array[] $termGroups
  *
  * @return TermIndexEntry[]
  */
 private function makeTermsFromGroups(EntityId $entityId, array $termGroups)
 {
     $terms = array();
     foreach ($termGroups as $type => $group) {
         foreach ($group as $lang => $text) {
             $terms[] = new TermIndexEntry(array('termType' => $type, 'termLanguage' => $lang, 'termText' => $text, 'entityType' => $entityId->getEntityType(), 'entityId' => $entityId->getNumericId()));
         }
     }
     return $terms;
 }
 /**
  * Updates the $entityInfo structure and makes the ID
  * available via the $numericIdsByType and $entityIds caches.
  *
  * @param EntityId $id
  */
 private function updateEntityInfo(EntityId $id)
 {
     $type = $id->getEntityType();
     $key = $id->getSerialization();
     // NOTE: we assume that the type of entity never changes.
     $this->initEntityInfo($key, array('type' => $type));
     $this->entityIds[$key] = $id;
     $this->entityInfo[$key]['id'] = $key;
     // FIXME: this will fail for IDs that do not have a numeric form
     $this->numericIdsByType[$type][$key] = $id->getNumericId();
 }