3 * A License class for use on Special:Upload
6 * @subpackage SpecialPage
8 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
9 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
10 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
25 var $licenses = array();
36 * @param string $str The string to build the licenses member from, will use
37 * wfMsgForContent( 'licenses' ) if null (default: null)
39 function Licenses( $str = null ) {
40 // PHP sucks, this should be possible in the constructor
42 global $wgLanguageCode ;
43 if ( is_null( $str ) ) {
44 # Try for language-localized license list; if not, try the deault
45 $t = 'licenses/'.$wgLanguageCode ;
46 $s = wfMsgForContent( $t ) ;
47 if ( $s == '<'.$t.'>' )
48 $t = wfMsgForContent( 'licenses' ) ;
54 # $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str; # Old code, do not use!
57 $this->makeLicenses();
58 $tmp = $this->getLicenses();
59 $this->makeHtml( $tmp );
65 function makeLicenses() {
67 $lines = explode( "\n", $this->msg
);
69 foreach ( $lines as $line ) {
70 if ( strpos( $line, '*' ) !== 0 ) {
73 list( $level, $line ) = $this->trimStars( $line );
75 if ( strpos( $line, '|' ) !== false ) {
76 $obj = new License( $line );
77 $this->stackItem( $this->licenses
, $levels, $obj );
79 if ( $level < count( $levels ) ) {
80 $levels = array_slice( $levels, 0, $level );
82 if ( $level == count( $levels ) ) {
83 $levels[$level - 1] = $line;
84 } elseif ( $level > count( $levels ) ) {
92 function trimStars( $str ) {
96 while ($str[$i++
] == '*')
100 return array( $count, ltrim( $str, '* ' ) );
103 function stackItem( &$list, $path, $item ) {
106 foreach( $path as $key )
107 $position =& $position[$key];
111 function makeHtml( &$tagset, $depth = 0 ) {
112 foreach ( $tagset as $key => $val )
113 if ( is_array( $val ) ) {
114 $this->html
.= $this->outputOption(
118 'disabled' => 'disabled',
122 $this->makeHtml( $val, $depth +
1 );
124 $this->html
.= $this->outputOption(
125 $this->msg( $val->text
),
127 'value' => $val->template
,
128 'title' => '{{' . $val->template
. '}}'
135 function outputOption( $val, $attribs = null, $depth ) {
136 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $val;
137 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
140 function msg( $str ) {
141 $out = wfMsg( $str );
142 return wfEmptyMsg( $str, $out ) ?
$str : $out;
148 * Accessor for $this->licenses
152 function getLicenses() { return $this->licenses
; }
155 * Accessor for $this->html
159 function getHtml() { return $this->html
; }
178 function License( $str ) {
179 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
181 $this->template
= strrev( $template );
182 $this->text
= strrev( $text );