Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / parser / CoreTagHooks.php
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 /**
25 * Various tag hooks, registered in Parser::firstCallInit()
26 * @ingroup Parser
27 */
28 class CoreTagHooks {
29 /**
30 * @param Parser $parser
31 * @return void
32 */
33 public static function register( $parser ) {
34 global $wgRawHtml;
35 $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
36 $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
37 $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
38 $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
39 if ( $wgRawHtml ) {
40 $parser->setHook( 'html', [ __CLASS__, 'html' ] );
41 }
42 }
43
44 /**
45 * Core parser tag hook function for 'pre'.
46 * Text is treated roughly as 'nowiki' wrapped in an HTML 'pre' tag;
47 * valid HTML attributes are passed on.
48 *
49 * @param string $text
50 * @param array $attribs
51 * @param Parser $parser
52 * @return string HTML
53 */
54 public static function pre( $text, $attribs, $parser ) {
55 // Backwards-compatibility hack
56 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
57
58 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
59 // We need to let both '"' and '&' through,
60 // for strip markers and entities respectively.
61 $content = str_replace(
62 [ '>', '<' ],
63 [ '&gt;', '&lt;' ],
64 $content
65 );
66 return Html::rawElement( 'pre', $attribs, $content );
67 }
68
69 /**
70 * Core parser tag hook function for 'html', used only when
71 * $wgRawHtml is enabled.
72 *
73 * This is potentially unsafe and should be used only in very careful
74 * circumstances, as the contents are emitted as raw HTML.
75 *
76 * Uses undocumented extended tag hook return values, introduced in r61913.
77 *
78 * @param string $content
79 * @param array $attributes
80 * @param Parser $parser
81 * @throws MWException
82 * @return array
83 */
84 public static function html( $content, $attributes, $parser ) {
85 global $wgRawHtml;
86 if ( $wgRawHtml ) {
87 return [ $content, 'markerType' => 'nowiki' ];
88 } else {
89 throw new MWException( '<html> extension tag encountered unexpectedly' );
90 }
91 }
92
93 /**
94 * Core parser tag hook function for 'nowiki'. Text within this section
95 * gets interpreted as a string of text with HTML-compatible character
96 * references, and wiki markup within it will not be expanded.
97 *
98 * Uses undocumented extended tag hook return values, introduced in r61913.
99 *
100 * @param string $content
101 * @param array $attributes
102 * @param Parser $parser
103 * @return array
104 */
105 public static function nowiki( $content, $attributes, $parser ) {
106 $content = strtr( $content, [
107 // lang converter
108 '-{' => '-&#123;',
109 '}-' => '&#125;-',
110 // html tags
111 '<' => '&lt;',
112 '>' => '&gt;'
113 // Note: Both '"' and '&' are not converted.
114 // This allows strip markers and entities through.
115 ] );
116 return [ $content, 'markerType' => 'nowiki' ];
117 }
118
119 /**
120 * Core parser tag hook function for 'gallery'.
121 *
122 * Renders a thumbnail list of the given images, with optional captions.
123 * Full syntax documented on the wiki:
124 *
125 * https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
126 *
127 * @todo break Parser::renderImageGallery out here too.
128 *
129 * @param string $content
130 * @param array $attributes
131 * @param Parser $parser
132 * @return string HTML
133 */
134 public static function gallery( $content, $attributes, $parser ) {
135 return $parser->renderImageGallery( $content, $attributes );
136 }
137
138 /**
139 * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in
140 * the top-right corner of the page, outside of the main content.
141 *
142 * @param string $content
143 * @param array $attributes
144 * @param Parser $parser
145 * @param PPFrame $frame
146 * @return string
147 * @since 1.25
148 */
149 public static function indicator( $content, array $attributes, Parser $parser, PPFrame $frame ) {
150 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
151 return '<span class="error">' .
152 wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
153 '</span>';
154 }
155
156 $parser->getOutput()->setIndicator(
157 trim( $attributes['name'] ),
158 Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
159 );
160
161 return '';
162 }
163 }