1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File holding the MenuFromLines class |
4
|
|
|
* |
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 - 2015, Stephan Gambke |
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
9
|
|
|
* |
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
13
|
|
|
* later version. |
14
|
|
|
* |
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18
|
|
|
* details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU General Public License along |
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
* @file |
24
|
|
|
* @ingroup Skins |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Menu; |
28
|
|
|
|
29
|
|
|
use Title; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class MenuFromLines |
33
|
|
|
* |
34
|
|
|
* @author Stephan Gambke |
35
|
|
|
* @since 1.0 |
36
|
|
|
* @ingroup Skins |
37
|
|
|
*/ |
38
|
|
|
class MenuFromLines extends Menu { |
39
|
|
|
|
40
|
|
|
private $lines = null; |
41
|
|
|
private $inContentLanguage = false; |
42
|
|
|
private $menuItemData = null; |
43
|
|
|
|
44
|
|
|
private $needsParse = true; |
45
|
|
|
|
46
|
|
|
/** @var Menu[] */ |
47
|
|
|
private $children = array(); |
48
|
|
|
private $html = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string[] $lines |
52
|
|
|
* @param bool $inContentLanguage |
53
|
|
|
* @param null|string[] $itemData |
54
|
|
|
*/ |
55
|
2 |
|
public function __construct( &$lines, $inContentLanguage = false, $itemData = null ) { |
56
|
|
|
|
57
|
2 |
|
$this->lines = &$lines; |
58
|
2 |
|
$this->inContentLanguage = $inContentLanguage; |
59
|
|
|
|
60
|
2 |
|
if ( $itemData !== null ) { |
61
|
1 |
|
$this->menuItemData = $itemData; |
62
|
1 |
|
} else { |
63
|
1 |
|
$this->menuItemData = array( |
64
|
1 |
|
'text' => '', |
65
|
1 |
|
'href' => '#', |
66
|
|
|
'depth' => 0 |
67
|
1 |
|
); |
68
|
|
|
} |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function getHtml() { |
75
|
|
|
|
76
|
1 |
|
if ( $this->html === null ) { |
77
|
|
|
|
78
|
1 |
|
$this->parseLines(); |
79
|
1 |
|
$this->html = $this->buildHtml(); |
80
|
|
|
|
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
return $this->html; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string[]|null |
88
|
|
|
*/ |
89
|
1 |
|
public function parseLines() { |
90
|
|
|
|
91
|
1 |
|
if ( !$this->needsParse ) { |
92
|
1 |
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$this->needsParse = false; |
96
|
|
|
|
97
|
1 |
|
$line = $this->getNextLine(); |
98
|
1 |
|
$subItemData = $this->parseOneLine( $line ); |
99
|
|
|
|
100
|
1 |
|
while ( $subItemData !== null && $subItemData[ 'depth' ] > $this->menuItemData[ 'depth' ] ) { |
101
|
|
|
|
102
|
1 |
|
$subItemData = $this->createChildAndParseNextLine( $subItemData ); |
103
|
|
|
|
104
|
1 |
|
} |
105
|
|
|
|
106
|
1 |
|
return $subItemData; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
1 |
|
protected function getNextLine() { |
113
|
1 |
|
$line = ''; |
114
|
|
|
|
115
|
1 |
|
while ( count( $this->lines ) > 0 && empty( $line ) ) { |
116
|
1 |
|
$line = trim( array_shift( $this->lines ) ); |
117
|
1 |
|
}; |
118
|
1 |
|
return $line; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Will return an array of the form |
123
|
|
|
* array( |
124
|
|
|
* 'text' => $text, // link text |
125
|
|
|
* 'href' => $href, // parsed link target |
126
|
|
|
* 'depth' => $depth |
127
|
|
|
* ); |
128
|
|
|
* |
129
|
|
|
* @param string $rawLine |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
1 |
|
protected function parseOneLine( $rawLine ) { |
134
|
|
|
|
135
|
1 |
|
if ( empty( $rawLine ) ) { |
136
|
1 |
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
list( $depth, $linkDescription ) = $this->extractDepthAndLine( $rawLine ); |
140
|
1 |
|
list( $href, $text ) = $this->extractHrefAndLinkText( $linkDescription ); |
141
|
|
|
|
142
|
|
|
return array( |
143
|
1 |
|
'text' => $text, |
144
|
1 |
|
'href' => $href, |
145
|
|
|
'depth' => $depth |
146
|
1 |
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $rawLine |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
1 |
|
protected function extractDepthAndLine( $rawLine ) { |
155
|
|
|
|
156
|
1 |
|
$matches = array(); |
157
|
1 |
|
preg_match( '/(\**)(.*)/', ltrim( $rawLine ), $matches ); |
158
|
|
|
|
159
|
1 |
|
$depth = strlen( $matches[ 1 ] ); |
160
|
1 |
|
$line = $matches[ 2 ]; |
161
|
|
|
|
162
|
1 |
|
return array( $depth, $line ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $linkDescription |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
1 |
|
protected function extractHrefAndLinkText( $linkDescription ) { |
171
|
|
|
|
172
|
1 |
|
$linkAttributes = array_map( 'trim', explode( '|', $linkDescription, 2 ) ); |
173
|
|
|
|
174
|
1 |
|
$linkTarget = trim( trim( $linkAttributes[ 0 ], '[]' ) ); |
175
|
1 |
|
$linkTarget = $this->getTextFromMessageName( $linkTarget ); |
176
|
1 |
|
$href = $this->getHrefForTarget( $linkTarget ); |
177
|
|
|
|
178
|
1 |
|
$linkDescription = count( $linkAttributes ) > 1 ? $linkAttributes[ 1 ] : ''; |
179
|
1 |
|
$text = $linkDescription === '' ? $linkTarget : $this->getTextFromMessageName( $linkDescription ); |
180
|
|
|
|
181
|
1 |
|
return array( $href, $text ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $messageName |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
1 |
|
protected function getTextFromMessageName( $messageName ) { |
190
|
1 |
|
$msgObj = $this->inContentLanguage ? wfMessage( $messageName )->inContentLanguage() : wfMessage( $messageName ); |
191
|
1 |
|
$messageText = ( $msgObj->isDisabled() ? $messageName : trim( $msgObj->inContentLanguage()->text() ) ); |
192
|
1 |
|
return $messageText; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $linkTarget |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
* @throws \MWException |
200
|
|
|
*/ |
201
|
1 |
|
protected function getHrefForTarget( $linkTarget ) { |
202
|
|
|
|
203
|
1 |
|
if ( empty( $linkTarget ) ) { |
204
|
1 |
|
return '#'; |
205
|
1 |
|
} elseif ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $linkTarget ) || $linkTarget[ 0 ] === '#' ) { |
206
|
1 |
|
return $linkTarget; |
207
|
|
|
} else { |
208
|
1 |
|
return $this->getHrefForWikiPage( $linkTarget ); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $linkTarget |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
* @throws \MWException |
217
|
|
|
*/ |
218
|
1 |
|
protected function getHrefForWikiPage( $linkTarget ) { |
219
|
1 |
|
$title = Title::newFromText( $linkTarget ); |
220
|
|
|
|
221
|
1 |
|
if ( $title instanceof Title ) { |
222
|
1 |
|
return $title->fixSpecialName()->getLocalURL(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return '#'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string[] $subItemData |
230
|
|
|
* |
231
|
|
|
* @return null|string[] |
232
|
|
|
*/ |
233
|
1 |
|
protected function createChildAndParseNextLine( $subItemData ) { |
234
|
1 |
|
$child = new self( $this->lines, $this->inContentLanguage, $subItemData ); |
235
|
1 |
|
$child->setMenuItemFormatter( $this->getMenuItemFormatter() ); |
236
|
1 |
|
$child->setItemListFormatter( $this->getItemListFormatter() ); |
237
|
1 |
|
$subItemData = $child->parseLines(); |
238
|
1 |
|
$this->children[ ] = $child; |
239
|
1 |
|
return $subItemData; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
1 |
|
protected function buildHtml() { |
246
|
|
|
|
247
|
1 |
|
$submenuHtml = $this->buildSubmenuHtml(); |
248
|
|
|
|
249
|
1 |
|
if ( $this->menuItemData[ 'text' ] !== '' ) { |
250
|
1 |
|
return $this->getHtmlForMenuItem( $this->menuItemData[ 'href' ], $this->menuItemData[ 'text' ], $this->menuItemData[ 'depth' ], $submenuHtml ); |
251
|
|
|
} else { |
252
|
1 |
|
return $submenuHtml; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
1 |
|
protected function buildSubmenuHtml() { |
260
|
|
|
|
261
|
1 |
|
if ( empty( $this->children ) ) { |
262
|
1 |
|
return ''; |
263
|
|
|
} |
264
|
|
|
|
265
|
1 |
|
$itemList = ''; |
266
|
1 |
|
foreach ( $this->children as $child ) { |
267
|
1 |
|
$itemList .= $child->getHtml(); |
268
|
1 |
|
} |
269
|
|
|
|
270
|
1 |
|
return $this->getHtmlForMenuItemList( $itemList, $this->menuItemData[ 'depth' ] ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|