Revision: Remove some unnecessary temporary variables for returns
[mediawiki.git] / maintenance / populateInterwiki.php
blob1a26a1bdd6ba0d2a1f04be7c0b7747a2baed2e4c
1 <?php
3 /**
4 * Maintenance script that populates the interwiki table with list of sites from
5 * a source wiki, such as English Wikipedia. (the default source)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @ingroup Maintenance
24 * @author Katie Filbert < aude.wiki@gmail.com >
27 use MediaWiki\MediaWikiServices;
29 require_once __DIR__ . '/Maintenance.php';
31 class PopulateInterwiki extends Maintenance {
33 /**
34 * @var string
36 private $source;
38 public function __construct() {
39 parent::__construct();
41 $this->addDescription( <<<TEXT
42 This script will populate the interwiki table, pulling in interwiki links that are used on Wikipedia
43 or another MediaWiki wiki.
45 When the script has finished, it will make a note of this in the database, and will not run again
46 without the --force option.
48 --source parameter is the url for the source wiki api, such as "https://en.wikipedia.org/w/api.php"
49 (the default) from which the script fetches the interwiki data and uses here to populate
50 the interwiki database table.
51 TEXT
54 $this->addOption( 'source', 'Source wiki for interwiki table, such as '
55 . 'https://en.wikipedia.org/w/api.php (the default)', false, true );
56 $this->addOption( 'force', 'Run regardless of whether the database says it has '
57 . 'been run already.' );
60 public function execute() {
61 $force = $this->hasOption( 'force' );
62 $this->source = $this->getOption( 'source', 'https://en.wikipedia.org/w/api.php' );
64 $data = $this->fetchLinks();
66 if ( $data === false ) {
67 $this->error( "Error during fetching data." );
68 } else {
69 $this->doPopulate( $data, $force );
73 /**
74 * @return array[]|bool The 'interwikimap' sub-array or false on failure.
76 protected function fetchLinks() {
77 $url = wfArrayToCgi( [
78 'action' => 'query',
79 'meta' => 'siteinfo',
80 'siprop' => 'interwikimap',
81 'sifilteriw' => 'local',
82 'format' => 'json'
83 ] );
85 if ( !empty( $this->source ) ) {
86 $url = rtrim( $this->source, '?' ) . '?' . $url;
89 $json = MediaWikiServices::getInstance()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
90 $data = json_decode( $json, true );
92 if ( is_array( $data ) ) {
93 return $data['query']['interwikimap'];
94 } else {
95 return false;
99 /**
100 * @param array[] $data
101 * @param bool $force
103 * @return bool
105 protected function doPopulate( array $data, $force ) {
106 $dbw = wfGetDB( DB_MASTER );
108 if ( !$force ) {
109 $row = $dbw->selectRow(
110 'updatelog',
111 '1',
112 [ 'ul_key' => 'populate interwiki' ],
113 __METHOD__
116 if ( $row ) {
117 $this->output( "Interwiki table already populated. Use php " .
118 "maintenance/populateInterwiki.php\n--force from the command line " .
119 "to override.\n" );
120 return true;
124 $lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
125 foreach ( $data as $d ) {
126 $prefix = $d['prefix'];
128 $row = $dbw->selectRow(
129 'interwiki',
130 '1',
131 [ 'iw_prefix' => $prefix ],
132 __METHOD__
135 if ( !$row ) {
136 $dbw->insert(
137 'interwiki',
139 'iw_prefix' => $prefix,
140 'iw_url' => $d['url'],
141 'iw_local' => 1
143 __METHOD__,
144 [ 'IGNORE' ]
148 $lookup->invalidateCache( $prefix );
151 $this->output( "Interwiki links are populated.\n" );
153 return true;
158 $maintClass = PopulateInterwiki::class;
159 require_once RUN_MAINTENANCE_IF_MAIN;