|
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 XMLParse { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Converts an XML url or string to a PHP array format |
|
24
|
|
|
* |
|
25
|
|
|
* @static |
|
26
|
|
|
* @access public |
|
27
|
|
|
* @param string $data Either an url to an xml file, or a raw XML string. Peachy will autodetect which is which. |
|
28
|
|
|
* @return array Parsed XML |
|
29
|
|
|
* @throws BadEntryError |
|
30
|
|
|
* @throws DependencyError |
|
31
|
|
|
* @throws HookError |
|
32
|
|
|
* @throws XMLError |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function load( $data ) { |
|
35
|
|
|
$http = HTTP::getDefaultInstance(); |
|
36
|
|
|
|
|
37
|
|
|
if( !function_exists( 'simplexml_load_string' ) ) { |
|
38
|
|
|
throw new DependencyError( "SimpleXML", "http://us.php.net/manual/en/book.simplexml.php" ); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
libxml_use_internal_errors( true ); |
|
42
|
|
|
|
|
43
|
|
|
if( in_string( "<?xml", $data ) ) { |
|
44
|
|
|
$xmlout = $data; |
|
45
|
|
|
} else { |
|
46
|
|
|
$xmlout = $http->get( $data ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
Hooks::runHook( 'PreSimpleXMLLoad', array( &$xmlout ) ); |
|
50
|
|
|
|
|
51
|
|
|
$xml = simplexml_load_string( $xmlout ); |
|
52
|
|
|
|
|
53
|
|
|
Hooks::runHook( 'PostSimpleXMLLoad', array( &$xml ) ); |
|
54
|
|
|
|
|
55
|
|
|
if( !$xml ) { |
|
56
|
|
|
foreach( libxml_get_errors() as $error ){ |
|
57
|
|
|
throw new XMLError( $error ); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$outArr = array(); |
|
62
|
|
|
|
|
63
|
|
|
$namespaces = $xml->getNamespaces( true ); |
|
64
|
|
|
$namespaces['default'] = ''; |
|
65
|
|
|
|
|
66
|
|
|
self::recurse( $xml, $outArr, $namespaces ); |
|
67
|
|
|
|
|
68
|
|
|
libxml_clear_errors(); |
|
69
|
|
|
|
|
70
|
|
|
return $outArr; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param SimpleXMLElement $xml |
|
75
|
|
|
* @param $arr |
|
76
|
|
|
* @param $namespaces |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function recurse( $xml, &$arr, $namespaces ) { |
|
79
|
|
|
|
|
80
|
|
|
foreach( $namespaces as $namespace ){ |
|
81
|
|
|
|
|
82
|
|
|
foreach( $xml->children( $namespace ) as $elementName => $node ){ |
|
83
|
|
|
|
|
84
|
|
|
$key = count( $arr ); |
|
85
|
|
|
|
|
86
|
|
|
if( isset( $arr['_name'] ) ) $key--; |
|
87
|
|
|
if( isset( $arr['_attributes'] ) ) $key--; |
|
88
|
|
|
|
|
89
|
|
|
$arr[$key] = array(); |
|
90
|
|
|
$arr[$key]['_name'] = $elementName; |
|
91
|
|
|
|
|
92
|
|
|
$arr[$key]['_attributes'] = array(); |
|
93
|
|
|
|
|
94
|
|
|
foreach( $node->attributes( $namespace ) as $aname => $avalue ){ |
|
95
|
|
|
$arr[$key]['_attributes'][$aname] = trim( $avalue ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if( !count( $arr[$key]['_attributes'] ) ) { |
|
99
|
|
|
unset( $arr[$key]['_attributes'] ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$text = trim( (string)$node ); |
|
103
|
|
|
|
|
104
|
|
|
if( strlen( $text ) > 0 ) $arr[$key]['_text'] = $text; |
|
105
|
|
|
|
|
106
|
|
|
self::recurse( $node, $arr[$key], $namespaces ); |
|
107
|
|
|
|
|
108
|
|
|
if( count( $arr[$key] ) == 1 && isset( $arr[$key]['_text'] ) ) { |
|
109
|
|
|
$arr[$key] = $arr[$key]['_text']; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: