3 * Maintenance script to import all scripts in the MediaWiki namespace from a
9 require_once( dirname( __FILE__
) . '/Maintenance.php' );
11 class ImportSiteScripts
extends Maintenance
{
12 public function __construct() {
13 parent
::__construct();
14 $this->mDescription
= 'Import site scripts from a site';
15 $this->addArg( 'api', 'API base url' );
16 $this->addArg( 'index', 'index.php base url' );
17 $this->addOption( 'username', 'User name of the script importer' );
20 public function execute() {
23 $user = User
::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
26 $baseUrl = $this->getArg( 1 );
27 $pageList = $this->fetchScriptList();
28 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
30 foreach ( $pageList as $page ) {
31 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, $page );
33 $this->error( "$page is an invalid title; it will not be imported\n" );
37 $this->output( "Importing $page\n" );
38 $url = wfAppendQuery( $baseUrl, array(
40 'title' => "MediaWiki:{$page}" ) );
41 $text = Http
::get( $url );
43 $wikiPage = WikiPage
::factory( $title );
44 $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
49 protected function fetchScriptList() {
52 'format' => 'php',//'json',
57 $baseUrl = $this->getArg( 0 );
61 $url = wfAppendQuery( $baseUrl, $data );
62 $strResult = Http
::get( $url );
63 //$result = FormatJson::decode( $strResult ); // Still broken
64 $result = unserialize( $strResult );
66 if ( !empty( $result['query']['allpages'] ) ) {
67 foreach ( $result['query']['allpages'] as $page ) {
68 if ( substr( $page['title'], -3 ) === '.js' ) {
69 strtok( $page['title'], ':' );
70 $pages[] = strtok( '' );
74 if ( !empty( $result['query-continue'] ) ) {
75 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
76 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
78 } while ( isset( $result['query-continue'] ) );
85 $maintClass = 'ImportSiteScripts';
86 require_once( RUN_MAINTENANCE_IF_MAIN
);