introduce a new setting that allows extension authors to whitelist deprecated funtion...
[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 * @param $params array
33 */
34 public function __construct( $params ) {
35 parent::__construct( $params );
36
37 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
38 $this->selected = null;
39
40 $this->makeLicenses();
41 }
42
43 /**
44 * @private
45 */
46 protected function makeLicenses() {
47 $levels = array();
48 $lines = explode( "\n", $this->msg );
49
50 foreach ( $lines as $line ) {
51 if ( strpos( $line, '*' ) !== 0 ) {
52 continue;
53 } else {
54 list( $level, $line ) = $this->trimStars( $line );
55
56 if ( strpos( $line, '|' ) !== false ) {
57 $obj = new License( $line );
58 $this->stackItem( $this->licenses, $levels, $obj );
59 } else {
60 if ( $level < count( $levels ) ) {
61 $levels = array_slice( $levels, 0, $level );
62 }
63 if ( $level == count( $levels ) ) {
64 $levels[$level - 1] = $line;
65 } elseif ( $level > count( $levels ) ) {
66 $levels[] = $line;
67 }
68 }
69 }
70 }
71 }
72
73 /**
74 * @param $str
75 * @return array
76 */
77 protected function trimStars( $str ) {
78 $numStars = strspn( $str, '*' );
79 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
80 }
81
82 /**
83 * @param $list
84 * @param $path
85 * @param $item
86 */
87 protected function stackItem( &$list, $path, $item ) {
88 $position =& $list;
89 if ( $path ) {
90 foreach( $path as $key ) {
91 $position =& $position[$key];
92 }
93 }
94 $position[] = $item;
95 }
96
97 /**
98 * @param $tagset
99 * @param $depth int
100 */
101 protected function makeHtml( $tagset, $depth = 0 ) {
102 foreach ( $tagset as $key => $val )
103 if ( is_array( $val ) ) {
104 $this->html .= $this->outputOption(
105 $this->msg( $key ), '',
106 array(
107 'disabled' => 'disabled',
108 'style' => 'color: GrayText', // for MSIE
109 ),
110 $depth
111 );
112 $this->makeHtml( $val, $depth + 1 );
113 } else {
114 $this->html .= $this->outputOption(
115 $this->msg( $val->text ), $val->template,
116 array( 'title' => '{{' . $val->template . '}}' ),
117 $depth
118 );
119 }
120 }
121
122 /**
123 * @param $text
124 * @param $value
125 * @param $attribs null
126 * @param $depth int
127 * @return string
128 */
129 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
130 $attribs['value'] = $value;
131 if ( $value === $this->selected )
132 $attribs['selected'] = 'selected';
133 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
134 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
135 }
136
137 /**
138 * @param $str string
139 * @return String
140 */
141 protected function msg( $str ) {
142 $msg = wfMessage( $str );
143 return $msg->exists() ? $msg->text() : $str;
144 }
145
146 /**#@-*/
147
148 /**
149 * Accessor for $this->licenses
150 *
151 * @return array
152 */
153 public function getLicenses() {
154 return $this->licenses;
155 }
156
157 /**
158 * Accessor for $this->html
159 *
160 * @param $value bool
161 *
162 * @return string
163 */
164 public function getInputHTML( $value ) {
165 $this->selected = $value;
166
167 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
168 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
169 $this->makeHtml( $this->getLicenses() );
170
171 $attribs = array(
172 'name' => $this->mName,
173 'id' => $this->mID
174 );
175 if ( !empty( $this->mParams['disabled'] ) ) {
176 $attibs['disabled'] = 'disabled';
177 }
178
179 return Html::rawElement( 'select', $attribs, $this->html );
180 }
181 }
182
183 /**
184 * A License class for use on Special:Upload (represents a single type of license).
185 */
186 class License {
187 /**
188 * @var string
189 */
190 var $template;
191
192 /**
193 * @var string
194 */
195 var $text;
196
197 /**
198 * Constructor
199 *
200 * @param $str String: license name??
201 */
202 function __construct( $str ) {
203 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
204
205 $this->template = strrev( $template );
206 $this->text = strrev( $text );
207 }
208 }