* Support for a license selection box on Special:Upload, configurable from MediaWiki...
[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 foreach ( $arr as $item )
90 $str .= '["' . addslashes( $item ) . '"]';
91
92 return $str;
93 }
94
95 function makeHtml( &$tagset, $depth = 0 ) {
96 foreach ( $tagset as $key => $val )
97 if ( is_array( $val ) ) {
98
99 $this->html .= $this->outputOption(
100 $this->msg( $key ),
101 array(
102 'value' => ''
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 ),
113 $depth
114 );
115 }
116 }
117
118 function outputOption( $val, $attribs = null, $depth ) {
119 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth ) . $val;
120 return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
121 }
122
123 function msg( $str ) {
124 $out = wfMsg( $str );
125 return wfNoMsg( $str, $out ) ? $str : $out;
126 }
127
128 /**#@-*/
129
130 /**
131 * Accessor for $this->licenses
132 *
133 * @return array
134 */
135 function getLicenses() { return $this->licenses; }
136
137 /**
138 * Accessor for $this->html
139 *
140 * @return string
141 */
142 function getHtml() { return $this->html; }
143 }
144
145 class License {
146 /**
147 * @var string
148 */
149 var $template;
150
151 /**
152 * @var string
153 */
154 var $text;
155
156 /**
157 * Constructor
158 *
159 * @param string $str
160 */
161 function License( $str ) {
162 list( $template, $text ) = explode( '|', $str, 2 );
163
164 $this->template = $template;
165 $this->text = $text;
166 }
167 }
168 ?>