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 use MediaWiki\MediaWikiServices
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to import all scripts in the MediaWiki namespace from a
32 * @ingroup Maintenance
34 class ImportSiteScripts
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Import site scripts from a site' );
38 $this->addArg( 'api', 'API base url' );
39 $this->addArg( 'index', 'index.php base url' );
40 $this->addOption( 'username', 'User name of the script importer' );
43 public function execute() {
46 $username = $this->getOption( 'username', false );
47 if ( $username === false ) {
48 $user = User
::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
50 $user = User
::newFromName( $username );
54 $baseUrl = $this->getArg( 1 );
55 $pageList = $this->fetchScriptList();
56 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
58 foreach ( $pageList as $page ) {
59 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, $page );
61 $this->error( "$page is an invalid title; it will not be imported\n" );
65 $this->output( "Importing $page\n" );
66 $url = wfAppendQuery( $baseUrl, [
68 'title' => "MediaWiki:{$page}" ] );
69 $text = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
70 get( $url, [], __METHOD__
);
72 $wikiPage = WikiPage
::factory( $title );
73 $content = ContentHandler
::makeContent( $text, $wikiPage->getTitle() );
74 $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
78 protected function fetchScriptList() {
87 $baseUrl = $this->getArg( 0 );
91 $url = wfAppendQuery( $baseUrl, $data );
92 $strResult = MediaWikiServices
::getInstance()->getHttpRequestFactory()->
93 get( $url, [], __METHOD__
);
94 $result = FormatJson
::decode( $strResult, true );
97 foreach ( $result['query']['allpages'] as $page ) {
98 if ( substr( $page['title'], -3 ) === '.js' ) {
99 strtok( $page['title'], ':' );
100 $pages[] = strtok( '' );
104 if ( $page !== null ) {
105 $this->output( "Fetched list up to {$page['title']}\n" );
108 if ( isset( $result['continue'] ) ) { // >= 1.21
109 $data = array_replace( $data, $result['continue'] );
110 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
111 $data = array_replace( $data, $result['query-continue']['allpages'] );
121 $maintClass = ImportSiteScripts
::class;
122 require_once RUN_MAINTENANCE_IF_MAIN
;