Commons upload license list language fix
[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 * @access 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 string $str 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
42 global $wgLanguageCode ;
43 if ( is_null( $str ) ) {
44 # Try for language-localized license list; if not, try the deault
45 $t = 'licenses/'.$wgLanguageCode ;
46 $s = wfMsgForContent( $t ) ;
47 if ( $s == '&lt;'.$t.'&gt;' )
48 $t = wfMsgForContent( 'licenses' ) ;
49 $this->msg = $s ;
50 } else {
51 # Use passed string
52 $this->msg = $str ;
53 }
54 # $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str; # Old code, do not use!
55 $this->html = '';
56
57 $this->makeLicenses();
58 $tmp = $this->getLicenses();
59 $this->makeHtml( $tmp );
60 }
61
62 /**#@+
63 * @access private
64 */
65 function makeLicenses() {
66 $levels = array();
67 $lines = explode( "\n", $this->msg );
68
69 foreach ( $lines as $line ) {
70 if ( strpos( $line, '*' ) !== 0 ) {
71 continue;
72 } else {
73 list( $level, $line ) = $this->trimStars( $line );
74
75 if ( strpos( $line, '|' ) !== false ) {
76 $obj = new License( $line );
77 $this->stackItem( $this->licenses, $levels, $obj );
78 } else {
79 if ( $level < count( $levels ) ) {
80 $levels = array_slice( $levels, 0, $level );
81 }
82 if ( $level == count( $levels ) ) {
83 $levels[$level - 1] = $line;
84 } elseif ( $level > count( $levels ) ) {
85 $levels[] = $line;
86 }
87 }
88 }
89 } // end foreach
90 }
91
92 function trimStars( $str ) {
93 $i = $count = 0;
94
95 wfSuppressWarnings();
96 while ($str[$i++] == '*')
97 ++$count;
98 wfRestoreWarnings();
99
100 return array( $count, ltrim( $str, '* ' ) );
101 }
102
103 function stackItem( &$list, $path, $item ) {
104 $position =& $list;
105 if ( $path )
106 foreach( $path as $key )
107 $position =& $position[$key];
108 $position[] = $item;
109 }
110
111 function makeHtml( &$tagset, $depth = 0 ) {
112 foreach ( $tagset as $key => $val )
113 if ( is_array( $val ) ) {
114 $this->html .= $this->outputOption(
115 $this->msg( $key ),
116 array(
117 'value' => '',
118 'disabled' => 'disabled',
119 ),
120 $depth
121 );
122 $this->makeHtml( $val, $depth + 1 );
123 } else {
124 $this->html .= $this->outputOption(
125 $this->msg( $val->text ),
126 array(
127 'value' => $val->template,
128 'title' => '{{' . $val->template . '}}'
129 ),
130 $depth
131 );
132 }
133 }
134
135 function outputOption( $val, $attribs = null, $depth ) {
136 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $val;
137 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
138 }
139
140 function msg( $str ) {
141 $out = wfMsg( $str );
142 return wfEmptyMsg( $str, $out ) ? $str : $out;
143 }
144
145 /**#@-*/
146
147 /**
148 * Accessor for $this->licenses
149 *
150 * @return array
151 */
152 function getLicenses() { return $this->licenses; }
153
154 /**
155 * Accessor for $this->html
156 *
157 * @return string
158 */
159 function getHtml() { return $this->html; }
160 }
161
162 class License {
163 /**
164 * @var string
165 */
166 var $template;
167
168 /**
169 * @var string
170 */
171 var $text;
172
173 /**
174 * Constructor
175 *
176 * @param string $str
177 */
178 function License( $str ) {
179 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
180
181 $this->template = strrev( $template );
182 $this->text = strrev( $text );
183 }
184 }
185 ?>