Merge "Update 'OpenSearchDescription' links"
[lhc/web/wiklou.git] / includes / specials / formfields / Licenses.php
index 603c62f..a2f3128 100644 (file)
@@ -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/<lang>)
+               // 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;
        }
 }