PopulateSitesTable::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\Lib\Maintenance;
4
5
use Maintenance;
6
use MediaWiki\MediaWikiServices;
7
use MWException;
8
use Wikibase\Lib\Sites\SiteMatrixParser;
9
use Wikibase\Lib\Sites\SitesBuilder;
10
11
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false
12
	? getenv( 'MW_INSTALL_PATH' )
13
	: __DIR__ . '/../../../..';
14
15
require_once $basePath . '/maintenance/Maintenance.php';
16
17
if ( !class_exists( SitesBuilder::class ) ) {
18
	require_once __DIR__ . '/../includes/Sites/SitesBuilder.php';
19
}
20
21
if ( !class_exists( SiteMatrixParser::class ) ) {
22
	require_once __DIR__ . '/../includes/Sites/SiteMatrixParser.php';
23
}
24
25
/**
26
 * Maintenance script for populating the Sites table from another wiki that runs the
27
 * SiteMatrix extension.
28
 *
29
 * @note: this should move out of Wikibase
30
 *
31
 * @license GPL-2.0-or-later
32
 * @author Daniel Kinzler
33
 * @author Katie Filbert < [email protected] >
34
 */
35
class PopulateSitesTable extends Maintenance {
36
37
	public function __construct() {
38
		parent::__construct();
39
40
		$this->addDescription( 'Populate the sites table from another wiki that runs the SiteMatrix extension' );
41
42
		$this->addOption( 'strip-protocols', "Strip http/https from URLs to make them protocol relative." );
43
		$this->addOption( 'force-protocol', "Force a specific protocol for all URLs (like http/https).", false, true );
44
		$this->addOption( 'load-from', "Full URL to the API of the wiki to fetch the site info from. "
45
				. "Default is https://meta.wikimedia.org/w/api.php", false, true );
46
		$this->addOption( 'script-path', 'Script path to use for wikis in the site matrix. '
47
				. ' (e.g. "/w/$1")', false, true );
48
		$this->addOption( 'article-path', 'Article path for wikis in the site matrix. '
49
				. ' (e.g. "/wiki/$1")', false, true );
50
		$this->addOption( 'site-group', 'Site group that this wiki is a member of.  Used to populate '
51
				. ' local interwiki identifiers in the site identifiers table.  If not set and --wiki'
52
				. ' is set, the script will try to determine which site group the wiki is part of'
53
				. ' and populate interwiki ids for sites in that group.', false, true );
54
		$this->addOption( 'no-expand-group', 'Do not expand site group codes in site matrix. '
55
				. ' By default, "wiki" is expanded to "wikipedia".' );
56
	}
57
58
	public function execute() {
59
		$stripProtocols = (bool)$this->getOption( 'strip-protocols', false );
60
		$forceProtocol = $this->getOption( 'force-protocol', null );
61
		$url = $this->getOption( 'load-from', 'https://meta.wikimedia.org/w/api.php' );
62
		$scriptPath = $this->getOption( 'script-path', '/w/$1' );
63
		$articlePath = $this->getOption( 'article-path', '/wiki/$1' );
64
		$expandGroup = !$this->getOption( 'no-expand-group', false );
65
		$siteGroup = $this->getOption( 'site-group' );
66
		$wikiId = $this->getOption( 'wiki' );
67
68
		if ( $stripProtocols && is_string( $forceProtocol ) ) {
69
			$this->fatalError( "You can't use both strip-protocols and force-protocol" );
70
		}
71
72
		$protocol = true;
73
		if ( $stripProtocols ) {
74
			$protocol = false;
75
		} elseif ( is_string( $forceProtocol ) ) {
76
			$protocol = $forceProtocol;
77
		}
78
79
		// @todo make it configurable, such as from a config file.
80
		$validGroups = [ 'wikipedia', 'wikivoyage', 'wikiquote', 'wiktionary',
81
			'wikibooks', 'wikisource', 'wikiversity', 'wikinews' ];
82
83
		try {
84
			$json = $this->getSiteMatrixData( $url );
85
86
			$siteMatrixParser = new SiteMatrixParser( $scriptPath, $articlePath,
87
				$protocol, $expandGroup );
88
89
			$sites = $siteMatrixParser->sitesFromJson( $json );
90
91
			$store = MediaWikiServices::getInstance()->getSiteStore();
92
			$sitesBuilder = new SitesBuilder( $store, $validGroups );
93
			$sitesBuilder->buildStore( $sites, $siteGroup, $wikiId );
94
95
		} catch ( MWException $e ) {
0 ignored issues
show
Bug introduced by
The class MWException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
96
			$this->output( $e->getMessage() );
97
		}
98
99
		$this->output( "done.\n" );
100
	}
101
102
	/**
103
	 * @param string $url
104
	 *
105
	 * @throws MWException
106
	 * @return string
107
	 */
108
	protected function getSiteMatrixData( $url ) {
109
		$url .= '?action=sitematrix&format=json';
110
111
		$json = MediaWikiServices::getInstance()->getHttpRequestFactory()->get( $url, [], __METHOD__ );
112
113
		if ( !$json ) {
114
			throw new MWException( "Got no data from $url\n" );
115
		}
116
117
		return $json;
118
	}
119
120
}
121
122
$maintClass = PopulateSitesTable::class;
123
require_once RUN_MAINTENANCE_IF_MAIN;
124