MediaWiki  master
populateInterwiki.php
Go to the documentation of this file.
1 <?php
2 
27 require_once __DIR__ . '/Maintenance.php';
28 
30 
34  private $source;
35 
36  public function __construct() {
37  parent::__construct();
38 
39  $this->addDescription( <<<TEXT
40 This script will populate the interwiki table, pulling in interwiki links that are used on Wikipedia
41 or another MediaWiki wiki.
42 
43 When the script has finished, it will make a note of this in the database, and will not run again
44 without 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
48 the interwiki database table.
49 TEXT
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->getDB( DB_PRIMARY );
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->insert(
133  'interwiki',
134  [
135  'iw_prefix' => $prefix,
136  'iw_url' => $d['url'],
137  'iw_local' => 1,
138  'iw_api' => '',
139  'iw_wikiid' => '',
140  ],
141  __METHOD__,
142  [ 'IGNORE' ]
143  );
144  }
145 
146  $lookup->invalidateCache( $prefix );
147  }
148 
149  $this->output( "Interwiki links are populated.\n" );
150 
151  return true;
152  }
153 
154 }
155 
156 $maintClass = PopulateInterwiki::class;
157 require_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....
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
The MediaWiki class is the helper class for the index.php entry point.
Definition: MediaWiki.php:50
execute()
Do the actual work.
__construct()
Default constructor.
doPopulate(array $data, $force)
const DB_PRIMARY
Definition: defines.php:28