]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/parser/CoreTagHooks.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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|string Output of tag hook
83          */
84         public static function html( $content, $attributes, $parser ) {
85                 global $wgRawHtml;
86                 if ( $wgRawHtml ) {
87                         if ( $parser->getOptions()->getAllowUnsafeRawHtml() ) {
88                                 return [ $content, 'markerType' => 'nowiki' ];
89                         } else {
90                                 // In a system message where raw html is
91                                 // not allowed (but it is allowed in other
92                                 // contexts).
93                                 return Html::rawElement(
94                                         'span',
95                                         [ 'class' => 'error' ],
96                                         // Using ->text() not ->parse() as
97                                         // a paranoia measure against a loop.
98                                         wfMessage( 'rawhtml-notallowed' )->escaped()
99                                 );
100                         }
101                 } else {
102                         throw new MWException( '<html> extension tag encountered unexpectedly' );
103                 }
104         }
105
106         /**
107          * Core parser tag hook function for 'nowiki'. Text within this section
108          * gets interpreted as a string of text with HTML-compatible character
109          * references, and wiki markup within it will not be expanded.
110          *
111          * Uses undocumented extended tag hook return values, introduced in r61913.
112          *
113          * @param string $content
114          * @param array $attributes
115          * @param Parser $parser
116          * @return array
117          */
118         public static function nowiki( $content, $attributes, $parser ) {
119                 $content = strtr( $content, [
120                         // lang converter
121                         '-{' => '-&#123;',
122                         '}-' => '&#125;-',
123                         // html tags
124                         '<' => '&lt;',
125                         '>' => '&gt;'
126                         // Note: Both '"' and '&' are not converted.
127                         // This allows strip markers and entities through.
128                 ] );
129                 return [ $content, 'markerType' => 'nowiki' ];
130         }
131
132         /**
133          * Core parser tag hook function for 'gallery'.
134          *
135          * Renders a thumbnail list of the given images, with optional captions.
136          * Full syntax documented on the wiki:
137          *
138          *   https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
139          *
140          * @todo break Parser::renderImageGallery out here too.
141          *
142          * @param string $content
143          * @param array $attributes
144          * @param Parser $parser
145          * @return string HTML
146          */
147         public static function gallery( $content, $attributes, $parser ) {
148                 return $parser->renderImageGallery( $content, $attributes );
149         }
150
151         /**
152          * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in
153          * the top-right corner of the page, outside of the main content.
154          *
155          * @param string $content
156          * @param array $attributes
157          * @param Parser $parser
158          * @param PPFrame $frame
159          * @return string
160          * @since 1.25
161          */
162         public static function indicator( $content, array $attributes, Parser $parser, PPFrame $frame ) {
163                 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
164                         return '<span class="error">' .
165                                 wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
166                                 '</span>';
167                 }
168
169                 $parser->getOutput()->setIndicator(
170                         trim( $attributes['name'] ),
171                         Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
172                 );
173
174                 return '';
175         }
176 }