MediaWiki  REL1_19
importSiteScripts.php
Go to the documentation of this file.
00001 <?php
00009 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00010 
00011 class ImportSiteScripts extends Maintenance {
00012         public function __construct() {
00013                 parent::__construct();
00014                 $this->mDescription = 'Import site scripts from a site';
00015                 $this->addArg( 'api', 'API base url' );
00016                 $this->addArg( 'index', 'index.php base url' );
00017                 $this->addOption( 'username', 'User name of the script importer' );
00018         }
00019         
00020         public function execute() {
00021                 global $wgUser;
00022 
00023                 $user = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
00024                 $wgUser = $user;
00025                 
00026                 $baseUrl = $this->getArg( 1 );
00027                 $pageList = $this->fetchScriptList();
00028                 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
00029                 
00030                 foreach ( $pageList as $page ) {
00031                         $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
00032                         if ( !$title ) {
00033                                 $this->error( "$page is an invalid title; it will not be imported\n" );
00034                                 continue;
00035                         }
00036 
00037                         $this->output( "Importing $page\n" );
00038                         $url = wfAppendQuery( $baseUrl, array( 
00039                                 'action' => 'raw', 
00040                                 'title' => "MediaWiki:{$page}" ) );
00041                         $text = Http::get( $url );
00042 
00043                         $wikiPage = WikiPage::factory( $title );
00044                         $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
00045                 }
00046                 
00047         }
00048         
00049         protected function fetchScriptList() {
00050                 $data = array( 
00051                         'action' => 'query',
00052                         'format' => 'php',//'json',
00053                         'list' => 'allpages',
00054                         'apnamespace' => '8',
00055                         'aplimit' => '500', 
00056                 );
00057                 $baseUrl = $this->getArg( 0 );
00058                 $pages = array();
00059                 
00060                 do {
00061                         $url = wfAppendQuery( $baseUrl, $data );
00062                         $strResult = Http::get( $url );
00063                         //$result = FormatJson::decode( $strResult ); // Still broken
00064                         $result = unserialize( $strResult );
00065                         
00066                         if ( !empty( $result['query']['allpages'] ) ) {
00067                                 foreach ( $result['query']['allpages'] as $page ) {
00068                                         if ( substr( $page['title'], -3 ) === '.js' ) {
00069                                                 strtok( $page['title'], ':' );
00070                                                 $pages[] = strtok( '' );
00071                                         }
00072                                 }
00073                         }
00074                         if ( !empty( $result['query-continue'] ) ) {
00075                                 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
00076                                 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
00077                         }
00078                 } while ( isset( $result['query-continue'] ) );
00079                 
00080                 return $pages;
00081                 
00082         }
00083 }
00084 
00085 $maintClass = 'ImportSiteScripts';
00086 require_once( RUN_MAINTENANCE_IF_MAIN );