#3364: pt_br name typo
[mediawiki.git] / includes / Licenses.php
blob2f0b1ae14c58bb47e10424c29d2911db0368f953
1 <?php
2 /**
3 * A License class for use on Special:Upload
5 * @package MediaWiki
6 * @subpackage SpecialPage
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
12 class Licenses {
13 /**#@+
14 * @access private
16 /**
17 * @var string
19 var $msg;
21 /**
22 * @var array
24 var $licenses = array();
26 /**
27 * @var string
29 var $html;
30 /**#@-*/
32 /**
33 * Constrictor
35 * @param string $str The string to build the licenses member from, will use
36 * wfMsgForContent( 'licenses' ) if null (default: null)
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 = '';
43 $this->makeLicenses();
44 $tmp = $this->getLicenses();
45 $this->makeHtml( $tmp );
48 /**#@+
49 * @access private
51 function makeLicenses() {
52 $levels = array();
53 $lines = explode( "\n", $this->msg );
55 foreach ( $lines as $line ) {
56 if ( strpos( $line, '*' ) !== 0 )
57 continue;
58 else {
59 list( $level, $line ) = $this->trimStars( $line );
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;
76 function trimStars( $str ) {
77 $i = $count = 0;
79 wfSuppressWarnings();
80 while ($str[$i++] == '*')
81 ++$count;
82 wfRestoreWarnings();
84 return array( $count, ltrim( $str, '* ' ) );
87 function stackItem( &$list, $path, $item ) {
88 $position =& $list;
89 if( $path ) {
90 foreach( $path as $key ) {
91 $position =& $position[$key];
94 $position[] = $item;
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',
106 $depth
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
116 $depth
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";
126 function msg( $str ) {
127 $out = wfMsg( $str );
128 return wfNoMsg( $str, $out ) ? $str : $out;
131 /**#@-*/
134 * Accessor for $this->licenses
136 * @return array
138 function getLicenses() { return $this->licenses; }
141 * Accessor for $this->html
143 * @return string
145 function getHtml() { return $this->html; }
148 class License {
150 * @var string
152 var $template;
155 * @var string
157 var $text;
160 * Constructor
162 * @param string $str
164 function License( $str ) {
165 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
167 $this->template = strrev( $template );
168 $this->text = strrev( $text );