public function execute() { $this->dryrun = !$this->hasOption('really'); $sourceDomain = SiteMapper::getDomainCode($this->getOption('source')); $targetDomain = SiteMapper::getDomainCode($this->getOption('target')); $category = $this->getOption('category'); if ($this->dryrun) { $this->output("DRY-RUN mode: actions are NOT executed\n"); } else { $this->output("EXECUTE mode: actions ARE executed\n"); } $apiUrl = "https://{$sourceDomain}.wikipedia.org/w/api.php?"; $pages = $this->getUntranslatedPages($apiUrl, $category, $targetDomain); $count = count($pages); if (!$this->dryrun) { $this->createFeaturedSuggestions($pages); $this->output("{$count} pages added to the list successfully.\n"); } else { $this->output("Found {$count} pages:\n"); foreach ($pages as $page) { $this->output("{$page}\n"); } $this->output("Use --really to insert these pages.\n"); } }
private function getExistingTitles(array $suggestions) { $params = $this->extractRequestParams(); $titles = array(); $sourceLanguage = $params['from']; $targetLanguage = $params['to']; $domain = SiteMapper::getDomainCode($sourceLanguage); $existingTitles = array(); foreach ($suggestions as $suggestion) { $titles[] = $suggestion->getTitle()->getPrefixedText(); } $params = array('action' => 'query', 'format' => 'json', 'titles' => implode('|', $titles), 'prop' => 'langlinks', 'lllimit' => $params['limit'], 'lllang' => SiteMapper::getDomainCode($targetLanguage), 'redirects' => true); $apiUrl = SiteMapper::getApiURL($sourceLanguage, $params); $json = Http::get($apiUrl); $response = FormatJson::decode($json, true); if (!isset($response['query']) || !isset($response['query']['pages'])) { // Something wrong with response. Should we throw exception? return $existingTitles; } $pages = $response['query']['pages']; foreach ($pages as $page) { if (isset($page['langlinks'])) { // API returns titles in PrefixedText format $existingTitles[] = $page['title']; } } return $existingTitles; }