MediaWiki  REL1_22
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             $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
00066             $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
00067         }
00068 
00069     }
00070 
00071     protected function fetchScriptList() {
00072         $data = array(
00073             'action' => 'query',
00074             'format' => 'php',//'json',
00075             'list' => 'allpages',
00076             'apnamespace' => '8',
00077             'aplimit' => '500',
00078         );
00079         $baseUrl = $this->getArg( 0 );
00080         $pages = array();
00081 
00082         do {
00083             $url = wfAppendQuery( $baseUrl, $data );
00084             $strResult = Http::get( $url );
00085             //$result = FormatJson::decode( $strResult ); // Still broken
00086             $result = unserialize( $strResult );
00087 
00088             if ( !empty( $result['query']['allpages'] ) ) {
00089                 foreach ( $result['query']['allpages'] as $page ) {
00090                     if ( substr( $page['title'], -3 ) === '.js' ) {
00091                         strtok( $page['title'], ':' );
00092                         $pages[] = strtok( '' );
00093                     }
00094                 }
00095             }
00096             if ( !empty( $result['query-continue'] ) ) {
00097                 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
00098                 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
00099             }
00100         } while ( isset( $result['query-continue'] ) );
00101 
00102         return $pages;
00103 
00104     }
00105 }
00106 
00107 $maintClass = 'ImportSiteScripts';
00108 require_once RUN_MAINTENANCE_IF_MAIN;