(bug 37183) Removed hard coded parentheses in SpecialListfiles.php
[lhc/web/wiklou.git] / includes / Licenses.php
1 <?php
2 /**
3 * License selector for use on Special:Upload.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
24 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
25 */
26
27 /**
28 * A License class for use on Special:Upload
29 */
30 class Licenses extends HTMLFormField {
31 /**
32 * @var string
33 */
34 protected $msg;
35
36 /**
37 * @var array
38 */
39 protected $licenses = array();
40
41 /**
42 * @var string
43 */
44 protected $html;
45 /**#@-*/
46
47 /**
48 * Constructor
49 *
50 * @param $params array
51 */
52 public function __construct( $params ) {
53 parent::__construct( $params );
54
55 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
56 $this->selected = null;
57
58 $this->makeLicenses();
59 }
60
61 /**
62 * @private
63 */
64 protected function makeLicenses() {
65 $levels = array();
66 $lines = explode( "\n", $this->msg );
67
68 foreach ( $lines as $line ) {
69 if ( strpos( $line, '*' ) !== 0 ) {
70 continue;
71 } else {
72 list( $level, $line ) = $this->trimStars( $line );
73
74 if ( strpos( $line, '|' ) !== false ) {
75 $obj = new License( $line );
76 $this->stackItem( $this->licenses, $levels, $obj );
77 } else {
78 if ( $level < count( $levels ) ) {
79 $levels = array_slice( $levels, 0, $level );
80 }
81 if ( $level == count( $levels ) ) {
82 $levels[$level - 1] = $line;
83 } elseif ( $level > count( $levels ) ) {
84 $levels[] = $line;
85 }
86 }
87 }
88 }
89 }
90
91 /**
92 * @param $str
93 * @return array
94 */
95 protected function trimStars( $str ) {
96 $numStars = strspn( $str, '*' );
97 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
98 }
99
100 /**
101 * @param $list
102 * @param $path
103 * @param $item
104 */
105 protected function stackItem( &$list, $path, $item ) {
106 $position =& $list;
107 if ( $path ) {
108 foreach( $path as $key ) {
109 $position =& $position[$key];
110 }
111 }
112 $position[] = $item;
113 }
114
115 /**
116 * @param $tagset
117 * @param $depth int
118 */
119 protected function makeHtml( $tagset, $depth = 0 ) {
120 foreach ( $tagset as $key => $val )
121 if ( is_array( $val ) ) {
122 $this->html .= $this->outputOption(
123 $this->msg( $key ), '',
124 array(
125 'disabled' => 'disabled',
126 'style' => 'color: GrayText', // for MSIE
127 ),
128 $depth
129 );
130 $this->makeHtml( $val, $depth + 1 );
131 } else {
132 $this->html .= $this->outputOption(
133 $this->msg( $val->text ), $val->template,
134 array( 'title' => '{{' . $val->template . '}}' ),
135 $depth
136 );
137 }
138 }
139
140 /**
141 * @param $text
142 * @param $value
143 * @param $attribs null
144 * @param $depth int
145 * @return string
146 */
147 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
148 $attribs['value'] = $value;
149 if ( $value === $this->selected )
150 $attribs['selected'] = 'selected';
151 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
152 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
153 }
154
155 /**
156 * @param $str string
157 * @return String
158 */
159 protected function msg( $str ) {
160 $msg = wfMessage( $str );
161 return $msg->exists() ? $msg->text() : $str;
162 }
163
164 /**#@-*/
165
166 /**
167 * Accessor for $this->licenses
168 *
169 * @return array
170 */
171 public function getLicenses() {
172 return $this->licenses;
173 }
174
175 /**
176 * Accessor for $this->html
177 *
178 * @param $value bool
179 *
180 * @return string
181 */
182 public function getInputHTML( $value ) {
183 $this->selected = $value;
184
185 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
186 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
187 $this->makeHtml( $this->getLicenses() );
188
189 $attribs = array(
190 'name' => $this->mName,
191 'id' => $this->mID
192 );
193 if ( !empty( $this->mParams['disabled'] ) ) {
194 $attibs['disabled'] = 'disabled';
195 }
196
197 return Html::rawElement( 'select', $attribs, $this->html );
198 }
199 }
200
201 /**
202 * A License class for use on Special:Upload (represents a single type of license).
203 */
204 class License {
205 /**
206 * @var string
207 */
208 var $template;
209
210 /**
211 * @var string
212 */
213 var $text;
214
215 /**
216 * Constructor
217 *
218 * @param $str String: license name??
219 */
220 function __construct( $str ) {
221 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
222
223 $this->template = strrev( $template );
224 $this->text = strrev( $text );
225 }
226 }