summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Info.php
blob: 5f6e9ac9a4e0921b1eebc245abab8fe927bc3203 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

namespace SMW\MediaWiki\Api;

use ApiBase;
use SMW\ApplicationFactory;
use SMW\Site;

/**
 * API module to obtain info about the SMW install, primarily targeted at
 * usage by the SMW registry.
 *
 * @ingroup Api
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class Info extends ApiBase {

	/**
	 * @see ApiBase::execute
	 */
	public function execute() {

		$params = $this->extractRequestParams();
		$requestedInfo = $params['info'];

		$map = [];
		$semanticStats = [];

		if ( in_array( 'propcount', $requestedInfo )
			|| in_array( 'jobcount', $requestedInfo )
			|| in_array( 'errorcount', $requestedInfo )
			|| in_array( 'deletecount', $requestedInfo )
			|| in_array( 'totalpropcount', $requestedInfo )
			|| in_array( 'usedpropcount', $requestedInfo )
			|| in_array( 'proppagecount', $requestedInfo )
			|| in_array( 'querycount', $requestedInfo )
			|| in_array( 'querysize', $requestedInfo )
			|| in_array( 'formatcount', $requestedInfo )
			|| in_array( 'conceptcount', $requestedInfo )
			|| in_array( 'subobjectcount', $requestedInfo )
			|| in_array( 'declaredpropcount', $requestedInfo ) ) {

			$semanticStats = ApplicationFactory::getInstance()->getStore()->getStatistics();

			$map = [
				'propcount' => 'PROPUSES',
				'errorcount' => 'ERRORUSES',
				'deletecount' => 'DELETECOUNT',
				'usedpropcount' => 'USEDPROPS',
				'totalpropcount' => 'TOTALPROPS',
				'declaredpropcount' => 'DECLPROPS',
				'proppagecount' => 'OWNPAGE',
				'querycount' => 'QUERY',
				'querysize' => 'QUERYSIZE',
				'conceptcount' => 'CONCEPTS',
				'subobjectcount' => 'SUBOBJECTS',
			];
		}

		$this->getResult()->addValue(
			null,
			'info',
			$this->doMapResultInfoFrom( $map, $requestedInfo, $semanticStats )
		);
	}

	/**
	 * @codeCoverageIgnore
	 * @see ApiBase::getAllowedParams
	 *
	 * @return array
	 */
	public function getAllowedParams() {
		return [
			'info' => [
				ApiBase::PARAM_DFLT => 'propcount|usedpropcount|declaredpropcount',
				ApiBase::PARAM_ISMULTI => true,
				ApiBase::PARAM_TYPE => [
					'propcount',
					'errorcount',
					'deletecount',
					'usedpropcount',
					'totalpropcount',
					'declaredpropcount',
					'proppagecount',
					'querycount',
					'querysize',
					'formatcount',
					'conceptcount',
					'subobjectcount',
					'jobcount'
				]
			],
		];
	}

	/**
	 * @codeCoverageIgnore
	 * @see ApiBase::getParamDescription
	 *
	 * @return array
	 */
	public function getParamDescription() {
		return [
			'info' => 'The info to provide.'
		];
	}

	/**
	 * @codeCoverageIgnore
	 * @see ApiBase::getDescription
	 *
	 * @return array
	 */
	public function getDescription() {
		return [
			'API module get info about this SMW install.'
		];
	}

	/**
	 * @codeCoverageIgnore
	 * @see ApiBase::getExamples
	 *
	 * @return array
	 */
	protected function getExamples() {
		return [
			'api.php?action=smwinfo&info=proppagecount|propcount',
		];
	}

	/**
	 * @codeCoverageIgnore
	 * @see ApiBase::getVersion
	 *
	 * @return string
	 */
	public function getVersion() {
		return __CLASS__ . ': $Id$';
	}

	private function doMapResultInfoFrom( $map, $requestedInfo, $semanticStats ) {

		$resultInfo = [];

		foreach ( $map as $apiName => $smwName ) {
			if ( in_array( $apiName, $requestedInfo ) ) {
				$resultInfo[$apiName] = $semanticStats[$smwName];
			}
		}

		if ( in_array( 'formatcount', $requestedInfo ) ) {
			$resultInfo['formatcount'] = [];

			foreach ( $semanticStats['QUERYFORMATS'] as $name => $count ) {
				$resultInfo['formatcount'][$name] = $count;
			}
		}

		if ( in_array( 'jobcount', $requestedInfo ) ) {
			$resultInfo['jobcount'] = [];
			$jobQueue = ApplicationFactory::getInstance()->getJobQueue();

			foreach ( Site::getJobClasses( 'SMW' ) as $type => $class ) {
				$size = $jobQueue->getQueueSize( $type );

				if ( $size > 0 ) {
					$resultInfo['jobcount'][$type] = $size;
				}
			}
		}

		return $resultInfo;
	}

}