| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Parsoid\Tools; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
require_once __DIR__ . '/Maintenance.php'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class FetchingTool |
| 11 |
* @package Wikimedia\Parsoid\Tools |
| 12 |
*/ |
| 13 |
abstract class FetchingTool extends Maintenance { |
| 14 |
/** @var array Mapping between a prefix and its API URL */ |
| 15 |
private $apiUrls = []; |
| 16 |
|
| 17 |
/** @var array Mapping between a domain and its prefix */ |
| 18 |
private $domainsToPrefix = []; |
| 19 |
|
| 20 |
public function setup(): void { |
| 21 |
parent::setup(); |
| 22 |
$this->loadSiteMatrixForApiConfig(); |
| 23 |
} |
| 24 |
|
| 25 |
/** Loads ./data/wmf.sitematrix.json to get prefix -> domain and domain -> prefix |
| 26 |
* mappings |
| 27 |
* TODO: allow for parametrization of that file place |
| 28 |
*/ |
| 29 |
private function loadSiteMatrixForApiConfig(): void { |
| 30 |
$matrixJson = |
| 31 |
json_decode( file_get_contents( realpath( __DIR__ . |
| 32 |
'/data/wmf.sitematrix.json' ) ) )->sitematrix; |
| 33 |
|
| 34 |
foreach ( $matrixJson as $key => $data ) { |
| 35 |
if ( is_numeric( $key ) ) { |
| 36 |
foreach ( $data->site as $site ) { |
| 37 |
$this->addMatrixEntry( $site->url, $site->dbname ); |
| 38 |
} |
| 39 |
} elseif ( $key === 'specials' ) { |
| 40 |
foreach ( $data as $site ) { |
| 41 |
$this->addMatrixEntry( $site->url, $site->dbname ); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** Add entries mapping prefix to API URL and domain to prefix |
| 48 |
* @param string $url domain of the considered wiki |
| 49 |
* @param string $prefix prefix of the considered wiki |
| 50 |
*/ |
| 51 |
private function addMatrixEntry( string $url, string $prefix ): void { |
| 52 |
$this->apiUrls[$prefix] = $url . "/w/api.php"; |
| 53 |
$domain = parse_url( $url )['host']; |
| 54 |
$this->domainsToPrefix[$domain] = $prefix; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Gets the domain and prefix from the CLI options |
| 59 |
* @return array containing values for 'prefix' and 'domain' keys |
| 60 |
*/ |
| 61 |
public function getDomainAndPrefix(): array { |
| 62 |
$prefix = $this->hasOption( 'prefix' ) ? $this->getOption( 'prefix' ) : null; |
| 63 |
$domain = $this->hasOption( 'domain' ) ? $this->getOption( 'domain' ) : null; |
| 64 |
|
| 65 |
if ( $this->hasOption( 'apiURL' ) ) { |
| 66 |
$prefix = 'customwiki'; |
| 67 |
$url = $this->getOption( 'apiURL' ); |
| 68 |
$domain = parse_url( $url )['host']; |
| 69 |
$this->apiUrls[$prefix] = $url; |
| 70 |
$this->domainsToPrefix[$domain] = $prefix; |
| 71 |
} elseif ( !isset( $prefix ) && !isset( $domain ) ) { |
| 72 |
$domain = 'en.wikipedia.org'; |
| 73 |
} |
| 74 |
|
| 75 |
return [ 'domain' => $domain, 'prefix' => $prefix ]; |
| 76 |
} |
| 77 |
|
| 78 |
/** Creates options for the API call depending on the domain and prefix, and filling in the |
| 79 |
* API endpoint based on the $apiUrls |
| 80 |
* @param array $opts array containing at least one value for the 'prefix' or 'domain' keys |
| 81 |
* (the 'domain' key has priority) |
| 82 |
* @return array containing the API call parameters |
| 83 |
*/ |
| 84 |
public function getApiCall( array $opts ): array { |
| 85 |
$options = $opts ?? []; |
| 86 |
if ( array_key_exists( 'domain', $options ) && isset( $options['domain'] ) ) { |
| 87 |
$options['prefix'] = $this->domainsToPrefix[$options['domain']]; |
| 88 |
} |
| 89 |
if ( !array_key_exists( 'prefix', $options ) || |
| 90 |
!array_key_exists( $options['prefix'], $this->apiUrls ) ) { |
| 91 |
throw new InvalidArgumentException( 'No API URI available for prefix: ' . |
| 92 |
$options['prefix'] . '; domain: ' . $options['domain'] ); |
| 93 |
} |
| 94 |
$options['apiEndpoint'] = $this->apiUrls[$options['prefix']]; |
| 95 |
|
| 96 |
return $options; |
| 97 |
} |
| 98 |
|
| 99 |
} |