| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto; |
| 4 |
|
| 5 |
use MediaWiki\Content\CodeContentHandler; |
| 6 |
use MediaWiki\Content\Content; |
| 7 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 8 |
use MediaWiki\Content\TextContent; |
| 9 |
use MediaWiki\Content\ValidationParams; |
| 10 |
use MediaWiki\Html\Html; |
| 11 |
use MediaWiki\MediaWikiServices; |
| 12 |
use MediaWiki\Page\PageIdentity; |
| 13 |
use MediaWiki\Parser\ParserOutput; |
| 14 |
use MediaWiki\Registration\ExtensionRegistry; |
| 15 |
use MediaWiki\Status\Status; |
| 16 |
use MediaWiki\SyntaxHighlight\SyntaxHighlight; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class ScribuntoContentHandler extends CodeContentHandler { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct( |
| 36 |
$modelId = CONTENT_MODEL_SCRIBUNTO, $formats = [ CONTENT_FORMAT_TEXT ] |
| 37 |
) { |
| 38 |
parent::__construct( $modelId, $formats ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
protected function getContentClass() { |
| 43 |
return ScribuntoContent::class; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function isSupportedFormat( $format ) { |
| 51 |
|
| 52 |
if ( $format === 'CONTENT_FORMAT_TEXT' ) { |
| 53 |
$format = CONTENT_FORMAT_TEXT; |
| 54 |
} |
| 55 |
return parent::isSupportedFormat( $format ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function canBeUsedOn( Title $title ) { |
| 64 |
if ( $title->getNamespace() !== NS_MODULE ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
return parent::canBeUsedOn( $title ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public function supportsPreloadContent(): bool { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function validateSave( |
| 80 |
Content $content, |
| 81 |
ValidationParams $validationParams |
| 82 |
) { |
| 83 |
'@phan-var ScribuntoContent $content'; |
| 84 |
return $this->validate( $content, $validationParams->getPageIdentity() ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function validate( TextContent $content, PageIdentity $page ) { |
| 95 |
if ( !( $page instanceof Title ) ) { |
| 96 |
$titleFactory = MediaWikiServices::getInstance()->getTitleFactory(); |
| 97 |
$page = $titleFactory->newFromPageIdentity( $page ); |
| 98 |
} |
| 99 |
|
| 100 |
$engine = Scribunto::newDefaultEngine(); |
| 101 |
$engine->setTitle( $page ); |
| 102 |
return $engine->validate( $content->getText(), $page->getPrefixedDBkey() ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected function fillParserOutput( |
| 109 |
Content $content, |
| 110 |
ContentParseParams $cpoParams, |
| 111 |
ParserOutput &$parserOutput |
| 112 |
) { |
| 113 |
'@phan-var ScribuntoContent $content'; |
| 114 |
$page = $cpoParams->getPage(); |
| 115 |
$title = Title::newFromPageReference( $page ); |
| 116 |
$parserOptions = $cpoParams->getParserOptions(); |
| 117 |
$revId = $cpoParams->getRevId(); |
| 118 |
$generateHtml = $cpoParams->getGenerateHtml(); |
| 119 |
$parser = MediaWikiServices::getInstance()->getParserFactory()->getInstance(); |
| 120 |
$sourceCode = $content->getText(); |
| 121 |
$docTitle = Scribunto::getDocPage( $title ); |
| 122 |
$docMsg = $docTitle ? wfMessage( |
| 123 |
$docTitle->exists() ? 'scribunto-doc-page-show' : 'scribunto-doc-page-does-not-exist', |
| 124 |
$docTitle->getPrefixedText() |
| 125 |
)->inContentLanguage() : null; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
$html = ''; |
| 132 |
|
| 133 |
if ( $docMsg && !$docMsg->isDisabled() ) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if ( $parserOptions->getTargetLanguage() === null ) { |
| 147 |
$parserOptions->setTargetLanguage( $docTitle->getPageLanguage() ); |
| 148 |
} |
| 149 |
$parserOutput = $parser->parse( $docMsg->plain(), $page, $parserOptions, true, true, $revId ); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
$html .= $parserOutput->getRawText(); |
| 159 |
} else { |
| 160 |
$parserOutput = new ParserOutput(); |
| 161 |
$parserOutput->setLanguage( $parserOptions->getTargetLanguage() ?? $docTitle->getPageLanguage() ); |
| 162 |
} |
| 163 |
|
| 164 |
if ( $docTitle ) { |
| 165 |
|
| 166 |
|
| 167 |
$parserOutput->addTemplate( $docTitle, $docTitle->getArticleID(), $docTitle->getLatestRevID() ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$status = $this->validate( $content, $title ); |
| 173 |
if ( !$status->isOK() ) { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
$html .= Html::rawElement( 'div', [ 'class' => 'errorbox' ], |
| 182 |
$status->getHTML( 'scribunto-error-short', 'scribunto-error-long' ) |
| 183 |
); |
| 184 |
$trackingCategories = MediaWikiServices::getInstance()->getTrackingCategories(); |
| 185 |
$trackingCategories->addTrackingCategory( $parserOutput, 'scribunto-module-with-errors-category', $page ); |
| 186 |
} |
| 187 |
|
| 188 |
if ( !$generateHtml ) { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
$parserOutput->setRawText( '' ); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
$engine = Scribunto::newDefaultEngine(); |
| 197 |
$engine->setTitle( $title ); |
| 198 |
$codeLang = $engine->getGeSHiLanguage(); |
| 199 |
$html .= $this->highlight( $sourceCode, $parserOutput, $codeLang ); |
| 200 |
|
| 201 |
$parserOutput->setRawText( $html ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
private function highlight( $source, ParserOutput $parserOutput, $codeLang ) { |
| 215 |
$useGeSHi = MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoUseGeSHi' ); |
| 216 |
if ( |
| 217 |
$useGeSHi && $codeLang && ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' ) |
| 218 |
) { |
| 219 |
$status = SyntaxHighlight::highlight( $source, $codeLang, [ 'line' => true, 'linelinks' => 'L' ] ); |
| 220 |
if ( $status->isGood() ) { |
| 221 |
|
| 222 |
|
| 223 |
$parserOutput->addModuleStyles( [ 'ext.pygments' ] ); |
| 224 |
$parserOutput->addModules( [ 'ext.pygments.view' ] ); |
| 225 |
return $status->getValue(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return Html::element( 'pre', [ |
| 230 |
|
| 231 |
'lang' => 'en', |
| 232 |
'dir' => 'ltr', |
| 233 |
'class' => 'mw-code mw-script' |
| 234 |
], "\n$source\n" ); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function makeRedirectContent( Title $target, $text = '' ) { |
| 245 |
return Scribunto::newDefaultEngine()->makeRedirectContent( $target, $text ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function supportsRedirects() { |
| 252 |
return Scribunto::newDefaultEngine()->supportsRedirects(); |
| 253 |
} |
| 254 |
} |