Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / Licenses.php
blobda1a8da6b02d490cd57661797ed2707c6a10ed7c
1 <?php
2 /**
3 * License selector for use on Special:Upload.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
24 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
27 /**
28 * A License class for use on Special:Upload
30 class Licenses extends HTMLFormField {
31 /** @var string */
32 protected $msg;
34 /** @var array */
35 protected $licenses = [];
37 /** @var string */
38 protected $html;
39 /**#@-*/
41 /**
42 * @param array $params
44 public function __construct( $params ) {
45 parent::__construct( $params );
47 $this->msg = empty( $params['licenses'] )
48 ? wfMessage( 'licenses' )->inContentLanguage()->plain()
49 : $params['licenses'];
50 $this->selected = null;
52 $this->makeLicenses();
55 /**
56 * @private
58 protected function makeLicenses() {
59 $levels = [];
60 $lines = explode( "\n", $this->msg );
62 foreach ( $lines as $line ) {
63 if ( strpos( $line, '*' ) !== 0 ) {
64 continue;
65 } else {
66 list( $level, $line ) = $this->trimStars( $line );
68 if ( strpos( $line, '|' ) !== false ) {
69 $obj = new License( $line );
70 $this->stackItem( $this->licenses, $levels, $obj );
71 } else {
72 if ( $level < count( $levels ) ) {
73 $levels = array_slice( $levels, 0, $level );
75 if ( $level == count( $levels ) ) {
76 $levels[$level - 1] = $line;
77 } elseif ( $level > count( $levels ) ) {
78 $levels[] = $line;
85 /**
86 * @param string $str
87 * @return array
89 protected function trimStars( $str ) {
90 $numStars = strspn( $str, '*' );
91 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
94 /**
95 * @param array $list
96 * @param array $path
97 * @param mixed $item
99 protected function stackItem( &$list, $path, $item ) {
100 $position =& $list;
101 if ( $path ) {
102 foreach ( $path as $key ) {
103 $position =& $position[$key];
106 $position[] = $item;
110 * @param array $tagset
111 * @param int $depth
113 protected function makeHtml( $tagset, $depth = 0 ) {
114 foreach ( $tagset as $key => $val ) {
115 if ( is_array( $val ) ) {
116 $this->html .= $this->outputOption(
117 $key, '',
119 'disabled' => 'disabled',
120 'style' => 'color: GrayText', // for MSIE
122 $depth
124 $this->makeHtml( $val, $depth + 1 );
125 } else {
126 $this->html .= $this->outputOption(
127 $val->text, $val->template,
128 [ 'title' => '{{' . $val->template . '}}' ],
129 $depth
136 * @param string $message
137 * @param string $value
138 * @param null|array $attribs
139 * @param int $depth
140 * @return string
142 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
143 $msgObj = $this->msg( $message );
144 $text = $msgObj->exists() ? $msgObj->text() : $message;
145 $attribs['value'] = $value;
146 if ( $value === $this->selected ) {
147 $attribs['selected'] = 'selected';
150 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
151 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
154 /**#@-*/
157 * Accessor for $this->licenses
159 * @return array
161 public function getLicenses() {
162 return $this->licenses;
166 * Accessor for $this->html
168 * @param bool $value
170 * @return string
172 public function getInputHTML( $value ) {
173 $this->selected = $value;
175 $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
176 (bool)$this->selected ? null : [ 'selected' => 'selected' ] );
177 $this->makeHtml( $this->getLicenses() );
179 $attribs = [
180 'name' => $this->mName,
181 'id' => $this->mID
183 if ( !empty( $this->mParams['disabled'] ) ) {
184 $attibs['disabled'] = 'disabled';
187 return Html::rawElement( 'select', $attribs, $this->html );
192 * A License class for use on Special:Upload (represents a single type of license).
194 class License {
195 /** @var string */
196 public $template;
198 /** @var string */
199 public $text;
202 * @param string $str License name??
204 function __construct( $str ) {
205 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
207 $this->template = strrev( $template );
208 $this->text = strrev( $text );