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 $licenses = array();
42 * @param array $params
44 public function __construct( $params ) {
45 parent
::__construct( $params );
47 $this->msg
= empty( $params['licenses'] )
48 ?
wfMessage( 'licenses' )->inContentLanguage()->plain()
49 : $params['licenses'];
50 $this->selected
= null;
52 $this->makeLicenses();
58 protected function makeLicenses() {
60 $lines = explode( "\n", $this->msg
);
62 foreach ( $lines as $line ) {
63 if ( strpos( $line, '*' ) !== 0 ) {
66 list( $level, $line ) = $this->trimStars( $line );
68 if ( strpos( $line, '|' ) !== false ) {
69 $obj = new License( $line );
70 $this->stackItem( $this->licenses
, $levels, $obj );
72 if ( $level < count( $levels ) ) {
73 $levels = array_slice( $levels, 0, $level );
75 if ( $level == count( $levels ) ) {
76 $levels[$level - 1] = $line;
77 } elseif ( $level > count( $levels ) ) {
89 protected function trimStars( $str ) {
90 $numStars = strspn( $str, '*' );
91 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
99 protected function stackItem( &$list, $path, $item ) {
102 foreach ( $path as $key ) {
103 $position =& $position[$key];
110 * @param array $tagset
113 protected function makeHtml( $tagset, $depth = 0 ) {
114 foreach ( $tagset as $key => $val ) {
115 if ( is_array( $val ) ) {
116 $this->html
.= $this->outputOption(
119 'disabled' => 'disabled',
120 'style' => 'color: GrayText', // for MSIE
124 $this->makeHtml( $val, $depth +
1 );
126 $this->html
.= $this->outputOption(
127 $val->text
, $val->template
,
128 array( 'title' => '{{' . $val->template
. '}}' ),
136 * @param string $message
137 * @param string $value
138 * @param null|array $attribs
142 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
143 $msgObj = $this->msg( $message );
144 $text = $msgObj->exists() ?
$msgObj->text() : $message;
145 $attribs['value'] = $value;
146 if ( $value === $this->selected
) {
147 $attribs['selected'] = 'selected';
150 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
151 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
157 * Accessor for $this->licenses
161 public function getLicenses() {
162 return $this->licenses
;
166 * Accessor for $this->html
172 public function getInputHTML( $value ) {
173 $this->selected
= $value;
175 $this->html
= $this->outputOption( wfMessage( 'nolicense' )->text(), '',
176 (bool)$this->selected ?
null : array( 'selected' => 'selected' ) );
177 $this->makeHtml( $this->getLicenses() );
180 'name' => $this->mName
,
183 if ( !empty( $this->mParams
['disabled'] ) ) {
184 $attibs['disabled'] = 'disabled';
187 return Html
::rawElement( 'select', $attribs, $this->html
);
192 * A License class for use on Special:Upload (represents a single type of license).
202 * @param string $str License name??
204 function __construct( $str ) {
205 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
207 $this->template
= strrev( $template );
208 $this->text
= strrev( $text );