Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / maintenance / importSiteScripts.php
blob7fdb355a91aa88cb22f71b6d15fe7fd4f33056c8
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to import all scripts in the MediaWiki namespace from a
28 * local site.
30 * @ingroup Maintenance
32 class ImportSiteScripts extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( '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() {
42 global $wgUser;
44 $username = $this->getOption( 'username', false );
45 if ( $username === false ) {
46 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
47 } else {
48 $user = User::newFromName( $username );
50 $wgUser = $user;
52 $baseUrl = $this->getArg( 1 );
53 $pageList = $this->fetchScriptList();
54 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
56 foreach ( $pageList as $page ) {
57 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
58 if ( !$title ) {
59 $this->error( "$page is an invalid title; it will not be imported\n" );
60 continue;
63 $this->output( "Importing $page\n" );
64 $url = wfAppendQuery( $baseUrl, [
65 'action' => 'raw',
66 'title' => "MediaWiki:{$page}" ] );
67 $text = Http::get( $url, [], __METHOD__ );
69 $wikiPage = WikiPage::factory( $title );
70 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
71 $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
75 protected function fetchScriptList() {
76 $data = [
77 'action' => 'query',
78 'format' => 'json',
79 'list' => 'allpages',
80 'apnamespace' => '8',
81 'aplimit' => '500',
82 'continue' => '',
84 $baseUrl = $this->getArg( 0 );
85 $pages = [];
87 while ( true ) {
88 $url = wfAppendQuery( $baseUrl, $data );
89 $strResult = Http::get( $url, [], __METHOD__ );
90 $result = FormatJson::decode( $strResult, true );
92 $page = null;
93 foreach ( $result['query']['allpages'] as $page ) {
94 if ( substr( $page['title'], -3 ) === '.js' ) {
95 strtok( $page['title'], ':' );
96 $pages[] = strtok( '' );
100 if ( $page !== null ) {
101 $this->output( "Fetched list up to {$page['title']}\n" );
104 if ( isset( $result['continue'] ) ) { // >= 1.21
105 $data = array_replace( $data, $result['continue'] );
106 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
107 $data = array_replace( $data, $result['query-continue']['allpages'] );
108 } else {
109 break;
113 return $pages;
117 $maintClass = 'ImportSiteScripts';
118 require_once RUN_MAINTENANCE_IF_MAIN;