MediaWiki REL1_39
importSiteScripts.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Import site scripts from a site' );
38 $this->addArg( 'api', 'API base url' );
39 $this->addArg( 'index', 'index.php base url' );
40 $this->addOption( 'username', 'User name of the script importer' );
41 }
42
43 public function execute() {
44 $username = $this->getOption( 'username', false );
45 if ( $username === false ) {
46 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
47 } else {
48 $user = User::newFromName( $username );
49 }
50 '@phan-var User $user';
52
53 $baseUrl = $this->getArg( 1 );
54 $pageList = $this->fetchScriptList();
55 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
56 $services = MediaWikiServices::getInstance();
57 $wikiPageFactory = $services->getWikiPageFactory();
58 $httpRequestFactory = $services->getHttpRequestFactory();
59
60 foreach ( $pageList as $page ) {
61 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
62 if ( !$title ) {
63 $this->error( "$page is an invalid title; it will not be imported\n" );
64 continue;
65 }
66
67 $this->output( "Importing $page\n" );
68 $url = wfAppendQuery( $baseUrl, [
69 'action' => 'raw',
70 'title' => "MediaWiki:{$page}" ] );
71 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
72
73 $wikiPage = $wikiPageFactory->newFromTitle( $title );
74 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
75 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
76 }
77 }
78
79 protected function fetchScriptList() {
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 = MediaWikiServices::getInstance()->getHttpRequestFactory()->
94 get( $url, [], __METHOD__ );
95 $result = FormatJson::decode( $strResult, true );
96
97 $page = null;
98 foreach ( $result['query']['allpages'] as $page ) {
99 if ( substr( $page['title'], -3 ) === '.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$maintClass = ImportSiteScripts::class;
123require_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)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
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.
Service locator for MediaWiki core services.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromName( $name, $validate='valid')
Definition User.php:598
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:806
$content
Definition router.php:76