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