MediaWiki REL1_32
importSiteScripts.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Import site scripts from a site' );
36 $this->addArg( 'api', 'API base url' );
37 $this->addArg( 'index', 'index.php base url' );
38 $this->addOption( 'username', 'User name of the script importer' );
39 }
40
41 public function execute() {
42 global $wgUser;
43
44 $username = $this->getOption( 'username', false );
45 if ( $username === false ) {
46 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
47 } else {
49 }
50 $wgUser = $user;
51
52 $baseUrl = $this->getArg( 1 );
53 $pageList = $this->fetchScriptList();
54 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
55
56 foreach ( $pageList as $page ) {
57 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
58 if ( !$title ) {
59 $this->error( "$page is an invalid title; it will not be imported\n" );
60 continue;
61 }
62
63 $this->output( "Importing $page\n" );
64 $url = wfAppendQuery( $baseUrl, [
65 'action' => 'raw',
66 'title' => "MediaWiki:{$page}" ] );
67 $text = Http::get( $url, [], __METHOD__ );
68
69 $wikiPage = WikiPage::factory( $title );
70 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
71 $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
72 }
73 }
74
75 protected function fetchScriptList() {
76 $data = [
77 'action' => 'query',
78 'format' => 'json',
79 'list' => 'allpages',
80 'apnamespace' => '8',
81 'aplimit' => '500',
82 'continue' => '',
83 ];
84 $baseUrl = $this->getArg( 0 );
85 $pages = [];
86
87 while ( true ) {
88 $url = wfAppendQuery( $baseUrl, $data );
89 $strResult = Http::get( $url, [], __METHOD__ );
90 $result = FormatJson::decode( $strResult, true );
91
92 $page = null;
93 foreach ( $result['query']['allpages'] as $page ) {
94 if ( substr( $page['title'], -3 ) === '.js' ) {
95 strtok( $page['title'], ':' );
96 $pages[] = strtok( '' );
97 }
98 }
99
100 if ( $page !== null ) {
101 $this->output( "Fetched list up to {$page['title']}\n" );
102 }
103
104 if ( isset( $result['continue'] ) ) { // >= 1.21
105 $data = array_replace( $data, $result['continue'] );
106 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
107 $data = array_replace( $data, $result['query-continue']['allpages'] );
108 } else {
109 break;
110 }
111 }
112
113 return $pages;
114 }
115}
116
117$maintClass = ImportSiteScripts::class;
118require_once RUN_MAINTENANCE_IF_MAIN;
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
static get( $url, $options=[], $caller=__METHOD__)
Simple wrapper for Http::request( 'GET' )
Definition Http.php:98
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...
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.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:819
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2683
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:815
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
require_once RUN_MAINTENANCE_IF_MAIN
$content