MediaWiki master
Licenses.php
Go to the documentation of this file.
1<?php
15
19class Licenses extends HTMLFormField {
20 protected string $msg;
21 protected array $lines = [];
22 protected string $html;
23 protected ?string $selected;
24
28 public function __construct( $params ) {
29 parent::__construct( $params );
30
31 $this->msg = static::getMessageFromParams( $params );
32 $this->selected = null;
33
34 $this->makeLines();
35 }
36
41 protected static function getMessageFromParams( $params ) {
42 if ( !empty( $params['licenses'] ) ) {
43 return $params['licenses'];
44 }
45
46 // If the licenses page is in $wgForceUIMsgAsContentMsg (which is the case
47 // on Commons), translations will be in the database, in subpages of this
48 // message (e.g. MediaWiki:Licenses/<lang>)
49 // If there is no such translation, the result will be '-' (the empty default
50 // in the i18n files), so we'll need to force it to look up the actual licenses
51 // in the default site language (= get the translation from MediaWiki:Licenses)
52 // Also see https://phabricator.wikimedia.org/T3495
53 $defaultMsg = wfMessage( 'licenses' )->inContentLanguage();
54 if ( $defaultMsg->isDisabled() ) {
55 $defaultMsg = wfMessage( 'licenses' )->inLanguage(
56 MediaWikiServices::getInstance()->getContentLanguage() );
57 }
58
59 return $defaultMsg->plain();
60 }
61
66 protected function buildLine( $line ) {
67 return new License( $line );
68 }
69
73 protected function makeLines() {
74 $levels = [];
75 $lines = explode( "\n", $this->msg );
76
77 foreach ( $lines as $line ) {
78 if ( !str_starts_with( $line, '*' ) ) {
79 continue;
80 }
81 [ $level, $line ] = $this->trimStars( $line );
82
83 if ( str_contains( $line, '|' ) ) {
84 $obj = $this->buildLine( $line );
85 $this->stackItem( $this->lines, $levels, $obj );
86 } else {
87 if ( $level < count( $levels ) ) {
88 $levels = array_slice( $levels, 0, $level );
89 }
90 if ( $level == count( $levels ) ) {
91 $levels[$level - 1] = $line;
92 } elseif ( $level > count( $levels ) ) {
93 $levels[] = $line;
94 }
95 }
96 }
97 }
98
103 protected function trimStars( $str ) {
104 $numStars = strspn( $str, '*' );
105 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
106 }
107
113 protected function stackItem( &$list, $path, $item ) {
114 $position =& $list;
115 if ( $path ) {
116 foreach ( $path as $key ) {
117 $position =& $position[$key];
118 }
119 }
120 $position[] = $item;
121 }
122
128 protected function makeHtml( $tagset, $depth = 0 ) {
129 $html = '';
130
131 foreach ( $tagset as $key => $val ) {
132 if ( is_array( $val ) ) {
133 $html .= $this->outputOption(
134 $key, '',
135 [
136 'disabled' => 'disabled'
137 ],
138 $depth
139 );
140 $html .= $this->makeHtml( $val, $depth + 1 );
141 } else {
142 $html .= $this->outputOption(
143 $val->text, $val->template,
144 [ 'title' => '{{' . $val->template . '}}' ],
145 $depth
146 );
147 }
148 }
149
150 return $html;
151 }
152
160 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
161 $msgObj = $this->msg( $message );
162 $text = $msgObj->exists() ? $msgObj->text() : $message;
163 $attribs['value'] = $value;
164 if ( $value === $this->selected && !isset( $attribs['disabled'] ) ) {
165 $attribs['selected'] = 'selected';
166 }
167
168 $val = str_repeat( /* &nbsp */ "\u{00A0}", $depth * 2 ) . $text;
169 return str_repeat( "\t", $depth ) . Html::element( 'option', $attribs, $val ) . "\n";
170 }
171
177 public function getLines() {
178 return $this->lines;
179 }
180
188 public function getLicenses() {
189 return $this->getLines();
190 }
191
195 public function getInputHTML( $value ) {
196 $this->selected = $value;
197
198 // add a default "no license selected" option
199 $default = $this->buildLine( '|nolicense' );
200 array_unshift( $this->lines, $default );
201
202 $html = $this->makeHtml( $this->getLines() );
203
204 $attribs = [
205 'name' => $this->mName,
206 'id' => $this->mID
207 ];
208 if ( !empty( $this->mParams['disabled'] ) ) {
209 $attribs['disabled'] = 'disabled';
210 }
211
212 $html = Html::rawElement( 'select', $attribs, $html );
213
214 // remove default "no license selected" from lines again
215 array_shift( $this->lines );
216
217 return $html;
218 }
219}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A License class for use on Special:Upload (represents a single type of license).
Definition License.php:16
A License class for use on Special:Upload.
Definition Licenses.php:19
makeLines()
Definition Licenses.php:73
string $html
Definition Licenses.php:22
__construct( $params)
Definition Licenses.php:28
string $selected
Definition Licenses.php:23
buildLine( $line)
Definition Licenses.php:66
makeHtml( $tagset, $depth=0)
Definition Licenses.php:128
string $msg
Definition Licenses.php:20
static getMessageFromParams( $params)
Definition Licenses.php:41
outputOption( $message, $value, $attribs=null, $depth=0)
Definition Licenses.php:160
getLicenses()
Accessor for $this->lines.
Definition Licenses.php:188
array $lines
Definition Licenses.php:21
getLines()
Accessor for $this->lines.
Definition Licenses.php:177
stackItem(&$list, $path, $item)
Definition Licenses.php:113
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition Licenses.php:195
trimStars( $str)
Definition Licenses.php:103
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:43
Service locator for MediaWiki core services.