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