3 * Maintenance script to import all scripts in the MediaWiki namespace from a
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.
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.
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
22 * @ingroup Maintenance
25 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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' );
36 public function execute() {
39 $user = User
::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
42 $baseUrl = $this->getArg( 1 );
43 $pageList = $this->fetchScriptList();
44 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
46 foreach ( $pageList as $page ) {
47 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, $page );
49 $this->error( "$page is an invalid title; it will not be imported\n" );
53 $this->output( "Importing $page\n" );
54 $url = wfAppendQuery( $baseUrl, array(
56 'title' => "MediaWiki:{$page}" ) );
57 $text = Http
::get( $url );
59 $wikiPage = WikiPage
::factory( $title );
60 $wikiPage->doEdit( $text, "Importing from $url", 0, false, $user );
65 protected function fetchScriptList() {
68 'format' => 'php',//'json',
73 $baseUrl = $this->getArg( 0 );
77 $url = wfAppendQuery( $baseUrl, $data );
78 $strResult = Http
::get( $url );
79 //$result = FormatJson::decode( $strResult ); // Still broken
80 $result = unserialize( $strResult );
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( '' );
90 if ( !empty( $result['query-continue'] ) ) {
91 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
92 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
94 } while ( isset( $result['query-continue'] ) );
101 $maintClass = 'ImportSiteScripts';
102 require_once( RUN_MAINTENANCE_IF_MAIN
);