MediaWiki REL1_31
BlockLevelPass.php
Go to the documentation of this file.
1<?php
2
26 private $DTopen = false;
27 private $inPre = false;
28 private $lastSection = '';
29 private $lineStart;
30 private $text;
31
32 # State constants for the definition list colon extraction
34 const COLON_STATE_TAG = 1;
41 const COLON_STATE_LC = 8;
42
50 public static function doBlockLevels( $text, $lineStart ) {
51 $pass = new self( $text, $lineStart );
52 return $pass->execute();
53 }
54
58 private function __construct( $text, $lineStart ) {
59 $this->text = $text;
60 $this->lineStart = $lineStart;
61 }
62
68 private function closeParagraph() {
69 $result = '';
70 if ( $this->lastSection !== '' ) {
71 $result = '</' . $this->lastSection . ">\n";
72 }
73 $this->inPre = false;
74 $this->lastSection = '';
75 return $result;
76 }
77
87 private function getCommon( $st1, $st2 ) {
88 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
89
90 for ( $i = 0; $i < $shorter; ++$i ) {
91 if ( $st1[$i] !== $st2[$i] ) {
92 break;
93 }
94 }
95 return $i;
96 }
97
105 private function openList( $char ) {
106 $result = $this->closeParagraph();
107
108 if ( '*' === $char ) {
109 $result .= "<ul><li>";
110 } elseif ( '#' === $char ) {
111 $result .= "<ol><li>";
112 } elseif ( ':' === $char ) {
113 $result .= "<dl><dd>";
114 } elseif ( ';' === $char ) {
115 $result .= "<dl><dt>";
116 $this->DTopen = true;
117 } else {
118 $result = '<!-- ERR 1 -->';
119 }
120
121 return $result;
122 }
123
130 private function nextItem( $char ) {
131 if ( '*' === $char || '#' === $char ) {
132 return "</li>\n<li>";
133 } elseif ( ':' === $char || ';' === $char ) {
134 $close = "</dd>\n";
135 if ( $this->DTopen ) {
136 $close = "</dt>\n";
137 }
138 if ( ';' === $char ) {
139 $this->DTopen = true;
140 return $close . '<dt>';
141 } else {
142 $this->DTopen = false;
143 return $close . '<dd>';
144 }
145 }
146 return '<!-- ERR 2 -->';
147 }
148
155 private function closeList( $char ) {
156 if ( '*' === $char ) {
157 $text = "</li></ul>";
158 } elseif ( '#' === $char ) {
159 $text = "</li></ol>";
160 } elseif ( ':' === $char ) {
161 if ( $this->DTopen ) {
162 $this->DTopen = false;
163 $text = "</dt></dl>";
164 } else {
165 $text = "</dd></dl>";
166 }
167 } else {
168 return '<!-- ERR 3 -->';
169 }
170 return $text;
171 }
172
177 private function execute() {
179 # Parsing through the text line by line. The main thing
180 # happening here is handling of block-level elements p, pre,
181 # and making lists from lines starting with * # : etc.
182 $textLines = StringUtils::explode( "\n", $text );
183
184 $lastPrefix = $output = '';
185 $this->DTopen = $inBlockElem = false;
186 $prefixLength = 0;
187 $pendingPTag = false;
188 $inBlockquote = false;
189
190 foreach ( $textLines as $inputLine ) {
191 # Fix up $lineStart
192 if ( !$this->lineStart ) {
193 $output .= $inputLine;
194 $this->lineStart = true;
195 continue;
196 }
197 # * = ul
198 # # = ol
199 # ; = dt
200 # : = dd
201
202 $lastPrefixLength = strlen( $lastPrefix );
203 $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
204 $preOpenMatch = preg_match( '/<pre/i', $inputLine );
205 # If not in a <pre> element, scan for and figure out what prefixes are there.
206 if ( !$this->inPre ) {
207 # Multiple prefixes may abut each other for nested lists.
208 $prefixLength = strspn( $inputLine, '*#:;' );
209 $prefix = substr( $inputLine, 0, $prefixLength );
210
211 # eh?
212 # ; and : are both from definition-lists, so they're equivalent
213 # for the purposes of determining whether or not we need to open/close
214 # elements.
215 $prefix2 = str_replace( ';', ':', $prefix );
216 $t = substr( $inputLine, $prefixLength );
217 $this->inPre = (bool)$preOpenMatch;
218 } else {
219 # Don't interpret any other prefixes in preformatted text
220 $prefixLength = 0;
221 $prefix = $prefix2 = '';
222 $t = $inputLine;
223 }
224
225 # List generation
226 if ( $prefixLength && $lastPrefix === $prefix2 ) {
227 # Same as the last item, so no need to deal with nesting or opening stuff
228 $output .= $this->nextItem( substr( $prefix, -1 ) );
229 $pendingPTag = false;
230
231 if ( substr( $prefix, -1 ) === ';' ) {
232 # The one nasty exception: definition lists work like this:
233 # ; title : definition text
234 # So we check for : in the remainder text to split up the
235 # title and definition, without b0rking links.
236 $term = $t2 = '';
237 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
238 $t = $t2;
239 // Trim whitespace in list items
240 $output .= trim( $term ) . $this->nextItem( ':' );
241 }
242 }
243 } elseif ( $prefixLength || $lastPrefixLength ) {
244 # We need to open or close prefixes, or both.
245
246 # Either open or close a level...
247 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
248 $pendingPTag = false;
249
250 # Close all the prefixes which aren't shared.
251 while ( $commonPrefixLength < $lastPrefixLength ) {
252 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
253 --$lastPrefixLength;
254 }
255
256 # Continue the current prefix if appropriate.
257 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
258 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
259 }
260
261 # Close an open <dt> if we have a <dd> (":") starting on this line
262 if ( $this->DTopen && $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
263 $output .= $this->nextItem( ':' );
264 }
265
266 # Open prefixes where appropriate.
267 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
268 $output .= "\n";
269 }
270 while ( $prefixLength > $commonPrefixLength ) {
271 $char = $prefix[$commonPrefixLength];
272 $output .= $this->openList( $char );
273
274 if ( ';' === $char ) {
275 # @todo FIXME: This is dupe of code above
276 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
277 $t = $t2;
278 // Trim whitespace in list items
279 $output .= trim( $term ) . $this->nextItem( ':' );
280 }
281 }
282 ++$commonPrefixLength;
283 }
284 if ( !$prefixLength && $lastPrefix ) {
285 $output .= "\n";
286 }
287 $lastPrefix = $prefix2;
288 }
289
290 # If we have no prefixes, go to paragraph mode.
291 if ( 0 == $prefixLength ) {
292 # No prefix (not in list)--go to paragraph mode
293 # @todo consider using a stack for nestable elements like span, table and div
294 $openMatch = preg_match(
295 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
296 . '<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)\\b/iS',
297 $t
298 );
299 $closeMatch = preg_match(
300 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
301 . '<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
302 . Parser::MARKER_PREFIX
303 . '-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)\\b/iS',
304 $t
305 );
306
307 if ( $openMatch || $closeMatch ) {
308 $pendingPTag = false;
309 // Only close the paragraph if we're not inside a <pre> tag, or if
310 // that <pre> tag has just been opened
311 if ( !$this->inPre || $preOpenMatch ) {
312 // @todo T7718: paragraph closed
313 $output .= $this->closeParagraph();
314 }
315 if ( $preOpenMatch && !$preCloseMatch ) {
316 $this->inPre = true;
317 }
318 $bqOffset = 0;
319 while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
320 $bqMatch, PREG_OFFSET_CAPTURE, $bqOffset )
321 ) {
322 $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
323 $bqOffset = $bqMatch[0][1] + strlen( $bqMatch[0][0] );
324 }
325 $inBlockElem = !$closeMatch;
326 } elseif ( !$inBlockElem && !$this->inPre ) {
327 if ( ' ' == substr( $t, 0, 1 )
328 && ( $this->lastSection === 'pre' || trim( $t ) != '' )
329 && !$inBlockquote
330 ) {
331 # pre
332 if ( $this->lastSection !== 'pre' ) {
333 $pendingPTag = false;
334 $output .= $this->closeParagraph() . '<pre>';
335 $this->lastSection = 'pre';
336 }
337 $t = substr( $t, 1 );
338 } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
339 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
340 # But it should clear $pendingPTag.
341 if ( $pendingPTag ) {
342 $output .= $this->closeParagraph();
343 $pendingPTag = false;
344 $this->lastSection = '';
345 }
346 } else {
347 # paragraph
348 if ( trim( $t ) === '' ) {
349 if ( $pendingPTag ) {
350 $output .= $pendingPTag . '<br />';
351 $pendingPTag = false;
352 $this->lastSection = 'p';
353 } else {
354 if ( $this->lastSection !== 'p' ) {
355 $output .= $this->closeParagraph();
356 $this->lastSection = '';
357 $pendingPTag = '<p>';
358 } else {
359 $pendingPTag = '</p><p>';
360 }
361 }
362 } else {
363 if ( $pendingPTag ) {
364 $output .= $pendingPTag;
365 $pendingPTag = false;
366 $this->lastSection = 'p';
367 } elseif ( $this->lastSection !== 'p' ) {
368 $output .= $this->closeParagraph() . '<p>';
369 $this->lastSection = 'p';
370 }
371 }
372 }
373 }
374 }
375 # somewhere above we forget to get out of pre block (T2785)
376 if ( $preCloseMatch && $this->inPre ) {
377 $this->inPre = false;
378 }
379 if ( $pendingPTag === false ) {
380 if ( $prefixLength === 0 ) {
381 $output .= $t;
382 $output .= "\n";
383 } else {
384 // Trim whitespace in list items
385 $output .= trim( $t );
386 }
387 }
388 }
389 while ( $prefixLength ) {
390 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
391 --$prefixLength;
392 if ( !$prefixLength ) {
393 $output .= "\n";
394 }
395 }
396 if ( $this->lastSection !== '' ) {
397 $output .= '</' . $this->lastSection . '>';
398 $this->lastSection = '';
399 }
400
401 return $output;
402 }
403
414 private function findColonNoLinks( $str, &$before, &$after ) {
415 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE ) ) {
416 # Nothing to find!
417 return false;
418 }
419
420 if ( $m[0][0] === ':' ) {
421 # Easy; no tag nesting to worry about
422 $colonPos = $m[0][1];
423 $before = substr( $str, 0, $colonPos );
424 $after = substr( $str, $colonPos + 1 );
425 return $colonPos;
426 }
427
428 # Ugly state machine to walk through avoiding tags.
429 $state = self::COLON_STATE_TEXT;
430 $ltLevel = 0;
431 $lcLevel = 0;
432 $len = strlen( $str );
433 for ( $i = $m[0][1]; $i < $len; $i++ ) {
434 $c = $str[$i];
435
436 switch ( $state ) {
438 switch ( $c ) {
439 case "<":
440 # Could be either a <start> tag or an </end> tag
442 break;
443 case ":":
444 if ( $ltLevel === 0 ) {
445 # We found it!
446 $before = substr( $str, 0, $i );
447 $after = substr( $str, $i + 1 );
448 return $i;
449 }
450 # Embedded in a tag; don't break it.
451 break;
452 default:
453 # Skip ahead looking for something interesting
454 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
455 # Nothing else interesting
456 return false;
457 }
458 if ( $m[0][0] === '-{' ) {
459 $state = self::COLON_STATE_LC;
460 $lcLevel++;
461 $i = $m[0][1] + 1;
462 } else {
463 # Skip ahead to next interesting character.
464 $i = $m[0][1] - 1;
465 }
466 break;
467 }
468 break;
470 # In language converter markup -{ ... }-
471 if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE, $i ) ) {
472 # Nothing else interesting to find; abort!
473 # We're nested in language converter markup, but there
474 # are no close tags left. Abort!
475 break 2;
476 } elseif ( $m[0][0] === '-{' ) {
477 $i = $m[0][1] + 1;
478 $lcLevel++;
479 } elseif ( $m[0][0] === '}-' ) {
480 $i = $m[0][1] + 1;
481 $lcLevel--;
482 if ( $lcLevel === 0 ) {
483 $state = self::COLON_STATE_TEXT;
484 }
485 }
486 break;
488 # In a <tag>
489 switch ( $c ) {
490 case ">":
491 $ltLevel++;
492 $state = self::COLON_STATE_TEXT;
493 break;
494 case "/":
495 # Slash may be followed by >?
497 break;
498 default:
499 # ignore
500 }
501 break;
503 switch ( $c ) {
504 case "/":
506 break;
507 case "!":
509 break;
510 case ">":
511 # Illegal early close? This shouldn't happen D:
512 $state = self::COLON_STATE_TEXT;
513 break;
514 default:
515 $state = self::COLON_STATE_TAG;
516 }
517 break;
519 # In a </tag>
520 if ( $c === ">" ) {
521 if ( $ltLevel > 0 ) {
522 $ltLevel--;
523 } else {
524 # ignore the excess close tag, but keep looking for
525 # colons. (This matches Parsoid behavior.)
526 wfDebug( __METHOD__ . ": Invalid input; too many close tags\n" );
527 }
528 $state = self::COLON_STATE_TEXT;
529 }
530 break;
532 if ( $c === ">" ) {
533 # Yes, a self-closed tag <blah/>
534 $state = self::COLON_STATE_TEXT;
535 } else {
536 # Probably we're jumping the gun, and this is an attribute
537 $state = self::COLON_STATE_TAG;
538 }
539 break;
541 if ( $c === "-" ) {
543 }
544 break;
546 if ( $c === "-" ) {
548 } else {
550 }
551 break;
553 if ( $c === ">" ) {
554 $state = self::COLON_STATE_TEXT;
555 } else {
557 }
558 break;
559 default:
560 throw new MWException( "State machine error in " . __METHOD__ );
561 }
562 }
563 if ( $ltLevel > 0 || $lcLevel > 0 ) {
564 wfDebug(
565 __METHOD__ . ": Invalid input; not enough close tags " .
566 "(level $ltLevel/$lcLevel, state $state)\n"
567 );
568 return false;
569 }
570 return false;
571 }
572}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
openList( $char)
Open the list item element identified by the prefix character.
const COLON_STATE_COMMENTDASHDASH
__construct( $text, $lineStart)
Private constructor.
nextItem( $char)
Close the current list item and open the next one.
execute()
Execute the pass.
getCommon( $st1, $st2)
getCommon() returns the length of the longest common substring of both arguments, starting at the beg...
static doBlockLevels( $text, $lineStart)
Make lists from lines starting with ':', '*', '#', etc.
findColonNoLinks( $str, &$before, &$after)
Split up a string on ':', ignoring any occurrences inside tags to prevent illegal overlapping.
const COLON_STATE_COMMENTDASH
closeParagraph()
If a pre or p is open, return the corresponding close tag and update the state.
closeList( $char)
Close the current list item identified by the prefix character.
MediaWiki exception.
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
namespace being checked & $result
Definition hooks.txt:2323
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2255
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2845