Avar casts lvl 5 enhance to Licenses.php:
[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 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 */
11
12 class Licenses {
13 /**#@+
14 * @access private
15 */
16 /**
17 * @var string
18 */
19 var $msg;
20
21 /**
22 * @var array
23 */
24 var $licenses = array();
25
26 /**
27 * @var string
28 */
29 var $html;
30 /**#@-*/
31
32 /**
33 * Constrictor
34 *
35 * @param string $str The string to build the licenses member from, will use
36 * wfMsgForContent( 'licenses' ) if null (default: null)
37 */
38 function Licenses( $str = null ) {
39 // PHP sucks, this should be possible in the constructor
40 $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
41 $this->html = '';
42
43 $this->makeLicenses();
44 $tmp = $this->getLicenses();
45 $this->makeHtml( $tmp );
46 }
47
48 /**#@+
49 * @access private
50 */
51 function makeLicenses() {
52 $levels = array();
53 $lines = explode( "\n", $this->msg );
54
55 foreach ( $lines as $line ) {
56 if ( strpos( $line, '*' ) !== 0 )
57 continue;
58 else {
59 list( $level, $line ) = $this->trimStars( $line );
60
61 if ( strpos( $line, '|' ) !== false ) {
62 $obj = new License( $line );
63 // TODO: Do this without using eval()
64 eval( '$this->licenses' . $this->makeIndexes( $levels ) . '[] = $obj;' );
65 } else {
66 if ( $level < count( $levels ) )
67 $levels = array_slice( $levels, count( $levels ) - $level );
68 if ( $level == count( $levels ) )
69 $levels[$level - 1] = $line;
70 else if ( $level > count( $levels ) )
71 $levels[] = $line;
72
73 }
74 }
75 }
76 }
77
78 function trimStars( $str ) {
79 $i = $count = 0;
80
81 while ($str[$i++] == '*')
82 ++$count;
83
84 return array( $count, ltrim( $str, '* ' ) );
85 }
86
87 function makeIndexes( $arr ) {
88 $str = '';
89
90 wfSuppressWarnings();
91 foreach ( $arr as $item )
92 $str .= '["' . addslashes( $item ) . '"]';
93
94 wfRestoreWarnings();
95 return $str;
96 }
97
98 function makeHtml( &$tagset, $depth = 0 ) {
99 foreach ( $tagset as $key => $val )
100 if ( is_array( $val ) ) {
101
102 $this->html .= $this->outputOption(
103 $this->msg( $key ),
104 array(
105 'value' => '',
106 'disabled' => 'disabled',
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 wfNoMsg( $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 string $str
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 ?>