| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
om> |
| 23 |
ason |
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\Html\Html; |
| 27 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class Licenses extends HTMLFormField { |
| 34 |
protected string $msg; |
| 35 |
protected array $lines = []; |
| 36 |
protected string $html; |
| 37 |
protected ?string $selected; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected static function getMessageFromParams( $params ) { |
| 56 |
if ( !empty( $params['licenses'] ) ) { |
| 57 |
return $params['licenses']; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
protected function buildLine( $line ) { |
| 81 |
return new License( $line ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected function trimStars( $str ) { |
| 118 |
$numStars = strspn( $str, '*' ); |
| 119 |
return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ]; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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( "\u{00A0}", $depth * 2 ) . $text; |
| 183 |
return str_repeat( "\t", $depth ) . Html::element( 'option', $attribs, $val ) . "\n"; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getLines() { |
| 192 |
return $this->lines; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function getLicenses() { |
| 203 |
return $this->getLines(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public function getInputHTML( $value ) { |
| 210 |
$this->selected = $value; |
| 211 |
|
| 212 |
|
| 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 |
|
| 229 |
array_shift( $this->lines ); |
| 230 |
|
| 231 |
return $html; |
| 232 |
} |
| 233 |
} |