Semantic MediaWiki and related extensions
|
This section explains how to create a new result printer to be used to visualize the result of a #ask
query.
Each result printer is implemented as a derived class from ResultPrinter.php
and will require at least to implement the following 3 methods:
/** * GNU GPL v2+ *
return $definitions; }
/** * This method gets the query result object and is supposed to return * whatever output the format creates. For example, in the list format, it * goes through all results and constructs an HTML list, which is then * returned. Looping through the result object is somewhat complex, and * requires some understanding of the `QueryResult` class. * *
Returns a human readable label for this printer.
/** *
Parameters passed to your result printer can be accessed via the$params
field, which gets set by the base class beforeResultPrinter::getResultText
is called.
For example, if you want to retrieved the value for parameter foobar, use `$this->params['foobar']`. It is '''not''' needed to check if these parameters are set, if they are of the right type, or adhere to any restrictions you might want to put on them. This will already have happened at this point in the base class.
ResultPrinter::getResultText
would not get called.TheResultPrinter::getParamDefinitions
function returns the allowed parameters for a query that uses the specific format. It should return an array of Parameter objects. These define in a declarative fashion which parameters the result printer accepts, what their type is, and their default values. See ParamProcessor for more information about the supported declarations.
/** *
$definitions[] = [ 'name' => 'separator', 'message' => 'smw-paramdesc-separator', 'default' => '', ];
return $definitions; }
Building an output
This is an example from theDsvResultPrinter.php
.
/** * @see ResultPrinter::getResultText * * {@inheritDoc} */ protected function getResultText( QueryResult $queryResult, $outputMode ) { if ( $outputMode !== SMW_OUTPUT_FILE ) { return $this->getDsvLink( $queryResult, $outputMode ); } return $this->buildContents( $queryResult ); } private function buildContents( QueryResult $queryResult ) { $lines = []; // Do not allow backspaces as delimiter, as they'll break stuff. if ( trim( $this->params['separator'] ) != '\\' ) { $this->params['separator'] = trim( $this->params['separator'] ); } /** *