MediaWiki master
populateInterwiki.php
Go to the documentation of this file.
1<?php
2
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
20
24 private $source;
25
26 public function __construct() {
27 parent::__construct();
28
29 $this->addDescription( <<<TEXT
30This script will populate the interwiki table, pulling in interwiki links that are used on Wikipedia
31or another MediaWiki wiki.
32
33When the script has finished, it will make a note of this in the database, and will not run again
34without the --force option.
35
36--source parameter is the url for the source wiki api, such as "https://en.wikipedia.org/w/api.php"
37(the default) from which the script fetches the interwiki data and uses here to populate
38the interwiki database table.
39TEXT
40 );
41
42 $this->addOption( 'source', 'Source wiki for interwiki table, such as '
43 . 'https://en.wikipedia.org/w/api.php (the default)', false, true );
44 $this->addOption( 'force', 'Run regardless of whether the database says it has '
45 . 'been run already.' );
46 }
47
48 public function execute() {
49 $force = $this->hasOption( 'force' );
50 $this->source = $this->getOption( 'source', 'https://en.wikipedia.org/w/api.php' );
51
52 $data = $this->fetchLinks();
53
54 if ( $data === false ) {
55 $this->error( "Error during fetching data." );
56 } else {
57 $this->doPopulate( $data, $force );
58 }
59 }
60
64 protected function fetchLinks() {
66 'action' => 'query',
67 'meta' => 'siteinfo',
68 'siprop' => 'interwikimap',
69 'sifilteriw' => 'local',
70 'format' => 'json'
71 ] );
72
73 if ( $this->source ) {
74 $url = rtrim( $this->source, '?' ) . '?' . $url;
75 }
76
77 $json = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
78 $data = json_decode( $json, true );
79
80 if ( is_array( $data ) ) {
81 return $data['query']['interwikimap'];
82 } else {
83 return false;
84 }
85 }
86
93 protected function doPopulate( array $data, $force ) {
94 $dbw = $this->getPrimaryDB();
95
96 if ( !$force ) {
97 $row = $dbw->newSelectQueryBuilder()
98 ->select( '1' )
99 ->from( 'updatelog' )
100 ->where( [ 'ul_key' => 'populate interwiki' ] )
101 ->caller( __METHOD__ )->fetchRow();
102
103 if ( $row ) {
104 $this->output( "Interwiki table already populated. Use php " .
105 "maintenance/populateInterwiki.php\n--force from the command line " .
106 "to override.\n" );
107 return true;
108 }
109 }
110
111 $lookup = $this->getServiceContainer()->getInterwikiLookup();
112 foreach ( $data as $d ) {
113 $prefix = $d['prefix'];
114
115 $row = $dbw->newSelectQueryBuilder()
116 ->select( '1' )
117 ->from( 'interwiki' )
118 ->where( [ 'iw_prefix' => $prefix ] )
119 ->caller( __METHOD__ )->fetchRow();
120
121 if ( !$row ) {
122 $dbw->newInsertQueryBuilder()
123 ->insertInto( 'interwiki' )
124 ->ignore()
125 ->row( [
126 'iw_prefix' => $prefix,
127 'iw_url' => $d['url'],
128 'iw_local' => 1,
129 'iw_api' => '',
130 'iw_wikiid' => '',
131 ] )
132 ->caller( __METHOD__ )->execute();
133 }
134
135 $lookup->invalidateCache( $prefix );
136 }
137
138 $this->output( "Interwiki links are populated.\n" );
139
140 return true;
141 }
142
143}
144
145// @codeCoverageIgnoreStart
146$maintClass = PopulateInterwiki::class;
147require_once RUN_MAINTENANCE_IF_MAIN;
148// @codeCoverageIgnoreEnd
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...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
execute()
Do the actual work.
__construct()
Default constructor.
doPopulate(array $data, $force)
Helper trait for implementations \DAO.