Debian Codegraph @

maintenance/importSiteScripts.php

raw symbols SCIP
1 <?php
2 /**
3 * Import all scripts in the MediaWiki namespace from a local site.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 use MediaWiki\Content\ContentHandler;
25 use MediaWiki\Json\FormatJson;
26 use MediaWiki\StubObject\StubGlobalUser;
27 use MediaWiki\Title\Title;
28 use MediaWiki\User\User;
29
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
33
34 /**
35 * Maintenance script to import all scripts in the MediaWiki namespace from a
36 * local site.
37 *
38 * @ingroup Maintenance
39 */
40 class ImportSiteScripts extends Maintenance {
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Import site scripts from a site' );
44 $this->addArg( 'api', 'API base url' );
45 $this->addArg( 'index', 'index.php base url' );
46 $this->addOption( 'username', 'User name of the script importer' );
47 }
48
49 public function execute() {
50 $username = $this->getOption( 'username', false );
51 if ( $username === false ) {
52 $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
53 } else {
54 $user = User::newFromName( $username );
55 }
56 '@phan-var User $user';
57 StubGlobalUser::setUser( $user );
58
59 $baseUrl = $this->getArg( 1 );
60 $pageList = $this->fetchScriptList();
61 $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
62 $services = $this->getServiceContainer();
63 $wikiPageFactory = $services->getWikiPageFactory();
64 $httpRequestFactory = $services->getHttpRequestFactory();
65
66 foreach ( $pageList as $page ) {
67 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
68 if ( !$title ) {
69 $this->error( "$page is an invalid title; it will not be imported\n" );
70 continue;
71 }
72
73 $this->output( "Importing $page\n" );
74 $url = wfAppendQuery( $baseUrl, [
75 'action' => 'raw',
76 'title' => "MediaWiki:{$page}" ] );
77 $text = $httpRequestFactory->get( $url, [], __METHOD__ );
78
79 $wikiPage = $wikiPageFactory->newFromTitle( $title );
80 $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
81 $wikiPage->doUserEditContent( $content, $user, "Importing from $url" );
82 }
83 }
84
85 protected function fetchScriptList() {
86 $data = [
87 'action' => 'query',
88 'format' => 'json',
89 'list' => 'allpages',
90 'apnamespace' => '8',
91 'aplimit' => '500',
92 'continue' => '',
93 ];
94 $baseUrl = $this->getArg( 0 );
95 $pages = [];
96
97 while ( true ) {
98 $url = wfAppendQuery( $baseUrl, $data );
99 $strResult = $this->getServiceContainer()->getHttpRequestFactory()->
100 get( $url, [], __METHOD__ );
101 $result = FormatJson::decode( $strResult, true );
102
103 $page = null;
104 foreach ( $result['query']['allpages'] as $page ) {
105 if ( str_ends_with( $page['title'], '.js' ) ) {
106 strtok( $page['title'], ':' );
107 $pages[] = strtok( '' );
108 }
109 }
110
111 if ( $page !== null ) {
112 $this->output( "Fetched list up to {$page['title']}\n" );
113 }
114
115 if ( isset( $result['continue'] ) ) { // >= 1.21
116 $data = array_replace( $data, $result['continue'] );
117 } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
118 $data = array_replace( $data, $result['query-continue']['allpages'] );
119 } else {
120 break;
121 }
122 }
123
124 return $pages;
125 }
126 }
127
128 // @codeCoverageIgnoreStart
129 $maintClass = ImportSiteScripts::class;
130 require_once RUN_MAINTENANCE_IF_MAIN;
131 // @codeCoverageIgnoreEnd