Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLATFORM-8725 | Use recursive parser calls to preserve template dom cache #7

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions includes/LST.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ public static function includeTemplate( $parser, Lister $lister, $dplNr, $articl
}
} else {
// put a red link into the output
$output[0] = $parser->preprocess( '{{' . $defaultTemplate . '|%PAGE%=' . $page . '|%TITLE%=' . $title->getText() . '|%DATE%=' . $date . '|%USER%=' . $user . '}}', $parser->getPage(), $parser->getOptions() );
$output[0] = self::callParserPreprocess( $parser, '{{' . $defaultTemplate . '|%PAGE%=' . $page . '|%TITLE%=' . $title->getText() . '|%DATE%=' . $date . '|%USER%=' . $user . '}}', $parser->getPage(), $parser->getOptions() );
}

unset( $title );
Expand Down Expand Up @@ -754,7 +754,7 @@ public static function includeTemplate( $parser, Lister $lister, $dplNr, $articl
}

$argChain .= '|%DATE%=' . $date . '|%USER%=' . $user . '|%ARGS%=' . str_replace( '|', '§', preg_replace( '/[}]+/', '}', preg_replace( '/[{]+/', '{', substr( $invocation, strlen( $template2 ) + 2 ) ) ) ) . '}}';
$output[++$n] = $parser->preprocess( $argChain, $parser->getPage(), $parser->getOptions() );
$output[++$n] = self::callParserPreprocess( $parser, $argChain, $parser->getPage(), $parser->getOptions() );
}
break;
}
Expand Down Expand Up @@ -888,4 +888,39 @@ public static function spaceOrUnderscore( $pattern ) {
// returns a pettern that matches underscores as well as spaces
return str_replace( ' ', '[ _]', $pattern );
}

/**
wladekb marked this conversation as resolved.
Show resolved Hide resolved
* Preprocess given text according to the globally-configured method
*
* The default method uses Parser::preprocess() which does the job, but clears the internal cache every time.
* The improved method uses Parser::recursivePreprocess() that saves a decent amount of processing time
* by preserving the internal cache leveraging the repetitive call pattern.
*
* Parser::preprocess() was mainly called from LST::includeTemplate() for the same template(s) with different
* set of arguments for each article found. In the original implementation using Parser::preprocess(),
* the internal cache is cleared at each call and parsing the same template text into template DOM is repeated
* multiple times.
*
* Using Parser::recursivePreprocess() prevents the cache clear, and thus repetitive calls reuse the
* previously generated template DOM which brings a decent performance improvement when called multiple times.
*
* @see https://fandom.atlassian.net/browse/PLATFORM-8725
*
* @param Parser $parser
* @param string $text
* @param ?\MediaWiki\Page\PageReference $page
* @param \ParserOptions $options
* @return string
*/
protected static function callParserPreprocess( Parser $parser, $text, $page, $options ): string {
global $wgDplUseRecursivePreprocess;
if ( $wgDplUseRecursivePreprocess ) {
$parser->setOutputType( OT_PREPROCESS );
$text = $parser->recursivePreprocess( $text );

return $text;
} else {
return $parser->preprocess( $text, $page, $options );
}
}
}