Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
PopulateInterwiki | |
0.00% |
0 / 63 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
fetchLinks | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
doPopulate | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
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) |
6 | * |
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. |
11 | * |
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. |
16 | * |
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 |
21 | * |
22 | * @file |
23 | * @ingroup Maintenance |
24 | * @author Katie Filbert < aude.wiki@gmail.com > |
25 | */ |
26 | |
27 | use MediaWiki\Maintenance\Maintenance; |
28 | |
29 | // @codeCoverageIgnoreStart |
30 | require_once __DIR__ . '/Maintenance.php'; |
31 | // @codeCoverageIgnoreEnd |
32 | |
33 | class PopulateInterwiki extends Maintenance { |
34 | |
35 | /** |
36 | * @var string |
37 | */ |
38 | private $source; |
39 | |
40 | public function __construct() { |
41 | parent::__construct(); |
42 | |
43 | $this->addDescription( <<<TEXT |
44 | This script will populate the interwiki table, pulling in interwiki links that are used on Wikipedia |
45 | or another MediaWiki wiki. |
46 | |
47 | When the script has finished, it will make a note of this in the database, and will not run again |
48 | without the --force option. |
49 | |
50 | --source parameter is the url for the source wiki api, such as "https://en.wikipedia.org/w/api.php" |
51 | (the default) from which the script fetches the interwiki data and uses here to populate |
52 | the interwiki database table. |
53 | TEXT |
54 | ); |
55 | |
56 | $this->addOption( 'source', 'Source wiki for interwiki table, such as ' |
57 | . 'https://en.wikipedia.org/w/api.php (the default)', false, true ); |
58 | $this->addOption( 'force', 'Run regardless of whether the database says it has ' |
59 | . 'been run already.' ); |
60 | } |
61 | |
62 | public function execute() { |
63 | $force = $this->hasOption( 'force' ); |
64 | $this->source = $this->getOption( 'source', 'https://en.wikipedia.org/w/api.php' ); |
65 | |
66 | $data = $this->fetchLinks(); |
67 | |
68 | if ( $data === false ) { |
69 | $this->error( "Error during fetching data." ); |
70 | } else { |
71 | $this->doPopulate( $data, $force ); |
72 | } |
73 | } |
74 | |
75 | /** |
76 | * @return array[]|false The 'interwikimap' sub-array or false on failure. |
77 | */ |
78 | protected function fetchLinks() { |
79 | $url = wfArrayToCgi( [ |
80 | 'action' => 'query', |
81 | 'meta' => 'siteinfo', |
82 | 'siprop' => 'interwikimap', |
83 | 'sifilteriw' => 'local', |
84 | 'format' => 'json' |
85 | ] ); |
86 | |
87 | if ( $this->source ) { |
88 | $url = rtrim( $this->source, '?' ) . '?' . $url; |
89 | } |
90 | |
91 | $json = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ ); |
92 | $data = json_decode( $json, true ); |
93 | |
94 | if ( is_array( $data ) ) { |
95 | return $data['query']['interwikimap']; |
96 | } else { |
97 | return false; |
98 | } |
99 | } |
100 | |
101 | /** |
102 | * @param array[] $data |
103 | * @param bool $force |
104 | * |
105 | * @return bool |
106 | */ |
107 | protected function doPopulate( array $data, $force ) { |
108 | $dbw = $this->getPrimaryDB(); |
109 | |
110 | if ( !$force ) { |
111 | $row = $dbw->newSelectQueryBuilder() |
112 | ->select( '1' ) |
113 | ->from( 'updatelog' ) |
114 | ->where( [ 'ul_key' => 'populate interwiki' ] ) |
115 | ->caller( __METHOD__ )->fetchRow(); |
116 | |
117 | if ( $row ) { |
118 | $this->output( "Interwiki table already populated. Use php " . |
119 | "maintenance/populateInterwiki.php\n--force from the command line " . |
120 | "to override.\n" ); |
121 | return true; |
122 | } |
123 | } |
124 | |
125 | $lookup = $this->getServiceContainer()->getInterwikiLookup(); |
126 | foreach ( $data as $d ) { |
127 | $prefix = $d['prefix']; |
128 | |
129 | $row = $dbw->newSelectQueryBuilder() |
130 | ->select( '1' ) |
131 | ->from( 'interwiki' ) |
132 | ->where( [ 'iw_prefix' => $prefix ] ) |
133 | ->caller( __METHOD__ )->fetchRow(); |
134 | |
135 | if ( !$row ) { |
136 | $dbw->newInsertQueryBuilder() |
137 | ->insertInto( 'interwiki' ) |
138 | ->ignore() |
139 | ->row( [ |
140 | 'iw_prefix' => $prefix, |
141 | 'iw_url' => $d['url'], |
142 | 'iw_local' => 1, |
143 | 'iw_api' => '', |
144 | 'iw_wikiid' => '', |
145 | ] ) |
146 | ->caller( __METHOD__ )->execute(); |
147 | } |
148 | |
149 | $lookup->invalidateCache( $prefix ); |
150 | } |
151 | |
152 | $this->output( "Interwiki links are populated.\n" ); |
153 | |
154 | return true; |
155 | } |
156 | |
157 | } |
158 | |
159 | // @codeCoverageIgnoreStart |
160 | $maintClass = PopulateInterwiki::class; |
161 | require_once RUN_MAINTENANCE_IF_MAIN; |
162 | // @codeCoverageIgnoreEnd |