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 $licenses = [];
41 * @param array $params
43 public function __construct( $params ) {
44 parent
::__construct( $params );
46 $this->msg
= empty( $params['licenses'] )
47 ?
wfMessage( 'licenses' )->inContentLanguage()->plain()
48 : $params['licenses'];
49 $this->selected
= null;
51 $this->makeLicenses();
57 protected function makeLicenses() {
59 $lines = explode( "\n", $this->msg
);
61 foreach ( $lines as $line ) {
62 if ( strpos( $line, '*' ) !== 0 ) {
65 list( $level, $line ) = $this->trimStars( $line );
67 if ( strpos( $line, '|' ) !== false ) {
68 $obj = new License( $line );
69 $this->stackItem( $this->licenses
, $levels, $obj );
71 if ( $level < count( $levels ) ) {
72 $levels = array_slice( $levels, 0, $level );
74 if ( $level == count( $levels ) ) {
75 $levels[$level - 1] = $line;
76 } elseif ( $level > count( $levels ) ) {
88 protected function trimStars( $str ) {
89 $numStars = strspn( $str, '*' );
90 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
98 protected function stackItem( &$list, $path, $item ) {
101 foreach ( $path as $key ) {
102 $position =& $position[$key];
109 * @param array $tagset
112 protected function makeHtml( $tagset, $depth = 0 ) {
113 foreach ( $tagset as $key => $val ) {
114 if ( is_array( $val ) ) {
115 $this->html
.= $this->outputOption(
118 'disabled' => 'disabled',
119 'style' => 'color: GrayText', // for MSIE
123 $this->makeHtml( $val, $depth +
1 );
125 $this->html
.= $this->outputOption(
126 $val->text
, $val->template
,
127 [ 'title' => '{{' . $val->template
. '}}' ],
135 * @param string $message
136 * @param string $value
137 * @param null|array $attribs
141 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
142 $msgObj = $this->msg( $message );
143 $text = $msgObj->exists() ?
$msgObj->text() : $message;
144 $attribs['value'] = $value;
145 if ( $value === $this->selected
) {
146 $attribs['selected'] = 'selected';
149 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
150 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
156 * Accessor for $this->licenses
160 public function getLicenses() {
161 return $this->licenses
;
165 * Accessor for $this->html
171 public function getInputHTML( $value ) {
172 $this->selected
= $value;
174 $this->html
= $this->outputOption( wfMessage( 'nolicense' )->text(), '',
175 (bool)$this->selected ?
null : [ 'selected' => 'selected' ] );
176 $this->makeHtml( $this->getLicenses() );
179 'name' => $this->mName
,
182 if ( !empty( $this->mParams
['disabled'] ) ) {
183 $attibs['disabled'] = 'disabled';
186 return Html
::rawElement( 'select', $attribs, $this->html
);