Debian Codegraph @

tests/parser/ParserTestParserHook.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Html\Html;
4 use MediaWiki\Parser\Parser;
5
6 /**
7 * A basic extension that's used by the parser tests to test whether input and
8 * arguments are passed to extensions properly.
9 *
10 * Copyright © 2005, 2006 Ævar Arnfjörð Bjarmason
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup Testing
29 * @author Ævar Arnfjörð Bjarmason <[email protected]om>
30 */
31
32 class ParserTestParserHook {
33
34 public static function setup( Parser $parser ) {
35 $parser->setHook( 'tag', [ __CLASS__, 'dumpHook' ] );
36 $parser->setHook( 'tåg', [ __CLASS__, 'dumpHook' ] );
37 $parser->setHook( 'statictag', [ __CLASS__, 'staticTagHook' ] );
38 $parser->setHook( 'asidetag', [ __CLASS__, 'asideTagHook' ] );
39 $parser->setHook( 'pwraptest', [ __CLASS__, 'pWrapTestHook' ] );
40 $parser->setHook( 'spantag', [ __CLASS__, 'spanTagHook' ] );
41 return true;
42 }
43
44 public static function dumpHook( $in, $argv ) {
45 // @phan-suppress-next-line SecurityCheck-XSS
46 return "<pre>\n" .
47 var_export( $in, true ) . "\n" .
48 var_export( $argv, true ) . "\n" .
49 "</pre>";
50 }
51
52 /**
53 * @param string $in
54 * @param array $argv
55 * @param Parser $parser
56 * @return string
57 * @suppress SecurityCheck-XSS
58 * @suppress UnusedSuppression
59 */
60 public static function staticTagHook( $in, $argv, $parser ) {
61 $KEY = 'mw:tests:static-tag-hook';
62 $po = $parser->getOutput();
63 if ( !count( $argv ) ) {
64 $po->appendExtensionData( $KEY, $in );
65 return '';
66 } elseif ( count( $argv ) === 1 && isset( $argv['action'] )
67 && $argv['action'] === 'flush' && $in === null
68 ) {
69 // This pattern is deprecated, since the order of parsing will
70 // in the future not be guaranteed. A better approach is to
71 // collect/emit the buffered content in a post-processing pass
72 // over the document after parsing of the article and all contained
73 // fragments is completed and the fragments are merged.
74 // T357838, T300979
75 $vals = $po->getExtensionData( $KEY );
76 if ( $vals === null ) {
77 return '';
78 }
79 return array_key_last( $vals );
80 } else { // wtf?
81 return "\nCall this extension as <statictag>string</statictag> or as" .
82 " <statictag action=flush/>, not in any other way.\n" .
83 "text: " . var_export( $in, true ) . "\n" .
84 "argv: " . var_export( $argv, true ) . "\n";
85 }
86 }
87
88 public static function asideTagHook(): string {
89 return Html::element( 'aside', [], 'Some aside content' );
90 }
91
92 public static function pWrapTestHook(): string {
93 return '<!--CMT--><style>p{}</style>';
94 }
95
96 /**
97 * @param string $in
98 * @param array $argv
99 * @param Parser $parser
100 * @return string
101 */
102 public static function spanTagHook( $in, $argv, $parser ): string {
103 return '<span>' .
104 Parser::stripOuterParagraph( $parser->recursiveTagParse( $in ) ) .
105 '</span>';
106 }
107 }