Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[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 global $wgContLang;
61
62 if ( !empty( $params['licenses'] ) ) {
63 return $params['licenses'];
64 }
65
66 // If the licenses page is in $wgForceUIMsgAsContentMsg (which is the case
67 // on Commons), translations will be in the database, in subpages of this
68 // message (e.g. MediaWiki:Licenses/<lang>)
69 // If there is no such translation, the result will be '-' (the empty default
70 // in the i18n files), so we'll need to force it to look up the actual licenses
71 // in the default site language (= get the translation from MediaWiki:Licenses)
72 // Also see https://phabricator.wikimedia.org/T3495
73 $defaultMsg = wfMessage( 'licenses' )->inContentLanguage();
74 if ( !$defaultMsg->exists() || $defaultMsg->plain() === '-' ) {
75 $defaultMsg = wfMessage( 'licenses' )->inLanguage( $wgContLang );
76 }
77
78 return $defaultMsg->plain();
79 }
80
81 /**
82 * @param string $line
83 * @return License
84 */
85 protected function buildLine( $line ) {
86 return new License( $line );
87 }
88
89 /**
90 * @private
91 */
92 protected function makeLines() {
93 $levels = [];
94 $lines = explode( "\n", $this->msg );
95
96 foreach ( $lines as $line ) {
97 if ( strpos( $line, '*' ) !== 0 ) {
98 continue;
99 } else {
100 list( $level, $line ) = $this->trimStars( $line );
101
102 if ( strpos( $line, '|' ) !== false ) {
103 $obj = $this->buildLine( $line );
104 $this->stackItem( $this->lines, $levels, $obj );
105 } else {
106 if ( $level < count( $levels ) ) {
107 $levels = array_slice( $levels, 0, $level );
108 }
109 if ( $level == count( $levels ) ) {
110 $levels[$level - 1] = $line;
111 } elseif ( $level > count( $levels ) ) {
112 $levels[] = $line;
113 }
114 }
115 }
116 }
117 }
118
119 /**
120 * @param string $str
121 * @return array
122 */
123 protected function trimStars( $str ) {
124 $numStars = strspn( $str, '*' );
125 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
126 }
127
128 /**
129 * @param array &$list
130 * @param array $path
131 * @param mixed $item
132 */
133 protected function stackItem( &$list, $path, $item ) {
134 $position =& $list;
135 if ( $path ) {
136 foreach ( $path as $key ) {
137 $position =& $position[$key];
138 }
139 }
140 $position[] = $item;
141 }
142
143 /**
144 * @param array $tagset
145 * @param int $depth
146 * @return string
147 */
148 protected function makeHtml( $tagset, $depth = 0 ) {
149 $html = '';
150
151 foreach ( $tagset as $key => $val ) {
152 if ( is_array( $val ) ) {
153 $html .= $this->outputOption(
154 $key, '',
155 [
156 'disabled' => 'disabled',
157 'style' => 'color: GrayText', // for MSIE
158 ],
159 $depth
160 );
161 $html .= $this->makeHtml( $val, $depth + 1 );
162 } else {
163 $html .= $this->outputOption(
164 $val->text, $val->template,
165 [ 'title' => '{{' . $val->template . '}}' ],
166 $depth
167 );
168 }
169 }
170
171 return $html;
172 }
173
174 /**
175 * @param string $message
176 * @param string $value
177 * @param null|array $attribs
178 * @param int $depth
179 * @return string
180 */
181 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
182 $msgObj = $this->msg( $message );
183 $text = $msgObj->exists() ? $msgObj->text() : $message;
184 $attribs['value'] = $value;
185 if ( $value === $this->selected ) {
186 $attribs['selected'] = 'selected';
187 }
188
189 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
190 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
191 }
192
193 /**#@-*/
194
195 /**
196 * Accessor for $this->lines
197 *
198 * @return array
199 */
200 public function getLines() {
201 return $this->lines;
202 }
203
204 /**
205 * Accessor for $this->lines
206 *
207 * @return array
208 *
209 * @deprecated since 1.31 Use getLines() instead
210 */
211 public function getLicenses() {
212 return $this->getLines();
213 }
214
215 /**
216 * {@inheritdoc}
217 */
218 public function getInputHTML( $value ) {
219 $this->selected = $value;
220
221 // add a default "no license selected" option
222 $default = $this->buildLine( '|nolicense' );
223 array_unshift( $this->lines, $default );
224
225 $html = $this->makeHtml( $this->getLines() );
226
227 $attribs = [
228 'name' => $this->mName,
229 'id' => $this->mID
230 ];
231 if ( !empty( $this->mParams['disabled'] ) ) {
232 $attribs['disabled'] = 'disabled';
233 }
234
235 $html = Html::rawElement( 'select', $attribs, $html );
236
237 // remove default "no license selected" from lines again
238 array_shift( $this->lines );
239
240 return $html;
241 }
242 }