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

fix(PLATFORM-10832): remove dead code to fix reflection exception #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"Fandom\\UsingData\\": "src"
},
"Hooks": {
"BeforeParserFetchTemplateRevisionRecord": "main",
"GetMagicVariableIDs": "main",
"ParserFirstCallInit": "main",
"ParserGetVariableValueSwitch": "main"
Expand Down
67 changes: 2 additions & 65 deletions src/UsingDataHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,27 @@

namespace Fandom\UsingData;

use MediaWiki\Hook\BeforeParserFetchTemplateRevisionRecordHook;
use MediaWiki\Hook\GetMagicVariableIDsHook;
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\ParserGetVariableValueSwitchHook;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\PPFrame;
use MediaWiki\Parser\PPNode;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFactory;
use ReflectionProperty;

/**
* Registers and defines parser functions for UsingData.
*/
class UsingDataHooks implements
ParserFirstCallInitHook,
GetMagicVariableIDsHook,
ParserGetVariableValueSwitchHook,
BeforeParserFetchTemplateRevisionRecordHook
ParserGetVariableValueSwitchHook
{
/** @var UsingDataPPFrameDOM[] Data frames for each page */
private array $dataFrames = [];

/** @var bool Whether we are currently searching for data */
private bool $isInDataSearchMode = false;

public function __construct(
private readonly TitleFactory $titleFactory,
private readonly NamespaceInfo $namespaceInfo,
Expand Down Expand Up @@ -69,18 +60,6 @@ public function onParserGetVariableValueSwitch(
}
}

public function onBeforeParserFetchTemplateRevisionRecord(
?LinkTarget $contextTitle, LinkTarget $title,
bool &$skip, ?RevisionRecord &$revRecord
): bool {
if ( $this->isInDataSearchMode ) {
$skip = true;
return false;
}

return true;
}

/**
* Handles {{ANCESTORNAME:depth}}
*/
Expand Down Expand Up @@ -117,48 +96,17 @@ private function getDataFrame(
if ( ( $sourcePage != '' && $sourcePage != $parsingTitle?->getPrefixedText() )
|| $parser->getOptions()->getIsSectionPreview()
) {
$text = null;
if ( $title ) {
[ $text, $title ] = $parser->fetchTemplateAndTitle( $title );
}
if ( $title && $title->getPrefixedText() != $sourcePage ) {
$this->dataFrames[$title->getPrefixedText()] = $this->dataFrames[$sourcePage];
}
if ( $text !== null ) {
$this->makeDataParserAndRun( $parser,
static function ( Parser $dataParser ) use ( $text, $title, $parser ) {
$dataParser->preprocess( $text, $title, clone $parser->getOptions() );
$parser->mPPNodeCount += $dataParser->mPPNodeCount;
}
);
}
}
}
return $this->dataFrames[$sourcePage];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be double-checked: Based on REL1_39 code

private function getDataFrame( $sourcePage, $title, &$parser, $frame ) {
global $wgHooks;
if ( !isset( $this->dataFrames[$sourcePage] ) ) {
$this->dataFrames[$sourcePage] = new UsingDataPPFrameDOM( $frame, $sourcePage );
if ( $sourcePage != ''
&& ( $sourcePage != $parser->getTitle()->getPrefixedText() )
|| $parser->getOptions()->getIsSectionPreview() ) {
[ $text, $fTitle ] = $parser->fetchTemplateAndTitle( $title );
if ( is_object( $fTitle ) && $fTitle->getPrefixedText() != $sourcePage ) {
$this->dataFrames[$fTitle->getPrefixedText()] = $this->dataFrames[$sourcePage];
}
if ( is_string( $text ) && $text != '' ) {
$this->searchingForData = true;
$clearStateHooks = $wgHooks['ParserClearState'];
// Other extensions tend to assume the hook is only called by wgParser and reset internal state
$wgHooks['ParserClearState'] = [];
$subParser = clone $parser;
$subParser->preprocess( $text, $fTitle, clone $parser->getOptions() );
// We might've blocked access to templates while preprocessing; should not be cached
$subParser->clearState();
if ( $parser->getOutput()->hasText() ) {
$subParser->getOutput()->setText( $parser->getOutput()->getText() );
}
$wgHooks['ParserClearState'] = empty( $wgHooks['ParserClearState'] )
? $clearStateHooks
: array_merge( $clearStateHooks, $wgHooks['ParserClearState'] );
$parser->mPPNodeCount += $subParser->mPPNodeCount;
$this->searchingForData = false;
}
}
}
return $this->dataFrames[$sourcePage];
}

Basically, this part could be removed https://github.com/Wikia/usingdata/blob/ee996924d65b057abce474d4b03e56f74ea0f977/src/UsingDataHooks.php#L71C5-L88C6

// This flag is restored at the end
					$this->searchingForData = true;
// Preserve ParserClearState hook handlers to be restored at the end
					$clearStateHooks = $wgHooks['ParserClearState'];
					// Other extensions tend to assume the hook is only called by wgParser and reset internal state
// Disable ParserClearState hook
					$wgHooks['ParserClearState'] = [];
// Cloning original parser state
					$subParser = clone $parser;
// We are working on clone and parameters are not passed as reference. No side-effects except some hooks being called. Hooks registered in UsingData doesn't preserve it's state outside affected parser
					$subParser->preprocess( $text, $fTitle, clone $parser->getOptions() );
					// We might've blocked access to templates while preprocessing; should not be cached
// Clearing state will set internal `mPPNodeCount` to `0`
					$subParser->clearState();
					if ( $parser->getOutput()->hasText() ) {
// $subParser is not used after that assignment
						$subParser->getOutput()->setText( $parser->getOutput()->getText() );
					}
// Restore original ParserClearState hook handlers
					$wgHooks['ParserClearState'] = empty( $wgHooks['ParserClearState'] )
						? $clearStateHooks
						: array_merge( $clearStateHooks, $wgHooks['ParserClearState'] );
// When calling $subParser->clearState(); mPPNodeCount is set to 0
					$parser->mPPNodeCount += $subParser->mPPNodeCount;
// Restore flag value
					$this->searchingForData = false;

}

private function makeDataParserAndRun( Parser $parser, callable $callback ): void {
$hookRunnerProperty = new ReflectionProperty( $parser, 'hookRunner' );
$originalHookRunner = $hookRunnerProperty->getValue( $parser );

$hookContainerProperty = new ReflectionProperty( $originalHookRunner, 'container' );
$hookContainer = $hookContainerProperty->getValue( $originalHookRunner );

$newHookRunner = new class ( $hookContainer ) extends HookRunner {
public function onParserClearState( $parser ): bool {
return true;
}
};

try {
$dataParser = clone $parser;
$hookRunnerProperty->setValue( $dataParser, $newHookRunner );
$callback( $dataParser );
} finally {
$this->isInDataSearchMode = false;
}
}

/**
* {{#using:Page#Hash|Template|Default|...}} parses Template using #data from Page's Hash fragment; or Default
* if no data from Page can be found. Named arguments override those in the #data tag.
Expand Down Expand Up @@ -203,10 +151,6 @@ public function renderFunctionUsingArg( Parser $parser, PPFrame $frame, array $a
* Parses common elements of #using syntax.
*/
private function parseUsingCommons( Parser $parser, PPFrame $frame, array $args ): ?array {
if ( $this->isInDataSearchMode ) {
return null;
}

$source = trim( $frame->expand( $args[0] ) );
if ( str_contains( $source, '%' ) ) {
$source = str_replace( [ '<', '>' ], [ '&lt;', '&gt;' ], urldecode( $source ) );
Expand Down Expand Up @@ -246,10 +190,6 @@ private function parseUsingCommons( Parser $parser, PPFrame $frame, array $args
public function renderTagUsing(
string $text, array $args, Parser $parser, PPFrame $frame
): array {
if ( $this->isInDataSearchMode ) {
return [ '', 'markerType' => 'none' ];
}

$source = isset( $args['page'] ) ? $parser->replaceVariables( $args['page'], $frame ) : '';
unset( $args['page'] );
if ( str_contains( $source, '%' ) ) {
Expand Down Expand Up @@ -300,12 +240,9 @@ public function renderFunctionData( Parser $parser, PPFrame $frame, array $args
$fragment = substr( $templateName, 1 );
}

if ( $frame->depth == 0 || $this->isInDataSearchMode ) {
if ( $frame->depth == 0 ) {
$this->dataFrames[$hostPage] ??= new UsingDataPPFrameDOM( $frame, $hostPage );
$this->dataFrames[$hostPage]->addArgs( $frame, $args, $fragment );
if ( $this->isInDataSearchMode ) {
return '';
}
}
if ( !is_object( $templateTitle ) ) {
return '';
Expand Down