Skip to content

Commit 4111e65

Browse files
Datatables fix template on ajax navigation (#931)
* add initializePrintoutParametersAndParser function * remove unused vars * fix phpcs * fix expand template * fix sort statements
1 parent 4782e36 commit 4111e65

1 file changed

Lines changed: 79 additions & 5 deletions

File tree

formats/datatables/DataTables.php

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
namespace SRF;
1414

1515
use Html;
16+
use MediaWiki\MediaWikiServices;
17+
use Mediawiki\Title\Title;
18+
use MWException;
1619
use Parser;
1720
use RequestContext;
1821
use SMW\DataValues\PropertyValue;
@@ -419,9 +422,13 @@ protected function buildResult( QueryResult $results ): array {
419422
$this->isHTML = true;
420423
$this->hasTemplates = false;
421424

422-
$this->parser = $this->copyParser();
425+
$outputMode = ( $this->params['apicall'] !== 'apicall' ? SMW_OUTPUT_HTML : $this->outputMode );
423426

424-
$outputMode = ( $this->params['apicall'] !== "apicall" ? SMW_OUTPUT_HTML : $this->outputMode );
427+
if ( $this->params['apicall'] === 'apicall' ) {
428+
$this->initializePrintoutParametersAndParser( $results );
429+
} else {
430+
$this->parser = $this->copyParser();
431+
}
425432

426433
// Get output from printer:
427434
$result = $this->getResultText( $results, $outputMode );
@@ -435,6 +442,33 @@ protected function buildResult( QueryResult $results ): array {
435442
return $result;
436443
}
437444

445+
/**
446+
* @param QueryResult $results
447+
*/
448+
protected function initializePrintoutParametersAndParser( QueryResult $results ) {
449+
// rebuild $this->printoutsParameters from
450+
// printouts since $this->getPrintouts is not invoked
451+
// alternatively use the $data['printouts'] from the Api
452+
$printRequests = $results->getPrintRequests();
453+
foreach ( $printRequests as $printRequest ) {
454+
$canonicalLabel = $printRequest->getCanonicalLabel();
455+
$this->printoutsParameters[$canonicalLabel] = $printRequest->getParameters();
456+
}
457+
458+
// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/pull/854
459+
// the following ensures that $this->parser->recursiveTagParseFully
460+
// (getCellContent) will work
461+
$context = RequestContext::getMain();
462+
$performer = $context->getUser();
463+
$output = $context->getOutput();
464+
465+
$this->parser = MediaWikiServices::getInstance()->getParserFactory()->getInstance();
466+
$this->parser->setTitle( $output->getTitle() );
467+
$this->parser->setOptions( $output->parserOptions() );
468+
$this->parser->setOutputType( Parser::OT_HTML );
469+
$this->parser->clearState();
470+
}
471+
438472
/**
439473
* {@inheritDoc}
440474
*/
@@ -462,6 +496,8 @@ protected function handleNonFileResult( $result, QueryResult $results, $outputmo
462496

463497
// Apply intro parameter
464498
if ( ( $this->mIntro ) && ( $results->getCount() > 0 ) ) {
499+
// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/issues/853
500+
// $result = $this->parser->recursiveTagParseFully( $this->mIntro ) . $result;
465501
if ( $outputmode == SMW_OUTPUT_HTML && $this->isHTML ) {
466502
$result = Message::get( [ 'smw-parse', $this->mIntro ], Message::PARSE ) . $result;
467503
} elseif ( $outputmode !== SMW_OUTPUT_RAW ) {
@@ -471,6 +507,8 @@ protected function handleNonFileResult( $result, QueryResult $results, $outputmo
471507

472508
// Apply outro parameter
473509
if ( ( $this->mOutro ) && ( $results->getCount() > 0 ) ) {
510+
// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/issues/853
511+
// $result = $result . $this->parser->recursiveTagParseFully( $this->mOutro );
474512
if ( $outputmode == SMW_OUTPUT_HTML && $this->isHTML ) {
475513
$result = $result . Message::get( [ 'smw-parse', $this->mOutro ], Message::PARSE );
476514
} elseif ( $outputmode !== SMW_OUTPUT_RAW ) {
@@ -931,9 +969,13 @@ public function getCellContent(
931969
}
932970

933971
if ( $template ) {
934-
// escape pipe character
935-
$value_ = str_replace( '|', '|', (string)$value );
936-
$value = $this->parser->recursiveTagParseFully( '{{' . $template . '|' . $value_ . '}}' );
972+
// @fixme use named parameter ?
973+
$titleTemplate = Title::makeTitle( NS_TEMPLATE,
974+
Title::capitalize( $template, NS_TEMPLATE ) );
975+
$value_ = $this->expandTemplate( $titleTemplate, [ 1 => $value ] );
976+
$value = Parser::stripOuterParagraph(
977+
$this->parser->recursiveTagParseFully( $value_ )
978+
);
937979
}
938980

939981
$values[] = $value === '' ? ' ' : $value;
@@ -966,6 +1008,38 @@ public function getCellContent(
9661008
];
9671009
}
9681010

1011+
/**
1012+
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/VisualData/+/refs/heads/master/includes/classes/ResultPrinter.php
1013+
* @param Title|Mediawiki\Title\Title $title
1014+
* @param array $args
1015+
* @return array
1016+
*/
1017+
public function expandTemplate( $title, $args ) {
1018+
$titleText = $title->getText();
1019+
$frame = $this->parser->getPreprocessor()->newFrame();
1020+
1021+
if ( $frame->depth >= $this->parser->getOptions()->getMaxTemplateDepth() ) {
1022+
throw new MWException( 'expandTemplate: template depth limit exceeded' );
1023+
}
1024+
1025+
if ( MediaWikiServices::getInstance()->getNamespaceInfo()->isNonincludable( $title->getNamespace() ) ) {
1026+
throw new MWException( 'expandTemplate: template inclusion denied' );
1027+
}
1028+
1029+
[ $dom, $finalTitle ] = $this->parser->getTemplateDom( $title );
1030+
if ( $dom === false ) {
1031+
throw new MWException( "expandTemplate: template \"$titleText\" does not exist" );
1032+
}
1033+
1034+
if ( !$frame->loopCheck( $finalTitle ) ) {
1035+
throw new MWException( 'expandTemplate: template loop detected' );
1036+
}
1037+
1038+
$fargs = $this->parser->getPreprocessor()->newPartNodeArray( $args );
1039+
$newFrame = $frame->newChild( $fargs, $finalTitle );
1040+
return $newFrame->expand( $dom );
1041+
}
1042+
9691043
/**
9701044
* {@inheritDoc}
9711045
*/

0 commit comments

Comments
 (0)