Code housekeeping stuff (and barring any stuff-ups on my behalf, there should be...
[lhc/web/wiklou.git] / includes / Licenses.php
1 <?php
2 /**
3 * A License class for use on Special:Upload
4 *
5 * @package MediaWiki
6 * @subpackage SpecialPage
7 *
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
11 */
12
13 class Licenses {
14 /**#@+
15 * @private
16 */
17 /**
18 * @var string
19 */
20 var $msg;
21
22 /**
23 * @var array
24 */
25 var $licenses = array();
26
27 /**
28 * @var string
29 */
30 var $html;
31 /**#@-*/
32
33 /**
34 * Constrictor
35 *
36 * @param $str String: the string to build the licenses member from, will use
37 * wfMsgForContent( 'licenses' ) if null (default: null)
38 */
39 function Licenses( $str = null ) {
40 // PHP sucks, this should be possible in the constructor
41 $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
42 $this->html = '';
43
44 $this->makeLicenses();
45 $tmp = $this->getLicenses();
46 $this->makeHtml( $tmp );
47 }
48
49 /**#@+
50 * @private
51 */
52 function makeLicenses() {
53 $levels = array();
54 $lines = explode( "\n", $this->msg );
55
56 foreach ( $lines as $line ) {
57 if ( strpos( $line, '*' ) !== 0 )
58 continue;
59 else {
60 list( $level, $line ) = $this->trimStars( $line );
61
62 if ( strpos( $line, '|' ) !== false ) {
63 $obj = new License( $line );
64 $this->stackItem( $this->licenses, $levels, $obj );
65 } else {
66 if ( $level < count( $levels ) ) {
67 $levels = array_slice( $levels, 0, $level );
68 }
69 if ( $level == count( $levels ) ) {
70 $levels[$level - 1] = $line;
71 } else if ( $level > count( $levels ) ) {
72 $levels[] = $line;
73 }
74 }
75 }
76 }
77 }
78
79 function trimStars( $str ) {
80 $i = $count = 0;
81
82 wfSuppressWarnings();
83 while ($str[$i++] == '*')
84 ++$count;
85 wfRestoreWarnings();
86
87 return array( $count, ltrim( $str, '* ' ) );
88 }
89
90 function stackItem( &$list, $path, $item ) {
91 $position =& $list;
92 if ( $path )
93 foreach( $path as $key )
94 $position =& $position[$key];
95 $position[] = $item;
96 }
97
98 function makeHtml( &$tagset, $depth = 0 ) {
99 foreach ( $tagset as $key => $val )
100 if ( is_array( $val ) ) {
101 $this->html .= $this->outputOption(
102 $this->msg( $key ),
103 array(
104 'value' => '',
105 'disabled' => 'disabled',
106 'style' => 'color: GrayText', // for MSIE
107 ),
108 $depth
109 );
110 $this->makeHtml( $val, $depth + 1 );
111 } else {
112 $this->html .= $this->outputOption(
113 $this->msg( $val->text ),
114 array(
115 'value' => $val->template,
116 'title' => '{{' . $val->template . '}}'
117 ),
118 $depth
119 );
120 }
121 }
122
123 function outputOption( $val, $attribs = null, $depth ) {
124 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $val;
125 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
126 }
127
128 function msg( $str ) {
129 $out = wfMsg( $str );
130 return wfEmptyMsg( $str, $out ) ? $str : $out;
131 }
132
133 /**#@-*/
134
135 /**
136 * Accessor for $this->licenses
137 *
138 * @return array
139 */
140 function getLicenses() { return $this->licenses; }
141
142 /**
143 * Accessor for $this->html
144 *
145 * @return string
146 */
147 function getHtml() { return $this->html; }
148 }
149
150 class License {
151 /**
152 * @var string
153 */
154 var $template;
155
156 /**
157 * @var string
158 */
159 var $text;
160
161 /**
162 * Constructor
163 *
164 * @param $str String: license name??
165 */
166 function License( $str ) {
167 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
168
169 $this->template = strrev( $template );
170 $this->text = strrev( $text );
171 }
172 }
173 ?>