summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/Deferred.php
blob: 23ed138285fc395640e291d2683d1de677e2762c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

namespace SMW\Query;

use Html;
use ParserOutput;
use SMWQuery as Query;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class Deferred {

	/**
	 * Identifies the showMode
	 */
	const SHOW_MODE = 'dq.showmode';

	/**
	 * Identifies unparsed parameters
	 */
	const QUERY_PARAMETERS = 'dq.parameters';

	/**
	 * Identifies the @control element
	 */
	const CONTROL_ELEMENT = 'dq.control';

	/**
	 * @since 3.0
	 *
	 * @param ParserOutput $parserOutput
	 */
	public static function registerResources( ParserOutput $parserOutput ) {
		$parserOutput->addModuleStyles( 'ext.smw.deferred.styles' );
		$parserOutput->addModules( 'ext.smw.deferred' );
	}

	/**
	 * @since 3.0
	 *
	 * @param Query $query
	 *
	 * @return string
	 */
	public static function buildHTML( Query $query ) {

		$isShowMode = $query->getOption( self::SHOW_MODE );
		$params = $query->getOption( 'query.params' );

		// Ensures that a generated string can appear next to another text
		$element = $isShowMode ? 'span' : 'div';

		$result = Html::rawElement(
			$element,
			[
				'class' => 'smw-deferred-query' . ( isset( $params['class'] ) ? ' ' . $params['class'] : '' ),
				'data-query'  => json_encode(
					[
						'query'  => trim( $query->getOption( self::QUERY_PARAMETERS ) ),
						'params' => $params,
						'limit'  => $query->getLimit(),
						'offset' => $query->getOffset(),
						'max'    => $GLOBALS['smwgQMaxInlineLimit'],
						'cmd'    => $isShowMode ? 'show' : 'ask'
					]
				)
			],
			Html::rawElement(
				$element,
				[
					'id' => 'deferred-control',
					'data-control' => $isShowMode ? '' : $query->getOption( self::CONTROL_ELEMENT )
				]
			) . Html::rawElement(
				$element,
				[
					'id' => 'deferred-output',
					'class' => 'smw-loading-image-dots'
				]
			)
		);

		return $result;
	}

}