MediaWiki REL1_30
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 ( !empty( $this->source ) ) {
84 $url = rtrim( $this->source, '?' ) . '?' . $url;
85 }
86
87 $json = Http::get( $url );
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 = wfGetDB( DB_MASTER );
105
106 if ( !$force ) {
107 $row = $dbw->selectRow(
108 'updatelog',
109 '1',
110 [ 'ul_key' => 'populate interwiki' ],
111 __METHOD__
112 );
113
114 if ( $row ) {
115 $this->output( "Interwiki table already populated. Use php " .
116 "maintenance/populateInterwiki.php\n--force from the command line " .
117 "to override.\n" );
118 return true;
119 }
120 }
121
122 foreach ( $data as $d ) {
123 $prefix = $d['prefix'];
124
125 $row = $dbw->selectRow(
126 'interwiki',
127 '1',
128 [ 'iw_prefix' => $prefix ],
129 __METHOD__
130 );
131
132 if ( !$row ) {
133 $dbw->insert(
134 'interwiki',
135 [
136 'iw_prefix' => $prefix,
137 'iw_url' => $d['url'],
138 'iw_local' => 1
139 ],
140 __METHOD__,
141 'IGNORE'
142 );
143 }
144
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;
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
static invalidateCache( $prefix)
Purge the cache (local and persistent) for an interwiki prefix.
Definition Interwiki.php:90
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
hasOption( $name)
Checks to see if a particular param exists.
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)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition deferred.txt:16
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
require_once RUN_MAINTENANCE_IF_MAIN
MediaWiki has optional support for a high distributed memory object caching system For general information on but for a larger site with heavy like Wikipedia
Definition memcached.txt:6
A helper class for throttling authentication attempts.
const DB_MASTER
Definition defines.php:26
This document describes the XML format used to represent information about external sites known to a MediaWiki installation This information about external sites is used to allow inter wiki links
in the order they appear.
Definition sitelist.txt:3