Merge "Follow-up Idae8d920 (8c65834): no need to call getContext() and escape the...
[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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class ImportSiteScripts extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = 'Import site scripts from a site';
31 $this->addArg( 'api', 'API base url' );
32 $this->addArg( 'index', 'index.php base url' );
33 $this->addOption( 'username', 'User name of the script importer' );
34 }
35
36 public function execute() {
37 global $wgUser;
38
39 $user = User::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
40 $wgUser = $user;
41
42 $baseUrl = $this->getArg( 1 );
43 $pageList = $this->fetchScriptList();
44 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
45
46 foreach ( $pageList as $page ) {
47 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
48 if ( !$title ) {
49 $this->error( "$page is an invalid title; it will not be imported\n" );
50 continue;
51 }
52
53 $this->output( "Importing $page\n" );
54 $url = wfAppendQuery( $baseUrl, array(
55 'action' => 'raw',
56 'title' => "MediaWiki:{$page}" ) );
57 $text = Http::get( $url );
58
59 $wikiPage = WikiPage::factory( $title );
60 $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
61 }
62
63 }
64
65 protected function fetchScriptList() {
66 $data = array(
67 'action' => 'query',
68 'format' => 'php',//'json',
69 'list' => 'allpages',
70 'apnamespace' => '8',
71 'aplimit' => '500',
72 );
73 $baseUrl = $this->getArg( 0 );
74 $pages = array();
75
76 do {
77 $url = wfAppendQuery( $baseUrl, $data );
78 $strResult = Http::get( $url );
79 //$result = FormatJson::decode( $strResult ); // Still broken
80 $result = unserialize( $strResult );
81
82 if ( !empty( $result['query']['allpages'] ) ) {
83 foreach ( $result['query']['allpages'] as $page ) {
84 if ( substr( $page['title'], -3 ) === '.js' ) {
85 strtok( $page['title'], ':' );
86 $pages[] = strtok( '' );
87 }
88 }
89 }
90 if ( !empty( $result['query-continue'] ) ) {
91 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
92 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
93 }
94 } while ( isset( $result['query-continue'] ) );
95
96 return $pages;
97
98 }
99 }
100
101 $maintClass = 'ImportSiteScripts';
102 require_once( RUN_MAINTENANCE_IF_MAIN );