Debian Codegraph @

vendor/wikimedia/parsoid/tools/FetchWt.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
9 /**
10 * Class FetchWt
11 * Fetch the wikitext for a page, given title or revision id.
12 *
13 * This is very useful for extracting test cases which can then be passed
14 * to bin/parse.php.
15 */
16 class FetchWt extends FetchingTool {
17
18 /** Creates supported parameters and description for the fetchwt script and adds the
19 * generic ones
20 */
21 public function addDefaultParams(): void {
22 $this->addOption( 'output', 'Write page to given file', false, true );
23
24 $this->addOption( 'prefix',
25 'Which wiki prefix to use; e.g. "enwiki" for English wikipedia, "eswiki" ' .
26 'for Spanish, "mediawikiwiki" for mediawiki.org', false, true );
27
28 $this->addOption( 'domain', 'Which wiki to use; e.g. "en.wikipedia.org" ' .
29 'for English wikipedia, "es.wikipedia.org" for Spanish, ' .
30 '"www.mediawiki.org" for mediawiki.org', false, true );
31
32 $this->addOption( 'revid', 'Page revision to fetch', false, true );
33
34 $this->addOption( 'title', 'Page title to fetch (only if revid is not present)',
35 false, true );
36
37 parent::addDefaultParams();
38 }
39
40 public function execute() {
41 $this->maybeHelp();
42
43 if ( $this->hasOption( 'title' ) && $this->hasOption( 'revid' ) ) {
44 die( 'Can\'t specify title and revid at the same time.' );
45 }
46 if ( !$this->hasOption( 'title' ) && !$this->hasOption( 'revid' ) ) {
47 die( 'Must specify a title or revision id.' );
48 }
49
50 $configOpts = [];
51 if ( $this->hasOption( 'title' ) ) {
52 $configOpts['title'] = $this->getOption( 'title' );
53 }
54
55 if ( $this->hasOption( 'revid' ) ) {
56 $configOpts['revId'] = (int)$this->getOption( 'revid' );
57 }
58
59 $dompref = $this->getDomainAndPrefix();
60 $apiCall = $this->getApiCall( $dompref );
61 $apiHelper = new ApiHelper( $apiCall );
62
63 $apiArgs = $this->createApiArgs( $configOpts );
64 $response = $apiHelper->makeRequest( $apiArgs );
65
66 if ( isset( $response['query']['pages'] ) ) {
67 $page = $response['query']['pages'][0];
68 if ( isset( $page['revisions'] ) ) {
69 $content = $page['revisions'][0]['content'];
70 if ( $this->hasOption( 'output' ) ) {
71 file_put_contents( $this->getOption( 'output' ), $content );
72 } else {
73 $this->output( $content );
74 }
75 } else {
76 die( 'Could not retrieve page content' );
77 }
78 } else {
79 die( 'Could not retrieve page content' );
80 }
81 }
82
83 /**
84 * Create the array of options needed for the API call
85 * @param array $configOpts
86 * @return array
87 */
88 private function createApiArgs( array $configOpts ): array {
89 $apiArgs = [
90 'format' => 'json',
91 'action' => 'query',
92 'prop' => 'revisions',
93 'rawcontinue' => 1,
94 'rvprop' => 'content'
95 ];
96
97 if ( isset( $configOpts['revId'] ) ) {
98 $apiArgs['revids'] = $configOpts['revId'];
99 } else {
100 $apiArgs['titles'] = $configOpts['title'];
101 }
102 return $apiArgs;
103 }
104 }
105
106 $maintClass = FetchWt::class;
107 require_once PARSOID_RUN_MAINTENANCE_IF_MAIN;