r82932 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82931‎ | r82932 | r82933 >
Date:13:54, 28 February 2011
Author:btongminh
Status:ok
Tags:
Comment:
Maintenance script to import all scripts in the MediaWiki namespace from a local site.
Useful for local debugging of broken scripts.
Modified paths:
  • /trunk/phase3/maintenance/importSiteScripts.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/importSiteScripts.php
@@ -0,0 +1,76 @@
 2+<?php
 3+/**
 4+ * Maintenance script to import all scripts in the MediaWiki namespace from a
 5+ * local site.
 6+ */
 7+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 8+
 9+class ImportSiteScripts extends Maintenance {
 10+ public function __construct() {
 11+ parent::__construct();
 12+ $this->mDescription = 'Import site scripts from a site';
 13+ $this->addArg( 'api', 'API base url' );
 14+ $this->addArg( 'index', 'index.php base url' );
 15+ $this->addOption( 'username', 'User name of the script importer' );
 16+ }
 17+
 18+ public function execute() {
 19+ global $wgUser;
 20+ $wgUser = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
 21+
 22+ $baseUrl = $this->getArg( 1 );
 23+ $pageList = $this->fetchScriptList();
 24+ $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
 25+
 26+ foreach ( $pageList as $page ) {
 27+ $this->output( "Importing $page\n" );
 28+ $url = wfAppendQuery( $baseUrl, array(
 29+ 'action' => 'raw',
 30+ 'title' => "MediaWiki:{$page}" ) );
 31+ $text = Http::get( $url );
 32+
 33+ $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
 34+ $article = new Article( $title );
 35+ $article->doEdit( $text, "Importing from $url", 0 );
 36+ }
 37+
 38+ }
 39+
 40+ protected function fetchScriptList() {
 41+ $data = array(
 42+ 'action' => 'query',
 43+ 'format' => 'php',//'json',
 44+ 'list' => 'allpages',
 45+ 'apnamespace' => '8',
 46+ 'aplimit' => '500',
 47+ );
 48+ $baseUrl = $this->getArg( 0 );
 49+ $pages = array();
 50+
 51+ do {
 52+ $url = wfAppendQuery( $baseUrl, $data );
 53+ $strResult = Http::get( $url );
 54+ //$result = FormatJson::decode( $strResult ); // Still broken
 55+ $result = unserialize( $strResult );
 56+
 57+ if ( !empty( $result['query']['allpages'] ) ) {
 58+ foreach ( $result['query']['allpages'] as $page ) {
 59+ if ( substr( $page['title'], -3 ) === '.js' ) {
 60+ strtok( $page['title'], ':' );
 61+ $pages[] = strtok( '' );
 62+ }
 63+ }
 64+ }
 65+ if ( !empty( $result['query-continue'] ) ) {
 66+ $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
 67+ $this->output( "Fetching new batch from {$data['apfrom']}\n" );
 68+ }
 69+ } while ( isset( $result['query-continue'] ) );
 70+
 71+ return $pages;
 72+
 73+ }
 74+}
 75+
 76+$maintClass = 'ImportSiteScripts';
 77+require_once( RUN_MAINTENANCE_IF_MAIN );
Property changes on: trunk/phase3/maintenance/importSiteScripts.php
___________________________________________________________________
Added: svn:eol-style
178 + native

Status & tagging log