summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Maintenance/PropertyStatisticsRebuilder.php
blob: 9ee99fa1b47c1814fbcddbeb4514458911d3ed04 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

namespace SMW\Maintenance;

use Onoi\MessageReporter\MessageReporter;
use Onoi\MessageReporter\MessageReporterFactory;
use SMW\SQLStore\PropertyStatisticsStore;
use SMW\Store;

/**
 * Simple class for rebuilding property usage statistics.
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author Nischay Nahata
 */
class PropertyStatisticsRebuilder {

	/**
	 * @var Store
	 */
	private $store = null;

	/**
	 * @var PropertyStatisticsStore
	 */
	private $propertyStatisticsStore;

	/**
	 * @var MessageReporter
	 */
	private $messageReporter;

	/**
	 * @since 1.9
	 *
	 * @param Store $store
	 * @param PropertyStatisticsStore $propertyStatisticsStore
	 */
	public function __construct( Store $store, PropertyStatisticsStore $propertyStatisticsStore ) {
		$this->store = $store;
		$this->propertyStatisticsStore = $propertyStatisticsStore;
		$this->messageReporter = MessageReporterFactory::getInstance()->newNullMessageReporter();
	}

	/**
	 * @since  2.2
	 *
	 * @param MessageReporter $messageReporter
	 */
	public function setMessageReporter( MessageReporter $messageReporter ) {
		$this->messageReporter = $messageReporter;
	}

	/**
	 * @since 1.9
	 */
	public function rebuild() {
		$this->reportMessage( "\nRebulding property statistics (this may take a while) ..." );
		$table = $this->propertyStatisticsStore->getStatisticsTable();

		$this->reportMessage( "\n   ... deleting `$table` content ..." );
		$this->propertyStatisticsStore->deleteAll();

		$connection = $this->store->getConnection( 'mw.db' );

		$res = $connection->select(
			\SMWSql3SmwIds::TABLE_NAME,
			[ 'smw_id', 'smw_title' ],
			[
				'smw_namespace' => SMW_NS_PROPERTY,
				'smw_subobject' => ''
			],
			__METHOD__
		);

		$propCount = $res->numRows();
		$this->reportMessage( "\n   ... selecting $propCount properties ...\n" );

		$i = 0;

		foreach ( $res as $row ) {

			$i++;

			$this->reportMessage(
				"\r". sprintf( "%-47s%s", "   ... updating", sprintf( "%4.0f%% (%s/%s)", round( ( $i / $propCount ) * 100 ), $i, $propCount ) )
			);

			$this->propertyStatisticsStore->insertUsageCount(
				(int)$row->smw_id,
				$this->getCountFormRow( $row )
			);
		}

		$connection->freeResult( $res );

		$this->reportMessage( "\n   ... done.\n" );
	}

	private function getCountFormRow( $row ) {

		$usageCount = 0;
		$nullCount = 0;

		foreach ( $this->store->getPropertyTables() as $propertyTable ) {

			if ( $propertyTable->isFixedPropertyTable() && $propertyTable->getFixedProperty() !== $row->smw_title ) {
				// This table cannot store values for this property
				continue;
			}

			list( $uCount, $nCount ) = $this->getPropertyTableRowCount(
				$propertyTable,
				$row->smw_id
			);

			$usageCount += $uCount;
			$nullCount += $nCount;
		}

		return [ $usageCount, $nullCount ];
	}

	private function getPropertyTableRowCount( $propertyTable, $pid ) {

		$condition = [];
		$connection = $this->store->getConnection( 'mw.db' );

		if ( !$propertyTable->isFixedPropertyTable() ) {
			$condition = [ 'p_id' => $pid ];
		}

		$tableFields = $this->store->getDataItemHandlerForDIType( $propertyTable->getDiType() )->getTableFields();
		$tableName = $propertyTable->getName();

		$usageCount = 0;
		$nullCount = 0;

		// Select all (incl. NULL since for example blob table can have a null
		// for when only the hash field is used, substract NULL in a second step)
		$row = $connection->selectRow(
			$tableName,
			'Count(*) as count',
			$condition,
			__METHOD__
		);

		if ( $row !== false ) {
			$usageCount = $row->count;
		}

		// Select only those that match NULL for all fields
		foreach ( $tableFields as $field => $type ) {
			$condition[] = "$field IS NULL";
		}

		$nRow = $connection->selectRow(
			$tableName,
			'Count(*) as count',
			$condition,
			__METHOD__
		);

		if ( $nRow !== false ) {
			$nullCount = $nRow->count;
		}

		if ( $usageCount > 0 ) {
			$usageCount = $usageCount - $nullCount;
		}

		return [ $usageCount, $nullCount ];
	}

	private function progress( $propCount, $i ) {

		if ( $i % 60 === 0 ) {
			if ( $i < 1 ) {
				return "\n";
			}

			return ' ' . round( ( $i / $propCount ) * 100 ) . ' %' . "\n";
		}

		return '.';
	}

	protected function reportMessage( $message ) {
		$this->messageReporter->reportMessage( $message );
	}

}