Don't look for pipes in the root node.
[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 function trimStars( $str ) {
72 $numStars = strspn( $str, '*' );
73 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
74 }
75
76 protected function stackItem( &$list, $path, $item ) {
77 $position =& $list;
78 if ( $path )
79 foreach( $path as $key )
80 $position =& $position[$key];
81 $position[] = $item;
82 }
83
84 protected function makeHtml( $tagset, $depth = 0 ) {
85 foreach ( $tagset as $key => $val )
86 if ( is_array( $val ) ) {
87 $this->html .= $this->outputOption(
88 $this->msg( $key ), '',
89 array(
90 'disabled' => 'disabled',
91 'style' => 'color: GrayText', // for MSIE
92 ),
93 $depth
94 );
95 $this->makeHtml( $val, $depth + 1 );
96 } else {
97 $this->html .= $this->outputOption(
98 $this->msg( $val->text ), $val->template,
99 array( 'title' => '{{' . $val->template . '}}' ),
100 $depth
101 );
102 }
103 }
104
105 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
106 $attribs['value'] = $value;
107 if ( $value === $this->selected )
108 $attribs['selected'] = 'selected';
109 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
110 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
111 }
112
113 protected function msg( $str ) {
114 $msg = wfMessage( $str );
115 return $msg->exists() ? $msg->text() : $str;
116 }
117
118 /**#@-*/
119
120 /**
121 * Accessor for $this->licenses
122 *
123 * @return array
124 */
125 public function getLicenses() { return $this->licenses; }
126
127 /**
128 * Accessor for $this->html
129 *
130 * @return string
131 */
132 public function getInputHTML( $value ) {
133 $this->selected = $value;
134
135 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
136 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
137 $this->makeHtml( $this->getLicenses() );
138
139 $attribs = array(
140 'name' => $this->mName,
141 'id' => $this->mID
142 );
143 if ( !empty( $this->mParams['disabled'] ) )
144 $attibs['disabled'] = 'disabled';
145
146 return Html::rawElement( 'select', $attribs, $this->html );
147 }
148 }
149
150 /**
151 * A License class for use on Special:Upload (represents a single type of license).
152 */
153 class License {
154 /**
155 * @var string
156 */
157 var $template;
158
159 /**
160 * @var string
161 */
162 var $text;
163
164 /**
165 * Constructor
166 *
167 * @param $str String: license name??
168 */
169 function __construct( $str ) {
170 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
171
172 $this->template = strrev( $template );
173 $this->text = strrev( $text );
174 }
175 }