updated
[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 $this->stackItem( $this->licenses, $levels, $obj );
64 } else {
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 ) )
70 $levels[] = $line;
71 }
72 }
73 }
74 }
75
76 function trimStars( $str ) {
77 $i = $count = 0;
78
79 wfSuppressWarnings();
80 while ($str[$i++] == '*')
81 ++$count;
82 wfRestoreWarnings();
83
84 return array( $count, ltrim( $str, '* ' ) );
85 }
86
87 function stackItem( &$list, $path, $item ) {
88 $position =& $list;
89 if ( $path )
90 foreach( $path as $key )
91 $position =& $position[$key];
92 $position[] = $item;
93 }
94
95 function makeHtml( &$tagset, $depth = 0 ) {
96 foreach ( $tagset as $key => $val )
97 if ( is_array( $val ) ) {
98 $this->html .= $this->outputOption(
99 $this->msg( $key ),
100 array(
101 'value' => '',
102 'disabled' => 'disabled',
103 ),
104 $depth
105 );
106 $this->makeHtml( $val, $depth + 1 );
107 } else {
108 $this->html .= $this->outputOption(
109 $this->msg( $val->text ),
110 array(
111 'value' => $val->template,
112 'title' => '{{' . $val->template . '}}'
113 ),
114 $depth
115 );
116 }
117 }
118
119 function outputOption( $val, $attribs = null, $depth ) {
120 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $val;
121 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
122 }
123
124 function msg( $str ) {
125 $out = wfMsg( $str );
126 return wfEmptyMsg( $str, $out ) ? $str : $out;
127 }
128
129 /**#@-*/
130
131 /**
132 * Accessor for $this->licenses
133 *
134 * @return array
135 */
136 function getLicenses() { return $this->licenses; }
137
138 /**
139 * Accessor for $this->html
140 *
141 * @return string
142 */
143 function getHtml() { return $this->html; }
144 }
145
146 class License {
147 /**
148 * @var string
149 */
150 var $template;
151
152 /**
153 * @var string
154 */
155 var $text;
156
157 /**
158 * Constructor
159 *
160 * @param string $str
161 */
162 function License( $str ) {
163 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
164
165 $this->template = strrev( $template );
166 $this->text = strrev( $text );
167 }
168 }
169 ?>