|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File holding the IdRegistry class |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2013 - 2015, Stephan Gambke |
|
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
|
9
|
|
|
* |
|
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
|
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
|
13
|
|
|
* later version. |
|
14
|
|
|
* |
|
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
|
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
18
|
|
|
* details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU General Public License along |
|
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
* @file |
|
24
|
|
|
* @ingroup Skins |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Skins\Chameleon; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class IdRegistry provides a registry and access methods to ensure each id is only used once per HTML page. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Stephan Gambke |
|
33
|
|
|
* @since 1.0 |
|
34
|
|
|
* @ingroup Skins |
|
35
|
|
|
*/ |
|
36
|
|
|
class IdRegistry { |
|
37
|
|
|
|
|
38
|
|
|
private static $sInstance; |
|
39
|
|
|
private $mRegistry = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return IdRegistry |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getRegistry() { |
|
45
|
|
|
|
|
46
|
|
|
if ( self::$sInstance === null ) { |
|
47
|
|
|
self::$sInstance = new IdRegistry(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return self::$sInstance; |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the opening tag of an HTML element in a string. |
|
56
|
|
|
* |
|
57
|
|
|
* The advantage over Html::openElement is that any id attribute is ensured to be unique. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $tag |
|
60
|
|
|
* @param array $attributes |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
public function openElement( $tag, $attributes = array() ) { |
|
65
|
|
|
|
|
66
|
|
|
if ( is_array( $attributes ) && isset( $attributes[ 'id' ] ) ) { |
|
67
|
|
|
$attributes[ 'id' ] = $this->getId( $attributes[ 'id' ] ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return \Html::openElement( $tag, $attributes ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param null|string $id |
|
75
|
|
|
* @param null|mixed $component |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getId( $id = null, $component = null ) { |
|
79
|
|
|
|
|
80
|
|
|
if ( empty( $id ) ) { |
|
81
|
|
|
|
|
82
|
|
|
// no specific id requested, just return a unique string |
|
83
|
|
|
return base_convert( uniqid(), 16, 36 ); |
|
84
|
|
|
|
|
85
|
|
|
} elseif ( array_key_exists( $id, $this->mRegistry ) ) { |
|
86
|
|
|
|
|
87
|
|
|
// specific id requested, but already in use |
|
88
|
|
|
// return a string derived from the id and a unique string |
|
89
|
|
|
$key = "$id-" . base_convert( uniqid(), 16, 36 ); |
|
90
|
|
|
$this->mRegistry[ $id ][ $key ] = $component; |
|
91
|
|
|
return $key; |
|
92
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
|
|
95
|
|
|
// specific id requested that is not yet in use |
|
96
|
|
|
// return the id |
|
97
|
|
|
$this->mRegistry[ $id ][ $id ] = $component; |
|
98
|
|
|
return $id; |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns an HTML element in a string. The contents are NOT escaped. |
|
105
|
|
|
* |
|
106
|
|
|
* The advantage over Html::rawElement is that any id attribute is ensured to be unique. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $tag |
|
109
|
|
|
* @param array $attributes |
|
110
|
|
|
* @param string $contents |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public function element( $tag, $attributes = array(), $contents = '' ) { |
|
115
|
|
|
|
|
116
|
|
|
if ( is_array( $attributes ) && isset( $attributes[ 'id' ] ) ) { |
|
117
|
|
|
$attributes[ 'id' ] = $this->getId( $attributes[ 'id' ] ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return \Html::rawElement( $tag, $attributes, $contents ); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|