summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/Indexer/ReplicationStatus.php
blob: 3e59c6e4f7956ee0fd719802a5da886cfecaec92 (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
<?php

namespace SMW\Elastic\Indexer;

use SMW\Elastic\Connection\Client as ElasticClient;
use SMW\Elastic\QueryEngine\FieldMapper;
use SMWDITime as DITime;
use SMW\DIProperty;
use RuntimeException;

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

	/**
	 * @var ElasticClient
	 */
	private $elasticClient;

	/**
	 * @var FieldMapper
	 */
	private $fieldMapper;

	/**
	 * @since 3.0
	 *
	 * @param ElasticClient $elasticClient
	 */
	public function __construct( ElasticClient $connection ) {
		$this->connection = $connection;
		$this->fieldMapper = new FieldMapper();
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 *
	 * @return string
	 * @throws RuntimeException
	 */
	public function get( $key ) {

		if ( !is_callable( [ $this, $key ] ) ) {
			throw new RuntimeException( "`$key` as accessor is unknown!" );
		}

		return $this->{$key}();
	}

	/**
	 * @since 3.0
	 */
	private function refresh_interval() {

		$refresh_interval = null;

		$settings = $this->connection->getSettings(
			[
				'index' => $this->connection->getIndexName( ElasticClient::TYPE_DATA )
			]
		);

		foreach ( $settings as $key => $value ) {
			if ( isset( $value['settings']['index']['refresh_interval'] ) ) {
				$refresh_interval = $value['settings']['index']['refresh_interval'];
			}
		}

		return $refresh_interval;
	}

	/**
	 * @since 3.0
	 */
	private function last_update() {

		$pid = $this->fieldMapper->getPID( \SMWSql3SmwIds::$special_ids['_MDAT'] );
		$field = $this->fieldMapper->getField( new DIProperty( '_MDAT' ) );

		$params = $this->fieldMapper->exists( "$pid.$field" );

		$body = [
			'_source' => [ "$pid.$field", "subject" ],
			'size'    => 1,
			'query'   => $params,
			'sort'    => [ "$pid.$field" => [ 'order' => 'desc' ] ]
		];

		$params = [
			'index' => $this->connection->getIndexName( ElasticClient::TYPE_DATA ),
			'type'  => ElasticClient::TYPE_DATA,
			'body'  => $body
		];

		list( $res, $errors ) = $this->connection->search( $params );
		$time = null;

		foreach ( $res as $result ) {

			if ( !isset( $result['hits'] ) ) {
				continue;
			}

			foreach ( $result['hits'] as $key => $value ) {
				foreach ( $value as $key => $v ) {
					if ( $key === '_source' ) {
						$time = DITime::newFromJD( end( $v[$pid][$field] ) );
					}
				}
			}
		}

		if ( $time !== null ) {
			$time = $time->asDateTime()->format( 'Y-m-d H:i:s' );
		} else {
			$time = '0000-00-00 00:00:00';
		}

		return $time;
	}

}