Merge "Http::getProxy() method to get proxy configuration"
[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 return Xml::openElement( 'pre', $attribs ) .
60 Xml::escapeTagsOnly( $content ) .
61 '</pre>';
62 }
63
64 /**
65 * Core parser tag hook function for 'html', used only when
66 * $wgRawHtml is enabled.
67 *
68 * This is potentially unsafe and should be used only in very careful
69 * circumstances, as the contents are emitted as raw HTML.
70 *
71 * Uses undocumented extended tag hook return values, introduced in r61913.
72 *
73 * @param string $content
74 * @param array $attributes
75 * @param Parser $parser
76 * @throws MWException
77 * @return array
78 */
79 public static function html( $content, $attributes, $parser ) {
80 global $wgRawHtml;
81 if ( $wgRawHtml ) {
82 return [ $content, 'markerType' => 'nowiki' ];
83 } else {
84 throw new MWException( '<html> extension tag encountered unexpectedly' );
85 }
86 }
87
88 /**
89 * Core parser tag hook function for 'nowiki'. Text within this section
90 * gets interpreted as a string of text with HTML-compatible character
91 * references, and wiki markup within it will not be expanded.
92 *
93 * Uses undocumented extended tag hook return values, introduced in r61913.
94 *
95 * @param string $content
96 * @param array $attributes
97 * @param Parser $parser
98 * @return array
99 */
100 public static function nowiki( $content, $attributes, $parser ) {
101 $content = strtr( $content, [ '-{' => '-&#123;', '}-' => '&#125;-' ] );
102 return [ Xml::escapeTagsOnly( $content ), 'markerType' => 'nowiki' ];
103 }
104
105 /**
106 * Core parser tag hook function for 'gallery'.
107 *
108 * Renders a thumbnail list of the given images, with optional captions.
109 * Full syntax documented on the wiki:
110 *
111 * https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
112 *
113 * @todo break Parser::renderImageGallery out here too.
114 *
115 * @param string $content
116 * @param array $attributes
117 * @param Parser $parser
118 * @return string HTML
119 */
120 public static function gallery( $content, $attributes, $parser ) {
121 return $parser->renderImageGallery( $content, $attributes );
122 }
123
124 /**
125 * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in
126 * the top-right corner of the page, outside of the main content.
127 *
128 * @param string $content
129 * @param array $attributes
130 * @param Parser $parser
131 * @param PPFrame $frame
132 * @return string
133 * @since 1.25
134 */
135 public static function indicator( $content, array $attributes, Parser $parser, PPFrame $frame ) {
136 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
137 return '<span class="error">' .
138 wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
139 '</span>';
140 }
141
142 $parser->getOutput()->setIndicator(
143 trim( $attributes['name'] ),
144 Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
145 );
146
147 return '';
148 }
149 }