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\Content\ContentHandler
;
25 use MediaWiki\Json\FormatJson
;
26 use MediaWiki\Maintenance\Maintenance
;
27 use MediaWiki\StubObject\StubGlobalUser
;
28 use MediaWiki\Title\Title
;
29 use MediaWiki\User\User
;
31 // @codeCoverageIgnoreStart
32 require_once __DIR__
. '/Maintenance.php';
33 // @codeCoverageIgnoreEnd
36 * Maintenance script to import all scripts in the MediaWiki namespace from a
39 * @ingroup Maintenance
41 class ImportSiteScripts
extends Maintenance
{
42 public function __construct() {
43 parent
::__construct();
44 $this->addDescription( 'Import site scripts from a site' );
45 $this->addArg( 'api', 'API base url' );
46 $this->addArg( 'index', 'index.php base url' );
47 $this->addOption( 'username', 'User name of the script importer' );
48 $this->setBatchSize( 100 );
51 public function execute() {
52 $username = $this->getOption( 'username', false );
53 if ( $username === false ) {
54 $user = User
::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
56 $user = User
::newFromName( $username );
58 '@phan-var User $user';
59 StubGlobalUser
::setUser( $user );
61 $baseUrl = $this->getArg( 1 );
62 $pageList = $this->fetchScriptList();
63 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
64 $services = $this->getServiceContainer();
65 $wikiPageFactory = $services->getWikiPageFactory();
66 $httpRequestFactory = $services->getHttpRequestFactory();
68 /** @var iterable<Title[]> $pageBatches */
69 $pageBatches = $this->newBatchIterator( $pageList );
71 foreach ( $pageBatches as $pageBatch ) {
72 $this->beginTransactionRound( __METHOD__
);
73 foreach ( $pageBatch as $page ) {
74 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, $page );
76 $this->error( "$page is an invalid title; it will not be imported\n" );
80 $this->output( "Importing $page\n" );
81 $url = wfAppendQuery( $baseUrl, [
83 'title' => "MediaWiki:{$page}" ] );
84 $text = $httpRequestFactory->get( $url, [], __METHOD__
);
86 $wikiPage = $wikiPageFactory->newFromTitle( $title );
87 $content = ContentHandler
::makeContent( $text, $wikiPage->getTitle() );
89 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
91 $this->commitTransactionRound( __METHOD__
);
95 protected function fetchScriptList() {
100 'apnamespace' => '8',
104 $baseUrl = $this->getArg( 0 );
108 $url = wfAppendQuery( $baseUrl, $data );
109 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
110 get( $url, [], __METHOD__
);
111 $result = FormatJson
::decode( $strResult, true );
114 foreach ( $result['query']['allpages'] as $page ) {
115 if ( str_ends_with( $page['title'], '.js' ) ) {
116 strtok( $page['title'], ':' );
117 $pages[] = strtok( '' );
121 if ( $page !== null ) {
122 $this->output( "Fetched list up to {$page['title']}\n" );
125 if ( isset( $result['continue'] ) ) { // >= 1.21
126 $data = array_replace( $data, $result['continue'] );
127 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
128 $data = array_replace( $data, $result['query-continue']['allpages'] );
138 // @codeCoverageIgnoreStart
139 $maintClass = ImportSiteScripts
::class;
140 require_once RUN_MAINTENANCE_IF_MAIN
;
141 // @codeCoverageIgnoreEnd