| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
require_once __DIR__ . '/Maintenance.php'; |
| 29 |
|
| 30 |
|
| 31 |
class PopulateInterwiki extends Maintenance { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private $source; |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
|
| 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. |
| 44 |
|
| 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. |
| 47 |
|
| 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 |
| 52 |
); |
| 53 |
|
| 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.' ); |
| 58 |
} |
| 59 |
|
| 60 |
public function execute() { |
| 61 |
$force = $this->hasOption( 'force' ); |
| 62 |
$this->source = $this->getOption( 'source', 'https://en.wikipedia.org/w/api.php' ); |
| 63 |
|
| 64 |
$data = $this->fetchLinks(); |
| 65 |
|
| 66 |
if ( $data === false ) { |
| 67 |
$this->error( "Error during fetching data." ); |
| 68 |
} else { |
| 69 |
$this->doPopulate( $data, $force ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected function fetchLinks() { |
| 77 |
$url = wfArrayToCgi( [ |
| 78 |
'action' => 'query', |
| 79 |
'meta' => 'siteinfo', |
| 80 |
'siprop' => 'interwikimap', |
| 81 |
'sifilteriw' => 'local', |
| 82 |
'format' => 'json' |
| 83 |
] ); |
| 84 |
|
| 85 |
if ( $this->source ) { |
| 86 |
$url = rtrim( $this->source, '?' ) . '?' . $url; |
| 87 |
} |
| 88 |
|
| 89 |
$json = $this->getServiceContainer()->getHttpRequestFactory()->get( $url, [], __METHOD__ ); |
| 90 |
$data = json_decode( $json, true ); |
| 91 |
|
| 92 |
if ( is_array( $data ) ) { |
| 93 |
return $data['query']['interwikimap']; |
| 94 |
} else { |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
protected function doPopulate( array $data, $force ) { |
| 106 |
$dbw = $this->getPrimaryDB(); |
| 107 |
|
| 108 |
if ( !$force ) { |
| 109 |
$row = $dbw->newSelectQueryBuilder() |
| 110 |
->select( '1' ) |
| 111 |
->from( 'updatelog' ) |
| 112 |
->where( [ 'ul_key' => 'populate interwiki' ] ) |
| 113 |
->caller( __METHOD__ )->fetchRow(); |
| 114 |
|
| 115 |
if ( $row ) { |
| 116 |
$this->output( "Interwiki table already populated. Use php " . |
| 117 |
"maintenance/populateInterwiki.php\n--force from the command line " . |
| 118 |
"to override.\n" ); |
| 119 |
return true; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$lookup = $this->getServiceContainer()->getInterwikiLookup(); |
| 124 |
foreach ( $data as $d ) { |
| 125 |
$prefix = $d['prefix']; |
| 126 |
|
| 127 |
$row = $dbw->newSelectQueryBuilder() |
| 128 |
->select( '1' ) |
| 129 |
->from( 'interwiki' ) |
| 130 |
->where( [ 'iw_prefix' => $prefix ] ) |
| 131 |
->caller( __METHOD__ )->fetchRow(); |
| 132 |
|
| 133 |
if ( !$row ) { |
| 134 |
$dbw->newInsertQueryBuilder() |
| 135 |
->insertInto( 'interwiki' ) |
| 136 |
->ignore() |
| 137 |
->row( [ |
| 138 |
'iw_prefix' => $prefix, |
| 139 |
'iw_url' => $d['url'], |
| 140 |
'iw_local' => 1, |
| 141 |
'iw_api' => '', |
| 142 |
'iw_wikiid' => '', |
| 143 |
] ) |
| 144 |
->caller( __METHOD__ )->execute(); |
| 145 |
} |
| 146 |
|
| 147 |
$lookup->invalidateCache( $prefix ); |
| 148 |
} |
| 149 |
|
| 150 |
$this->output( "Interwiki links are populated.\n" ); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
$maintClass = PopulateInterwiki::class; |
| 159 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 160 |
|