* Use WikiPage instead of Article to call doEdit()
[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
20 $user = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
21 $wgUser = $user;
22
23 $baseUrl = $this->getArg( 1 );
24 $pageList = $this->fetchScriptList();
25 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
26
27 foreach ( $pageList as $page ) {
28 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
29 if ( !$title ) {
30 $this->error( "$page is an invalid title; it will not be imported\n" );
31 continue;
32 }
33
34 $this->output( "Importing $page\n" );
35 $url = wfAppendQuery( $baseUrl, array(
36 'action' => 'raw',
37 'title' => "MediaWiki:{$page}" ) );
38 $text = Http::get( $url );
39
40 $wikiPage = WikiPage::factory( $title );
41 $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
42 }
43
44 }
45
46 protected function fetchScriptList() {
47 $data = array(
48 'action' => 'query',
49 'format' => 'php',//'json',
50 'list' => 'allpages',
51 'apnamespace' => '8',
52 'aplimit' => '500',
53 );
54 $baseUrl = $this->getArg( 0 );
55 $pages = array();
56
57 do {
58 $url = wfAppendQuery( $baseUrl, $data );
59 $strResult = Http::get( $url );
60 //$result = FormatJson::decode( $strResult ); // Still broken
61 $result = unserialize( $strResult );
62
63 if ( !empty( $result['query']['allpages'] ) ) {
64 foreach ( $result['query']['allpages'] as $page ) {
65 if ( substr( $page['title'], -3 ) === '.js' ) {
66 strtok( $page['title'], ':' );
67 $pages[] = strtok( '' );
68 }
69 }
70 }
71 if ( !empty( $result['query-continue'] ) ) {
72 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
73 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
74 }
75 } while ( isset( $result['query-continue'] ) );
76
77 return $pages;
78
79 }
80 }
81
82 $maintClass = 'ImportSiteScripts';
83 require_once( RUN_MAINTENANCE_IF_MAIN );