summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/queryprinters/RawResultPrinter.php
blob: 1e975162169197e682ca010ef69cf9fde517228e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

namespace SMW;

use FormatJson;
use Html;
use SMWOutputs;
use SMWQueryResult;

/**
 * Base class for result printers that use the serialized results
 *
 * @since 1.9
 *
 * @license GNU GPL v2 or later
 * @author mwjames
 */
abstract class RawResultPrinter extends ResultPrinter {

	/**
	 * Returns html output.
	 *
	 * @since 1.9
	 */
	abstract protected function getHtml( array $data );

	/**
	 * Convenience method to register resources
	 *
	 * @since 1.9
	 *
	 * @param string $resource
	 */
	protected function addResources( $resource ) {
		SMWOutputs::requireResource( $resource );
	}

	/**
	 * Convenience method to create a unique id
	 *
	 * @since 1.9
	 */
	protected function getId( ) {
		return 'smw-' . uniqid();
	}

	/**
	 * Convenience method generating a visual placeholder before any
	 * JS is registered to indicate that resources (JavaScript, CSS)
	 * are being loaded and once ready ensure to set
	 * ( '.smw-spinner' ).hide()
	 *
	 * @since 1.9
	 */
	protected function createLoadingHtmlPlaceholder() {
		$this->addResources( 'ext.smw.style' );

		return Html::rawElement(
			'div',
			[ 'class' => 'smw-spinner left mw-small-spinner' ],
			Html::element(
				'p',
				[ 'class' => 'text' ],
				$this->msg( 'smw-livepreview-loading' )->inContentLanguage()->text()
			)
		);
	}

	/**
	 * @deprecated since 2.0
	 */
	protected function loading() {
		return $this->createLoadingHtmlPlaceholder();
	}

	/**
	 * Convenience method to encode output data
	 *
	 * @since 1.9
	 *
	 * @param string $id
	 * @param array $data
	 */
	protected function encodeToJsonForId( $id, $data ) {
		SMWOutputs::requireHeadItem(
			$id,
			$this->getSkin()->makeVariablesScript(  [ $id => FormatJson::encode( $data ) ]
		) );

		return $this;
	}

	/**
	 * @deprecated since 2.0
	 */
	protected function encode( $id, $data ) {
		return $this->encodeToJsonForId( $id, $data );
	}

	/**
	 * Returns serialised content
	 *
	 * @see SMWResultPrinter::getResultText()
	 *
	 * @param SMWQueryResult $queryResult
	 * @param $outputMode
	 *
	 * @return string
	 */
	protected function getResultText( SMWQueryResult $queryResult, $outputMode ) {

		// Add parameters that are only known to the specific printer
		$ask = $queryResult->getQuery()->toArray();
		foreach ( $this->params as $key => $value ) {
			if ( is_string( $value ) || is_integer( $value ) || is_bool( $value ) ) {
				$ask['parameters'][$key] = $value;
			}
		}

		// Combine all data into one object
		$data = [
			'query' => [
				'result' => $queryResult->toArray(),
				'ask'    => $ask
			]
		];

		return $this->getHtml( $data );
	}
}