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