Non-word characters don't terminate tag names.
[mediawiki.git] / includes / Licenses.php
blob6c582aafee0198a773a4fcc3101a14479a7e236c
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'] ) ? wfMessage( 'licenses' )->inContentLanguage()->plain() : $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 $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 $val->text, $val->template,
134 array( 'title' => '{{' . $val->template . '}}' ),
135 $depth
142 * @param $message
143 * @param $value
144 * @param $attribs null
145 * @param $depth int
146 * @return string
148 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
149 $msgObj = $this->msg( $message );
150 $text = $msgObj->exists() ? $msgObj->text() : $message;
151 $attribs['value'] = $value;
152 if ( $value === $this->selected ) {
153 $attribs['selected'] = 'selected';
156 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
157 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
160 /**#@-*/
163 * Accessor for $this->licenses
165 * @return array
167 public function getLicenses() {
168 return $this->licenses;
172 * Accessor for $this->html
174 * @param $value bool
176 * @return string
178 public function getInputHTML( $value ) {
179 $this->selected = $value;
181 $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
182 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
183 $this->makeHtml( $this->getLicenses() );
185 $attribs = array(
186 'name' => $this->mName,
187 'id' => $this->mID
189 if ( !empty( $this->mParams['disabled'] ) ) {
190 $attibs['disabled'] = 'disabled';
193 return Html::rawElement( 'select', $attribs, $this->html );
198 * A License class for use on Special:Upload (represents a single type of license).
200 class License {
202 * @var string
204 var $template;
207 * @var string
209 var $text;
212 * Constructor
214 * @param string $str license name??
216 function __construct( $str ) {
217 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
219 $this->template = strrev( $template );
220 $this->text = strrev( $text );