Debian Codegraph @

includes/parser/CoreTagHooks.php

raw symbols SCIP
1 <?php
2 /**
3 * Tag hooks provided by MediaWiki core
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 namespace MediaWiki\Parser;
25
26 use MediaWiki\Config\ServiceOptions;
27 use MediaWiki\Html\Html;
28 use MediaWiki\Language\LanguageCode;
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\MediaWikiServices;
31 use StringUtils;
32 use UnexpectedValueException;
33
34 /**
35 * Various tag hooks, registered in every Parser
36 * @ingroup Parser
37 */
38 class CoreTagHooks {
39
40 /**
41 * @internal
42 */
43 public const REGISTER_OPTIONS = [
44 // See documentation for the corresponding config options
45 MainConfigNames::RawHtml,
46 ];
47
48 /**
49 * @param Parser $parser
50 * @param ServiceOptions $options
51 *
52 * @return void
53 * @internal
54 */
55 public static function register( Parser $parser, ServiceOptions $options ) {
56 $options->assertRequiredOptions( self::REGISTER_OPTIONS );
57 $rawHtml = $options->get( MainConfigNames::RawHtml );
58 $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
59 $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
60 $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
61 $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
62 $parser->setHook( 'langconvert', [ __CLASS__, 'langconvert' ] );
63 if ( $rawHtml ) {
64 $parser->setHook( 'html', [ __CLASS__, 'html' ] );
65 }
66 }
67
68 /**
69 * Core parser tag hook function for 'pre'.
70 * Text is treated roughly as 'nowiki' wrapped in an HTML 'pre' tag;
71 * valid HTML attributes are passed on.
72 *
73 * @param ?string $content
74 * @param array $attribs
75 * @param Parser $parser
76 * @return string HTML
77 * @internal
78 */
79 public static function pre( ?string $content, array $attribs, Parser $parser ): string {
80 // Backwards-compatibility hack
81 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $content ?? '', 'i' );
82
83 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
84 // We need to let both '"' and '&' through,
85 // for strip markers and entities respectively.
86 $content = str_replace(
87 [ '>', '<' ],
88 [ '&gt;', '&lt;' ],
89 $content
90 );
91 return Html::rawElement( 'pre', $attribs, $content );
92 }
93
94 /**
95 * Core parser tag hook function for 'html', used only when
96 * $wgRawHtml is enabled.
97 *
98 * This is potentially unsafe and should be used only in very careful
99 * circumstances, as the contents are emitted as raw HTML.
100 *
101 * Uses undocumented extended tag hook return values, introduced in r61913.
102 *
103 * @suppress SecurityCheck-XSS
104 * @param ?string $content
105 * @param array $attributes
106 * @param Parser $parser
107 * @return array|string Output of tag hook
108 * @internal
109 */
110 public static function html( ?string $content, array $attributes, Parser $parser ) {
111 $rawHtml = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::RawHtml );
112 if ( $rawHtml ) {
113 if ( $parser->getOptions()->getAllowUnsafeRawHtml() ) {
114 return [ $content ?? '', 'markerType' => 'nowiki' ];
115 } else {
116 // In a system message where raw html is
117 // not allowed (but it is allowed in other
118 // contexts).
119 return Html::rawElement(
120 'span',
121 [ 'class' => 'error' ],
122 // Using ->text() not ->parse() as
123 // a paranoia measure against a loop.
124 $parser->msg( 'rawhtml-notallowed' )->escaped()
125 );
126 }
127 } else {
128 throw new UnexpectedValueException( '<html> extension tag encountered unexpectedly' );
129 }
130 }
131
132 /**
133 * Core parser tag hook function for 'nowiki'. Text within this section
134 * gets interpreted as a string of text with HTML-compatible character
135 * references, and wiki markup within it will not be expanded.
136 *
137 * Uses undocumented extended tag hook return values, introduced in r61913.
138 *
139 * Uses custom html escaping which phan-taint-check won't recognize
140 * hence we suppress the error.
141 * @suppress SecurityCheck-XSS
142 *
143 * @param ?string $content
144 * @param array $attributes
145 * @param Parser $parser
146 * @return array
147 * @internal
148 */
149 public static function nowiki( ?string $content, array $attributes, Parser $parser ): array {
150 $content = strtr( $content ?? '', [
151 // lang converter
152 '-{' => '-&#123;',
153 '}-' => '&#125;-',
154 // html tags
155 '<' => '&lt;',
156 '>' => '&gt;'
157 // Note: Both '"' and '&' are not converted.
158 // This allows strip markers and entities through.
159 ] );
160 return [ $content, 'markerType' => 'nowiki' ];
161 }
162
163 /**
164 * Core parser tag hook function for 'gallery'.
165 *
166 * Renders a thumbnail list of the given images, with optional captions.
167 * Full syntax documented on the wiki:
168 *
169 * https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
170 *
171 * @todo break Parser::renderImageGallery out here too.
172 *
173 * @param ?string $content
174 * @param array $attributes
175 * @param Parser $parser
176 * @return string HTML
177 * @internal
178 */
179 public static function gallery( ?string $content, array $attributes, Parser $parser ): string {
180 return $parser->renderImageGallery( $content ?? '', $attributes );
181 }
182
183 /**
184 * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in
185 * the top-right corner of the page, outside of the main content.
186 *
187 * @param ?string $content
188 * @param array $attributes
189 * @param Parser $parser
190 * @param PPFrame $frame
191 * @return string
192 * @since 1.25
193 * @internal
194 */
195 public static function indicator( ?string $content, array $attributes, Parser $parser, PPFrame $frame ): string {
196 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
197 return '<span class="error">' .
198 $parser->msg( 'invalid-indicator-name' )->parse() .
199 '</span>';
200 }
201
202 $parser->getOutput()->setIndicator(
203 trim( $attributes['name'] ),
204 Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content ?? '', $frame ) )
205 );
206
207 return '';
208 }
209
210 /**
211 * Returns content converted into the requested language variant, using LanguageConverter.
212 *
213 * @param ?string $content
214 * @param array $attributes
215 * @param Parser $parser
216 * @param PPFrame $frame
217 * @return string
218 * @since 1.36
219 * @internal
220 */
221 public static function langconvert( ?string $content, array $attributes, Parser $parser, PPFrame $frame ): string {
222 if ( isset( $attributes['from'] ) && isset( $attributes['to'] ) ) {
223 $fromArg = trim( $attributes['from'] );
224 $toArg = trim( $attributes['to'] );
225 $fromLangCode = explode( '-', $fromArg )[0];
226 if ( $fromLangCode && $fromLangCode === explode( '-', $toArg )[0] ) {
227 $lang = MediaWikiServices::getInstance()->getLanguageFactory()
228 ->getLanguage( $fromLangCode );
229 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
230 ->getLanguageConverter( $lang );
231
232 # ensure that variants are available,
233 # and the variants are valid BCP 47 codes
234 if ( $converter->hasVariants()
235 && strcasecmp( $fromArg, LanguageCode::bcp47( $fromArg ) ) === 0
236 && strcasecmp( $toArg, LanguageCode::bcp47( $toArg ) ) === 0
237 ) {
238 $toVariant = $converter->validateVariant( $toArg );
239
240 if ( $toVariant ) {
241 return $converter->autoConvert(
242 $parser->recursiveTagParse( $content ?? '', $frame ),
243 $toVariant
244 );
245 }
246 }
247 }
248 }
249
250 return Html::rawElement(
251 'span',
252 [ 'class' => 'error' ],
253 $parser->msg( 'invalid-langconvert-attrs' )->parse()
254 );
255 }
256
257 }
258
259 /** @deprecated class alias since 1.43 */
260 class_alias( CoreTagHooks::class, 'CoreTagHooks' );