(bug 22617), FileRepo::append() definition does not match child, change it to be...
[lhc/web/wiklou.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 extends HTMLFormField {
13 /**
14 * @var string
15 */
16 protected $msg;
17
18 /**
19 * @var array
20 */
21 protected $licenses = array();
22
23 /**
24 * @var string
25 */
26 protected $html;
27 /**#@-*/
28
29 /**
30 * Constructor
31 */
32 public function __construct( $params ) {
33 parent::__construct( $params );
34
35 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
36 $this->selected = null;
37
38 $this->makeLicenses();
39 }
40
41 /**#@+
42 * @private
43 */
44 protected function makeLicenses() {
45 $levels = array();
46 $lines = explode( "\n", $this->msg );
47
48 foreach ( $lines as $line ) {
49 if ( strpos( $line, '*' ) !== 0 )
50 continue;
51 else {
52 list( $level, $line ) = $this->trimStars( $line );
53
54 if ( strpos( $line, '|' ) !== false ) {
55 $obj = new License( $line );
56 $this->stackItem( $this->licenses, $levels, $obj );
57 } else {
58 if ( $level < count( $levels ) ) {
59 $levels = array_slice( $levels, 0, $level );
60 }
61 if ( $level == count( $levels ) ) {
62 $levels[$level - 1] = $line;
63 } else if ( $level > count( $levels ) ) {
64 $levels[] = $line;
65 }
66 }
67 }
68 }
69 }
70
71 protected function trimStars( $str ) {
72 $i = $count = 0;
73
74 $numStars = strspn( $str, '*' );
75 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
76 }
77
78 protected function stackItem( &$list, $path, $item ) {
79 $position =& $list;
80 if ( $path )
81 foreach( $path as $key )
82 $position =& $position[$key];
83 $position[] = $item;
84 }
85
86 protected function makeHtml( $tagset, $depth = 0 ) {
87 foreach ( $tagset as $key => $val )
88 if ( is_array( $val ) ) {
89 $this->html .= $this->outputOption(
90 $this->msg( $key ), '',
91 array(
92 'disabled' => 'disabled',
93 'style' => 'color: GrayText', // for MSIE
94 ),
95 $depth
96 );
97 $this->makeHtml( $val, $depth + 1 );
98 } else {
99 $this->html .= $this->outputOption(
100 $this->msg( $val->text ), $val->template,
101 array( 'title' => '{{' . $val->template . '}}' ),
102 $depth
103 );
104 }
105 }
106
107 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
108 $attribs['value'] = $value;
109 if ( $value === $this->selected )
110 $attribs['selected'] = 'selected';
111 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
112 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
113 }
114
115 protected function msg( $str ) {
116 $out = wfMsg( $str );
117 return wfEmptyMsg( $str, $out ) ? $str : $out;
118 }
119
120 /**#@-*/
121
122 /**
123 * Accessor for $this->licenses
124 *
125 * @return array
126 */
127 public function getLicenses() { return $this->licenses; }
128
129 /**
130 * Accessor for $this->html
131 *
132 * @return string
133 */
134 public function getInputHTML( $value ) {
135 $this->selected = $value;
136
137 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
138 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
139 $this->makeHtml( $this->getLicenses() );
140
141 $attribs = array(
142 'name' => $this->mName,
143 'id' => $this->mID
144 );
145 if ( !empty( $this->mParams['disabled'] ) )
146 $attibs['disabled'] = 'disabled';
147
148 return Html::rawElement( 'select', $attribs, $this->html );
149 }
150 }
151
152 /**
153 * A License class for use on Special:Upload (represents a single type of license).
154 */
155 class License {
156 /**
157 * @var string
158 */
159 var $template;
160
161 /**
162 * @var string
163 */
164 var $text;
165
166 /**
167 * Constructor
168 *
169 * @param $str String: license name??
170 */
171 function License( $str ) {
172 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
173
174 $this->template = strrev( $template );
175 $this->text = strrev( $text );
176 }
177 }