Debian Codegraph @

vendor/wikimedia/parsoid/tools/SyncBaseConfig.php

raw
1 <?php
2
3 namespace Wikimedia\Parsoid\Tools;
4
5 require_once __DIR__ . '/FetchingTool.php';
6
7 use Wikimedia\Parsoid\Config\Api\ApiHelper;
8 use Wikimedia\Parsoid\Config\Api\SiteConfig;
9
10 /**
11 * Class SyncBaseConfig
12 * @package Wikimedia\Parsoid\Tools
13 * This tool allows to sync the parsoid/baseconfig files with the latest version running. See
14 * README in that directory for more information.
15 */
16
17 class SyncBaseConfig extends FetchingTool {
18 /** Creates supported parameters and description for the syncbaseconfig script and adds the
19 * generic ones
20 */
21 public function addDefaultParams(): void {
22 $this->addOption( 'prefix',
23 'Which wiki prefix to use; e.g. "enwiki" for English wikipedia, "eswiki" ' .
24 'for Spanish, "mediawikiwiki" for mediawiki.org' );
25
26 $this->addOption( 'domain', 'Which wiki to use; e.g. "en.wikipedia.org" ' .
27 'for English wikipedia, "es.wikipedia.org" for Spanish, ' .
28 '"www.mediawiki.org" for mediawiki.org' );
29
30 $this->addOption( 'formatversion',
31 'Which formatversion to use for the JSON response format (1 or 2); default is 1' );
32
33 $this->addOption( 'apiURL', 'The API URL of a custom wiki to use (for wikis ' .
34 'that are not part of the list provided in wmf.sitematrix.json). The corresponding ' .
35 'file is stored as [wikiId].json.' );
36
37 $this->addDescription( 'Rewrites one cached siteinfo configuration.' .
38 'Use --domain, --prefix or --apiURL to select which one to rewrite.' );
39
40 parent::addDefaultParams();
41 }
42
43 /** Update an individual baseconfig file corresponding to a single format version of a single
44 * prefix/domain
45 */
46 public function execute() {
47 $this->maybeHelp();
48
49 $dompref = $this->getDomainAndPrefix();
50 $apiCall = $this->getApiCall( $dompref );
51 $apiHelper = new ApiHelper( $apiCall );
52
53 // API JSON response format version: see https://www.mediawiki.org/wiki/API:JSON_version_2
54 $formatVersion =
55 $this->hasOption( 'formatversion' ) ? $this->getOption( 'formatversion' ) : "2";
56
57 $apiReqParameters = $this->getApiReqParameters( $formatVersion );
58 $response = $apiHelper->makeRequest( $apiReqParameters );
59
60 $fileName = $this->getFileName( $response['query']['general']['wikiid'], $formatVersion );
61
62 $strippedResponse = [
63 'query' => $response['query'],
64 ];
65 $resultStr =
66 json_encode( $strippedResponse,
67 JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
68 file_put_contents( $fileName, $resultStr );
69
70 print( "Wrote $fileName\n" );
71 }
72
73 /** Creates parameters for the API call to get the baseconfig file
74 * @param int $formatversion the desired format version of the JSON response
75 * @return array of parameters for the API call
76 */
77 private function getApiReqParameters( int $formatversion ): array {
78 $res = SiteConfig::SITE_CONFIG_QUERY_PARAMS;
79 $res['formatversion'] = $formatversion;
80 $res['format'] = 'json';
81 $res['rawcontinue'] = 1;
82
83 return $res;
84 }
85
86 /** Creates the full file name from the wikiId and formatVersion
87 * @param string $wikiId wiki identifier (https://www.mediawiki.org/wiki/Manual:Wiki_ID)
88 * @param string $formatVersion either "1" or "2"
89 * @return string the full path of the file where the baseconfig will be stored
90 */
91 private function getFileName( string $wikiId, string $formatVersion ): string {
92 // HACK for be-tarask
93 if ( $wikiId === 'be_x_oldwiki' ) {
94 $wikiId = 'be-taraskwiki';
95 }
96
97 $configDir = realpath( __DIR__ . "/.." );
98
99 return $configDir . '/baseconfig/' . ( ( $formatVersion !== "2" ) ? "$formatVersion/" : '' ) . $wikiId .
100 '.json';
101 }
102 }
103
104 $maintClass = SyncBaseConfig::class;
105 require_once PARSOID_RUN_MAINTENANCE_IF_MAIN;