| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Wikitext; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Config\Env; |
| 7 |
use Wikimedia\Parsoid\Tokens\Token; |
| 8 |
use Wikimedia\Parsoid\Wt2Html\PegTokenizer; |
| 9 |
|
| 10 |
/** |
| 11 |
* This class represents core wikitext concepts that are currently represented |
| 12 |
* as methods of Parser.php (in core) OR Parsoid.php (here) or other classes. |
| 13 |
* Care should be taken to have this class represent first-class wikitext |
| 14 |
* concepts and operations and not so much implementation concepts, but that is |
| 15 |
* understandably a hard line to draw. Given that, this suggestion is more of a |
| 16 |
* guideline to help with code hygiene. |
| 17 |
*/ |
| 18 |
class Wikitext { |
| 19 |
/** |
| 20 |
* Equivalent of 'preprocess' from Parser.php in core. |
| 21 |
* - expands templates |
| 22 |
* - replaces magic variables |
| 23 |
* |
| 24 |
* Notably, this doesn't support replacing template args from a frame, |
| 25 |
* i.e. the preprocessing here is of *standalone wikitext*, not in |
| 26 |
* reference to something else which is where a frame would be used. |
| 27 |
* |
| 28 |
* This does not run any Parser hooks either, but support for which |
| 29 |
* could eventually be added that is triggered by input options. |
| 30 |
* |
| 31 |
* This also updates resource usage and returns an error if limits |
| 32 |
* are breached. |
| 33 |
* |
| 34 |
* @param Env $env |
| 35 |
* @param string $wt |
| 36 |
* @return array |
| 37 |
* - 'error' did we hit resource limits? |
| 38 |
* - 'src' expanded wikitext OR error message to print |
| 39 |
* FIXME: Maybe error message should be localizable |
| 40 |
*/ |
| 41 |
public static function preprocess( Env $env, string $wt ): array { |
| 42 |
$start = microtime( true ); |
| 43 |
$ret = $env->getDataAccess()->preprocessWikitext( $env->getPageConfig(), $env->getMetadata(), $wt ); |
| 44 |
|
| 45 |
// FIXME: Should this bump be len($ret) - len($wt)? |
| 46 |
// I could argue both ways. |
| 47 |
if ( !$env->bumpWt2HtmlResourceUse( 'wikitextSize', strlen( $ret ) ) ) { |
| 48 |
return [ |
| 49 |
'error' => true, |
| 50 |
'src' => "wt2html: wikitextSize limit exceeded", |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
if ( $env->profiling() ) { |
| 55 |
$profile = $env->getCurrentProfile(); |
| 56 |
$profile->bumpMWTime( "Template", 1000 * ( microtime( true ) - $start ), "api" ); |
| 57 |
$profile->bumpCount( "Template" ); |
| 58 |
} |
| 59 |
|
| 60 |
return [ |
| 61 |
'error' => false, |
| 62 |
'src' => $ret, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Perform pre-save transformations |
| 68 |
* |
| 69 |
* @param Env $env |
| 70 |
* @param string $wt |
| 71 |
* @param bool $substTLTemplates Prefix each top-level template with 'subst' |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public static function pst( Env $env, string $wt, bool $substTLTemplates = false ) { |
| 75 |
if ( $substTLTemplates ) { |
| 76 |
// To make sure we do this for the correct templates, tokenize the |
| 77 |
// starting wikitext and use that to detect top-level templates. |
| 78 |
// Then, substitute each starting '{{' with '{{subst' using the |
| 79 |
// template token's tsr. |
| 80 |
$tokenizer = new PegTokenizer( $env ); |
| 81 |
$tokens = $tokenizer->tokenizeSync( $wt, [ 'sol' => true ] ); |
| 82 |
$tsrIncr = 0; |
| 83 |
foreach ( $tokens as $token ) { |
| 84 |
/** @var Token $token */ |
| 85 |
if ( $token->getName() === 'template' ) { |
| 86 |
$tsr = $token->dataParsoid->tsr; |
| 87 |
$wt = substr( $wt, 0, $tsr->start + $tsrIncr ) |
| 88 |
. '{{subst:' . substr( $wt, $tsr->start + $tsrIncr + 2 ); |
| 89 |
$tsrIncr += 6; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $env->getDataAccess()->doPst( $env->getPageConfig(), $wt ); |
| 94 |
} |
| 95 |
} |