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();
35 * @param string $str The string to build the licenses member from, will use
36 * wfMsgForContent( 'licenses' ) if null (default: null)
38 function Licenses( $str = null ) {
39 // PHP sucks, this should be possible in the constructor
40 $this->msg
= is_null( $str ) ?
wfMsgForContent( 'licenses' ) : $str;
43 $this->makeLicenses();
44 $tmp = $this->getLicenses();
45 $this->makeHtml( $tmp );
51 function makeLicenses() {
53 $lines = explode( "\n", $this->msg
);
55 foreach ( $lines as $line ) {
56 if ( strpos( $line, '*' ) !== 0 )
59 list( $level, $line ) = $this->trimStars( $line );
61 if ( strpos( $line, '|' ) !== false ) {
62 $obj = new License( $line );
63 $this->stackItem( $this->licenses
, $levels, $obj );
65 if ( $level < count( $levels ) )
66 $levels = array_slice( $levels, 0, $level );
67 if ( $level == count( $levels ) )
68 $levels[$level - 1] = $line;
69 else if ( $level > count( $levels ) )
76 function trimStars( $str ) {
78 $len = strlen( $str );
80 while ($len < $i && $str[$i++
] == '*')
83 return array( $count, ltrim( $str, '* ' ) );
86 function stackItem( &$list, $path, $item ) {
89 foreach ( $path as $key ) {
90 $position =& $position[$key];
96 function makeHtml( &$tagset, $depth = 0 ) {
97 foreach ( $tagset as $key => $val )
98 if ( is_array( $val ) ) {
99 $this->html
.= $this->outputOption(
103 'disabled' => 'disabled',
107 $this->makeHtml( $val, $depth +
1 );
109 $this->html
.= $this->outputOption(
110 $this->msg( $val->text
),
112 'value' => $val->template
,
113 'title' => $val->template
120 function outputOption( $val, $attribs = null, $depth ) {
121 $val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $val;
122 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
125 function msg( $str ) {
126 $out = wfMsg( $str );
127 return wfEmptyMsg( $str, $out ) ?
$str : $out;
133 * Accessor for $this->licenses
137 function getLicenses() { return $this->licenses
; }
140 * Accessor for $this->html
144 function getHtml() { return $this->html
; }
163 function License( $str ) {
164 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
166 $this->template
= strrev( $template );
167 $this->text
= strrev( $text );