MediaWiki master
importSiteScripts.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( 'Import site scripts from a site' );
31 $this->addArg( 'api', 'API base url' );
32 $this->addArg( 'index', 'index.php base url' );
33 $this->addOption( 'username', 'User name of the script importer' );
34 $this->setBatchSize( 100 );
35 }
36
37 public function execute() {
38 $username = $this->getOption( 'username', false );
39 if ( $username === false ) {
40 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
41 } else {
42 $user = User::newFromName( $username );
43 }
44 '@phan-var User $user';
45 StubGlobalUser::setUser( $user );
46
47 $baseUrl = $this->getArg( 1 );
48 $pageList = $this->fetchScriptList();
49 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
50 $services = $this->getServiceContainer();
51 $wikiPageFactory = $services->getWikiPageFactory();
52 $httpRequestFactory = $services->getHttpRequestFactory();
53
55 $pageBatches = $this->newBatchIterator( $pageList );
56
57 foreach ( $pageBatches as $pageBatch ) {
58 $this->beginTransactionRound( __METHOD__ );
59 foreach ( $pageBatch as $page ) {
60 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
61 if ( !$title ) {
62 $this->error( "$page is an invalid title; it will not be imported\n" );
63 continue;
64 }
65
66 $this->output( "Importing $page\n" );
67 $url = wfAppendQuery( $baseUrl, [
68 'action' => 'raw',
69 'title' => "MediaWiki:{$page}" ] );
70 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
71
72 $wikiPage = $wikiPageFactory->newFromTitle( $title );
73 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
74
75 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
76 }
77 $this->commitTransactionRound( __METHOD__ );
78 }
79 }
80
81 protected function fetchScriptList(): array {
82 $data = [
83 'action' => 'query',
84 'format' => 'json',
85 'list' => 'allpages',
86 'apnamespace' => '8',
87 'aplimit' => '500',
88 'continue' => '',
89 ];
90 $baseUrl = $this->getArg( 0 );
91 $pages = [];
92
93 while ( true ) {
94 $url = wfAppendQuery( $baseUrl, $data );
95 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
96 get( $url, [], __METHOD__ );
97 $result = FormatJson::decode( $strResult, true );
98
99 $page = null;
100 foreach ( $result['query']['allpages'] as $page ) {
101 if ( str_ends_with( $page['title'], '.js' ) ) {
102 strtok( $page['title'], ':' );
103 $pages[] = strtok( '' );
104 }
105 }
106
107 if ( $page !== null ) {
108 $this->output( "Fetched list up to {$page['title']}\n" );
109 }
110
111 if ( isset( $result['continue'] ) ) { // >= 1.21
112 $data = array_replace( $data, $result['continue'] );
113 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
114 $data = array_replace( $data, $result['query-continue']['allpages'] );
115 } else {
116 break;
117 }
118 }
119
120 return $pages;
121 }
122}
123
124// @codeCoverageIgnoreStart
125$maintClass = ImportSiteScripts::class;
126require_once RUN_MAINTENANCE_IF_MAIN;
127// @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.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:108