MediaWiki master
populateInterwiki.php
Go to the documentation of this file.
1<?php
2
27require_once __DIR__ . '/Maintenance.php';
28
30
34 private $source;
35
36 public function __construct() {
37 parent::__construct();
38
39 $this->addDescription( <<<TEXT
40This script will populate the interwiki table, pulling in interwiki links that are used on Wikipedia
41or another MediaWiki wiki.
42
43When the script has finished, it will make a note of this in the database, and will not run again
44without the --force option.
45
46--source parameter is the url for the source wiki api, such as "https://en.wikipedia.org/w/api.php"
47(the default) from which the script fetches the interwiki data and uses here to populate
48the interwiki database table.
49TEXT
50 );
51
52 $this->addOption( 'source', 'Source wiki for interwiki table, such as '
53 . 'https://en.wikipedia.org/w/api.php (the default)', false, true );
54 $this->addOption( 'force', 'Run regardless of whether the database says it has '
55 . 'been run already.' );
56 }
57
58 public function execute() {
59 $force = $this->hasOption( 'force' );
60 $this->source = $this->getOption( 'source', 'https://en.wikipedia.org/w/api.php' );
61
62 $data = $this->fetchLinks();
63
64 if ( $data === false ) {
65 $this->error( "Error during fetching data." );
66 } else {
67 $this->doPopulate( $data, $force );
68 }
69 }
70
74 protected function fetchLinks() {
75 $url = wfArrayToCgi( [
76 'action' => 'query',
77 'meta' => 'siteinfo',
78 'siprop' => 'interwikimap',
79 'sifilteriw' => 'local',
80 'format' => 'json'
81 ] );
82
83 if ( $this->source ) {
84 $url = rtrim( $this->source, '?' ) . '?' . $url;
85 }
86
87 $json = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
88 $data = json_decode( $json, true );
89
90 if ( is_array( $data ) ) {
91 return $data['query']['interwikimap'];
92 } else {
93 return false;
94 }
95 }
96
103 protected function doPopulate( array $data, $force ) {
104 $dbw = $this->getPrimaryDB();
105
106 if ( !$force ) {
107 $row = $dbw->newSelectQueryBuilder()
108 ->select( '1' )
109 ->from( 'updatelog' )
110 ->where( [ 'ul_key' => 'populate interwiki' ] )
111 ->caller( __METHOD__ )->fetchRow();
112
113 if ( $row ) {
114 $this->output( "Interwiki table already populated. Use php " .
115 "maintenance/populateInterwiki.php\n--force from the command line " .
116 "to override.\n" );
117 return true;
118 }
119 }
120
121 $lookup = $this->getServiceContainer()->getInterwikiLookup();
122 foreach ( $data as $d ) {
123 $prefix = $d['prefix'];
124
125 $row = $dbw->newSelectQueryBuilder()
126 ->select( '1' )
127 ->from( 'interwiki' )
128 ->where( [ 'iw_prefix' => $prefix ] )
129 ->caller( __METHOD__ )->fetchRow();
130
131 if ( !$row ) {
132 $dbw->newInsertQueryBuilder()
133 ->insertInto( 'interwiki' )
134 ->ignore()
135 ->row( [
136 'iw_prefix' => $prefix,
137 'iw_url' => $d['url'],
138 'iw_local' => 1,
139 'iw_api' => '',
140 'iw_wikiid' => '',
141 ] )
142 ->caller( __METHOD__ )->execute();
143 }
144
145 $lookup->invalidateCache( $prefix );
146 }
147
148 $this->output( "Interwiki links are populated.\n" );
149
150 return true;
151 }
152
153}
154
155$maintClass = PopulateInterwiki::class;
156require_once RUN_MAINTENANCE_IF_MAIN;
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
run()
Run the job.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
execute()
Do the actual work.
__construct()
Default constructor.
doPopulate(array $data, $force)
A helper class for throttling authentication attempts.