3 * A License class for use on Special:Upload
6 * @subpackage SpecialPage
8 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
24 var $licenses = array();
34 * @param string $str The string to build the licenses member from, will use
35 * wfMsgForContent( 'licenses' ) if null (default: null)
37 function Licenses( $str = null ) {
38 // PHP sucks, this should be possible in the constructor
39 $this->msg
= is_null( $str ) ?
wfMsgForContent( 'licenses' ) : $str;
42 $this->makeLicenses();
43 $tmp = $this->getLicenses();
44 $this->makeHtml( $tmp );
50 function makeLicenses() {
52 $lines = explode( "\n", $this->msg
);
54 foreach ( $lines as $line ) {
55 if ( strpos( $line, '*' ) !== 0 )
58 list( $level, $line ) = $this->trimStars( $line );
60 if ( strpos( $line, '|' ) !== false ) {
61 $obj = new License( $line );
62 // TODO: Do this without using eval()
63 eval( '$this->licenses' . $this->makeIndexes( $levels ) . '[] = $obj;' );
65 if ( $level < count( $levels ) )
66 $levels = array_slice( $levels, count( $levels ) - $level );
67 if ( $level == count( $levels ) )
68 $levels[$level - 1] = $line;
69 else if ( $level > count( $levels ) )
77 function trimStars( $str ) {
80 while ($str[$i++
] == '*')
83 return array( $count, ltrim( $str, '* ' ) );
86 function makeIndexes( $arr ) {
89 foreach ( $arr as $item )
90 $str .= '["' . addslashes( $item ) . '"]';
95 function makeHtml( &$tagset, $depth = 0 ) {
96 foreach ( $tagset as $key => $val )
97 if ( is_array( $val ) ) {
99 $this->html
.= $this->outputOption(
106 $this->makeHtml( $val, $depth +
1 );
108 $this->html
.= $this->outputOption(
109 $this->msg( $val->text
),
111 'value' => $val->template
118 function outputOption( $val, $attribs = null, $depth ) {
119 $val = str_repeat( /*   */ "\xc2\xa0", $depth ) . $val;
120 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
123 function msg( $str ) {
124 $out = wfMsg( $str );
125 return wfNoMsg( $str, $out ) ?
$str : $out;
131 * Accessor for $this->licenses
135 function getLicenses() { return $this->licenses
; }
138 * Accessor for $this->html
142 function getHtml() { return $this->html
; }
161 function License( $str ) {
162 list( $template, $text ) = explode( '|', $str, 2 );
164 $this->template
= $template;