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