Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
Wikitext | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
preprocess | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Wikitext; |
5 | |
6 | use Wikimedia\Parsoid\Config\Env; |
7 | use Wikimedia\Parsoid\Fragments\PFragment; |
8 | |
9 | /** |
10 | * This class represents core wikitext concepts that are currently represented |
11 | * as methods of Parser.php (in core) OR Parsoid.php (here) or other classes. |
12 | * Care should be taken to have this class represent first-class wikitext |
13 | * concepts and operations and not so much implementation concepts, but that is |
14 | * understandably a hard line to draw. Given that, this suggestion is more of a |
15 | * guideline to help with code hygiene. |
16 | */ |
17 | class Wikitext { |
18 | /** |
19 | * Equivalent of 'preprocess' from Parser.php in core. |
20 | * - expands templates |
21 | * - replaces magic variables |
22 | * |
23 | * Notably, this doesn't support replacing template args from a frame, |
24 | * i.e. the preprocessing here is of *standalone wikitext*, not in |
25 | * reference to something else which is where a frame would be used. |
26 | * |
27 | * This does not run any Parser hooks either, but support for which |
28 | * could eventually be added that is triggered by input options. |
29 | * |
30 | * This also updates resource usage and returns an error if limits |
31 | * are breached. |
32 | * |
33 | * @param Env $env |
34 | * @param string $wt |
35 | * @return array{error:bool,src?:string,fragment?:PFragment} |
36 | * - 'error' did we hit resource limits? |
37 | * - 'src' expanded wikitext OR error message to print |
38 | * FIXME: Maybe error message should be localizable |
39 | * - 'fragment' Optional fragment (wikitext plus strip state) |
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 | $wikitextSize = strlen( $wt ); |
45 | if ( is_string( $ret ) ) { |
46 | // FIXME: Should this bump be len($ret) - len($wt)? |
47 | // I could argue both ways. |
48 | $wikitextSize = strlen( $ret ); |
49 | } |
50 | if ( !$env->bumpWt2HtmlResourceUse( 'wikitextSize', $wikitextSize ) ) { |
51 | return [ |
52 | 'error' => true, |
53 | 'src' => "wt2html: wikitextSize limit exceeded", |
54 | ]; |
55 | } |
56 | |
57 | if ( $env->profiling() ) { |
58 | $profile = $env->getCurrentProfile(); |
59 | $profile->bumpMWTime( "Template", 1000 * ( microtime( true ) - $start ), "api" ); |
60 | $profile->bumpCount( "Template" ); |
61 | } |
62 | |
63 | return is_string( $ret ) ? [ |
64 | 'error' => false, |
65 | 'src' => $ret, |
66 | ] : [ |
67 | 'error' => false, |
68 | 'fragment' => $ret, |
69 | ]; |
70 | } |
71 | } |