aboutsummaryrefslogtreecommitdiff
path: root/Code.php
blob: 045448ee5d5118d9f409a5fe6b73aba6449e9047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace MediaWiki\Extension\Code;

use Content;
use ErrorPageError;
use ExtensionRegistry;
use FatalError;
use Html;
use MediaWiki\Content\Renderer\ContentParseParams;
use MediaWiki\MediaWikiServices;
use Parser;
use ParserOptions;
use ParserOutput;
use Sanitizer;
use SpecialPage;
use TextContent;
use TextContentHandler;
use Title;
use WikiPage;
use Xml;

class CodeHooks {
	public static function onParserFirstCallInit( Parser $parser ) {
		global $wgCode_languages;
		foreach ( $wgCode_languages as $lang ) {
			$parser->setHook(
				$lang['tag'],
				fn ( $input, $args ) => renderCode( $input, $lang, $parser->getOutput(), $args )
			);
		}
	}

	public static function onContentHandlerDefaultModelFor( Title $title, &$model ) {
		if ( getLangByPageName( $title->getDBkey() ) ) {
			$model = CodeContent::MODEL;
			return false;
		} else {
			return true;
		}
	}
}

function getLangByPageName( string $dbKey ) {
	global $wgCode_languages;
	$suffix = '.' . pathinfo( $dbKey, PATHINFO_EXTENSION );
	return current( array_filter( $wgCode_languages, fn( $lang ) => $lang['suffix'] == $suffix ) );
}

function pre( $code, $attr ) {
	return Xml::element( 'pre', Sanitizer::validateTagAttributes( $attr, 'pre' ), $code );
}

/**
 * Render the given code and return the HTML.
 *
 * @param string $code The code to be rendered.
 * @param ? $lang The language configuration.
 * @param OutputPage|ParserOutput|null $outputPage
 * @param ?array $tagAttrs Associative array of tag attributes or null in case of a code page.
 * @return string The rendered HTML.
 */
function renderCode( string $code, $lang, $outputPage, ?array $tagAttrs = null ) {
	if ( ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' ) ) {
		$result = \SyntaxHighlight::highlight( $code, $lang['pygmentsLexer'], $tagAttrs ?? [ 'line' => true ] );
		if ( $result->isOk() ) {
			$codeBlock = $result->getValue();
			$outputPage->addModuleStyles( [ 'ext.pygments' ] );
		} else {
			$codeBlock = pre( $code, $tagAttrs );
		}
	} else {
		$codeBlock = pre( $code, $tagAttrs );
	}

	$actions = '';

	foreach ( $lang['actions'] as $action => $url ) {
		if ( str_contains( $url, '$url' ) ) {
			// FUTURE: it would be nice to still display such actions for code pages
			continue;
		}

		$encodedCode = rawurlencode( trim( $code ) );

		// The wfMessage allows admins to create MediaWiki:codeaction-label with a template call
		// if they want to enable their users to customize or localize the code action labels.
		$actions .= Xml::openElement( 'a', [ 'href' => str_replace( '$code', $encodedCode, $url ) ] );
		$actions .= wfMessage( "codeaction-label", $action )->parse();
		$actions .= Xml::closeElement( 'a' ) . ' ';
	}

	$additionalOutput = '';

	$scribuntoModule = $lang['scribuntoModule'] ?? null;
	if ( $scribuntoModule && ExtensionRegistry::getInstance()->isLoaded( 'Scribunto' ) ) {
		$callFunction = static function ( Parser $parser, string $function, array $extraArgs = [] ) use ( $scribuntoModule, $code, $lang, $tagAttrs ) {
			$args = [ $scribuntoModule, $function, $code, $lang['tag'], $tagAttrs == null ];
			$args = array_merge( $args, $extraArgs );
			if ( $tagAttrs ) {
				$args = array_merge( $args, $tagAttrs );
			}
			$frame = $parser->getPreprocessor()->newFrame();
			$res = $parser->callParserFunction( $frame, '#invoke', $args );
			if ( !$res['found'] ) {
				throw new FatalError( "unexpected condition in Code extension: the Scribunto module is loaded but the #invoke parser function wasn't found" );
			}
			return Sanitizer::removeSomeTags( $res['text'], [ 'extraTags' => [ 'a' ], 'extra' ] );
		};
		if ( $tagAttrs ) {
			$parser = MediaWikiServices::getInstance()->getParser();
		} else {
			$parser = MediaWikiServices::getInstance()->getParserFactory()->create();
			$parser->startExternalParse( null, ParserOptions::newFromAnon(), Parser::OT_HTML );
		}
		$codeBlock = $callFunction( $parser, 'formatCode', [ $codeBlock ] );
		$additionalOutput = $callFunction( $parser, 'additionalOutput' );
	}

	return $codeBlock . $actions . $additionalOutput;
}

class ContentHandler extends TextContentHandler {
	public function __construct( $modelId = CodeContent::MODEL ) {
		parent::__construct( $modelId, [ CONTENT_FORMAT_TEXT ] );
	}

	public function fillParserOutput( Content $content, ContentParseParams $params, ParserOutput &$output ) {
		$lang = getLangByPageName( $params->getPage()->getDBkey() );
		$out = '';
		if ( !$lang ) {
			global $wgCode_languages;
			$supportedSuffixes = implode( ', ', array_map( fn( $lang ) => $lang['suffix'], $wgCode_languages ) );

			MediaWikiServices::getInstance()->getTrackingCategories()
				->addTrackingCategory( $output, 'codepage-error-category', $params->getPage() );

			$out = Html::errorBox( wfMessage( 'codepage-unknown-suffix', 'code', "[$supportedSuffixes]" ) );
			$lang = [
				'pygmentsLexer' => 'text',
				'actions' => [],
			];
		}
		$out .= renderCode( $content->getText(), $lang, $output );
		$output->setText( $out );
	}

	protected function getContentClass() {
		return CodeContent::class;
	}
}

class CodeContent extends TextContent {
	// Has to match the ContentHandlers entry in extension.json.
	public const MODEL = 'code';

	public function __construct( $text, $model_id = self::MODEL ) {
		parent::__construct( $text, $model_id );
	}
}

class SpecialCodeAction extends SpecialPage {
	public function __construct() {
		parent::__construct( 'CodeAction' );
	}

	public function execute( $par ) {
		$namespacesWithCodePages = $this->getConfig()->get( 'Code_namespacesWithCodePages' );
		$languages = $this->getConfig()->get( 'Code_languages' );

		if ( empty( $namespacesWithCodePages ) ) {
			$exampleConfig = htmlspecialchars( file_get_contents( __DIR__ . '/examples/namespacesWithCodePages.php' ) );
			throw new ErrorPageError( 'codeactions-unavailable-title', 'codeactions-unavailable-no-namespaces', "<pre>$exampleConfig</pre>" );
		}

		$exampleLang = current( array_filter( $languages, fn( $lang ) => count( $lang['actions'] ) > 0 ) );
		if ( !$exampleLang ) {
			$exampleConfig = htmlspecialchars( file_get_contents( __DIR__ . '/examples/languages.php' ) );
			throw new ErrorPageError( 'codeactions-unavailable-title', 'codeactions-unavailable-no-lang-with-actions', "<pre>$exampleConfig</pre>" );
		}

		$contLang = MediaWikiServices::getInstance()->getContentLanguage();

		$output = $this->getOutput();
		$args = explode( '/', $par, 2 );

		if ( count( $args ) != 2 ) {
			$formatter = MediaWikiServices::getInstance()->getTitleFormatter();

			$title = $formatter->formatTitle( array_keys( $namespacesWithCodePages )[0], 'Example' . $exampleLang['suffix'] );
			$action = htmlspecialchars( array_keys( $exampleLang['actions'] )[0] );
			$output->setPageTitle( $this->msg( 'notargettitle' ) );
			$output->addWikiMsg( 'codeaction-howto', $this->getPageTitle(), $action, $title );
			$output->addWikiTextAsInterface(
				Xml::element( 'h2', null, $this->msg( 'codeactions-available' ) )
			);
			$output->addHTML(
				Xml::buildTable(
					array_map( fn ( $lang ) => [ $lang['suffix'], implode( ', ', array_keys( $lang['actions'] ) ) ], $languages ),
				 [ 'class' => 'wikitable' ], [ $this->msg( 'code-pagesuffix' ), $this->msg( 'codeactions-available' ) ] )
			);
			return;
		}
		[ $action, $pagename ] = $args;

		$title = Title::newFromText( $pagename );
		if ( !$title ) {
			throw new ErrorPageError( 'badtitle', 'title-invalid' );
		}

		if ( !array_key_exists( $title->getNamespace(), $namespacesWithCodePages ) ) {
			$nsName = $this->getContext()->getLanguage()->getFormattedNsText( $title->getNamespace() );
			throw new ErrorPageError( 'codeaction-unsupported-namespace-title', 'codeaction-unsupported-namespace-text', $nsName );
		}

		$lang = getLangByPageName( $title->getDBkey() );
		if ( !$lang ) {
			$supportedSuffixes = implode( ', ', array_map( fn( $lang ) => $lang['suffix'], $languages ) );
			throw new ErrorPageError( 'codeaction-unknown-suffix-title', 'codeaction-unknown-suffix-text', [ $pagename, "[$supportedSuffixes]" ] );
		}

		$url = $lang['actions'][$action] ?? null;

		if ( !$url ) {
			$supportedActions = implode( ', ', array_keys( $lang['actions'] ) );
			throw new ErrorPageError( 'codeaction-unknown-action-title', 'codeaction-unknown-action-text', [ $action, "[$supportedActions]" ] );
		}

		if ( str_contains( $url, '$code' ) ) {
			$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();

			$errors = $permissionManager->getPermissionErrors( 'read', $this->getUser(), $title );
			if ( !empty( $errors ) ) {
				$output->showPermissionsErrorPage( $errors, 'read' );
				return;
			}

			$content = WikiPage::factory( $title )->getContent();
			if ( $content == null ) {
				throw new ErrorPageError( 'codeaction-page-not-found-title', 'codeaction-page-not-found-text', $title );
			}
			$content = ContentHandler::getContentText( $content );

			$codePrefix = $lang['codePrefix'] ?? null;
			if ( $codePrefix ) {
				$content = str_replace( '$url', $title->getFullURL(), $codePrefix ) . "\n\n" . $content;
			}

			$url = str_replace( '$code', rawurlencode( $content ), $url );
		} else {
			$url = str_replace( '$url', rawurlencode( $title->getFullURL() ), $url );
		}

		$output->redirect( $url );
	}
}