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
{
39 protected $licenses = array();
50 * @param $params array
52 public function __construct( $params ) {
53 parent
::__construct( $params );
55 $this->msg
= empty( $params['licenses'] ) ?
wfMessage( 'licenses' )->inContentLanguage()->plain() : $params['licenses'];
56 $this->selected
= null;
58 $this->makeLicenses();
64 protected function makeLicenses() {
66 $lines = explode( "\n", $this->msg
);
68 foreach ( $lines as $line ) {
69 if ( strpos( $line, '*' ) !== 0 ) {
72 list( $level, $line ) = $this->trimStars( $line );
74 if ( strpos( $line, '|' ) !== false ) {
75 $obj = new License( $line );
76 $this->stackItem( $this->licenses
, $levels, $obj );
78 if ( $level < count( $levels ) ) {
79 $levels = array_slice( $levels, 0, $level );
81 if ( $level == count( $levels ) ) {
82 $levels[$level - 1] = $line;
83 } elseif ( $level > count( $levels ) ) {
95 protected function trimStars( $str ) {
96 $numStars = strspn( $str, '*' );
97 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
105 protected function stackItem( &$list, $path, $item ) {
108 foreach( $path as $key ) {
109 $position =& $position[$key];
119 protected function makeHtml( $tagset, $depth = 0 ) {
120 foreach ( $tagset as $key => $val ) {
121 if ( is_array( $val ) ) {
122 $this->html
.= $this->outputOption(
125 'disabled' => 'disabled',
126 'style' => 'color: GrayText', // for MSIE
130 $this->makeHtml( $val, $depth +
1 );
132 $this->html
.= $this->outputOption(
133 $val->text
, $val->template
,
134 array( 'title' => '{{' . $val->template
. '}}' ),
144 * @param $attribs null
148 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
149 $msgObj = $this->msg( $message );
150 $text = $msgObj->exists() ?
$msgObj->text() : $message;
151 $attribs['value'] = $value;
152 if ( $value === $this->selected
) {
153 $attribs['selected'] = 'selected';
156 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
157 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
163 * Accessor for $this->licenses
167 public function getLicenses() {
168 return $this->licenses
;
172 * Accessor for $this->html
178 public function getInputHTML( $value ) {
179 $this->selected
= $value;
181 $this->html
= $this->outputOption( wfMessage( 'nolicense' )->text(), '',
182 (bool)$this->selected ?
null : array( 'selected' => 'selected' ) );
183 $this->makeHtml( $this->getLicenses() );
186 'name' => $this->mName
,
189 if ( !empty( $this->mParams
['disabled'] ) ) {
190 $attibs['disabled'] = 'disabled';
193 return Html
::rawElement( 'select', $attribs, $this->html
);
198 * A License class for use on Special:Upload (represents a single type of license).
214 * @param string $str license name??
216 function __construct( $str ) {
217 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
219 $this->template
= strrev( $template );
220 $this->text
= strrev( $text );