MediaWiki master
Licenses.php
Go to the documentation of this file.
1<?php
29
33class Licenses extends HTMLFormField {
34 protected string $msg;
35 protected array $lines = [];
36 protected string $html;
37 protected ?string $selected;
38
42 public function __construct( $params ) {
43 parent::__construct( $params );
44
45 $this->msg = static::getMessageFromParams( $params );
46 $this->selected = null;
47
48 $this->makeLines();
49 }
50
55 protected static function getMessageFromParams( $params ) {
56 if ( !empty( $params['licenses'] ) ) {
57 return $params['licenses'];
58 }
59
60 // If the licenses page is in $wgForceUIMsgAsContentMsg (which is the case
61 // on Commons), translations will be in the database, in subpages of this
62 // message (e.g. MediaWiki:Licenses/<lang>)
63 // If there is no such translation, the result will be '-' (the empty default
64 // in the i18n files), so we'll need to force it to look up the actual licenses
65 // in the default site language (= get the translation from MediaWiki:Licenses)
66 // Also see https://phabricator.wikimedia.org/T3495
67 $defaultMsg = wfMessage( 'licenses' )->inContentLanguage();
68 if ( $defaultMsg->isDisabled() ) {
69 $defaultMsg = wfMessage( 'licenses' )->inLanguage(
70 MediaWikiServices::getInstance()->getContentLanguage() );
71 }
72
73 return $defaultMsg->plain();
74 }
75
80 protected function buildLine( $line ) {
81 return new License( $line );
82 }
83
87 protected function makeLines() {
88 $levels = [];
89 $lines = explode( "\n", $this->msg );
90
91 foreach ( $lines as $line ) {
92 if ( !str_starts_with( $line, '*' ) ) {
93 continue;
94 }
95 [ $level, $line ] = $this->trimStars( $line );
96
97 if ( str_contains( $line, '|' ) ) {
98 $obj = $this->buildLine( $line );
99 $this->stackItem( $this->lines, $levels, $obj );
100 } else {
101 if ( $level < count( $levels ) ) {
102 $levels = array_slice( $levels, 0, $level );
103 }
104 if ( $level == count( $levels ) ) {
105 $levels[$level - 1] = $line;
106 } elseif ( $level > count( $levels ) ) {
107 $levels[] = $line;
108 }
109 }
110 }
111 }
112
117 protected function trimStars( $str ) {
118 $numStars = strspn( $str, '*' );
119 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
120 }
121
127 protected function stackItem( &$list, $path, $item ) {
128 $position =& $list;
129 if ( $path ) {
130 foreach ( $path as $key ) {
131 $position =& $position[$key];
132 }
133 }
134 $position[] = $item;
135 }
136
142 protected function makeHtml( $tagset, $depth = 0 ) {
143 $html = '';
144
145 foreach ( $tagset as $key => $val ) {
146 if ( is_array( $val ) ) {
147 $html .= $this->outputOption(
148 $key, '',
149 [
150 'disabled' => 'disabled'
151 ],
152 $depth
153 );
154 $html .= $this->makeHtml( $val, $depth + 1 );
155 } else {
156 $html .= $this->outputOption(
157 $val->text, $val->template,
158 [ 'title' => '{{' . $val->template . '}}' ],
159 $depth
160 );
161 }
162 }
163
164 return $html;
165 }
166
174 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
175 $msgObj = $this->msg( $message );
176 $text = $msgObj->exists() ? $msgObj->text() : $message;
177 $attribs['value'] = $value;
178 if ( $value === $this->selected && !isset( $attribs['disabled'] ) ) {
179 $attribs['selected'] = 'selected';
180 }
181
182 $val = str_repeat( /* &nbsp */ "\u{00A0}", $depth * 2 ) . $text;
183 return str_repeat( "\t", $depth ) . Html::element( 'option', $attribs, $val ) . "\n";
184 }
185
191 public function getLines() {
192 return $this->lines;
193 }
194
202 public function getLicenses() {
203 return $this->getLines();
204 }
205
209 public function getInputHTML( $value ) {
210 $this->selected = $value;
211
212 // add a default "no license selected" option
213 $default = $this->buildLine( '|nolicense' );
214 array_unshift( $this->lines, $default );
215
216 $html = $this->makeHtml( $this->getLines() );
217
218 $attribs = [
219 'name' => $this->mName,
220 'id' => $this->mID
221 ];
222 if ( !empty( $this->mParams['disabled'] ) ) {
223 $attribs['disabled'] = 'disabled';
224 }
225
226 $html = Html::rawElement( 'select', $attribs, $html );
227
228 // remove default "no license selected" from lines again
229 array_shift( $this->lines );
230
231 return $html;
232 }
233}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
A License class for use on Special:Upload (represents a single type of license).
Definition License.php:30
A License class for use on Special:Upload.
Definition Licenses.php:33
makeLines()
Definition Licenses.php:87
string $html
Definition Licenses.php:36
__construct( $params)
Definition Licenses.php:42
string $selected
Definition Licenses.php:37
buildLine( $line)
Definition Licenses.php:80
makeHtml( $tagset, $depth=0)
Definition Licenses.php:142
string $msg
Definition Licenses.php:34
static getMessageFromParams( $params)
Definition Licenses.php:55
outputOption( $message, $value, $attribs=null, $depth=0)
Definition Licenses.php:174
getLicenses()
Accessor for $this->lines.
Definition Licenses.php:202
array $lines
Definition Licenses.php:35
getLines()
Accessor for $this->lines.
Definition Licenses.php:191
stackItem(&$list, $path, $item)
Definition Licenses.php:127
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition Licenses.php:209
trimStars( $str)
Definition Licenses.php:117
The parent class to generate form fields.
msg( $key,... $params)
Get a translated interface message.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Service locator for MediaWiki core services.