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