protected function execute(InputInterface $input, OutputInterface $output) { $wiki = $input->getOption('wiki'); $user = $input->getOption('user'); $userDetails = $this->appConfig->offsetGet('users.' . $user); $wikiDetails = $this->appConfig->offsetGet('wikis.' . $wiki); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } if ($wikiDetails === null) { throw new RuntimeException('Wiki not found in config'); } $pageIdentifier = null; if ($input->getOption('pageid') != null) { $pageIdentifier = new PageIdentifier(null, (int) $input->getOption('pageid')); } elseif ($input->getOption('title') != null) { $pageIdentifier = new PageIdentifier(new Title($input->getOption('title'))); } else { throw new \RuntimeException('No titles or pageids were set!'); } $wiki = $input->getOption('wiki'); $wikiDetails = $this->appConfig->offsetGet('wikis.' . $wiki); $api = new MediawikiApi($wikiDetails['url']); $loggedIn = $api->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { $output->writeln('Failed to log in'); return -1; } $mwFactory = new MediawikiFactory($api); $saver = $mwFactory->newRevisionSaver(); $saver->save(new Revision(new Content($input->getArgument('text')), $pageIdentifier)); $output->writeln('Done'); return 0; }
protected function execute(InputInterface $input, OutputInterface $output) { $user = $input->getOption('user'); $userDetails = $this->appConfig->offsetGet('users.' . $user); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } $wiki = $input->getOption('wiki'); $wikiDetails = $this->appConfig->offsetGet('wikis.' . $wiki); if ($wikiDetails === null) { throw new RuntimeException('Wiki not found in config'); } $sparql = $input->getOption('sparql'); if ($sparql === null || empty($sparql)) { throw new RuntimeException('SPARQL endpoint must be passed'); } $this->setServices($wikiDetails['url'], $sparql); $propertyString = $input->getOption('property'); $property = new PropertyId($propertyString); if ($propertyString === null || $propertyString === '' || $property === null) { throw new RuntimeException('No property given'); } $output->writeln('Running SPARQL query to find items to check'); $queryBuilder = new QueryBuilder(array('wdt' => 'http://www.wikidata.org/prop/direct/')); $itemIds = $this->sparqlQueryRunner->getItemIdsFromQuery($queryBuilder->select('?item')->where('?item', 'wdt:' . $propertyString, '?value')->limit(10000)->__toString()); $loggedIn = $this->wikibaseApi->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { $output->writeln('Failed to log in to wikibase wiki'); return -1; } $itemLookup = $this->wikibaseFactory->newItemLookup(); $statementRemover = $this->wikibaseFactory->newStatementRemover(); foreach ($itemIds as $itemId) { $item = $itemLookup->getItemForId($itemId); foreach ($item->getStatements()->getIterator() as $statement) { if ($statement->getPropertyId()->equals($property)) { $statementRemover->remove($statement, new EditInfo('Removing Statement')); } } } return 0; }
protected function execute(InputInterface $input, OutputInterface $output) { $wiki = $input->getOption('wiki'); $user = $input->getOption('user'); $revids = $input->getArgument('revid'); $userDetails = $this->appConfig->offsetGet('users.' . $user); $wikiDetails = $this->appConfig->offsetGet('wikis.' . $wiki); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } if ($wikiDetails === null) { throw new RuntimeException('Wiki not found in config'); } $api = new MediawikiApi($wikiDetails['url']); $loggedIn = $api->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { $output->writeln('Failed to log in'); return -1; } $mwFactory = new MediawikiFactory($api); $getter = $mwFactory->newPageGetter(); $saver = $mwFactory->newRevisionSaver(); foreach ($revids as $revid) { $revid = intval($revid); $page = $getter->getFromRevisionId($revid); $page = $getter->getFromPage($page); $goodText = $page->getRevisions()->get($revid)->getContent()->getData(); $currentRevision = $page->getRevisions()->getLatest(); $currentText = $currentRevision->getContent()->getData(); if ($goodText === $currentText) { $output->writeln('Page already has same content as revision: ' . $revid); return null; } if ($input->getOption('asheader')) { if (strstr($currentText, $goodText)) { $goodText = $goodText . "\n\n" . trim(str_replace($goodText, '', $currentText)); } else { $goodText = $goodText . "\n\n" . $currentText; } } $newRevision = new Revision(new Content($goodText), $page->getPageIdentifier()); $success = $saver->save($newRevision, new EditInfo($this->getEditSummary($input->getOption('summary'), $revid), boolval($input->getOption('minor')), boolval($input->getOption('bot')))); if ($success) { $output->writeln('Restored revision: ' . $revid); } else { $output->writeln('Failed to restore revision: ' . $revid); } } return null; }
protected function execute(InputInterface $input, OutputInterface $output) { $this->initServices(); if (is_string($input->getOption('tmpDir'))) { $this->tmpDir = $input->getOption('tmpDir'); } else { $this->tmpDir = sys_get_temp_dir(); } if (!is_writable($this->tmpDir)) { throw new RuntimeException('Temp dir: ' . $this->tmpDir . ' is not writable'); } /** @var FormatterHelper $formatter */ $formatter = $this->getHelper('formatter'); $output->writeln($formatter->formatBlock(array('Wikidata Referencer', 'This script is in development, If something goes wrong while you use it it is your fault!', 'Temp file: ' . $this->getProcessedListPath()), 'info')); // Get options $user = $input->getOption('user'); $userDetails = $this->appConfig->offsetGet('users.' . $user); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } $sparqlQueryParts = $input->getOption('sparql'); $item = $input->getOption('item'); $force = false; // Get a list of ItemIds if ($item !== null) { $output->writeln($formatter->formatSection('Init', 'Using item passed in item parameter')); $itemIds = array(new ItemId($item)); // Force if explicitly passed an ItemId $force = true; } elseif (!empty($sparqlQueryParts)) { $output->writeln($formatter->formatSection('Init', 'Using items from SPARQL QUERY (running)')); $itemIds = $this->sparqlQueryRunner->getItemIdsForSimpleQueryParts($sparqlQueryParts); } else { throw new RuntimeException('You must pass an instance id or an item'); } shuffle($itemIds); $output->writeln($formatter->formatSection('Init', 'Got ' . count($itemIds) . ' items to investigate')); // Log in to Wikidata $loggedIn = $this->wikibaseApi->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { throw new RuntimeException('Failed to log in to wikibase wiki'); } $this->executeForItemIds($output, $itemIds, $force); return 0; }
private function purgePages(array $pagesToPurge, array $siteApis, CommandHelper $commandHelper, ApiUser $apiUser) { $allCount = 0; foreach ($siteApis as $siteId => $url) { if (!isset($pagesToPurge[$siteId])) { $commandHelper->writeln("\nNo pages found to purge for site {$siteId} ({$url})"); continue; } $count = count($pagesToPurge[$siteId]); $commandHelper->writeln("\nPurging {$count} pages for site {$siteId} ({$url})"); $api = new MediawikiApi($url); $api->login($apiUser); $progressBar = $commandHelper->getProgressBar($count); $progressBar->start(); foreach (array_chunk($pagesToPurge[$siteId], 10) as $pages) { $params = array('titles' => implode('|', $pages), 'forcelinkupdate' => true); $api->postRequest(new SimpleRequest('purge', $params)); $progressBar->advance(10); } $progressBar->finish(); $allCount += $count; } $commandHelper->writeln("\nFinished purging {$allCount} pages"); }
public function testBadLoginSequence() { $client = $this->getMockClient(); $user = new ApiUser('U1', 'P1'); $eq1 = array('action' => 'login', 'lgname' => 'U1', 'lgpassword' => 'P1'); $client->expects($this->at(0))->method('post')->with(null, $this->getExpectedOptsForPostParams($eq1))->will($this->returnValue($this->getMockResponse(array('login' => array('result' => 'NeedToken', 'token' => 'IamLoginTK'))))); $client->expects($this->at(1))->method('post')->with(null, $this->getExpectedOptsForPostParams(array_merge($eq1, array('lgtoken' => 'IamLoginTK'))))->will($this->returnValue($this->getMockResponse(array('login' => array('result' => 'BADTOKENorsmthin'))))); $api = new MediawikiApi($client); $this->setExpectedException('Mediawiki\\Api\\UsageException'); $api->login($user); }
return false; } list($yearA, $monthA, $dayA) = $this->explodeTimestamp($a->getTime()); list($yearB, $monthB, $dayB) = $this->explodeTimestamp($b->getTime()); return $yearA === $yearB && !($b->getPrecision() >= TimeValue::PRECISION_MONTH && $monthA !== $monthB) && !($b->getPrecision() >= TimeValue::PRECISION_DAY && $dayA !== $dayB); } private function explodeTimestamp($timestamp) { preg_match('/^([-+]\\d+)-(\\d\\d)-(\\d\\d)T(\\d\\d):(\\d\\d):(\\d\\d)Z$/', $timestamp, $m); array_shift($m); return $m; } //Very, very hacky private function hasMeaningfulReference(Statement $statement) { foreach ($statement->getReferences() as $reference) { foreach ($reference->getSnaks() as $snak) { if (!$snak->equals(new PropertyValueSnak(new PropertyId('P248'), new EntityIdValue(new ItemId('Q36578')))) && !$snak->getPropertyId()->equals(new PropertyId('P813')) && !$snak->getPropertyId()->equals(new PropertyId('P143'))) { return true; } } } return false; } } $api = new MediawikiApi('https://www.wikidata.org/w/api.php'); $api->login(new ApiUser(MY_USERNAME, MY_PASSWORD)); //TODO $bot = new Bot($api); $bot->addStatementFromTsvFile($argv[1]); //$bot->addStatementFromPrimarySourcesQuery(['property' => 'P1771']);
protected function execute(InputInterface $input, OutputInterface $output) { $user = $input->getOption('user'); $userDetails = $this->appConfig->get('users.' . $user); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } $pageIdentifier = null; if ($input->getOption('title') != null) { $sourceTitle = $input->getOption('title'); $pageIdentifier = new PageIdentifier(new Title($sourceTitle)); } else { throw new RuntimeException('No titles was set!'); } $sourceApi = new MediawikiApi("https://www.mediawiki.org/w/api.php"); $targetApi = new MediawikiApi("https://www.wikidata.org/w/api.php"); $loggedIn = $targetApi->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { $output->writeln('Failed to log in to target wiki'); return -1; } $sourceMwFactory = new MediawikiFactory($sourceApi); $sourceParser = $sourceMwFactory->newParser(); $parseResult = $sourceParser->parsePage($pageIdentifier); //Get the wikibase item if it exists $itemIdString = null; if (array_key_exists('properties', $parseResult)) { foreach ($parseResult['properties'] as $pageProp) { if ($pageProp['name'] == 'wikibase_item') { $itemIdString = $pageProp['*']; } } } $targetWbFactory = new WikibaseFactory($targetApi, new DataValueDeserializer(array('boolean' => 'DataValues\\BooleanValue', 'number' => 'DataValues\\NumberValue', 'string' => 'DataValues\\StringValue', 'unknown' => 'DataValues\\UnknownValue', 'globecoordinate' => 'DataValues\\Geo\\Values\\GlobeCoordinateValue', 'monolingualtext' => 'DataValues\\MonolingualTextValue', 'multilingualtext' => 'DataValues\\MultilingualTextValue', 'quantity' => 'DataValues\\QuantityValue', 'time' => 'DataValues\\TimeValue', 'wikibase-entityid' => 'Wikibase\\DataModel\\Entity\\EntityIdValue')), new DataValueSerializer()); // Create an item if there is no item yet! if ($itemIdString === null) { $output->writeln("Creating a new Item"); $item = new Item(); $item->setLabel('en', $sourceTitle); //TODO this siteid should come from somewhere? $item->getSiteLinkList()->setNewSiteLink('mediawikiwiki', $sourceTitle); $targetRevSaver = $targetWbFactory->newRevisionSaver(); $item = $targetRevSaver->save(new Revision(new Content($item))); } else { $item = $targetWbFactory->newItemLookup()->getItemForId(new ItemId($itemIdString)); } // Add instance of if not already there $hasInstanceOfExtension = false; foreach ($item->getStatements()->getByPropertyId(new PropertyId('P31'))->getMainSnaks() as $mainSnak) { if ($mainSnak instanceof PropertyValueSnak) { /** @var EntityIdValue $dataValue */ $dataValue = $mainSnak->getDataValue(); if ($dataValue->getEntityId()->equals(new ItemId('Q6805426'))) { $hasInstanceOfExtension = true; break; } } } if (!$hasInstanceOfExtension) { $output->writeln("Creating instance of Statement"); $targetWbFactory->newStatementCreator()->create(new PropertyValueSnak(new PropertyId('P31'), new EntityIdValue(new ItemId('Q6805426'))), $item->getId()); } // Try to add a licence $catLicenseMap = array('Public_domain_licensed_extensions' => 'Q19652'); $extensionLicenseItemIdString = null; if (array_key_exists('categories', $parseResult)) { foreach ($parseResult['categories'] as $categoryInfo) { if (array_key_exists($categoryInfo['*'], $catLicenseMap)) { $extensionLicenseItemIdString = $catLicenseMap[$categoryInfo['*']]; } } } if ($extensionLicenseItemIdString !== null) { $output->writeln("Creating Licence Statement"); $statementCreator = $targetWbFactory->newStatementCreator(); //TODO make sure it isn't already there???? $statementCreator->create(new PropertyValueSnak(new PropertyId('P275'), new EntityIdValue(new ItemId($extensionLicenseItemIdString))), $item->getId()); } }
protected function execute(InputInterface $input, OutputInterface $output) { // Get options $user = $input->getOption('user'); $userDetails = $this->appConfig->offsetGet('users.' . $user); if ($userDetails === null) { throw new RuntimeException('User not found in config'); } $items = $input->getOption('item'); if (empty($items)) { $output->writeln('Running SPARQL query to find items to check'); $queryBuilder = new QueryBuilder(array('prov' => 'http://www.w3.org/ns/prov#', 'wd' => 'http://www.wikidata.org/entity/', 'wikibase' => 'http://wikiba.se/ontology#', 'prv' => 'http://www.wikidata.org/prop/reference/value/')); $itemIds = $this->sparqlQueryRunner->getItemIdsFromQuery($queryBuilder->select('?item')->where('?ref', 'prv:P813', '?value')->also('?value', 'wikibase:timeCalendarModel', 'wd:Q1985786')->also('?st', 'prov:wasDerivedFrom', '?ref')->also('?item', '?pred', '?st')->limit(10000)->__toString()); } else { /** @var ItemId[] $itemIds */ $itemIds = array(); foreach (array_unique($items) as $itemIdString) { $itemIds[] = new ItemId($itemIdString); } } $itemIds = array_unique($itemIds); $output->writeln('Running for ' . count($itemIds) . ' items'); // Log in to Wikidata $loggedIn = $this->wikibaseApi->login(new ApiUser($userDetails['username'], $userDetails['password'])); if (!$loggedIn) { $output->writeln('Failed to log in to wikidata wiki'); return -1; } $itemLookup = $this->wikibaseFactory->newItemLookup(); foreach ($itemIds as $itemId) { $output->write($itemId->getSerialization() . ' '); $item = $itemLookup->getItemForId($itemId); foreach ($item->getStatements()->getIterator() as $statement) { foreach ($statement->getReferences() as $reference) { /** @var Reference $reference */ foreach ($reference->getSnaks()->getIterator() as $snak) { if ($snak instanceof PropertyValueSnak) { if ($snak->getPropertyId()->getSerialization() == 'P813') { /** @var TimeValue $dataValue */ $dataValue = $snak->getDataValue(); // We can assume ALL retrieval dates should be Gregorian! if ($dataValue->getCalendarModel() === TimeValue::CALENDAR_JULIAN) { $oldRefHash = $reference->getHash(); $statementGuid = $statement->getGuid(); $snakList = $reference->getSnaks(); $snakList = new SnakList($snakList->getArrayCopy()); $snakList->removeSnak($snak); $fixedTimestamp = $this->getFixedTimestamp($dataValue->getTime()); if ($fixedTimestamp) { $snakList->addSnak(new PropertyValueSnak(new PropertyId('P813'), new TimeValue($fixedTimestamp, $dataValue->getTimezone(), $dataValue->getBefore(), $dataValue->getAfter(), $dataValue->getPrecision(), TimeValue::CALENDAR_GREGORIAN))); $editSummary = 'Fix reference retrieval date'; $output->write('.'); } else { //TODO optionally remove rather than always doing so? $editSummary = 'Removing bad reference retrieval date'; $output->write('x'); } try { $this->wikibaseFactory->newReferenceSetter()->set(new Reference($snakList), $statementGuid, $oldRefHash, new EditInfo($editSummary)); } catch (UsageException $e) { $output->writeln(''); $output->write($e->getMessage()); } } } } } } } $output->writeln(''); } return 0; }
/** * @param string $wiki * @param string $user * @return MediawikiApi * @throws RuntimeException */ public function getMediawikiApi($wiki = null, $user = null) { $wiki = $this->input->getOption($wiki ?: 'wiki'); $wikiDetails = $this->appConfig->get('wikis.' . $wiki); if ($wikiDetails === null) { throw new RuntimeException("Wiki {$wiki} not found in config"); } $api = new MediawikiApi($wikiDetails['url']); $loggedIn = $api->login($this->getApiUser($user)); if (!$loggedIn) { throw new RuntimeException('Failed to login'); } return $api; }