1 | <?php |
||
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 ) { |
|
183 | |||
184 | /** |
||
185 | * @param string $messageName |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 1 | protected function getTextFromMessageName( $messageName ) { |
|
194 | |||
195 | /** |
||
196 | * @param string $linkTarget |
||
197 | * |
||
198 | * @return string |
||
199 | * @throws \MWException |
||
200 | */ |
||
201 | 1 | protected function getHrefForTarget( $linkTarget ) { |
|
211 | |||
212 | /** |
||
213 | * @param string $linkTarget |
||
214 | * |
||
215 | * @return string |
||
216 | * @throws \MWException |
||
217 | */ |
||
218 | 1 | protected function getHrefForWikiPage( $linkTarget ) { |
|
227 | |||
228 | /** |
||
229 | * @param string[] $subItemData |
||
230 | * |
||
231 | * @return null|string[] |
||
232 | */ |
||
233 | 1 | protected function createChildAndParseNextLine( $subItemData ) { |
|
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | */ |
||
245 | 1 | protected function buildHtml() { |
|
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | 1 | protected function buildSubmenuHtml() { |
|
272 | |||
273 | } |
||
274 |