Merge "Change X-UA-Compatible from <meta> tag to HTTP header"
[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 array $params
51 */
52 public function __construct( $params ) {
53 parent::__construct( $params );
54
55 $this->msg = empty( $params['licenses'] ) ? wfMessage( 'licenses' )->inContentLanguage()->plain() : $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 string $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 array $list
102 * @param array $path
103 * @param mixed $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 array $tagset
117 * @param int $depth
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 $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 $val->text, $val->template,
134 array( 'title' => '{{' . $val->template . '}}' ),
135 $depth
136 );
137 }
138 }
139 }
140
141 /**
142 * @param string $message
143 * @param string $value
144 * @param null|array $attribs
145 * @param int $depth
146 * @return string
147 */
148 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
149 $msgObj = $this->msg( $message );
150 $text = $msgObj->exists() ? $msgObj->text() : $message;
151 $attribs['value'] = $value;
152 if ( $value === $this->selected ) {
153 $attribs['selected'] = 'selected';
154 }
155
156 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
157 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
158 }
159
160 /**#@-*/
161
162 /**
163 * Accessor for $this->licenses
164 *
165 * @return array
166 */
167 public function getLicenses() {
168 return $this->licenses;
169 }
170
171 /**
172 * Accessor for $this->html
173 *
174 * @param bool $value
175 *
176 * @return string
177 */
178 public function getInputHTML( $value ) {
179 $this->selected = $value;
180
181 $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
182 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
183 $this->makeHtml( $this->getLicenses() );
184
185 $attribs = array(
186 'name' => $this->mName,
187 'id' => $this->mID
188 );
189 if ( !empty( $this->mParams['disabled'] ) ) {
190 $attibs['disabled'] = 'disabled';
191 }
192
193 return Html::rawElement( 'select', $attribs, $this->html );
194 }
195 }
196
197 /**
198 * A License class for use on Special:Upload (represents a single type of license).
199 */
200 class License {
201 /**
202 * @var string
203 */
204 var $template;
205
206 /**
207 * @var string
208 */
209 var $text;
210
211 /**
212 * Constructor
213 *
214 * @param string $str license name??
215 */
216 function __construct( $str ) {
217 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
218
219 $this->template = strrev( $template );
220 $this->text = strrev( $text );
221 }
222 }