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
24 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
28 * A License class for use on Special:Upload
30 class Licenses
extends HTMLFormField
{
35 protected $lines = [];
40 /** @var string|null */
45 * @param array $params
47 public function __construct( $params ) {
48 parent
::__construct( $params );
50 $this->msg
= static::getMessageFromParams( $params );
51 $this->selected
= null;
57 * @param array $params
60 protected static function getMessageFromParams( $params ) {
61 return empty( $params['licenses'] )
62 ?
wfMessage( 'licenses' )->inContentLanguage()->plain()
63 : $params['licenses'];
70 protected function buildLine( $line ) {
71 return new License( $line );
77 protected function makeLines() {
79 $lines = explode( "\n", $this->msg
);
81 foreach ( $lines as $line ) {
82 if ( strpos( $line, '*' ) !== 0 ) {
85 list( $level, $line ) = $this->trimStars( $line );
87 if ( strpos( $line, '|' ) !== false ) {
88 $obj = $this->buildLine( $line );
89 $this->stackItem( $this->lines
, $levels, $obj );
91 if ( $level < count( $levels ) ) {
92 $levels = array_slice( $levels, 0, $level );
94 if ( $level == count( $levels ) ) {
95 $levels[$level - 1] = $line;
96 } elseif ( $level > count( $levels ) ) {
108 protected function trimStars( $str ) {
109 $numStars = strspn( $str, '*' );
110 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
114 * @param array &$list
118 protected function stackItem( &$list, $path, $item ) {
121 foreach ( $path as $key ) {
122 $position =& $position[$key];
129 * @param array $tagset
133 protected function makeHtml( $tagset, $depth = 0 ) {
136 foreach ( $tagset as $key => $val ) {
137 if ( is_array( $val ) ) {
138 $html .= $this->outputOption(
141 'disabled' => 'disabled',
142 'style' => 'color: GrayText', // for MSIE
146 $html .= $this->makeHtml( $val, $depth +
1 );
148 $html .= $this->outputOption(
149 $val->text
, $val->template
,
150 [ 'title' => '{{' . $val->template
. '}}' ],
160 * @param string $message
161 * @param string $value
162 * @param null|array $attribs
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';
174 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
175 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
181 * Accessor for $this->lines
185 public function getLines() {
190 * Accessor for $this->lines
194 * @deprecated since 1.31 Use getLines() instead
196 public function getLicenses() {
197 return $this->getLines();
203 public function getInputHTML( $value ) {
204 $this->selected
= $value;
206 // add a default "no license selected" option
207 $default = $this->buildLine( '|nolicense' );
208 array_unshift( $this->lines
, $default );
210 $html = $this->makeHtml( $this->getLines() );
213 'name' => $this->mName
,
216 if ( !empty( $this->mParams
['disabled'] ) ) {
217 $attribs['disabled'] = 'disabled';
220 $html = Html
::rawElement( 'select', $attribs, $html );
222 // remove default "no license selected" from lines again
223 array_shift( $this->lines
);