Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ImportSiteScripts | |
0.00% |
0 / 63 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
30 | |||
fetchScriptList | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 |
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\Maintenance\Maintenance; |
27 | use MediaWiki\StubObject\StubGlobalUser; |
28 | use MediaWiki\Title\Title; |
29 | use MediaWiki\User\User; |
30 | |
31 | // @codeCoverageIgnoreStart |
32 | require_once __DIR__ . '/Maintenance.php'; |
33 | // @codeCoverageIgnoreEnd |
34 | |
35 | /** |
36 | * Maintenance script to import all scripts in the MediaWiki namespace from a |
37 | * local site. |
38 | * |
39 | * @ingroup Maintenance |
40 | */ |
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 ); |
49 | } |
50 | |
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 ); |
57 | } |
58 | '@phan-var User $user'; |
59 | StubGlobalUser::setUser( $user ); |
60 | |
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(); |
67 | |
68 | /** @var iterable<Title[]> $pageBatches */ |
69 | $pageBatches = $this->newBatchIterator( $pageList ); |
70 | |
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; |
78 | } |
79 | |
80 | $this->output( "Importing $page\n" ); |
81 | $url = wfAppendQuery( $baseUrl, [ |
82 | 'action' => 'raw', |
83 | 'title' => "MediaWiki:{$page}" ] ); |
84 | $text = $httpRequestFactory->get( $url, [], __METHOD__ ); |
85 | |
86 | $wikiPage = $wikiPageFactory->newFromTitle( $title ); |
87 | $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() ); |
88 | |
89 | $wikiPage->doUserEditContent( $content, $user, "Importing from $url" ); |
90 | } |
91 | $this->commitTransactionRound( __METHOD__ ); |
92 | } |
93 | } |
94 | |
95 | protected function fetchScriptList() { |
96 | $data = [ |
97 | 'action' => 'query', |
98 | 'format' => 'json', |
99 | 'list' => 'allpages', |
100 | 'apnamespace' => '8', |
101 | 'aplimit' => '500', |
102 | 'continue' => '', |
103 | ]; |
104 | $baseUrl = $this->getArg( 0 ); |
105 | $pages = []; |
106 | |
107 | while ( true ) { |
108 | $url = wfAppendQuery( $baseUrl, $data ); |
109 | $strResult = $this->getServiceContainer()->getHttpRequestFactory()-> |
110 | get( $url, [], __METHOD__ ); |
111 | $result = FormatJson::decode( $strResult, true ); |
112 | |
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( '' ); |
118 | } |
119 | } |
120 | |
121 | if ( $page !== null ) { |
122 | $this->output( "Fetched list up to {$page['title']}\n" ); |
123 | } |
124 | |
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; |
131 | } |
132 | } |
133 | |
134 | return $pages; |
135 | } |
136 | } |
137 | |
138 | // @codeCoverageIgnoreStart |
139 | $maintClass = ImportSiteScripts::class; |
140 | require_once RUN_MAINTENANCE_IF_MAIN; |
141 | // @codeCoverageIgnoreEnd |