Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Wikitext
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 preprocessFragment
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wikitext;
5
6use Wikimedia\Parsoid\Config\Env;
7use Wikimedia\Parsoid\Fragments\PFragment;
8use Wikimedia\Parsoid\Fragments\StripState;
9use Wikimedia\Parsoid\Fragments\WikitextPFragment;
10
11/**
12 * This class represents core wikitext concepts that are currently represented
13 * as methods of Parser.php (in core) OR Parsoid.php (here) or other classes.
14 * Care should be taken to have this class represent first-class wikitext
15 * concepts and operations and not so much implementation concepts, but that is
16 * understandably a hard line to draw. Given that, this suggestion is more of a
17 * guideline to help with code hygiene.
18 */
19class Wikitext {
20    /**
21     * Equivalent of 'preprocess' from Parser.php in core.
22     * - expands templates
23     * - replaces magic variables
24     *
25     * Notably, this doesn't support replacing template args from a frame,
26     * i.e. the preprocessing here is of *standalone wikitext*, not in
27     * reference to something else which is where a frame would be used.
28     *
29     * This does not run any Parser hooks either, but support for which
30     * could eventually be added that is triggered by input options.
31     *
32     * This also updates resource usage and returns an error if limits
33     * are breached.
34     *
35     * @param Env $env
36     * @param PFragment $fragment input wikitext, possibly with embedded
37     *  strip markers.
38     * @param ?bool &$error Set to true if we hit resource limits
39     * @return PFragment Expanded wikitext OR error message to print
40     */
41    public static function preprocessFragment( Env $env, PFragment $fragment, ?bool &$error = null ): PFragment {
42        $error = false;
43        $start = hrtime( true );
44        # $originalSize = strlen( $fragment->asMarkedWikitext( StripState::new() ) );
45        $ret = $env->getDataAccess()->preprocessWikitext( $env->getPageConfig(), $env->getMetadata(), $fragment );
46        if ( is_string( $ret ) ) {
47            $ret = WikitextPFragment::newFromWt( $ret, null );
48        }
49        $wikitextSize = strlen( $ret->asMarkedWikitext( StripState::new() ) );
50        // FIXME: Should this bump be $wikitextSize - $originalSize?
51        // We should try to figure out what core does and match it.
52        if ( !$env->bumpWt2HtmlResourceUse( 'wikitextSize', $wikitextSize ) ) {
53            $error = true;
54            return WikitextPFragment::newFromLiteral(
55                "wt2html: wikitextSize limit exceeded", null
56            );
57        }
58
59        if ( $env->profiling() ) {
60            $profile = $env->getCurrentProfile();
61            $profile->bumpMWTime( "Template", hrtime( true ) - $start, "api" );
62            $profile->bumpCount( "Template" );
63        }
64        return $ret;
65    }
66}