1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Peachy MediaWiki Bot API |
5
|
|
|
* |
6
|
|
|
* Peachy is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class SiteMatrix { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Loads list of all SiteMatrix wikis |
24
|
|
|
* |
25
|
|
|
* @static |
26
|
|
|
* @access public |
27
|
|
|
* @param Wiki &$wikiClass The Wiki class object |
28
|
|
|
* @return array List of all wikis |
29
|
|
|
* @throws AssertFailure |
30
|
|
|
* @throws DependencyError |
31
|
|
|
* @throws LoggedOut |
32
|
|
|
* @throws MWAPIError |
33
|
|
|
*/ |
34
|
|
|
public static function load( Wiki &$wikiClass ) { |
35
|
|
|
|
36
|
|
|
if( !array_key_exists( 'SiteMatrix', $wikiClass->get_extensions() ) ) { |
37
|
|
|
throw new DependencyError( "SiteMatrix" ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$SMres = $wikiClass->apiQuery( |
41
|
|
|
array( |
42
|
|
|
'action' => 'sitematrix', |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$wikis = $SMres['sitematrix']; |
47
|
|
|
//return $wikis; |
48
|
|
|
|
49
|
|
|
$retarray = array( |
50
|
|
|
'raw' => $wikis, |
51
|
|
|
'urls' => array(), |
52
|
|
|
'langs' => array(), |
53
|
|
|
'names' => array(), |
54
|
|
|
'privates' => array() |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
foreach( $wikis as $site ){ |
58
|
|
|
if( is_array( $site ) ) { |
59
|
|
|
if( isset( $site['site'] ) ) { |
60
|
|
|
|
61
|
|
|
$retarray['langs'][] = $site['code']; |
62
|
|
|
$retarray['names'][$site['code']] = $site['name']; |
63
|
|
|
|
64
|
|
|
foreach( $site['site'] as $site2 ){ |
65
|
|
|
$retarray['urls'][] = $site2['url']; |
66
|
|
|
|
67
|
|
|
if( isset( $site2['private'] ) ) $retarray['privates'][] = $site2; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
foreach( $site as $site2 ){ |
71
|
|
|
$sites2['urls'][] = $site2['url']; |
|
|
|
|
72
|
|
|
|
73
|
|
|
if( isset( $site2['private'] ) ) $retarray['privates'][] = $site2; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $retarray; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.