Big oops - merged to wrong branch.
[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 * @file
6 * @ingroup Maintenance
7 */
8
9 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
10
11 class ImportSiteScripts extends Maintenance {
12 public function __construct() {
13 parent::__construct();
14 $this->mDescription = 'Import site scripts from a site';
15 $this->addArg( 'api', 'API base url' );
16 $this->addArg( 'index', 'index.php base url' );
17 $this->addOption( 'username', 'User name of the script importer' );
18 }
19
20 public function execute() {
21 global $wgUser;
22
23 $user = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
24 $wgUser = $user;
25
26 $baseUrl = $this->getArg( 1 );
27 $pageList = $this->fetchScriptList();
28 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
29
30 foreach ( $pageList as $page ) {
31 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
32 if ( !$title ) {
33 $this->error( "$page is an invalid title; it will not be imported\n" );
34 continue;
35 }
36
37 $this->output( "Importing $page\n" );
38 $url = wfAppendQuery( $baseUrl, array(
39 'action' => 'raw',
40 'title' => "MediaWiki:{$page}" ) );
41 $text = Http::get( $url );
42
43 $wikiPage = WikiPage::factory( $title );
44 $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
45 }
46
47 }
48
49 protected function fetchScriptList() {
50 $data = array(
51 'action' => 'query',
52 'format' => 'php',//'json',
53 'list' => 'allpages',
54 'apnamespace' => '8',
55 'aplimit' => '500',
56 );
57 $baseUrl = $this->getArg( 0 );
58 $pages = array();
59
60 do {
61 $url = wfAppendQuery( $baseUrl, $data );
62 $strResult = Http::get( $url );
63 //$result = FormatJson::decode( $strResult ); // Still broken
64 $result = unserialize( $strResult );
65
66 if ( !empty( $result['query']['allpages'] ) ) {
67 foreach ( $result['query']['allpages'] as $page ) {
68 if ( substr( $page['title'], -3 ) === '.js' ) {
69 strtok( $page['title'], ':' );
70 $pages[] = strtok( '' );
71 }
72 }
73 }
74 if ( !empty( $result['query-continue'] ) ) {
75 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
76 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
77 }
78 } while ( isset( $result['query-continue'] ) );
79
80 return $pages;
81
82 }
83 }
84
85 $maintClass = 'ImportSiteScripts';
86 require_once( RUN_MAINTENANCE_IF_MAIN );