3 * Import all scripts in the MediaWiki namespace from a local site.
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.
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.
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
21 * @ingroup Maintenance
24 require_once( __DIR__
. '/Maintenance.php' );
27 * Maintenance script to import all scripts in the MediaWiki namespace from a
30 * @ingroup Maintenance
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' );
41 public function execute() {
44 $user = User
::newFromName( $this->getOption( 'username', 'ScriptImporter' ) );
47 $baseUrl = $this->getArg( 1 );
48 $pageList = $this->fetchScriptList();
49 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
51 foreach ( $pageList as $page ) {
52 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, $page );
54 $this->error( "$page is an invalid title; it will not be imported\n" );
58 $this->output( "Importing $page\n" );
59 $url = wfAppendQuery( $baseUrl, array(
61 'title' => "MediaWiki:{$page}" ) );
62 $text = Http
::get( $url );
64 $wikiPage = WikiPage
::factory( $title );
65 $content = ContentHandler
::makeContent( $text, $wikiPage->getTitle() );
66 $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
71 protected function fetchScriptList() {
74 'format' => 'php',//'json',
79 $baseUrl = $this->getArg( 0 );
83 $url = wfAppendQuery( $baseUrl, $data );
84 $strResult = Http
::get( $url );
85 //$result = FormatJson::decode( $strResult ); // Still broken
86 $result = unserialize( $strResult );
88 if ( !empty( $result['query']['allpages'] ) ) {
89 foreach ( $result['query']['allpages'] as $page ) {
90 if ( substr( $page['title'], -3 ) === '.js' ) {
91 strtok( $page['title'], ':' );
92 $pages[] = strtok( '' );
96 if ( !empty( $result['query-continue'] ) ) {
97 $data['apfrom'] = $result['query-continue']['allpages']['apfrom'];
98 $this->output( "Fetching new batch from {$data['apfrom']}\n" );
100 } while ( isset( $result['query-continue'] ) );
107 $maintClass = 'ImportSiteScripts';
108 require_once( RUN_MAINTENANCE_IF_MAIN
);