Allow extension of the Special:Upload form
[lhc/web/wiklou.git] / includes / specials / formfields / Licenses.php
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 <avarab@gmail.com>
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 $lines = [];
36
37 /** @var string */
38 protected $html;
39
40 /** @var string|null */
41 protected $selected;
42 /**#@-*/
43
44 /**
45 * @param array $params
46 */
47 public function __construct( $params ) {
48 parent::__construct( $params );
49
50 $this->msg = static::getMessageFromParams( $params );
51 $this->selected = null;
52
53 $this->makeLines();
54 }
55
56 /**
57 * @param array $params
58 * @return string
59 */
60 protected static function getMessageFromParams( $params ) {
61 return empty( $params['licenses'] )
62 ? wfMessage( 'licenses' )->inContentLanguage()->plain()
63 : $params['licenses'];
64 }
65
66 /**
67 * @param string $line
68 * @return License
69 */
70 protected function buildLine( $line ) {
71 return new License( $line );
72 }
73
74 /**
75 * @private
76 */
77 protected function makeLines() {
78 $levels = [];
79 $lines = explode( "\n", $this->msg );
80
81 foreach ( $lines as $line ) {
82 if ( strpos( $line, '*' ) !== 0 ) {
83 continue;
84 } else {
85 list( $level, $line ) = $this->trimStars( $line );
86
87 if ( strpos( $line, '|' ) !== false ) {
88 $obj = $this->buildLine( $line );
89 $this->stackItem( $this->lines, $levels, $obj );
90 } else {
91 if ( $level < count( $levels ) ) {
92 $levels = array_slice( $levels, 0, $level );
93 }
94 if ( $level == count( $levels ) ) {
95 $levels[$level - 1] = $line;
96 } elseif ( $level > count( $levels ) ) {
97 $levels[] = $line;
98 }
99 }
100 }
101 }
102 }
103
104 /**
105 * @param string $str
106 * @return array
107 */
108 protected function trimStars( $str ) {
109 $numStars = strspn( $str, '*' );
110 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
111 }
112
113 /**
114 * @param array &$list
115 * @param array $path
116 * @param mixed $item
117 */
118 protected function stackItem( &$list, $path, $item ) {
119 $position =& $list;
120 if ( $path ) {
121 foreach ( $path as $key ) {
122 $position =& $position[$key];
123 }
124 }
125 $position[] = $item;
126 }
127
128 /**
129 * @param array $tagset
130 * @param int $depth
131 * @return string
132 */
133 protected function makeHtml( $tagset, $depth = 0 ) {
134 $html = '';
135
136 foreach ( $tagset as $key => $val ) {
137 if ( is_array( $val ) ) {
138 $html .= $this->outputOption(
139 $key, '',
140 [
141 'disabled' => 'disabled',
142 'style' => 'color: GrayText', // for MSIE
143 ],
144 $depth
145 );
146 $html .= $this->makeHtml( $val, $depth + 1 );
147 } else {
148 $html .= $this->outputOption(
149 $val->text, $val->template,
150 [ 'title' => '{{' . $val->template . '}}' ],
151 $depth
152 );
153 }
154 }
155
156 return $html;
157 }
158
159 /**
160 * @param string $message
161 * @param string $value
162 * @param null|array $attribs
163 * @param int $depth
164 * @return string
165 */
166 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
167 $msgObj = $this->msg( $message );
168 $text = $msgObj->exists() ? $msgObj->text() : $message;
169 $attribs['value'] = $value;
170 if ( $value === $this->selected ) {
171 $attribs['selected'] = 'selected';
172 }
173
174 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
175 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
176 }
177
178 /**#@-*/
179
180 /**
181 * Accessor for $this->lines
182 *
183 * @return array
184 */
185 public function getLines() {
186 return $this->lines;
187 }
188
189 /**
190 * Accessor for $this->lines
191 *
192 * @return array
193 *
194 * @deprecated since 1.31 Use getLines() instead
195 */
196 public function getLicenses() {
197 return $this->getLines();
198 }
199
200 /**
201 * {@inheritdoc}
202 */
203 public function getInputHTML( $value ) {
204 $this->selected = $value;
205
206 // add a default "no license selected" option
207 $default = $this->buildLine( '|nolicense' );
208 array_unshift( $this->lines, $default );
209
210 $html = $this->makeHtml( $this->getLines() );
211
212 $attribs = [
213 'name' => $this->mName,
214 'id' => $this->mID
215 ];
216 if ( !empty( $this->mParams['disabled'] ) ) {
217 $attribs['disabled'] = 'disabled';
218 }
219
220 $html = Html::rawElement( 'select', $attribs, $html );
221
222 // remove default "no license selected" from lines again
223 array_shift( $this->lines );
224
225 return $html;
226 }
227 }