/** * Check all applicable permissions for redirecting the given $entityId. * * @param EntityId $entityId */ private function checkPermissions(EntityId $entityId) { $permissions = array('edit', $entityId->getEntityType() . '-merge'); foreach ($permissions as $permission) { $this->checkPermission($entityId, $permission); } }
/** * @param EntityId $entityId * @param EntityId $targetId * * @throws InvalidArgumentException */ public function __construct(EntityId $entityId, EntityId $targetId) { if ($entityId->getEntityType() !== $targetId->getEntityType()) { throw new InvalidArgumentException('$entityId and $targetId must refer to the same kind of entity.'); } $this->entityId = $entityId; $this->targetId = $targetId; }
/** * @see EntityDocumentLookup::getEntityDocumentForId */ public function getEntityDocumentForId(EntityId $entityId) { $document = $this->database->selectCollection($entityId->getEntityType())->findOne($this->buildGetEntityForIdQuery($entityId)); if ($document === null) { throw new EntityNotFoundException($entityId); } return $this->documentBuilder->buildEntityForDocument($document); }
public function getTitleForId(EntityId $entityId) { switch ($entityId->getEntityType()) { case Item::ENTITY_TYPE: return Title::makeTitle(NS_MAIN, 'ITEM-TEST--' . $entityId->getSerialization()); case Property::ENTITY_TYPE: return Title::makeTitle(NS_MAIN, 'PROPERTY-TEST--' . $entityId->getSerialization()); default: throw new LogicException("oops!"); } }
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; }
/** * @since 0.5 * * @param string $action The action name * @param EntityId $entityId * @param array $fields additional fields to set * * @return EntityChange */ public function newForEntity($action, EntityId $entityId, array $fields = array()) { $entityType = $entityId->getEntityType(); if (isset($this->changeClasses[$entityType])) { $class = $this->changeClasses[$entityType]; } else { $class = '\\Wikibase\\EntityChange'; } /** @var EntityChange $instance */ $instance = new $class($fields); if (!$instance->hasField('object_id')) { $instance->setField('object_id', $entityId->getSerialization()); } if (!$instance->hasField('info')) { $instance->setField('info', array()); } // Note: the change type determines how the client will // instantiate and handle the change $type = 'wikibase-' . $entityId->getEntityType() . '~' . $action; $instance->setField('type', $type); return $instance; }
/** * @param EntityId $id * * @throws InvalidArgumentException * @return Item|Property */ protected function makeEntity(EntityId $id) { if ($id instanceof ItemId) { $entity = new Item($id); $entity->getSiteLinkList()->addNewSiteLink('test', 'Foo'); } elseif ($id instanceof PropertyId) { $entity = new Property($id, null, 'wibblywobbly'); } else { throw new InvalidArgumentException('Unsupported entity type ' . $id->getEntityType()); } $entity->setLabel('en', 'label:' . $id->getSerialization()); return $entity; }
/** * @param string $action * @param EntityId $entityId * @param Diff $diff * @param array $fields * * @return EntityChange */ private function newEntityChange($action, EntityId $entityId, Diff $diff, array $fields) { /** @var EntityChange $instance */ $instance = new ItemChange($fields); if (!$instance->hasField('object_id')) { $instance->setField('object_id', $entityId->getSerialization()); } if (!$instance->hasField('info')) { $instance->setField('info', array()); } // Note: the change type determines how the client will // instantiate and handle the change $type = 'wikibase-' . $entityId->getEntityType() . '~' . $action; $instance->setField('type', $type); $instance->setDiff($diff); return $instance; }
/** * @param EntityId $fromId * @param EntityId $toId * * @throws RedirectCreationException */ private function checkCompatible(EntityId $fromId, EntityId $toId) { if ($fromId->getEntityType() !== $toId->getEntityType()) { throw new RedirectCreationException('Incompatible entity types', 'target-is-incompatible'); } }
/** * @return string */ public function getType() { return $this->id->getEntityType(); }
/** * @param EntityId $entityId * * @return Title */ public function getTitleForId(EntityId $entityId) { $name = $entityId->getEntityType() . ':' . $entityId->getSerialization(); return Title::makeTitle(NS_MAIN, $name); }
/** * @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 EntityDocumentLookup::getEntityDocumentForId */ public function getEntityDocumentForId(EntityId $entityId) { $document = $this->database->selectCollection($entityId->getEntityType())->findOne($this->buildGetEntityForIdQuery($entityId)); return $document === null ? null : $this->documentBuilder->buildEntityForDocument($document); }
/** * @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; }
/** * @param EntityId $entityId * * @return string HTML */ private function getHtmlForNonExistent(EntityId $entityId) { $attributes = array('class' => 'wb-entity-undefinedinfo'); $message = wfMessage('parentheses', wfMessage('wikibase-deletedentity-' . $entityId->getEntityType())->text()); $undefinedInfo = Html::element('span', $attributes, $message); $separator = wfMessage('word-separator')->text(); return $entityId->getSerialization() . $separator . $undefinedInfo; }
private function idMatchesType(EntityId $entityId) { return $this->entityType === null || $entityId->getEntityType() === $this->entityType; }
/** * @param EntityId $entityId * * @return string */ public function getEntityNamespace(EntityId $entityId) { $entityType = $entityId->getEntityType(); return $this->getNamespace($entityType); }
/** * @param int $changeId * @param EntityId $entityId * @param string $time * @param Diff|null $siteLinkDiff * * @return Change */ private function newChange($changeId, EntityId $entityId, $time, Diff $siteLinkDiff = null) { $changeClass = $entityId->getEntityType() === Item::ENTITY_TYPE ? 'Wikibase\\ItemChange' : 'Wikibase\\EntityChange'; $change = $this->getMockBuilder($changeClass)->disableOriginalConstructor()->getMock(); $change->expects($this->never())->method('getType'); $change->expects($this->never())->method('getUser'); $change->expects($this->any())->method('isEmpty')->will($this->returnValue(false)); $change->expects($this->any())->method('getTime')->will($this->returnValue($time)); $change->expects($this->any())->method('getAge')->will($this->returnValue((int) wfTimestamp(TS_UNIX, $time) - (int) wfTimestamp(TS_UNIX, $this->now))); $change->expects($this->any())->method('getId')->will($this->returnValue($changeId)); $change->expects($this->any())->method('getObjectId')->will($this->returnValue($entityId->getSerialization())); $change->expects($this->any())->method('getEntityId')->will($this->returnValue($entityId)); $change->expects($this->any())->method('getSiteLinkDiff')->will($this->returnValue($siteLinkDiff)); return $change; }
/** * Returns the Title object for the item with provided id. * * @since 0.3 * * @param EntityId $id * * @throws MWException * @throws OutOfBoundsException * @throws InvalidArgumentException * @return Title */ public function getTitleForId(EntityId $id) { $handler = $this->getContentHandlerForType($id->getEntityType()); return $handler->getTitleForId($id); }
/** * @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; }
/** * Get serialized information for the EntityId and add them to result * * @param EntityId $entityId * @param string|array|null $path * * @since 0.5 */ public function addBasicEntityInformation(EntityId $entityId, $path) { $this->setValue($path, 'id', $entityId->getSerialization()); $this->setValue($path, 'type', $entityId->getEntityType()); }
/** * Returns the appropriate page Title for the given EntityId. * * @warn This should not really be needed and may just go away! * * @since 0.5 * * @see EntityTitleLookup::getTitleForId * * @param EntityId $id * * @throws InvalidArgumentException if $id refers to an entity of the wrong type. * @return Title */ public function getTitleForId(EntityId $id) { if ($id->getEntityType() !== $this->getEntityType()) { throw new InvalidArgumentException('The given ID does not refer to an entity of type ' . $this->getEntityType()); } return Title::makeTitle($this->getEntityNamespace(), $id->getSerialization()); }
/** * 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(); }