Semantic MediaWiki and related extensions
boilerplate

This document contains an example for a ResultPrinter both for the PHP and JavaScript part and before diving into the details, please make sure you have read https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/architecture/writing.resultprinter.md ""Writing a result printer"".

PHP

namespace SMW;
use SMWQueryResult as QueryResult;
use SMWDataItem as DataItem;
use SMWDataValue as DataValue;
use Html;
/**
 * Boilerplate query printer
 *
 * Add your description here ...
 *
 *  GNU GPL v2+
 * 
Since
3.0 * *
Author
mwjames */ class BoilerplateResultPrinter extends ResultPrinter {
/**
 * @see ResultPrinter::getName
 *
 * {@inheritDoc}
 */
public function getName() {
    // Add your result printer name here
    return wfMessage( 'foo-boilerplate' )->text();
}

/**
 * @see ResultPrinter::getParamDefinitions
 *
 * {@inheritDoc}
 */
public function getParamDefinitions( array $definitions ) {
    $definitions = parent::getParamDefinitions( $definitions );

    // Add your parameters here

    // Example of a unit parameter
    $definitions['unit'] = [
        'message' => 'foo-paramdesc-unit',
        'default' => '',
    ];

    return $definitions;
}

/**
 * @see ResultPrinter::getResources
 *
 * {@inheritDoc}
 */
protected function getResources() {

    // Add resource definitions that has been registered with `Resource.php`
    // Resource definitions contain scripts, styles, messages etc.

    return [
        'modules' => [
            'foo.boilerplate'
        ],
        'styles' => [
            'foo.boilerplate.styles'
        ]
    ];
}

/**
 * @see ResultPrinter::getResultText
 *
 * {@inheritDoc}
 */
protected function getResultText( QueryResult $queryResult, $outputMode ) {

    // Data processing
    // It is advisable to separate data processing from output logic
    $data = $this->preprocess( $queryResult, $outputMode );

    // Check if the data processing returned any results otherwise just bailout
    if ( $data === [] ) {
        // Add an error message to return method
        return $queryResult->addErrors( 'some-error' );
    } else {
        // Add options if needed to format the output

        // $outputMode can be specified as
        // SMW_OUTPUT_HTML
        // SMW_OUTPUT_FILE
        // SMW_OUTPUT_WIKI

        // For implementing template support this options has to be set but if you
        // manipulate data via jQuery/JavaScript it is less likely that you need
        // this option since templates will influence how wiki text is parsed
        // but will have no influence in how a HTML representation is altered
        // $this->hasTemplates = true;

        $options = [
            'mode' => $outputMode
        ];

        // Return formatted results
        return $this->buildHTML( $data, $options );
    }
}

/**
 * Returns an array with data
 *
 * @return array
 */
private function preprocess( QueryResult $queryResult, $outputMode ) {

    $data = [];

    // This is an example implementation on how to select available data from
    // a result set. Please make appropriate adoptions necessary for your
    // application.

    // Some methods are declared as private to show case which objects are
    // directly accessible within SMWQueryResult

    // Get all SMWDIWikiPage objects that make up the results
    // $subjects = $this->getSubjects( $queryResult->getResults() );

    // Get all print requests property labels
    // $labels = $this->getLabels( $queryResult->getPrintRequests() );

    /**
     * Get all values for all rows that belong to the result set
     *
     * 

About | General disclaimer | Privacy policy