/** * @param PropertyId $propertyId * * @throws PropertyDataTypeLookupException */ private function verifyDataTypeIsSet(PropertyId $propertyId) { $numericId = $propertyId->getSerialization(); if (!array_key_exists($numericId, $this->dataTypeIds)) { throw new PropertyDataTypeLookupException($propertyId, "The DataType for property '{$numericId}' is not set"); } }
/** * @param PropertyId $propertyId * * @return string * @throws PropertyDataTypeLookupException */ public function getDataTypeIdForProperty(PropertyId $propertyId) { $serializedId = $propertyId->getSerialization(); if (!array_key_exists($serializedId, $this->propertyIds)) { $this->propertyIds[$serializedId] = $this->lookup->getDataTypeIdFOrProperty($propertyId); } return $this->propertyIds[$serializedId]; }
public function getPropertyEntityIdValueMatches(PropertyId $propertyId, EntityId $valueId) { $propertyText = $propertyId->getSerialization(); $valueText = $valueId->getSerialization(); $queryBuilder = new QueryBuilder($this->config->get('WikidataQueryPrefixes')); $queryBuilder->select('?id')->where("?id", "wdt:{$propertyText}", "wd:{$valueText}"); $queryExecuter = new QueryExecuter($this->config->get('WikidataQueryUrl')); $results = $queryExecuter->execute($queryBuilder->getSPARQL()); return $this->parseResults($results); }
private function getPropertyJson(string $id) : array { // TODO: handle id exception // https://groups.google.com/forum/#!topic/clean-code-discussion/GcQNqWG_fuo $id = new PropertyId($id); $propertyRow = $this->propertyStore->getPropertyRowByNumericPropertyId($id->getNumericId()); if ($propertyRow === null) { throw new NoNullableReturnTypesException(); } return json_decode($propertyRow->getPropertyJson(), true); }
public function getEntityTitleProvider() { $itemId = ItemId::newFromNumber(388); $propertyId = PropertyId::newFromNumber(472); $settings = $this->getRepoSettings(); return array(array('Q388', $settings[0], $itemId), array('Item:Q388', $settings[2], $itemId), array('Property:P472', $settings[0], $propertyId)); }
/** * @param int $propertyId * @param string|null $guid * @param int $rank * * @return Statement */ private function getStatement($propertyId, $guid, $rank = Statement::RANK_NORMAL) { $statement = $this->getMockBuilder('Wikibase\\DataModel\\Statement\\Statement')->disableOriginalConstructor()->getMock(); $statement->expects($this->any())->method('getGuid')->will($this->returnValue($guid)); $statement->expects($this->any())->method('getPropertyId')->will($this->returnValue(PropertyId::newFromNumber($propertyId))); $statement->expects($this->any())->method('getRank')->will($this->returnValue($rank)); return $statement; }
/** * @param string $entityType * @param int|float|string $numericId * * @return EntityId * @throws InvalidArgumentException */ public static function newIdFromTypeAndNumber($entityType, $numericId) { if ($entityType === 'item') { return ItemId::newFromNumber($numericId); } elseif ($entityType === 'property') { return PropertyId::newFromNumber($numericId); } throw new InvalidArgumentException('Invalid entityType ' . $entityType); }
/** * Can be integer since 0.1. * Can be PropertyId since 0.5. * Can be null since 1.0. * * @param PropertyId|int|null $id * * @throws InvalidArgumentException */ public function setId($id) { if ($id === null || $id instanceof PropertyId) { $this->id = $id; } elseif (is_int($id)) { $this->id = PropertyId::newFromNumber($id); } else { throw new InvalidArgumentException('$id must be an instance of PropertyId, an integer,' . ' or null'); } }
/** * @param Snak $snak * @param PropertyId $propertyId * @param int $rank */ private function processSnak(Snak $snak, PropertyId $propertyId, $rank = Statement::RANK_NORMAL) { $id = $propertyId->getSerialization(); $fileName = str_replace(' ', '_', $this->getString($snak)); if ($fileName === null || $fileName === '') { return; } if (!$this->isAcceptableRank($rank)) { return; } if (!$this->isAcceptablePriority($id)) { return; } if ($this->isSamePriority($id) && !$this->isBetterRank($rank)) { return; } $this->bestProperty = $id; $this->bestRank = $rank; $this->bestFileName = $fileName; }
/** * @dataProvider newSnakProvider */ public function testNewSnak($propertyId, $snakType, $rawValue, $expectedSnakClass, $expectedException = null) { if (is_int($propertyId)) { $propertyId = PropertyId::newFromNumber($propertyId); } if ($expectedException !== null) { $this->setExpectedException($expectedException); } $service = $this->newSnakConstructionService(); $snak = $service->newSnak($propertyId, $snakType, $rawValue); $this->assertInstanceOf($expectedSnakClass, $snak); }
public function testFilterSuggestions() { $p7 = PropertyId::newFromNumber(7); $p10 = PropertyId::newFromNumber(10); $p12 = PropertyId::newFromNumber(12); $p15 = PropertyId::newFromNumber(15); $p23 = PropertyId::newFromNumber(23); $suggestions = array(new Suggestion($p12, 0.9), new Suggestion($p23, 0.8), new Suggestion($p7, 0.7), new Suggestion($p15, 0.6)); $resultSize = 2; $this->termIndex->expects($this->any())->method('getTopMatchingTerms')->will($this->returnValue($this->getTermIndexEntryArrayWithIds(array($p7, $p10, $p15, $p12)))); $result = $this->suggestionGenerator->filterSuggestions($suggestions, 'foo', 'en', $resultSize); $this->assertEquals(array($suggestions[0], $suggestions[2]), $result); }
/** * Construct mock repository matching the test data. * * @return MockRepository */ public function getMockRepository() { static $repo; if (!empty($repo)) { return $repo; } $repo = new MockRepository(); foreach (self::getTestProperties() as $prop) { list($id, $type) = $prop; $fingerprint = new Fingerprint(); $fingerprint->setLabel('en', "Property{$id}"); $entity = new Property(PropertyId::newFromNumber($id), $fingerprint, $type); $repo->putEntity($entity); } $q42 = new ItemId('Q42'); $fingerprint = new Fingerprint(); $fingerprint->setLabel('en', 'Item42'); $entity = new Item($q42, $fingerprint); $repo->putEntity($entity); $repo->putRedirect(new EntityRedirect(new ItemId('Q4242'), $q42)); return $repo; }
/** * @param integer $offset Start to include at number of entries from the start title * @param integer $limit Stop at number of entries after start of inclusion * * @return PropertyId[] */ protected function getResult($offset = 0, $limit = 0) { $propertyInfo = array_slice($this->getPropertyInfo(), $offset, $limit, true); $propertyIds = array(); foreach ($propertyInfo as $numericId => $info) { $propertyIds[] = PropertyId::newFromNumber($numericId); } $this->bufferingTermLookup->prefetchTerms($propertyIds); return $propertyIds; }
/** * @see Serializable::unserialize * * @since 0.1 * * @param string $serialized */ public function unserialize($serialized) { $this->propertyId = PropertyId::newFromNumber(unserialize($serialized)); }
/** * @see PropertyInfoStore::removePropertyInfo * * @param PropertyId $propertyId * * @throws DBError * @throws InvalidArgumentException * @return bool */ public function removePropertyInfo(PropertyId $propertyId) { if ($this->isReadonly) { throw new DBError('Cannot write when in readonly mode'); } $dbw = $this->getConnection(DB_MASTER); $dbw->delete($this->tableName, array('pi_property_id' => $propertyId->getNumericId()), __METHOD__); $c = $dbw->affectedRows(); $this->releaseConnection($dbw); return $c > 0; }
private function propertyInfoToPropertyListElement(PropertyInfo $propertyInfo) : PropertyListElement { $id = PropertyId::newFromNumber($propertyInfo->getNumericPropertyId()); return new PropertyListElement($id, $propertyInfo->getPropertyType(), $this->urlBuilder->getWdEntityUrl($id), $this->urlBuilder->getApiPropertyUrl($id)); }
/** * Rebuild the property info entries. * Use the rebuildPropertyInfo.php maintenance script to invoke this from the command line. * * Database updates a batched into multiple transactions. Do not call this * method within an (explicit) database transaction. * * @since 0.4 */ public function rebuildPropertyInfo() { $dbw = $this->propertyInfoTable->getWriteConnection(); $rowId = $this->fromId - 1; $total = 0; $join = array(); $tables = array('wb_entity_per_page'); if (!$this->shouldUpdateAllEntities) { // Find properties in wb_entity_per_page with no corresponding // entry in wb_property_info. $piTable = $this->propertyInfoTable->getTableName(); $tables[] = $piTable; $join[$piTable] = array('LEFT JOIN', array('pi_property_id = epp_entity_id')); } while (true) { // Make sure we are not running too far ahead of the slaves, // as that would cause the site to be rendered read only. wfWaitForSlaves(); if ($this->useTransactions) { $dbw->begin(); } //FIXME: use an EntityIdPager from EntityPerPage $props = $dbw->select($tables, array('epp_entity_id'), array('epp_entity_type = ' . $dbw->addQuotes(Property::ENTITY_TYPE), 'epp_entity_id > ' . (int) $rowId, 'epp_redirect_target IS NULL', $this->shouldUpdateAllEntities ? '1' : 'pi_property_id IS NULL'), __METHOD__, array('LIMIT' => $this->batchSize, 'ORDER BY' => 'epp_entity_id ASC', 'FOR UPDATE'), $join); $c = 0; foreach ($props as $row) { $id = PropertyId::newFromNumber((int) $row->epp_entity_id); $this->updatePropertyInfo($id); $rowId = $row->epp_entity_id; $c++; } if ($this->useTransactions) { $dbw->commit(); } $this->reportMessage("Updated {$c} properties, up to ID {$rowId}."); $total += $c; if ($c < $this->batchSize) { // we are done. break; } } return $total; }
private function buildTimeValueForSearch(PropertyId $propertyId, TimeValue $timeValue) { $significantTimePart = preg_replace('/(-00)*T00:00:00Z$/', '', $timeValue->getTime()); return new MongoRegex('/^' . preg_quote($propertyId->getSerialization() . '-' . $significantTimePart, '/') . '/'); }
private function getSerialization(EntityDocument $entity, PropertyId $propertyId) { return array('id' => $entity->getId()->getSerialization(), 'type' => $entity->getType(), 'labels' => array('de' => array('language' => 'de', 'value' => 'Kuchen'), 'en' => array('language' => 'en', 'value' => 'Cake')), 'claims' => array($propertyId->getSerialization() => array(array('id' => $this->makeGuid($entity->getId()), 'mainsnak' => array('snaktype' => 'value', 'property' => $propertyId->getSerialization(), 'datavalue' => array('value' => 'kittens!', 'type' => 'string')), 'type' => 'statement', 'rank' => 'normal')))); }
public function testNotBuildExceptionIsThrownForByPropertyId() { $indexedArray = new ByPropertyIdArray(); $this->setExpectedException('RuntimeException'); $indexedArray->getByPropertyId(PropertyId::newFromNumber(9000)); }
/** * @dataProvider invalidNumericIdProvider */ public function testNewFromNumberWithInvalidNumericId($number) { $this->setExpectedException('InvalidArgumentException'); PropertyId::newFromNumber($number); }
/** * Checks if there are constraints registered for the given property id. * * @param PropertyId $propertyId * @return boolean */ public function hasConstraints(PropertyId $propertyId) { return isset($this->constraintsPerProperty[$propertyId->getSerialization()]); }
/** * @see PropertyInfoStore::removePropertyInfo * * @param PropertyId $propertyId * * @throws InvalidArgumentException * @return bool */ public function removePropertyInfo(PropertyId $propertyId) { $id = $propertyId->getNumericId(); if (array_key_exists($id, $this->propertyInfo)) { unset($this->propertyInfo[$id]); return true; } else { return false; } }
/** * Returns the index of a "property group" (the first object in the flat array that features * the specified property). Returns false if property id could not be found. * * @param PropertyId $propertyId * * @return bool|int */ private function getPropertyGroupIndex(PropertyId $propertyId) { $i = 0; foreach ($this->byId as $serializedPropertyId => $objects) { $pId = new PropertyId($serializedPropertyId); if ($pId->equals($propertyId)) { return $i; } $i += count($objects); } return false; }
public function testAddSnakValue_mention() { $propertyId = new PropertyId('P2'); $value = new EntityIdValue(new ItemId('Q42')); $snak = new PropertyValueSnak($propertyId, $value); $writer = $this->getTestData()->getNTriplesWriter(); $writer->about(RdfVocabulary::NS_ENTITY, 'Q11'); $mentioned = array(); $builder = $this->newBuilder(RdfVocabulary::NSP_DIRECT_CLAIM, $propertyId->getSerialization(), 'wikibase-item', $snak, $mentioned); $builder->addSnak($writer, $snak, RdfVocabulary::NSP_DIRECT_CLAIM); $this->assertEquals(array('P2'), array_keys($mentioned)); }
/** * @see PropertyInfoStore::removePropertyInfo * * @param PropertyId $propertyId * * @return bool */ public function removePropertyInfo(PropertyId $propertyId) { $id = $propertyId->getNumericId(); // if we don't know it, don't delete it. if (is_array($this->propertyInfo) && !array_key_exists($id, $this->propertyInfo)) { return false; } // update primary store $ok = $this->store->removePropertyInfo($propertyId); if (!$ok) { // nothing changed, nothing to do return false; } // NOTE: Even if we don't have the propertyInfo locally, we still need to // fully load it to update memcached. // Get local cached version. // NOTE: this may be stale at this point, if it was already loaded $propertyInfo = $this->getAllPropertyInfo(); // update local cache unset($propertyInfo[$id]); $this->propertyInfo = $propertyInfo; // update external cache wfDebugLog(__CLASS__, __FUNCTION__ . ': updating cache after removing property ' . $id); $this->cache->set($this->cacheKey, $propertyInfo, $this->cacheDuration); return true; }
public function getApiPropertyUrl(PropertyId $id) : string { return $this->apiUrl . '/properties/' . $id->getSerialization(); }
private function getNumericIdFromPropertyId(PropertyId $propertyId) { return $propertyId->getNumericId(); }
/** * Checks if there are PropertyIdProvider instances for the given PropertyId. * * @since 1.0 * * @param PropertyId $propertyId * * @return bool */ public function hasPropertyId(PropertyId $propertyId) { return isset($this->byPropertyId[$propertyId->getSerialization()]); }
/** * Injects data types from a DB result into the $entityInfo structure. * * @note: Keep in sync with ItemSerializer! * * @param ResultWrapper $dbResult * * @throws InvalidArgumentException */ private function injectDataTypes(ResultWrapper $dbResult) { foreach ($dbResult as $row) { $id = PropertyId::newFromNumber((int) $row->pi_property_id); $key = $id->getSerialization(); if (!isset($this->entityInfo[$key])) { continue; } $this->entityInfo[$key]['datatype'] = $row->pi_type; } }