public function addReferences(MicroData $microData, $item, $sourceUrl) { $referenceCounter = 0; foreach ($this->propMap as $propertyIdString => $schemaPropertyString) { $regexMap = $this->regexMap[$propertyIdString]; $values = array(); foreach ($microData->getProperty($schemaPropertyString, MicroData::PROP_STRING) as $propertyValue) { // Don't match URLS! if (strstr($propertyValue, '//')) { continue; } $values[] = $propertyValue; } $statements = $item->getStatements()->getByPropertyId(new PropertyId($propertyIdString)); foreach ($values as $value) { foreach ($statements->getIterator() as &$statement) { $mainSnak = $statement->getMainSnak(); if (!$mainSnak instanceof PropertyValueSnak) { continue; // Ignore some and no value statements } if (DataModelUtils::statementHasReferenceForUrlWithSameDomain($statement, $sourceUrl)) { continue; // Ignore statements that already have this URL domain as a ref } /** @var EntityIdValue $valueEntityIdValue */ $valueEntityIdValue = $mainSnak->getDataValue(); /** @var EntityId $valueEntityId */ $valueEntityId = $valueEntityIdValue->getEntityId(); $valueEntityIdString = $valueEntityId->getSerialization(); if (!array_key_exists($valueEntityIdString, $regexMap)) { //TODO log that this ItemId is missing? continue; } $regex = $regexMap[$valueEntityIdString]; if (!preg_match($regex, $value)) { // ItemId regex didn't match this schema value continue; } // Add the new reference! $newReference = DataModelUtils::getReferenceForUrl($sourceUrl); try { $this->wikibaseFactory->newReferenceSetter()->set($newReference, $statement, null, new EditInfo(urldecode($sourceUrl), EditInfo::NOTMINOR, EditInfo::BOT)); //NOTE: keep our in memory item copy up to date (yay such reference passing) $statement->addNewReference($newReference->getSnaks()); $referenceCounter++; } catch (UsageException $e) { //Ignore } } } } return $referenceCounter; }
public function addReferences(MicroData $microData, $item, $sourceUrl) { $referenceCounter = 0; foreach ($this->propMap as $propertyIdString => $schemaPropertyString) { /** @var TimeValue[] $timeValues */ $timeValues = array(); foreach ($microData->getProperty($schemaPropertyString, MicroData::PROP_STRING) as $propertyValue) { try { $date = new DateTime(trim($propertyValue)); $timeValues[] = $this->timeParser->parse($date->format('Y m d')); } catch (Exception $e) { // Ignore failed parsing } } $statements = $item->getStatements()->getByPropertyId(new PropertyId($propertyIdString)); foreach ($timeValues as $timeValue) { foreach ($statements->getIterator() as &$statement) { $mainSnak = $statement->getMainSnak(); if (!$mainSnak instanceof PropertyValueSnak) { continue; // Ignore some and no value statements } if (DataModelUtils::statementHasReferenceForUrlWithSameDomain($statement, $sourceUrl)) { continue; // Ignore statements that already have this URL domain as a ref } if (!$timeValue->equals($mainSnak->getDataValue())) { continue; } // Add the new reference! $newReference = DataModelUtils::getReferenceForUrl($sourceUrl); try { $this->wikibaseFactory->newReferenceSetter()->set($newReference, $statement, null, new EditInfo(urldecode($sourceUrl), EditInfo::NOTMINOR, EditInfo::BOT)); //NOTE: keep our in memory item copy up to date (yay such reference passing) $statement->addNewReference($newReference->getSnaks()); $referenceCounter++; } catch (UsageException $e) { //Ignore } } } } return $referenceCounter; }