]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/Licenses.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / Licenses.php
1 <?php
2 /**
3  * A License class for use on Special:Upload
4  *
5  * @ingroup SpecialPage
6  *
7  * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8  * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10  */
11
12 class Licenses {
13         /**#@+
14          * @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          * Constructor
34          *
35          * @param $str String: the string to build the licenses member from, will use
36          *                    wfMsgForContent( 'licenses' ) if null (default: null)
37          */
38         function __construct( $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          * @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                                         }
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
78         function trimStars( $str ) {
79                 $i = $count = 0;
80
81                 wfSuppressWarnings();
82                 while ($str[$i++] == '*')
83                         ++$count;
84                 wfRestoreWarnings();
85
86                 return array( $count, ltrim( $str, '* ' ) );
87         }
88
89         function stackItem( &$list, $path, $item ) {
90                 $position =& $list;
91                 if ( $path )
92                         foreach( $path as $key )
93                                 $position =& $position[$key];
94                 $position[] = $item;
95         }
96
97         function makeHtml( &$tagset, $depth = 0 ) {
98                 foreach ( $tagset as $key => $val )
99                         if ( is_array( $val ) ) {
100                                 $this->html .= $this->outputOption(
101                                         $this->msg( $key ),
102                                         array(
103                                                 'value' => '',
104                                                 'disabled' => 'disabled',
105                                                 'style' => 'color: GrayText', // for MSIE
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 ) . Xml::element( 'option', $attribs, $val ) . "\n";
125         }
126
127         function msg( $str ) {
128                 $out = wfMsg( $str );
129                 return wfEmptyMsg( $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 /**
150  * A License class for use on Special:Upload (represents a single type of license).
151  */
152 class License {
153         /**
154          * @var string
155          */
156         var $template;
157
158         /**
159          * @var string
160          */
161         var $text;
162
163         /**
164          * Constructor
165          *
166          * @param $str String: license name??
167          */
168         function License( $str ) {
169                 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
170
171                 $this->template = strrev( $template );
172                 $this->text = strrev( $text );
173         }
174 }