Bit more refactoring
[lhc/web/wiklou.git] / maintenance / importSiteScripts.php
1 <?php
2 /**
3 * Maintenance script to import all scripts in the MediaWiki namespace from a
4 * local site.
5 */
6 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
7
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' );
15 }
16
17 public function execute() {
18 global $wgUser;
19 $wgUser = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
20
21 $baseUrl = $this->getArg( 1 );
22 $pageList = $this->fetchScriptList();
23 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
24
25 foreach ( $pageList as $page ) {
26 $this->output( "Importing $page\n" );
27 $url = wfAppendQuery( $baseUrl, array(
28 'action' => 'raw',
29 'title' => "MediaWiki:{$page}" ) );
30 $text = Http::get( $url );
31
32 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
33 $article = new Article( $title );
34 $article->doEdit( $text, "Importing from $url", 0 );
35 }
36
37 }
38
39 protected function fetchScriptList() {
40 $data = array(
41 'action' => 'query',
42 'format' => 'php',//'json',
43 'list' => 'allpages',
44 'apnamespace' => '8',
45 'aplimit' => '500',
46 );
47 $baseUrl = $this->getArg( 0 );
48 $pages = array();
49
50 do {
51 $url = wfAppendQuery( $baseUrl, $data );
52 $strResult = Http::get( $url );
53 //$result = FormatJson::decode( $strResult ); // Still broken
54 $result = unserialize( $strResult );
55
56 if ( !empty( $result['query']['allpages'] ) ) {
57 foreach ( $result['query']['allpages'] as $page ) {
58 if ( substr( $page['title'], -3 ) === '.js' ) {
59 strtok( $page['title'], ':' );
60 $pages[] = strtok( '' );
61 }
62 }
63 }
64 if ( !empty( $result['query-continue'] ) ) {
65 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
66 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
67 }
68 } while ( isset( $result['query-continue'] ) );
69
70 return $pages;
71
72 }
73 }
74
75 $maintClass = 'ImportSiteScripts';
76 require_once( RUN_MAINTENANCE_IF_MAIN );