eedc2cd39e5213d88e74d73a764a0a8ef3916cbe
[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 * Constrictor
33 *
34 * @param string $str The string to build the licenses member from, will use
35 * wfMsgForContent( 'licenses' ) if null (default: null)
36 */
37 function Licenses( $str = null ) {
38 // PHP sucks, this should be possible in the constructor
39 $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
40 $this->html = '';
41
42 $this->makeLicenses();
43 $tmp = $this->getLicenses();
44 $this->makeHtml( $tmp );
45 }
46
47 /**#@+
48 * @access private
49 */
50 function makeLicenses() {
51 $levels = array();
52 $lines = explode( "\n", $this->msg );
53
54 foreach ( $lines as $line ) {
55 if ( strpos( $line, '*' ) !== 0 )
56 continue;
57 else {
58 list( $level, $line ) = $this->trimStars( $line );
59
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;' );
64 } else {
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 ) )
70 $levels[] = $line;
71
72 }
73 }
74 }
75 }
76
77 function trimStars( $str ) {
78 $i = $count = 0;
79
80 while ($str[$i++] == '*')
81 ++$count;
82
83 return array( $count, ltrim( $str, '* ' ) );
84 }
85
86 function makeIndexes( $arr ) {
87 $str = '';
88
89 wfSuppressWarnings();
90 foreach ( $arr as $item )
91 $str .= '["' . addslashes( $item ) . '"]';
92
93 wfRestoreWarnings();
94 return $str;
95 }
96
97 function makeHtml( &$tagset, $depth = 0 ) {
98 foreach ( $tagset as $key => $val )
99 if ( is_array( $val ) ) {
100
101 $this->html .= $this->outputOption(
102 $this->msg( $key ),
103 array(
104 'value' => '',
105 'disabled' => 'disabled',
106 ),
107 $depth
108 );
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
116 ),
117 $depth
118 );
119 }
120 }
121
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";
125 }
126
127 function msg( $str ) {
128 $out = wfMsg( $str );
129 return wfNoMsg( $str, $out ) ? $str : $out;
130 }
131
132 /**#@-*/
133
134 /**
135 * Accessor for $this->licenses
136 *
137 * @return array
138 */
139 function getLicenses() { return $this->licenses; }
140
141 /**
142 * Accessor for $this->html
143 *
144 * @return string
145 */
146 function getHtml() { return $this->html; }
147 }
148
149 class License {
150 /**
151 * @var string
152 */
153 var $template;
154
155 /**
156 * @var string
157 */
158 var $text;
159
160 /**
161 * Constructor
162 *
163 * @param string $str
164 */
165 function License( $str ) {
166 list( $template, $text ) = explode( '|', $str, 2 );
167
168 $this->template = $template;
169 $this->text = $text;
170 }
171 }
172 ?>