MediaWiki master
importSiteScripts.php
Go to the documentation of this file.
1<?php
15
16// @codeCoverageIgnoreStart
17require_once __DIR__ . '/Maintenance.php';
18// @codeCoverageIgnoreEnd
19
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'Import site scripts from a site' );
30 $this->addArg( 'api', 'API base url' );
31 $this->addArg( 'index', 'index.php base url' );
32 $this->addOption( 'username', 'User name of the script importer' );
33 $this->setBatchSize( 100 );
34 }
35
36 public function execute() {
37 $username = $this->getOption( 'username', false );
38 if ( $username === false ) {
39 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
40 } else {
41 $user = User::newFromName( $username );
42 }
43 '@phan-var User $user';
44
45 $baseUrl = $this->getArg( 1 );
46 $pageList = $this->fetchScriptList();
47 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
48 $services = $this->getServiceContainer();
49 $wikiPageFactory = $services->getWikiPageFactory();
50 $httpRequestFactory = $services->getHttpRequestFactory();
51
53 $pageBatches = $this->newBatchIterator( $pageList );
54
55 foreach ( $pageBatches as $pageBatch ) {
56 $this->beginTransactionRound( __METHOD__ );
57 foreach ( $pageBatch as $page ) {
58 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
59 if ( !$title ) {
60 $this->error( "$page is an invalid title; it will not be imported\n" );
61 continue;
62 }
63
64 $this->output( "Importing $page\n" );
65 $url = wfAppendQuery( $baseUrl, [
66 'action' => 'raw',
67 'title' => "MediaWiki:{$page}" ] );
68 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
69
70 $wikiPage = $wikiPageFactory->newFromTitle( $title );
71 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
72
73 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
74 }
75 $this->commitTransactionRound( __METHOD__ );
76 }
77 }
78
79 protected function fetchScriptList(): array {
80 $data = [
81 'action' => 'query',
82 'format' => 'json',
83 'list' => 'allpages',
84 'apnamespace' => '8',
85 'aplimit' => '500',
86 'continue' => '',
87 ];
88 $baseUrl = $this->getArg( 0 );
89 $pages = [];
90
91 while ( true ) {
92 $url = wfAppendQuery( $baseUrl, $data );
93 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
94 get( $url, [], __METHOD__ );
95 $result = FormatJson::decode( $strResult, true );
96
97 $page = null;
98 foreach ( $result['query']['allpages'] as $page ) {
99 if ( str_ends_with( $page['title'], '.js' ) ) {
100 strtok( $page['title'], ':' );
101 $pages[] = strtok( '' );
102 }
103 }
104
105 if ( $page !== null ) {
106 $this->output( "Fetched list up to {$page['title']}\n" );
107 }
108
109 if ( isset( $result['continue'] ) ) { // >= 1.21
110 $data = array_replace( $data, $result['continue'] );
111 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
112 $data = array_replace( $data, $result['query-continue']['allpages'] );
113 } else {
114 break;
115 }
116 }
117
118 return $pages;
119 }
120}
121
122// @codeCoverageIgnoreStart
123$maintClass = ImportSiteScripts::class;
124require_once RUN_MAINTENANCE_IF_MAIN;
125// @codeCoverageIgnoreEnd
const NS_MEDIAWIKI
Definition Defines.php:59
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Maintenance script to import all scripts in the MediaWiki namespace from a local site.
execute()
Do the actual work.
__construct()
Default constructor.
Base class for content handling.
JSON formatter wrapper class.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
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.
newBatchIterator( $source)
Wrap an entry iterator into a generator that returns batches of said entries.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:130