Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / importSiteScripts.php
blobabc66b5ff79121638c55898beefab75653d8f186
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 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
35 /**
36 * Maintenance script to import all scripts in the MediaWiki namespace from a
37 * local site.
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 ] );
55 } else {
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 );
75 if ( !$title ) {
76 $this->error( "$page is an invalid title; it will not be imported\n" );
77 continue;
80 $this->output( "Importing $page\n" );
81 $url = wfAppendQuery( $baseUrl, [
82 'action' => 'raw',
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() {
96 $data = [
97 'action' => 'query',
98 'format' => 'json',
99 'list' => 'allpages',
100 'apnamespace' => '8',
101 'aplimit' => '500',
102 'continue' => '',
104 $baseUrl = $this->getArg( 0 );
105 $pages = [];
107 while ( true ) {
108 $url = wfAppendQuery( $baseUrl, $data );
109 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
110 get( $url, [], __METHOD__ );
111 $result = FormatJson::decode( $strResult, true );
113 $page = null;
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'] );
129 } else {
130 break;
134 return $pages;
138 // @codeCoverageIgnoreStart
139 $maintClass = ImportSiteScripts::class;
140 require_once RUN_MAINTENANCE_IF_MAIN;
141 // @codeCoverageIgnoreEnd