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 ) {
90 foreach ( $arr as $item )
91 $str .= '["' . addslashes( $item ) . '"]';
97 function makeHtml( &$tagset, $depth = 0 ) {
98 foreach ( $tagset as $key => $val )
99 if ( is_array( $val ) ) {
101 $this->html
.= $this->outputOption(
105 'disabled' => 'disabled',
109 $this->makeHtml( $val, $depth +
1 );
111 $this->html
.= $this->outputOption(
112 $this->msg( $val->text
),
114 'value' => $val->template
,
115 'title' => $val->template
122 function outputOption( $val, $attribs = null, $depth ) {
123 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $val;
124 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
127 function msg( $str ) {
128 $out = wfMsg( $str );
129 return wfNoMsg( $str, $out ) ?
$str : $out;
135 * Accessor for $this->licenses
139 function getLicenses() { return $this->licenses
; }
142 * Accessor for $this->html
146 function getHtml() { return $this->html
; }
165 function License( $str ) {
166 list( $template, $text ) = explode( '|', $str, 2 );
168 $this->template
= $template;