3 * License selector for use on Special:Upload.
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.
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.
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
21 * @ingroup SpecialPage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
27 * A License class for use on Special:Upload
29 class Licenses
extends HTMLFormField
{
34 protected $lines = [];
39 /** @var string|null */
44 * @param array $params
46 public function __construct( $params ) {
47 parent
::__construct( $params );
49 $this->msg
= static::getMessageFromParams( $params );
50 $this->selected
= null;
56 * @param array $params
59 protected static function getMessageFromParams( $params ) {
60 return empty( $params['licenses'] )
61 ?
wfMessage( 'licenses' )->inContentLanguage()->plain()
62 : $params['licenses'];
69 protected function buildLine( $line ) {
70 return new License( $line );
76 protected function makeLines() {
78 $lines = explode( "\n", $this->msg
);
80 foreach ( $lines as $line ) {
81 if ( strpos( $line, '*' ) !== 0 ) {
84 list( $level, $line ) = $this->trimStars( $line );
86 if ( strpos( $line, '|' ) !== false ) {
87 $obj = $this->buildLine( $line );
88 $this->stackItem( $this->lines
, $levels, $obj );
90 if ( $level < count( $levels ) ) {
91 $levels = array_slice( $levels, 0, $level );
93 if ( $level == count( $levels ) ) {
94 $levels[$level - 1] = $line;
95 } elseif ( $level > count( $levels ) ) {
107 protected function trimStars( $str ) {
108 $numStars = strspn( $str, '*' );
109 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
113 * @param array &$list
117 protected function stackItem( &$list, $path, $item ) {
120 foreach ( $path as $key ) {
121 $position =& $position[$key];
128 * @param array $tagset
132 protected function makeHtml( $tagset, $depth = 0 ) {
135 foreach ( $tagset as $key => $val ) {
136 if ( is_array( $val ) ) {
137 $html .= $this->outputOption(
140 'disabled' => 'disabled',
141 'style' => 'color: GrayText', // for MSIE
145 $html .= $this->makeHtml( $val, $depth +
1 );
147 $html .= $this->outputOption(
148 $val->text
, $val->template
,
149 [ 'title' => '{{' . $val->template
. '}}' ],
159 * @param string $message
160 * @param string $value
161 * @param null|array $attribs
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';
173 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
174 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
180 * Accessor for $this->lines
184 public function getLines() {
189 * Accessor for $this->lines
193 * @deprecated since 1.31 Use getLines() instead
195 public function getLicenses() {
196 return $this->getLines();
202 public function getInputHTML( $value ) {
203 $this->selected
= $value;
205 // add a default "no license selected" option
206 $default = $this->buildLine( '|nolicense' );
207 array_unshift( $this->lines
, $default );
209 $html = $this->makeHtml( $this->getLines() );
212 'name' => $this->mName
,
215 if ( !empty( $this->mParams
['disabled'] ) ) {
216 $attribs['disabled'] = 'disabled';
219 $html = Html
::rawElement( 'select', $attribs, $html );
221 // remove default "no license selected" from lines again
222 array_shift( $this->lines
);