X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22suivi_revisions%22%29%20.%20%22?a=blobdiff_plain;f=includes%2Fspecials%2Fformfields%2FLicenses.php;h=a2f312846274175f37fd2ab2c2b39aa6ff53dc2e;hb=339adab63a1e6016446743f2003d42fbfc1fbace;hp=603c62f17b82f8540a513b1cbd0d867e44aee9e8;hpb=cce016b4a4cb04611657e3d3299cba94f2e354d2;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/specials/formfields/Licenses.php b/includes/specials/formfields/Licenses.php index 603c62f17b..a2f3128462 100644 --- a/includes/specials/formfields/Licenses.php +++ b/includes/specials/formfields/Licenses.php @@ -31,10 +31,13 @@ class Licenses extends HTMLFormField { protected $msg; /** @var array */ - protected $licenses = []; + protected $lines = []; /** @var string */ protected $html; + + /** @var string|null */ + protected $selected; /**#@-*/ /** @@ -43,18 +46,50 @@ class Licenses extends HTMLFormField { public function __construct( $params ) { parent::__construct( $params ); - $this->msg = empty( $params['licenses'] ) - ? wfMessage( 'licenses' )->inContentLanguage()->plain() - : $params['licenses']; + $this->msg = static::getMessageFromParams( $params ); $this->selected = null; - $this->makeLicenses(); + $this->makeLines(); + } + + /** + * @param array $params + * @return string + */ + protected static function getMessageFromParams( $params ) { + global $wgContLang; + + if ( !empty( $params['licenses'] ) ) { + return $params['licenses']; + } + + // If the licenses page is in $wgForceUIMsgAsContentMsg (which is the case + // on Commons), translations will be in the database, in subpages of this + // message (e.g. MediaWiki:Licenses/) + // If there is no such translation, the result will be '-' (the empty default + // in the i18n files), so we'll need to force it to look up the actual licenses + // in the default site language (= get the translation from MediaWiki:Licenses) + // Also see https://phabricator.wikimedia.org/T3495 + $defaultMsg = wfMessage( 'licenses' )->inContentLanguage(); + if ( !$defaultMsg->exists() || $defaultMsg->plain() === '-' ) { + $defaultMsg = wfMessage( 'licenses' )->inLanguage( $wgContLang ); + } + + return $defaultMsg->plain(); + } + + /** + * @param string $line + * @return License + */ + protected function buildLine( $line ) { + return new License( $line ); } /** * @private */ - protected function makeLicenses() { + protected function makeLines() { $levels = []; $lines = explode( "\n", $this->msg ); @@ -65,8 +100,8 @@ class Licenses extends HTMLFormField { list( $level, $line ) = $this->trimStars( $line ); if ( strpos( $line, '|' ) !== false ) { - $obj = new License( $line ); - $this->stackItem( $this->licenses, $levels, $obj ); + $obj = $this->buildLine( $line ); + $this->stackItem( $this->lines, $levels, $obj ); } else { if ( $level < count( $levels ) ) { $levels = array_slice( $levels, 0, $level ); @@ -108,11 +143,14 @@ class Licenses extends HTMLFormField { /** * @param array $tagset * @param int $depth + * @return string */ protected function makeHtml( $tagset, $depth = 0 ) { + $html = ''; + foreach ( $tagset as $key => $val ) { if ( is_array( $val ) ) { - $this->html .= $this->outputOption( + $html .= $this->outputOption( $key, '', [ 'disabled' => 'disabled', @@ -120,15 +158,17 @@ class Licenses extends HTMLFormField { ], $depth ); - $this->makeHtml( $val, $depth + 1 ); + $html .= $this->makeHtml( $val, $depth + 1 ); } else { - $this->html .= $this->outputOption( + $html .= $this->outputOption( $val->text, $val->template, [ 'title' => '{{' . $val->template . '}}' ], $depth ); } } + + return $html; } /** @@ -153,36 +193,50 @@ class Licenses extends HTMLFormField { /**#@-*/ /** - * Accessor for $this->licenses + * Accessor for $this->lines * * @return array */ - public function getLicenses() { - return $this->licenses; + public function getLines() { + return $this->lines; } /** - * Accessor for $this->html + * Accessor for $this->lines * - * @param bool $value + * @return array * - * @return string + * @deprecated since 1.31 Use getLines() instead + */ + public function getLicenses() { + return $this->getLines(); + } + + /** + * {@inheritdoc} */ public function getInputHTML( $value ) { $this->selected = $value; - $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '', - (bool)$this->selected ? null : [ 'selected' => 'selected' ] ); - $this->makeHtml( $this->getLicenses() ); + // add a default "no license selected" option + $default = $this->buildLine( '|nolicense' ); + array_unshift( $this->lines, $default ); + + $html = $this->makeHtml( $this->getLines() ); $attribs = [ 'name' => $this->mName, 'id' => $this->mID ]; if ( !empty( $this->mParams['disabled'] ) ) { - $attibs['disabled'] = 'disabled'; + $attribs['disabled'] = 'disabled'; } - return Html::rawElement( 'select', $attribs, $this->html ); + $html = Html::rawElement( 'select', $attribs, $html ); + + // remove default "no license selected" from lines again + array_shift( $this->lines ); + + return $html; } }