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