MediaWiki master
Licenses.php
Go to the documentation of this file.
1<?php
13
18
22class Licenses extends HTMLFormField {
23 protected string $msg;
24 protected array $lines = [];
25 protected string $html;
26 protected ?string $selected;
27
31 public function __construct( $params ) {
32 parent::__construct( $params );
33
34 $this->msg = static::getMessageFromParams( $params );
35 $this->selected = null;
36
37 $this->makeLines();
38 }
39
44 protected static function getMessageFromParams( $params ) {
45 if ( !empty( $params['licenses'] ) ) {
46 return $params['licenses'];
47 }
48
49 // If the licenses page is in $wgForceUIMsgAsContentMsg (which is the case
50 // on Commons), translations will be in the database, in subpages of this
51 // message (e.g. MediaWiki:Licenses/<lang>)
52 // If there is no such translation, the result will be '-' (the empty default
53 // in the i18n files), so we'll need to force it to look up the actual licenses
54 // in the default site language (= get the translation from MediaWiki:Licenses)
55 // Also see https://phabricator.wikimedia.org/T3495
56 $defaultMsg = wfMessage( 'licenses' )->inContentLanguage();
57 if ( $defaultMsg->isDisabled() ) {
58 $defaultMsg = wfMessage( 'licenses' )->inLanguage(
59 MediaWikiServices::getInstance()->getContentLanguage() );
60 }
61
62 return $defaultMsg->plain();
63 }
64
69 protected function buildLine( $line ) {
70 return new License( $line );
71 }
72
76 protected function makeLines() {
77 $levels = [];
78 $lines = explode( "\n", $this->msg );
79
80 foreach ( $lines as $line ) {
81 if ( !str_starts_with( $line, '*' ) ) {
82 continue;
83 }
84 [ $level, $line ] = $this->trimStars( $line );
85
86 if ( str_contains( $line, '|' ) ) {
87 $obj = $this->buildLine( $line );
88 $this->stackItem( $this->lines, $levels, $obj );
89 } else {
90 if ( $level < count( $levels ) ) {
91 $levels = array_slice( $levels, 0, $level );
92 }
93 if ( $level == count( $levels ) ) {
94 $levels[$level - 1] = $line;
95 } elseif ( $level > count( $levels ) ) {
96 $levels[] = $line;
97 }
98 }
99 }
100 }
101
106 protected function trimStars( $str ) {
107 $numStars = strspn( $str, '*' );
108 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
109 }
110
116 protected function stackItem( &$list, $path, $item ) {
117 $position =& $list;
118 if ( $path ) {
119 foreach ( $path as $key ) {
120 $position =& $position[$key];
121 }
122 }
123 $position[] = $item;
124 }
125
131 protected function makeHtml( $tagset, $depth = 0 ) {
132 $html = '';
133
134 foreach ( $tagset as $key => $val ) {
135 if ( is_array( $val ) ) {
136 $html .= $this->outputOption(
137 $key, '',
138 [
139 'disabled' => 'disabled'
140 ],
141 $depth
142 );
143 $html .= $this->makeHtml( $val, $depth + 1 );
144 } else {
145 $html .= $this->outputOption(
146 $val->text, $val->template,
147 [ 'title' => '{{' . $val->template . '}}' ],
148 $depth
149 );
150 }
151 }
152
153 return $html;
154 }
155
163 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
164 $msgObj = $this->msg( $message );
165 $text = $msgObj->exists() ? $msgObj->text() : $message;
166 $attribs['value'] = $value;
167 if ( $value === $this->selected && !isset( $attribs['disabled'] ) ) {
168 $attribs['selected'] = 'selected';
169 }
170
171 $val = str_repeat( /* &nbsp */ "\u{00A0}", $depth * 2 ) . $text;
172 return str_repeat( "\t", $depth ) . Html::element( 'option', $attribs, $val ) . "\n";
173 }
174
180 public function getLines() {
181 return $this->lines;
182 }
183
191 public function getLicenses() {
192 return $this->getLines();
193 }
194
198 public function getInputHTML( $value ) {
199 $this->selected = $value;
200
201 // add a default "no license selected" option
202 $default = $this->buildLine( '|nolicense' );
203 array_unshift( $this->lines, $default );
204
205 $html = $this->makeHtml( $this->getLines() );
206
207 $attribs = [
208 'name' => $this->mName,
209 'id' => $this->mID
210 ];
211 if ( !empty( $this->mParams['disabled'] ) ) {
212 $attribs['disabled'] = 'disabled';
213 }
214
215 $html = Html::rawElement( 'select', $attribs, $html );
216
217 // remove default "no license selected" from lines again
218 array_shift( $this->lines );
219
220 return $html;
221 }
222}
223
225class_alias( Licenses::class, 'Licenses' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
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:44
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
A License class for use on Special:Upload.
Definition Licenses.php:22
getLicenses()
Accessor for $this->lines.
Definition Licenses.php:191
outputOption( $message, $value, $attribs=null, $depth=0)
Definition Licenses.php:163
getLines()
Accessor for $this->lines.
Definition Licenses.php:180
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition Licenses.php:198
A License class for use on Special:Upload (represents a single type of license).
Definition License.php:18
element(SerializerNode $parent, SerializerNode $node, $contents)