Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.56% |
43 / 45 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
AddSite | |
95.56% |
43 / 45 |
|
50.00% |
1 / 2 |
12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
94.12% |
32 / 34 |
|
0.00% |
0 / 1 |
11.02 |
1 | <?php |
2 | |
3 | // @codeCoverageIgnoreStart |
4 | require_once __DIR__ . '/Maintenance.php'; |
5 | // @codeCoverageIgnoreEnd |
6 | |
7 | use MediaWiki\Maintenance\Maintenance; |
8 | use MediaWiki\Site\MediaWikiSite; |
9 | |
10 | /** |
11 | * Maintenance script for adding a site definition into the sites table. |
12 | * |
13 | * The sites table is cached in the local-server cache, |
14 | * so you should reload your webserver and other long-running MediaWiki |
15 | * PHP processes after running this script. |
16 | * |
17 | * @since 1.29 |
18 | * |
19 | * @license GPL-2.0-or-later |
20 | * @author Florian Schmidt |
21 | */ |
22 | class AddSite extends Maintenance { |
23 | |
24 | public function __construct() { |
25 | parent::__construct(); |
26 | |
27 | $this->addDescription( 'Add a site definition into the sites table.' ); |
28 | |
29 | $this->addArg( 'globalid', 'The global id of the site to add, e.g. "wikipedia".', true ); |
30 | $this->addArg( 'group', 'In which group this site should be sorted in.', true ); |
31 | $this->addOption( 'language', 'The language code of the site, e.g. "de".', false, true ); |
32 | $this->addOption( 'interwiki-id', 'The interwiki ID of the site.', false, true ); |
33 | $this->addOption( 'navigation-id', 'The navigation ID of the site.', false, true ); |
34 | $this->addOption( 'pagepath', 'The URL to pages of this site, e.g.' . |
35 | ' https://example.com/wiki/\$1.', false, true ); |
36 | $this->addOption( 'filepath', 'The URL to files of this site, e.g. https://example' . |
37 | '.com/w/\$1.', false, true ); |
38 | } |
39 | |
40 | /** |
41 | * Imports the site described by the parameters (see self::__construct()) passed to this |
42 | * maintenance sccript into the sites table of MediaWiki. |
43 | */ |
44 | public function execute() { |
45 | $siteStore = $this->getServiceContainer()->getSiteStore(); |
46 | if ( method_exists( $siteStore, 'reset' ) ) { |
47 | // @phan-suppress-next-line PhanUndeclaredMethod |
48 | $siteStore->reset(); |
49 | } |
50 | |
51 | $globalId = $this->getArg( 0 ); |
52 | $group = $this->getArg( 1 ); |
53 | $language = $this->getOption( 'language' ); |
54 | $interwikiId = $this->getOption( 'interwiki-id' ); |
55 | $navigationId = $this->getOption( 'navigation-id' ); |
56 | $pagepath = $this->getOption( 'pagepath' ); |
57 | $filepath = $this->getOption( 'filepath' ); |
58 | |
59 | if ( !is_string( $globalId ) || !is_string( $group ) ) { |
60 | $this->fatalError( 'Arguments globalid and group need to be strings.' ); |
61 | } |
62 | |
63 | if ( $siteStore->getSite( $globalId ) !== null ) { |
64 | $this->fatalError( "Site with global id $globalId already exists." ); |
65 | } |
66 | |
67 | $site = new MediaWikiSite(); |
68 | $site->setGlobalId( $globalId ); |
69 | $site->setGroup( $group ); |
70 | if ( $language !== null ) { |
71 | $site->setLanguageCode( $language ); |
72 | } |
73 | if ( $interwikiId !== null ) { |
74 | $site->addInterwikiId( $interwikiId ); |
75 | } |
76 | if ( $navigationId !== null ) { |
77 | $site->addNavigationId( $navigationId ); |
78 | } |
79 | if ( $pagepath !== null ) { |
80 | $site->setPagePath( $pagepath ); |
81 | } |
82 | if ( $filepath !== null ) { |
83 | $site->setFilePath( $filepath ); |
84 | } |
85 | |
86 | $siteStore->saveSites( [ $site ] ); |
87 | |
88 | if ( method_exists( $siteStore, 'reset' ) ) { |
89 | // @phan-suppress-next-line PhanUndeclaredMethod |
90 | $siteStore->reset(); |
91 | } |
92 | |
93 | $this->output( |
94 | 'Done. Reload the web server and other long-running PHP processes ' |
95 | . "to refresh the local-server cache of the sites table.\n" |
96 | ); |
97 | } |
98 | } |
99 | |
100 | // @codeCoverageIgnoreStart |
101 | $maintClass = AddSite::class; |
102 | require_once RUN_MAINTENANCE_IF_MAIN; |
103 | // @codeCoverageIgnoreEnd |