Merge "(bug 37182) Removed hard coded parentheses in SpecialStatistics.php"
[mediawiki.git] / includes / Licenses.php
blobc498a5786221cc5004681ca4640bb9da22d78183
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 /**
32 * @var string
34 protected $msg;
36 /**
37 * @var array
39 protected $licenses = array();
41 /**
42 * @var string
44 protected $html;
45 /**#@-*/
47 /**
48 * Constructor
50 * @param $params array
52 public function __construct( $params ) {
53 parent::__construct( $params );
55 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
56 $this->selected = null;
58 $this->makeLicenses();
61 /**
62 * @private
64 protected function makeLicenses() {
65 $levels = array();
66 $lines = explode( "\n", $this->msg );
68 foreach ( $lines as $line ) {
69 if ( strpos( $line, '*' ) !== 0 ) {
70 continue;
71 } else {
72 list( $level, $line ) = $this->trimStars( $line );
74 if ( strpos( $line, '|' ) !== false ) {
75 $obj = new License( $line );
76 $this->stackItem( $this->licenses, $levels, $obj );
77 } else {
78 if ( $level < count( $levels ) ) {
79 $levels = array_slice( $levels, 0, $level );
81 if ( $level == count( $levels ) ) {
82 $levels[$level - 1] = $line;
83 } elseif ( $level > count( $levels ) ) {
84 $levels[] = $line;
91 /**
92 * @param $str
93 * @return array
95 protected function trimStars( $str ) {
96 $numStars = strspn( $str, '*' );
97 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
101 * @param $list
102 * @param $path
103 * @param $item
105 protected function stackItem( &$list, $path, $item ) {
106 $position =& $list;
107 if ( $path ) {
108 foreach( $path as $key ) {
109 $position =& $position[$key];
112 $position[] = $item;
116 * @param $tagset
117 * @param $depth int
119 protected function makeHtml( $tagset, $depth = 0 ) {
120 foreach ( $tagset as $key => $val )
121 if ( is_array( $val ) ) {
122 $this->html .= $this->outputOption(
123 $this->msg( $key ), '',
124 array(
125 'disabled' => 'disabled',
126 'style' => 'color: GrayText', // for MSIE
128 $depth
130 $this->makeHtml( $val, $depth + 1 );
131 } else {
132 $this->html .= $this->outputOption(
133 $this->msg( $val->text ), $val->template,
134 array( 'title' => '{{' . $val->template . '}}' ),
135 $depth
141 * @param $text
142 * @param $value
143 * @param $attribs null
144 * @param $depth int
145 * @return string
147 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
148 $attribs['value'] = $value;
149 if ( $value === $this->selected )
150 $attribs['selected'] = 'selected';
151 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
152 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
156 * @param $str string
157 * @return String
159 protected function msg( $str ) {
160 $msg = wfMessage( $str );
161 return $msg->exists() ? $msg->text() : $str;
164 /**#@-*/
167 * Accessor for $this->licenses
169 * @return array
171 public function getLicenses() {
172 return $this->licenses;
176 * Accessor for $this->html
178 * @param $value bool
180 * @return string
182 public function getInputHTML( $value ) {
183 $this->selected = $value;
185 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
186 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
187 $this->makeHtml( $this->getLicenses() );
189 $attribs = array(
190 'name' => $this->mName,
191 'id' => $this->mID
193 if ( !empty( $this->mParams['disabled'] ) ) {
194 $attibs['disabled'] = 'disabled';
197 return Html::rawElement( 'select', $attribs, $this->html );
202 * A License class for use on Special:Upload (represents a single type of license).
204 class License {
206 * @var string
208 var $template;
211 * @var string
213 var $text;
216 * Constructor
218 * @param $str String: license name??
220 function __construct( $str ) {
221 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
223 $this->template = strrev( $template );
224 $this->text = strrev( $text );