MediaWiki master
importSiteScripts.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Import site scripts from a site' );
41 $this->addArg( 'api', 'API base url' );
42 $this->addArg( 'index', 'index.php base url' );
43 $this->addOption( 'username', 'User name of the script importer' );
44 }
45
46 public function execute() {
47 $username = $this->getOption( 'username', false );
48 if ( $username === false ) {
49 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
50 } else {
51 $user = User::newFromName( $username );
52 }
53 '@phan-var User $user';
54 StubGlobalUser::setUser( $user );
55
56 $baseUrl = $this->getArg( 1 );
57 $pageList = $this->fetchScriptList();
58 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
59 $services = $this->getServiceContainer();
60 $wikiPageFactory = $services->getWikiPageFactory();
61 $httpRequestFactory = $services->getHttpRequestFactory();
62
63 foreach ( $pageList as $page ) {
64 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
65 if ( !$title ) {
66 $this->error( "$page is an invalid title; it will not be imported\n" );
67 continue;
68 }
69
70 $this->output( "Importing $page\n" );
71 $url = wfAppendQuery( $baseUrl, [
72 'action' => 'raw',
73 'title' => "MediaWiki:{$page}" ] );
74 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
75
76 $wikiPage = $wikiPageFactory->newFromTitle( $title );
77 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
78 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
79 }
80 }
81
82 protected function fetchScriptList() {
83 $data = [
84 'action' => 'query',
85 'format' => 'json',
86 'list' => 'allpages',
87 'apnamespace' => '8',
88 'aplimit' => '500',
89 'continue' => '',
90 ];
91 $baseUrl = $this->getArg( 0 );
92 $pages = [];
93
94 while ( true ) {
95 $url = wfAppendQuery( $baseUrl, $data );
96 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
97 get( $url, [], __METHOD__ );
98 $result = FormatJson::decode( $strResult, true );
99
100 $page = null;
101 foreach ( $result['query']['allpages'] as $page ) {
102 if ( str_ends_with( $page['title'], '.js' ) ) {
103 strtok( $page['title'], ':' );
104 $pages[] = strtok( '' );
105 }
106 }
107
108 if ( $page !== null ) {
109 $this->output( "Fetched list up to {$page['title']}\n" );
110 }
111
112 if ( isset( $result['continue'] ) ) { // >= 1.21
113 $data = array_replace( $data, $result['continue'] );
114 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
115 $data = array_replace( $data, $result['query-continue']['allpages'] );
116 } else {
117 break;
118 }
119 }
120
121 return $pages;
122 }
123}
124
125$maintClass = ImportSiteScripts::class;
126require_once RUN_MAINTENANCE_IF_MAIN;
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.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
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.
JSON formatter wrapper class.
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
internal since 1.36
Definition User.php:93