Completed
Push — master ( 6dc7d8...407c40 )
by Karsten
15:45
created

formats/slideshow/SRF_SlideShowApi.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use ParamProcessor\ParamDefinition;
4
use SMW\DataValueFactory;
5
6
/**
7
 * API module to retrieve formatted results for a given page, printouts and template.
8
 *
9
 * @author Stephan Gambke
10
 * @ingroup SemanticResultFormats
11
 */
12
class SRFSlideShowApi extends ApiBase {
13
14
	/**
15
	 * Evaluates the parameters, performs the query, and sets up the result.
16
	 *
17
	 * The result data is stored in the ApiResult object available through getResult().
18
	 */
19
	public function execute() {
20
21
		// get request parameters
22
		$requestParams = $this->extractRequestParams();
23
24
		$title = Title::newFromID( $requestParams['pageid'] )->getPrefixedText();
25
26
		$rp = new SMWListResultPrinter( 'template', true );
27
28
		// get defaults of parameters for the 'template' result format as array of ParamDefinition
29
		$paramDefinitions = ParamDefinition::getCleanDefinitions( $rp->getParamDefinitions( [] ) );
30
31
		// transform into normal key-value array
32
		$queryParams = [];
33
34
		foreach ( $paramDefinitions as $def ) {
35
			$queryParams[$def->getName()] = $def->getDefault();
36
		}
37
38
		// add/set specific parameters for this call
39
		$queryParams = array_merge(
40
			$queryParams,
41
			[
42
				'format' => 'template',
43
				'template' => $requestParams['template'],
44
				'mainlabel' => '',
45
				'sort' => '',
46
				'order' => '',
47
				'intro' => null,
48
				'outro' => null,
49
				'searchlabel' => null,
50
				'link' => null,
51
				'default' => null,
52
				'headers' => null,
53
				'introtemplate' => '',
54
				'outrotemplate' => '',
55
			]
56
		);
57
58
		// A bit of a hack since the parser isn't run, avoids [[SMW::off]]/[[SMW::on]]
59
		$queryParams['import-annotation'] = 'true';
60
61
		// transform query parameters into format suitable for SMWQueryProcessor
62
		$queryParams = SMWQueryProcessor::getProcessedParams( $queryParams, [] );
63
64
		// build array of printouts
65
66
		$printoutsRaw = json_decode( $requestParams['printouts'], true );
67
		$printouts = [];
68
69
		foreach ( $printoutsRaw as $printoutData ) {
70
71
			// if printout mode is PRINT_PROP
72
			if ( $printoutData[0] == SMWPrintRequest::PRINT_PROP ) {
73
				// create property from property key
74
				$data = DataValueFactory::getInstance()->newPropertyValueByLabel( $printoutData[2] );
75
			} else {
76
				$data = null;
77
			}
78
79
			// create printrequest from request mode, label, property name, output format, parameters
80
			$printouts[] = new SMWPrintRequest(
81
				$printoutData[0],
82
				$printoutData[1],
83
				$data,
84
				$printoutData[3],
85
				$printoutData[4]
86
			);
87
		}
88
89
		// query SMWQueryProcessor and set query result as API call result
90
		$this->getResult()->addValue(
91
			null,
92
			$requestParams['pageid'],
93
			SMWQueryProcessor::getResultFromQueryString(
0 ignored issues
show
Deprecated Code introduced by
The method SMWQueryProcessor::getResultFromQueryString() has been deprecated with message: Will vanish after release of SMW 1.8.
See SMW_Ask.php for example code on how to get query results from
#ask function parameters.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
				'[[' . $title . ']]',
95
				$queryParams,
96
				$printouts,
97
				SMW_OUTPUT_HTML,
98
				SMWQueryProcessor::INLINE_QUERY
99
			)
100
		);
101
102
	}
103
104
	/**
105
	 * Returns the description string for this module
106
	 *
107
	 * @return mixed string or array of strings
108
	 */
109
	protected function getDescription() {
110
		return [
111
			'API module used by the SlideShow result printer to retrieve formatted results.',
112
			'This module is should not be called directly.'
113
		];
114
	}
115
116
	/**
117
	 * Returns usage examples for this module. Return false if no examples are available.
118
	 *
119
	 * @return bool|string|array
120
	 */
121
	protected function getExamples() {
122
		return false;
123
	}
124
125
	public function getHelpUrls() {
126
		return 'http://semantic-mediawiki.org/wiki/Help:Slideshow_format';
127
	}
128
129
	/**
130
	 * Returns an array of allowed parameters
131
	 *
132
	 * @return array|bool
133
	 */
134
	protected function getAllowedParams() {
135
		return [
136
			'pageid' => [
137
				ApiBase::PARAM_TYPE => 'integer',
138
				ApiBase::PARAM_ISMULTI => false,
139
				ApiBase::PARAM_REQUIRED => true,
140
			],
141
			'template' => [
142
				ApiBase::PARAM_TYPE => 'string',
143
				ApiBase::PARAM_ISMULTI => false,
144
				ApiBase::PARAM_REQUIRED => true,
145
			],
146
			'printouts' => [
147
				ApiBase::PARAM_TYPE => 'string',
148
				ApiBase::PARAM_ISMULTI => false,
149
				ApiBase::PARAM_REQUIRED => false,
150
				ApiBase::PARAM_DFLT => '[]',
151
			],
152
		];
153
	}
154
155
	/**
156
	 * Returns an array of parameter descriptions.
157
	 * Don't call this function directly: use getFinalParamDescription() to
158
	 * allow hooks to modify descriptions as needed.
159
	 *
160
	 * @return array|bool False on no parameter descriptions
161
	 */
162
	protected function getParamDescription() {
163
		return [
164
			'pageid' => 'Id of the page (subject) to be displayed',
165
			'template' => 'Template to use for formatting',
166
			'printouts' => 'Printouts to send to the template',
167
		];
168
	}
169
170
	/**
171
	 * Returns a string that identifies the version of the extending class.
172
	 * Typically includes the class name, the svn revision, timestamp, and
173
	 * last author. Usually done with SVN's Id keyword
174
	 *
175
	 * @return string
176
	 */
177
	public function getVersion() {
178
		global $srfgIP;
179
		$gitSha1 = SpecialVersion::getGitHeadSha1( $srfgIP );
180
		return __CLASS__ . '-' . SRF_VERSION . ( $gitSha1 !== false ) ? ' (' . substr( $gitSha1, 0, 7 ) . ')' : '';
181
	}
182
183
}
184