|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* License selector for use on Special:Upload. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License along |
|
16
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
17
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
|
19
|
|
|
* |
|
20
|
|
|
* @file |
|
21
|
|
|
* @ingroup SpecialPage |
|
22
|
|
|
* @author Ævar Arnfjörð Bjarmason <[email protected]> |
|
23
|
|
|
* @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason |
|
24
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A License class for use on Special:Upload |
|
29
|
|
|
*/ |
|
30
|
|
|
class Licenses extends HTMLFormField { |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $msg; |
|
33
|
|
|
|
|
34
|
|
|
/** @var array */ |
|
35
|
|
|
protected $licenses = []; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
protected $html; |
|
39
|
|
|
/**#@-*/ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $params |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( $params ) { |
|
45
|
|
|
parent::__construct( $params ); |
|
46
|
|
|
|
|
47
|
|
|
$this->msg = empty( $params['licenses'] ) |
|
48
|
|
|
? wfMessage( 'licenses' )->inContentLanguage()->plain() |
|
49
|
|
|
: $params['licenses']; |
|
50
|
|
|
$this->selected = null; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$this->makeLicenses(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @private |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function makeLicenses() { |
|
59
|
|
|
$levels = []; |
|
60
|
|
|
$lines = explode( "\n", $this->msg ); |
|
61
|
|
|
|
|
62
|
|
|
foreach ( $lines as $line ) { |
|
63
|
|
|
if ( strpos( $line, '*' ) !== 0 ) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} else { |
|
66
|
|
|
list( $level, $line ) = $this->trimStars( $line ); |
|
67
|
|
|
|
|
68
|
|
|
if ( strpos( $line, '|' ) !== false ) { |
|
69
|
|
|
$obj = new License( $line ); |
|
70
|
|
|
$this->stackItem( $this->licenses, $levels, $obj ); |
|
71
|
|
|
} else { |
|
72
|
|
|
if ( $level < count( $levels ) ) { |
|
73
|
|
|
$levels = array_slice( $levels, 0, $level ); |
|
74
|
|
|
} |
|
75
|
|
|
if ( $level == count( $levels ) ) { |
|
76
|
|
|
$levels[$level - 1] = $line; |
|
77
|
|
|
} elseif ( $level > count( $levels ) ) { |
|
78
|
|
|
$levels[] = $line; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $str |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function trimStars( $str ) { |
|
90
|
|
|
$numStars = strspn( $str, '*' ); |
|
91
|
|
|
return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $list |
|
96
|
|
|
* @param array $path |
|
97
|
|
|
* @param mixed $item |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function stackItem( &$list, $path, $item ) { |
|
100
|
|
|
$position =& $list; |
|
101
|
|
|
if ( $path ) { |
|
102
|
|
|
foreach ( $path as $key ) { |
|
103
|
|
|
$position =& $position[$key]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
$position[] = $item; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $tagset |
|
111
|
|
|
* @param int $depth |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function makeHtml( $tagset, $depth = 0 ) { |
|
114
|
|
|
foreach ( $tagset as $key => $val ) { |
|
115
|
|
|
if ( is_array( $val ) ) { |
|
116
|
|
|
$this->html .= $this->outputOption( |
|
117
|
|
|
$key, '', |
|
118
|
|
|
[ |
|
119
|
|
|
'disabled' => 'disabled', |
|
120
|
|
|
'style' => 'color: GrayText', // for MSIE |
|
121
|
|
|
], |
|
122
|
|
|
$depth |
|
123
|
|
|
); |
|
124
|
|
|
$this->makeHtml( $val, $depth + 1 ); |
|
125
|
|
|
} else { |
|
126
|
|
|
$this->html .= $this->outputOption( |
|
127
|
|
|
$val->text, $val->template, |
|
128
|
|
|
[ 'title' => '{{' . $val->template . '}}' ], |
|
129
|
|
|
$depth |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $message |
|
137
|
|
|
* @param string $value |
|
138
|
|
|
* @param null|array $attribs |
|
139
|
|
|
* @param int $depth |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) { |
|
143
|
|
|
$msgObj = $this->msg( $message ); |
|
144
|
|
|
$text = $msgObj->exists() ? $msgObj->text() : $message; |
|
145
|
|
|
$attribs['value'] = $value; |
|
146
|
|
|
if ( $value === $this->selected ) { |
|
147
|
|
|
$attribs['selected'] = 'selected'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text; |
|
151
|
|
|
return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n"; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/**#@-*/ |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Accessor for $this->licenses |
|
158
|
|
|
* |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getLicenses() { |
|
162
|
|
|
return $this->licenses; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Accessor for $this->html |
|
167
|
|
|
* |
|
168
|
|
|
* @param bool $value |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getInputHTML( $value ) { |
|
173
|
|
|
$this->selected = $value; |
|
174
|
|
|
|
|
175
|
|
|
$this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '', |
|
176
|
|
|
(bool)$this->selected ? null : [ 'selected' => 'selected' ] ); |
|
177
|
|
|
$this->makeHtml( $this->getLicenses() ); |
|
178
|
|
|
|
|
179
|
|
|
$attribs = [ |
|
180
|
|
|
'name' => $this->mName, |
|
181
|
|
|
'id' => $this->mID |
|
182
|
|
|
]; |
|
183
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) { |
|
184
|
|
|
$attibs['disabled'] = 'disabled'; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return Html::rawElement( 'select', $attribs, $this->html ); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* A License class for use on Special:Upload (represents a single type of license). |
|
193
|
|
|
*/ |
|
194
|
|
|
class License { |
|
195
|
|
|
/** @var string */ |
|
196
|
|
|
public $template; |
|
197
|
|
|
|
|
198
|
|
|
/** @var string */ |
|
199
|
|
|
public $text; |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $str License name?? |
|
203
|
|
|
*/ |
|
204
|
|
|
function __construct( $str ) { |
|
205
|
|
|
list( $text, $template ) = explode( '|', strrev( $str ), 2 ); |
|
206
|
|
|
|
|
207
|
|
|
$this->template = strrev( $template ); |
|
208
|
|
|
$this->text = strrev( $text ); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: