Skip to content
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
84 changes: 79 additions & 5 deletions formats/datatables/DataTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace SRF;

use Html;
use MediaWiki\MediaWikiServices;
use Mediawiki\Title\Title;
use MWException;
use Parser;
use RequestContext;
use SMW\DataValues\PropertyValue;
Expand Down Expand Up @@ -419,9 +422,13 @@
$this->isHTML = true;
$this->hasTemplates = false;

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

$outputMode = ( $this->params['apicall'] !== "apicall" ? SMW_OUTPUT_HTML : $this->outputMode );
if ( $this->params['apicall'] === 'apicall' ) {
$this->initializePrintoutParametersAndParser( $results );

Check warning on line 428 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L428

Added line #L428 was not covered by tests
} else {
$this->parser = $this->copyParser();
}

// Get output from printer:
$result = $this->getResultText( $results, $outputMode );
Expand All @@ -435,6 +442,33 @@
return $result;
}

/**
* @param QueryResult $results
*/
protected function initializePrintoutParametersAndParser( QueryResult $results ) {

Check warning on line 448 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L448

Added line #L448 was not covered by tests
// rebuild $this->printoutsParameters from
// printouts since $this->getPrintouts is not invoked
// alternatively use the $data['printouts'] from the Api
$printRequests = $results->getPrintRequests();
foreach ( $printRequests as $printRequest ) {
$canonicalLabel = $printRequest->getCanonicalLabel();
$this->printoutsParameters[$canonicalLabel] = $printRequest->getParameters();

Check warning on line 455 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L452-L455

Added lines #L452 - L455 were not covered by tests
}

// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/pull/854
// the following ensures that $this->parser->recursiveTagParseFully
// (getCellContent) will work
$context = RequestContext::getMain();
$performer = $context->getUser();
$output = $context->getOutput();

Check warning on line 463 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L461-L463

Added lines #L461 - L463 were not covered by tests

$this->parser = MediaWikiServices::getInstance()->getParserFactory()->getInstance();
$this->parser->setTitle( $output->getTitle() );
$this->parser->setOptions( $output->parserOptions() );
$this->parser->setOutputType( Parser::OT_HTML );
$this->parser->clearState();

Check warning on line 469 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L465-L469

Added lines #L465 - L469 were not covered by tests
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -462,6 +496,8 @@

// Apply intro parameter
if ( ( $this->mIntro ) && ( $results->getCount() > 0 ) ) {
// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/issues/853
// $result = $this->parser->recursiveTagParseFully( $this->mIntro ) . $result;
if ( $outputmode == SMW_OUTPUT_HTML && $this->isHTML ) {
$result = Message::get( [ 'smw-parse', $this->mIntro ], Message::PARSE ) . $result;
} elseif ( $outputmode !== SMW_OUTPUT_RAW ) {
Expand All @@ -471,6 +507,8 @@

// Apply outro parameter
if ( ( $this->mOutro ) && ( $results->getCount() > 0 ) ) {
// @see https://github.com/SemanticMediaWiki/SemanticResultFormats/issues/853
// $result = $result . $this->parser->recursiveTagParseFully( $this->mOutro );
if ( $outputmode == SMW_OUTPUT_HTML && $this->isHTML ) {
$result = $result . Message::get( [ 'smw-parse', $this->mOutro ], Message::PARSE );
} elseif ( $outputmode !== SMW_OUTPUT_RAW ) {
Expand Down Expand Up @@ -931,9 +969,13 @@
}

if ( $template ) {
// escape pipe character
$value_ = str_replace( '|', '|', (string)$value );
$value = $this->parser->recursiveTagParseFully( '{{' . $template . '|' . $value_ . '}}' );
// @fixme use named parameter ?
$titleTemplate = Title::makeTitle( NS_TEMPLATE,
Title::capitalize( $template, NS_TEMPLATE ) );
$value_ = $this->expandTemplate( $titleTemplate, [ 1 => $value ] );
$value = Parser::stripOuterParagraph(
$this->parser->recursiveTagParseFully( $value_ )
);

Check warning on line 978 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L973-L978

Added lines #L973 - L978 were not covered by tests
}

$values[] = $value === '' ? ' ' : $value;
Expand Down Expand Up @@ -966,6 +1008,38 @@
];
}

/**
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/VisualData/+/refs/heads/master/includes/classes/ResultPrinter.php
* @param Title|Mediawiki\Title\Title $title
* @param array $args
* @return array
*/
public function expandTemplate( $title, $args ) {
$titleText = $title->getText();
$frame = $this->parser->getPreprocessor()->newFrame();

Check warning on line 1019 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1017-L1019

Added lines #L1017 - L1019 were not covered by tests

if ( $frame->depth >= $this->parser->getOptions()->getMaxTemplateDepth() ) {
throw new MWException( 'expandTemplate: template depth limit exceeded' );

Check warning on line 1022 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1021-L1022

Added lines #L1021 - L1022 were not covered by tests
}

if ( MediaWikiServices::getInstance()->getNamespaceInfo()->isNonincludable( $title->getNamespace() ) ) {
throw new MWException( 'expandTemplate: template inclusion denied' );

Check warning on line 1026 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1025-L1026

Added lines #L1025 - L1026 were not covered by tests
}

[ $dom, $finalTitle ] = $this->parser->getTemplateDom( $title );
if ( $dom === false ) {
throw new MWException( "expandTemplate: template \"$titleText\" does not exist" );

Check warning on line 1031 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1029-L1031

Added lines #L1029 - L1031 were not covered by tests
}

if ( !$frame->loopCheck( $finalTitle ) ) {
throw new MWException( 'expandTemplate: template loop detected' );

Check warning on line 1035 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1034-L1035

Added lines #L1034 - L1035 were not covered by tests
}

$fargs = $this->parser->getPreprocessor()->newPartNodeArray( $args );
$newFrame = $frame->newChild( $fargs, $finalTitle );
return $newFrame->expand( $dom );

Check warning on line 1040 in formats/datatables/DataTables.php

View check run for this annotation

Codecov / codecov/patch

formats/datatables/DataTables.php#L1038-L1040

Added lines #L1038 - L1040 were not covered by tests
}

/**
* {@inheritDoc}
*/
Expand Down