More profile breakage.
[mediawiki.git] / includes / Licenses.php
blob6a034468755c8feb5582fb5bb4fb0e49a2a4513a
1 <?php
2 /**
3 * A License class for use on Special:Upload
4 *
5 * @addtogroup SpecialPage
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 {
13 /**#@+
14 * @private
16 /**
17 * @var string
19 var $msg;
21 /**
22 * @var array
24 var $licenses = array();
26 /**
27 * @var string
29 var $html;
30 /**#@-*/
32 /**
33 * Constructor
35 * @param $str String: the string to build the licenses member from, will use
36 * wfMsgForContent( 'licenses' ) if null (default: null)
38 function __construct( $str = null ) {
39 // PHP sucks, this should be possible in the constructor
40 $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
41 $this->html = '';
43 $this->makeLicenses();
44 $tmp = $this->getLicenses();
45 $this->makeHtml( $tmp );
48 /**#@+
49 * @private
51 function makeLicenses() {
52 $levels = array();
53 $lines = explode( "\n", $this->msg );
55 foreach ( $lines as $line ) {
56 if ( strpos( $line, '*' ) !== 0 )
57 continue;
58 else {
59 list( $level, $line ) = $this->trimStars( $line );
61 if ( strpos( $line, '|' ) !== false ) {
62 $obj = new License( $line );
63 $this->stackItem( $this->licenses, $levels, $obj );
64 } else {
65 if ( $level < count( $levels ) ) {
66 $levels = array_slice( $levels, 0, $level );
68 if ( $level == count( $levels ) ) {
69 $levels[$level - 1] = $line;
70 } else if ( $level > count( $levels ) ) {
71 $levels[] = $line;
78 function trimStars( $str ) {
79 $i = $count = 0;
81 wfSuppressWarnings();
82 while ($str[$i++] == '*')
83 ++$count;
84 wfRestoreWarnings();
86 return array( $count, ltrim( $str, '* ' ) );
89 function stackItem( &$list, $path, $item ) {
90 $position =& $list;
91 if ( $path )
92 foreach( $path as $key )
93 $position =& $position[$key];
94 $position[] = $item;
97 function makeHtml( &$tagset, $depth = 0 ) {
98 foreach ( $tagset as $key => $val )
99 if ( is_array( $val ) ) {
100 $this->html .= $this->outputOption(
101 $this->msg( $key ),
102 array(
103 'value' => '',
104 'disabled' => 'disabled',
105 'style' => 'color: GrayText', // for MSIE
107 $depth
109 $this->makeHtml( $val, $depth + 1 );
110 } else {
111 $this->html .= $this->outputOption(
112 $this->msg( $val->text ),
113 array(
114 'value' => $val->template,
115 'title' => '{{' . $val->template . '}}'
117 $depth
122 function outputOption( $val, $attribs = null, $depth ) {
123 $val = str_repeat( /* &nbsp */ "\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 wfEmptyMsg( $str, $out ) ? $str : $out;
132 /**#@-*/
135 * Accessor for $this->licenses
137 * @return array
139 function getLicenses() { return $this->licenses; }
142 * Accessor for $this->html
144 * @return string
146 function getHtml() { return $this->html; }
150 * A License class for use on Special:Upload (represents a single type of license).
152 class License {
154 * @var string
156 var $template;
159 * @var string
161 var $text;
164 * Constructor
166 * @param $str String: license name??
168 function License( $str ) {
169 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
171 $this->template = strrev( $template );
172 $this->text = strrev( $text );