3 * A License class for use on Special:Upload
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
12 class Licenses
extends HTMLFormField
{
21 protected $licenses = array();
32 * @param $params array
34 public function __construct( $params ) {
35 parent
::__construct( $params );
37 $this->msg
= empty( $params['licenses'] ) ?
wfMsgForContent( 'licenses' ) : $params['licenses'];
38 $this->selected
= null;
40 $this->makeLicenses();
46 protected function makeLicenses() {
48 $lines = explode( "\n", $this->msg
);
50 foreach ( $lines as $line ) {
51 if ( strpos( $line, '*' ) !== 0 ) {
54 list( $level, $line ) = $this->trimStars( $line );
56 if ( strpos( $line, '|' ) !== false ) {
57 $obj = new License( $line );
58 $this->stackItem( $this->licenses
, $levels, $obj );
60 if ( $level < count( $levels ) ) {
61 $levels = array_slice( $levels, 0, $level );
63 if ( $level == count( $levels ) ) {
64 $levels[$level - 1] = $line;
65 } elseif ( $level > count( $levels ) ) {
77 protected function trimStars( $str ) {
78 $numStars = strspn( $str, '*' );
79 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
87 protected function stackItem( &$list, $path, $item ) {
90 foreach( $path as $key ) {
91 $position =& $position[$key];
101 protected function makeHtml( $tagset, $depth = 0 ) {
102 foreach ( $tagset as $key => $val )
103 if ( is_array( $val ) ) {
104 $this->html
.= $this->outputOption(
105 $this->msg( $key ), '',
107 'disabled' => 'disabled',
108 'style' => 'color: GrayText', // for MSIE
112 $this->makeHtml( $val, $depth +
1 );
114 $this->html
.= $this->outputOption(
115 $this->msg( $val->text
), $val->template
,
116 array( 'title' => '{{' . $val->template
. '}}' ),
125 * @param $attribs null
129 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
130 $attribs['value'] = $value;
131 if ( $value === $this->selected
)
132 $attribs['selected'] = 'selected';
133 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
134 return str_repeat( "\t", $depth ) . Xml
::element( 'option', $attribs, $val ) . "\n";
141 protected function msg( $str ) {
142 $msg = wfMessage( $str );
143 return $msg->exists() ?
$msg->text() : $str;
149 * Accessor for $this->licenses
153 public function getLicenses() {
154 return $this->licenses
;
158 * Accessor for $this->html
164 public function getInputHTML( $value ) {
165 $this->selected
= $value;
167 $this->html
= $this->outputOption( wfMsg( 'nolicense' ), '',
168 (bool)$this->selected ?
null : array( 'selected' => 'selected' ) );
169 $this->makeHtml( $this->getLicenses() );
172 'name' => $this->mName
,
175 if ( !empty( $this->mParams
['disabled'] ) ) {
176 $attibs['disabled'] = 'disabled';
179 return Html
::rawElement( 'select', $attribs, $this->html
);
184 * A License class for use on Special:Upload (represents a single type of license).
200 * @param $str String: license name??
202 function __construct( $str ) {
203 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
205 $this->template
= strrev( $template );
206 $this->text
= strrev( $text );