Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[lhc/web/wiklou.git] / includes / specials / formfields / Licenses.php
index f499cc1..931cd24 100644 (file)
@@ -21,7 +21,6 @@
  * @ingroup SpecialPage
  * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  */
 
 /**
@@ -32,10 +31,13 @@ class Licenses extends HTMLFormField {
        protected $msg;
 
        /** @var array */
-       protected $licenses = [];
+       protected $lines = [];
 
        /** @var string */
        protected $html;
+
+       /** @var string|null */
+       protected $selected;
        /**#@-*/
 
        /**
@@ -44,18 +46,34 @@ class Licenses extends HTMLFormField {
        public function __construct( $params ) {
                parent::__construct( $params );
 
-               $this->msg = empty( $params['licenses'] )
+               $this->msg = static::getMessageFromParams( $params );
+               $this->selected = null;
+
+               $this->makeLines();
+       }
+
+       /**
+        * @param array $params
+        * @return string
+        */
+       protected static function getMessageFromParams( $params ) {
+               return empty( $params['licenses'] )
                        ? wfMessage( 'licenses' )->inContentLanguage()->plain()
                        : $params['licenses'];
-               $this->selected = null;
+       }
 
-               $this->makeLicenses();
+       /**
+        * @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 );
 
@@ -66,8 +84,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 );
@@ -109,11 +127,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',
@@ -121,15 +142,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;
        }
 
        /**
@@ -154,36 +177,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;
        }
 }