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