API: (bug 19004) Add support for tags. Patch by Matthew Britton
[lhc/web/wiklou.git] / includes / Licenses.php
1 <?php
2 /**
3 * A License class for use on Special:Upload
4 *
5 * @ingroup SpecialPage
6 *
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 */
11
12 class Licenses extends HTMLFormField {
13 /**
14 * @var string
15 */
16 protected $msg;
17
18 /**
19 * @var array
20 */
21 protected $licenses = array();
22
23 /**
24 * @var string
25 */
26 protected $html;
27 /**#@-*/
28
29 /**
30 * Constructor
31 */
32 public function __construct( $params ) {
33 parent::__construct( $params );
34
35 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
36 $this->selected = null;
37
38 $this->makeLicenses();
39 }
40
41 /**#@+
42 * @private
43 */
44 protected function makeLicenses() {
45 $levels = array();
46 $lines = explode( "\n", $this->msg );
47
48 foreach ( $lines as $line ) {
49 if ( strpos( $line, '*' ) !== 0 )
50 continue;
51 else {
52 list( $level, $line ) = $this->trimStars( $line );
53
54 if ( strpos( $line, '|' ) !== false ) {
55 $obj = new License( $line );
56 $this->stackItem( $this->licenses, $levels, $obj );
57 } else {
58 if ( $level < count( $levels ) ) {
59 $levels = array_slice( $levels, 0, $level );
60 }
61 if ( $level == count( $levels ) ) {
62 $levels[$level - 1] = $line;
63 } else if ( $level > count( $levels ) ) {
64 $levels[] = $line;
65 }
66 }
67 }
68 }
69 }
70
71 protected static function trimStars( $str ) {
72 $i = $count = 0;
73
74 $length = strlen( $str );
75 for ( $i = 0; $i < $length; $i++ ) {
76 if ( $str[$i] != '*' )
77 return array( $i, ltrim( $str, '* ' ) );
78 }
79 }
80
81 protected function stackItem( &$list, $path, $item ) {
82 $position =& $list;
83 if ( $path )
84 foreach( $path as $key )
85 $position =& $position[$key];
86 $position[] = $item;
87 }
88
89 protected function makeHtml( $tagset, $depth = 0 ) {
90 foreach ( $tagset as $key => $val )
91 if ( is_array( $val ) ) {
92 $this->html .= $this->outputOption(
93 $this->msg( $key ), '',
94 array(
95 'disabled' => 'disabled',
96 'style' => 'color: GrayText', // for MSIE
97 ),
98 $depth
99 );
100 $this->makeHtml( $val, $depth + 1 );
101 } else {
102 $this->html .= $this->outputOption(
103 $this->msg( $val->text ), $val->template,
104 array( 'title' => '{{' . $val->template . '}}' ),
105 $depth
106 );
107 }
108 }
109
110 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
111 $attribs['value'] = $value;
112 if ( $value === $this->selected )
113 $attribs['selected'] = 'selected';
114 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
115 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
116 }
117
118 protected function msg( $str ) {
119 $out = wfMsg( $str );
120 return wfEmptyMsg( $str, $out ) ? $str : $out;
121 }
122
123 /**#@-*/
124
125 /**
126 * Accessor for $this->licenses
127 *
128 * @return array
129 */
130 public function getLicenses() { return $this->licenses; }
131
132 /**
133 * Accessor for $this->html
134 *
135 * @return string
136 */
137 public function getInputHTML( $value ) {
138 $this->selected = $value;
139
140 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
141 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
142 $this->makeHtml( $this->getLicenses() );
143
144 $attribs = array(
145 'name' => $this->mName,
146 'id' => $this->mID
147 );
148 if ( !empty( $this->mParams['disabled'] ) )
149 $attibs['disabled'] = 'disabled';
150
151 return Html::rawElement( 'select', $attribs, $this->html );
152 }
153 }
154
155 /**
156 * A License class for use on Special:Upload (represents a single type of license).
157 */
158 class License {
159 /**
160 * @var string
161 */
162 var $template;
163
164 /**
165 * @var string
166 */
167 var $text;
168
169 /**
170 * Constructor
171 *
172 * @param $str String: license name??
173 */
174 function License( $str ) {
175 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
176
177 $this->template = strrev( $template );
178 $this->text = strrev( $text );
179 }
180 }