MediaWiki master
importSiteScripts.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Import site scripts from a site' );
40 $this->addArg( 'api', 'API base url' );
41 $this->addArg( 'index', 'index.php base url' );
42 $this->addOption( 'username', 'User name of the script importer' );
43 }
44
45 public function execute() {
46 $username = $this->getOption( 'username', false );
47 if ( $username === false ) {
48 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
49 } else {
50 $user = User::newFromName( $username );
51 }
52 '@phan-var User $user';
53 StubGlobalUser::setUser( $user );
54
55 $baseUrl = $this->getArg( 1 );
56 $pageList = $this->fetchScriptList();
57 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
58 $services = $this->getServiceContainer();
59 $wikiPageFactory = $services->getWikiPageFactory();
60 $httpRequestFactory = $services->getHttpRequestFactory();
61
62 foreach ( $pageList as $page ) {
63 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
64 if ( !$title ) {
65 $this->error( "$page is an invalid title; it will not be imported\n" );
66 continue;
67 }
68
69 $this->output( "Importing $page\n" );
70 $url = wfAppendQuery( $baseUrl, [
71 'action' => 'raw',
72 'title' => "MediaWiki:{$page}" ] );
73 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
74
75 $wikiPage = $wikiPageFactory->newFromTitle( $title );
76 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
77 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
78 }
79 }
80
81 protected function fetchScriptList() {
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$maintClass = ImportSiteScripts::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
const NS_MEDIAWIKI
Definition Defines.php:72
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.
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