summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php')
-rw-r--r--www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php93
1 files changed, 93 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php b/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php
new file mode 100644
index 00000000..990cd0ee
--- /dev/null
+++ b/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Query.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace SMW\MediaWiki\Api;
+
+use ApiBase;
+use SMW\ApplicationFactory;
+use SMWQuery;
+use SMWQueryProcessor;
+use SMWQueryResult;
+
+/**
+ * Base for API modules that query SMW
+ *
+ * @ingroup Api
+ *
+ * @license GNU GPL v2+
+ * @since 1.9
+ *
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ * @author mwjames
+ */
+abstract class Query extends ApiBase {
+
+ /**
+ * Returns a query object for the provided query string and list of printouts.
+ *
+ * @since 1.6.2
+ *
+ * @param string $queryString
+ * @param array $printouts
+ * @param array $parameters
+ *
+ * @return SMWQuery
+ */
+ protected function getQuery( $queryString, array $printouts, array $parameters = [] ) {
+
+ SMWQueryProcessor::addThisPrintout( $printouts, $parameters );
+
+ $query = SMWQueryProcessor::createQuery(
+ $queryString,
+ SMWQueryProcessor::getProcessedParams( $parameters, $printouts ),
+ SMWQueryProcessor::SPECIAL_PAGE,
+ '',
+ $printouts
+ );
+
+ $query->setOption( SMWQuery::PROC_CONTEXT, 'API' );
+
+ return $query;
+ }
+
+ /**
+ * Run the actual query and return the result.
+ *
+ * @since 1.6.2
+ *
+ * @param SMWQuery $query
+ *
+ * @return SMWQueryResult
+ */
+ protected function getQueryResult( SMWQuery $query ) {
+ return ApplicationFactory::getInstance()->getStore()->getQueryResult( $query );
+ }
+
+ /**
+ * Add the query result to the API output.
+ *
+ * @since 1.6.2
+ *
+ * @param SMWQueryResult $queryResult
+ */
+ protected function addQueryResult( SMWQueryResult $queryResult, $outputFormat = 'json' ) {
+
+ $result = $this->getResult();
+
+ $resultFormatter = new ApiQueryResultFormatter( $queryResult );
+ $resultFormatter->setIsRawMode( ( strpos( strtolower( $outputFormat ), 'xml' ) !== false ) );
+ $resultFormatter->doFormat();
+
+ if ( $resultFormatter->getContinueOffset() ) {
+ // $result->disableSizeCheck();
+ $result->addValue( null, 'query-continue-offset', $resultFormatter->getContinueOffset() );
+ // $result->enableSizeCheck();
+ }
+
+ $result->addValue(
+ null,
+ $resultFormatter->getType(),
+ $resultFormatter->getResult()
+ );
+ }
+
+}