Completed
Push — master ( 69614a...9cb597 )
by mw
14s
created

src/SQLStore/Lookup/PropertyUsageListLookup.php (1 issue)

Labels
Severity

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
namespace SMW\SQLStore\Lookup;
4
5
use RuntimeException;
6
use SMW\DIProperty;
7
use SMW\InvalidPropertyException;
8
use SMW\SQLStore\SQLStore;
9
use SMW\Store;
10
use SMW\Store\PropertyStatisticsStore;
11
use SMWDIError as DIError;
12
use SMWRequestOptions as RequestOptions;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 2.2
17
 *
18
 * @author mwjames
19
 */
20
class PropertyUsageListLookup implements ListLookup {
21
22
	/**
23
	 * @var Store
24
	 */
25
	private $store;
26
27
	/**
28
	 * @var PropertyStatisticsStore
29
	 */
30
	private $propertyStatisticsStore;
31
32
	/**
33
	 * @var RequestOptions
34
	 */
35
	private $requestOptions;
36
37
	/**
38
	 * @since 2.2
39
	 *
40
	 * @param Store $store
41
	 * @param PropertyStatisticsStore $propertyStatisticsStore
42
	 * @param RequestOptions $requestOptions|null
43
	 */
44 162
	public function __construct( Store $store, PropertyStatisticsStore $propertyStatisticsStore, RequestOptions $requestOptions = null ) {
45 162
		$this->store = $store;
46 162
		$this->propertyStatisticsStore = $propertyStatisticsStore;
47 162
		$this->requestOptions = $requestOptions;
48 162
	}
49
50
	/**
51
	 * @since 2.2
52
	 *
53
	 * @return DIProperty[]
54
	 * @throws RuntimeException
55
	 */
56 5
	public function fetchList() {
57
58 5
		if ( $this->requestOptions === null ) {
59 1
			throw new RuntimeException( "Missing requestOptions" );
60
		}
61
62 4
		return $this->getPropertyList( $this->doQueryPropertyTable() );
63
	}
64
65
	/**
66
	 * @since 2.2
67
	 *
68
	 * @return boolean
69
	 */
70 1
	public function isFromCache() {
71 1
		return false;
72
	}
73
74
	/**
75
	 * @since 2.2
76
	 *
77
	 * @return integer
78
	 */
79 2
	public function getTimestamp() {
80 2
		return wfTimestamp( TS_UNIX );
81
	}
82
83
	/**
84
	 * @since 2.2
85
	 *
86
	 * @return string
87
	 */
88 157
	public function getHash() {
89 157
		return __METHOD__ . '#' . ( $this->requestOptions !== null ? $this->requestOptions->getHash() : '' );
90
	}
91
92 4
	private function doQueryPropertyTable() {
93
94
		// the query needs to do the filtering of internal properties, else LIMIT is wrong
95 4
		$options = array( 'ORDER BY' => 'smw_sortkey' );
96
97
		$conditions = array(
98 4
			'smw_namespace' => SMW_NS_PROPERTY,
99 4
			'smw_iw' => '',
100 4
			'smw_subobject' => ''
101
		);
102
103 4
		if ( $this->requestOptions->limit > 0 ) {
104 2
			$options['LIMIT'] = $this->requestOptions->limit;
105 2
			$options['OFFSET'] = max( $this->requestOptions->offset, 0 );
106
		}
107
108 4
		if ( $this->requestOptions->getStringConditions() ) {
109 1
			$conditions[] = $this->store->getSQLConditions( $this->requestOptions, '', 'smw_sortkey', false );
110
		}
111
112 4
		$db = $this->store->getConnection( 'mw.db' );
113
114 4
		$res = $db->select(
115 4
			array( $db->tableName( SQLStore::ID_TABLE ), $db->tableName( SQLStore::PROPERTY_STATISTICS_TABLE ) ),
116 4
			array( 'smw_id', 'smw_title', 'usage_count' ),
117
			$conditions,
118 4
			__METHOD__,
119
			$options,
120 4
			array( $db->tableName( SQLStore::ID_TABLE ) => array( 'INNER JOIN', array( 'smw_id=p_id' ) ) )
121
		);
122
123 4
		return $res;
124
	}
125
126 4
	private function getPropertyList( $res ) {
127
128 4
		$result = array();
129
130 4
		foreach ( $res as $row ) {
131
132
			try {
133 4
				$property = new DIProperty( str_replace( ' ', '_', $row->smw_title ) );
134 3
				$property->id = $row->smw_id;
0 ignored issues
show
The property id does not seem to exist in SMW\DIProperty.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
135 1
			} catch ( InvalidPropertyException $e ) {
136 1
				$property = new DIError( new \Message( 'smw_noproperty', array( $row->smw_title ) ) );
137
			}
138
139 4
			$result[] = array( $property, (int)$row->usage_count );
140
		}
141
142 4
		return $result;
143
	}
144
145
}
146