/**
  * A convenience wrapper to a RevisionSaver.
  * @param string $title The title of the new page.
  * @param string $content The wikitext to save to the page.
  * @return Page The saved Page.
  */
 protected function savePage($title, $content)
 {
     $pageIdentifier = new PageIdentifier(new Title($title));
     $revision = new Revision(new Content($content), $pageIdentifier);
     $this->factory->newRevisionSaver()->save($revision);
     return $this->factory->newPageGetter()->getFromPageIdentifier($pageIdentifier);
 }
Beispiel #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $pageIdentifiers = array();
     if ($input->hasOption('pageid')) {
         foreach ($input->getOption('pageid') as $pageId) {
             $pageIdentifiers[] = new PageIdentifier(null, (int) $pageId);
         }
     } elseif ($input->hasOption('title')) {
         foreach ($input->getOption('title') as $title) {
             $pageIdentifiers[] = new PageIdentifier(new Title($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']);
     $mwFactory = new MediawikiFactory($api);
     $purger = $mwFactory->newPagePurger();
     /** @var PageIdentifier $identifier */
     foreach ($pageIdentifiers as $identifier) {
         if ($identifier->getId() != null) {
             $output->writeln('Purging page with id ' . $identifier->getId());
         } elseif ($identifier->getTitle() != null) {
             $output->writeln('Purging page with title ' . $identifier->getTitle()->getText());
         }
         $purger->purge(new Page($identifier));
     }
     $output->writeln('Done');
 }
 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)
 {
     $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)
 {
     $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());
     }
 }