Semantic MediaWiki and related extensions
writing

This section explains how to create a new result printer to be used to visualize the result of a #ask query.

Creating a printer class

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+
 * 
Since
3.0 * *
Author
... */ class FooResultPrinter extends ResultPrinter {
/** * Output a human readable label for this printer. * *
See also
ResultPrinter::getName * * */ public function getName() { return $this->msg( 'message-key-for-this-name' ); }
/** * Defines the list of available parameters to an individual result * printer. * *
See also
ResultPrinter::getParamDefinitions * * */ public function getParamDefinitions( array $definitions ) { $definitions = parent::getParamDefinitions( $definitions );
$definitions[] = [ 'name' => 'foo', 'message' => 'smw-paramdesc-foo', 'default' => '', ];
        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.
     *
     * 
See also
ResultPrinter::getResultText * * */ protected function getResultText( QueryResult $queryResult, $outputMode ) { return ''; } }

Returning a name

Returns a human readable label for this printer.
    /**
     * 
See also
ResultPrinter::getName * * */ public function getName() { return $this->msg( 'message-key-for-this-name' ); }

Handling parameters

Parameters passed to your result printer can be accessed via the $paramsfield, which gets set by the base class before ResultPrinter::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.
  • Invalid and non-set values will have been changed to their default.
  • Invalid or missing required parameters would have caused an abort earlier on, so ResultPrinter::getResultText would not get called.
  • When outputting any of these values, you will have to escape them using the core MediaWiki escaping functionality for security reasons.
The ResultPrinter::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.
    /**
     * 
See also
ResultPrinter::getParamDefinitions * * */ public function getParamDefinitions( array $definitions ) {
// You should always get the params added by the parent class, // using the parent. $definitions = parent::getParamDefinitions( $definitions );
        $definitions[] = [
            'name' => 'separator',
            'message' => 'smw-paramdesc-separator',
            'default' => '',
        ];
        return $definitions;
    }

Building an output

This is an example from the DsvResultPrinter.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'] );
    }

    /**
     * 

About | General disclaimer | Privacy policy