-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabNamespaceInfo.php
107 lines (94 loc) · 3.28 KB
/
grabNamespaceInfo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Maintenance script to grab namespace info from a wiki to use with a new wiki.
*
* @file
* @ingroup Maintenance
* @author Jack Phoenix <[email protected]>
* @author Calimonious the Estrange
* @version 1.1
* @date 5 August 2019
*/
require_once 'includes/ExternalWikiGrabber.php';
# Custom namespaces - make a list as these will need to be added to the localsettings/whatever
class GrabNamespaceInfo extends ExternalWikiGrabber {
public function __construct() {
parent::__construct();
$this->addDescription( 'Get namespace info from a source wiki to add to your LocalSettings.php' );
}
public function execute() {
parent::execute();
$this->output( "\n" );
$params = [
'meta' => 'siteinfo',
'siprop' => 'namespaces|namespacealiases'
];
$result = $this->bot->query( $params );
if ( !$result['query'] ) {
$this->fatalError( 'Got no namespaces...' );
}
$namespaces = $result['query']['namespaces'];
$customNamespaces = [];
$contentNamespaces = [];
$subpageNamespaces = [];
$namespaceAliases = []; # $wgNamespaceAliases['WP'] = NS_PROJECT;
foreach ( array_keys( $namespaces ) as $ns ) {
# Content?
if ( isset( $namespaces[$ns]['content'] ) ) {
$contentNamespaces[] = $ns;
}
# Subpages?
if ( isset( $namespaces[$ns]['subpages'] ) ) {
$subpageNamespaces[] = $ns;
}
if ( $ns >= 100 ) {
$customNamespaces[$ns] = $namespaces[$ns]['*'];
# Wikis in languages other than English where the namespaces are
# provided by extensions, have localized namespace in *, use it as
# the primary name and add canonical as an alias.
if ( $namespaces[$ns]['*'] != $namespaces[$ns]['canonical'] ) {
$namespaceAliases[$namespaces[$ns]['canonical']] = $ns;
}
}
}
foreach ( $result['query']['namespacealiases'] as $nsa ) {
$namespaceAliases[$nsa['*']] = $nsa['id'];
}
# Show stuff
$this->output( "# Extra namespaces\n" );
foreach ( array_keys( $customNamespaces ) as $ns ) {
$namespaceName = str_replace( ' ', '_', $customNamespaces[$ns] );
$this->output( '$wgExtraNamespaces[' . $ns . '] = "' . $namespaceName . '";' . "\n" );
}
# Print content namespace configuration if any
if ( count( $contentNamespaces ) > 1 ) {
$this->output( "\n# Content namespaces\n" );
$this->output( '$wgContentNamespaces = array_merge(' . "\n" );
$this->output( "\t" . '$wgContentNamespaces,' . "\n" );
$this->output( "\t" . '[ ' );
$this->output( $contentNamespaces[1] );
foreach ( array_keys( $contentNamespaces ) as $i ) {
if ( $i > 1 ) {
$this->output( ", {$contentNamespaces[$i]}" );
}
}
$this->output( " ]\n" );
$this->output( ");\n" );
}
# Print subpage namespace configuration if any
# TO IMPLEMENT; currently the common default configuration just assumes all of them
# Print namespaceAliases if any
if ( count( $namespaceAliases ) > 2 ) {
$this->output( "\n# Namespace aliases\n" );
foreach ( array_keys( $namespaceAliases ) as $nsa ) {
# Ignore if image/image talk; that's core
if ( $namespaceAliases[$nsa] != 6 && $namespaceAliases[$nsa] != 7 ) {
$this->output( '$wgNamespaceAliases["' . $nsa . '"] = ' . $namespaceAliases[$nsa] . ';' . "\n" );
}
}
}
$this->output( "\n" );
}
}
$maintClass = 'GrabNamespaceInfo';
require_once RUN_MAINTENANCE_IF_MAIN;