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