public function execute(CommandHelper $commandHelper) { $db = $commandHelper->getDatabase('repo'); $wikibaseFactory = $commandHelper->getWikibaseFactory(); $badgeIdsGetter = $wikibaseFactory->newBadgeIdsGetter(); $revisionsGetter = $wikibaseFactory->newRevisionsGetter(); $commandHelper->writeln('Fetching sites info...'); $siteApis = $this->getSiteApis($db); if ($siteApis === null) { throw new RuntimeException('Failed to fetch sites data'); } $commandHelper->writeln('Fetching badge ids...'); $badgeIds = $badgeIdsGetter->get(); $commandHelper->writeln('Got ' . implode(', ', $badgeIds)); $commandHelper->writeln('Fetching badge usages...'); $pagesToPurge = array(); $results = $db->query('SELECT page_title FROM pagelinks JOIN page ON pl_from = page_id WHERE pl_title IN( "' . implode('", "', $badgeIds) . '" ) AND pl_namespace = 0 AND pl_from_namespace = 0'); if (!$results) { throw new RuntimeException('Failed to fetch badge usage'); } $entityIds = array(); while ($row = $results->fetch_assoc()) { $entityIds[] = $row['page_title']; } $progressBar = $commandHelper->getProgressBar($results->num_rows); $results->free(); $commandHelper->writeln('Fetching entities...'); $progressBar->start(); $chunk = $commandHelper->getOption('chunk'); foreach (array_chunk($entityIds, $chunk) as $batch) { try { $revisions = $revisionsGetter->getRevisions($batch); foreach ($revisions->toArray() as $revision) { /** @var Item $item */ $item = $revision->getContent()->getData(); foreach ($item->getSiteLinkList()->toArray() as $siteLink) { if (!empty($siteLink->getBadges())) { $pagesToPurge[$siteLink->getSiteId()][] = $siteLink->getPageName(); } } } $progressBar->advance($chunk); } catch (Exception $ex) { $commandHelper->writeln('Failed to fetch data for ids ' . implode(', ', $batch) . ' (' . $ex->getMessage() . ')'); } } $progressBar->finish(); $commandHelper->writeln('Starting to purge pages'); $this->purgePages($pagesToPurge, $siteApis, $commandHelper, $commandHelper->getApiUser()); }
public function execute(CommandHelper $commandHelper) { $db = $commandHelper->getDatabase(); $wikibaseFactory = $commandHelper->getWikibaseFactory(); $revisionsGetter = $wikibaseFactory->newRevisionGetter(); $siteLinkSetter = $wikibaseFactory->newSiteLinkSetter(); $wiki = $commandHelper->getOption('wiki'); $badgeId = new ItemId($commandHelper->getOption('badge')); $editInfo = new EditInfo($this->getSummary($commandHelper->getOption('summary'), $commandHelper->getOption('category'), $wiki, $badgeId->getSerialization()), false, $commandHelper->getOption('bot')); $results = $db->query('SELECT page_title FROM categorylinks JOIN page ON page_id = cl_from WHERE cl_to = "' . $db->escape_string($commandHelper->getOption('category')) . '" AND page_namespace = 0'); $skipped = 0; $added = 0; $failed = 0; if ($results) { while ($row = $results->fetch_assoc()) { try { /** @var Item $item */ $revision = $revisionsGetter->getFromSiteAndTitle($wiki, $row['page_title']); if ($revision === false) { $commandHelper->writeln("\nNo item found for {$wiki}:{$row['page_title']}"); $failed++; continue; } $item = $revision->getContent()->getData(); $badges = $item->getSiteLinkList()->getBySiteId($wiki)->getBadges(); if (in_array($badgeId, $badges)) { $commandHelper->write('.'); $skipped++; continue; } $badges[] = $badgeId; $siteLinkSetter->set(new SiteLink($wiki, $row['page_title'], $badges), new SiteLink($wiki, $row['page_title']), $editInfo); $commandHelper->writeln("\nAdded badge for {$wiki}:{$row['page_title']}"); $added++; } catch (Exception $ex) { $commandHelper->writeln("\nFailed to add badge for {$wiki}:{$row['page_title']} (" . $ex->getMessage() . ")"); $failed++; } } } $results->free(); $commandHelper->writeln("Finished iterating through {$results->num_rows} site links."); $commandHelper->writeln("Added: {$added}"); $commandHelper->writeln("Skipped: {$skipped}"); $commandHelper->writeln("Failed: {$failed}"); }