summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/maintenance/populateHashField.php
blob: e19d0e6938eb8054ab552879b02be5b2143831da (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

namespace SMW\Maintenance;

use Onoi\MessageReporter\MessageReporter;
use SMW\ApplicationFactory;
use SMW\SQLStore\SQLStore;
use SMW\SQLStore\Installer;
use SMW\Setup;
use SMW\Store;

$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv('MW_INSTALL_PATH' ) : __DIR__ . '/../../..';

require_once $basePath . '/maintenance/Maintenance.php';

/**
 * @license GNU GPL v2+
 * @since 3.1
 *
 * @author mwjames
 */
class PopulateHashField extends \Maintenance {

	/**
	 * Threshold as the when the `populateHashField.php` should be used by an
	 * administrator instead.
	 *
	 * This postpones the execution to after `setupStore.php`/`update.php` in
	 * order to help minimize the time required for the initial setup/upgrade.
	 */
	const COUNT_SCRIPT_EXECUTION_THRESHOLD = 200000;

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

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

	/**
	 * @since 3.1
	 */
	public function __construct() {
		$this->mDescription = "Populate the 'smw_hash' field for all entities that have a missing entry.";
		parent::__construct();
	}

	/**
	 * @since 3.1
	 *
	 * @param Store $store
	 */
	public function setComplete( $incomplete ) {

		$this->reportMessage(
			"   ... writing the status to the setup information file ... \n"
		);

		Installer::setUpgradeFile(
			$GLOBALS,
			[
				Installer::POPULATE_HASH_FIELD_COMPLETE => $incomplete
			]
		);
	}

	/**
	 * @since 3.1
	 *
	 * @param Store $store
	 */
	public function setStore( Store $store ) {
		$this->store = $store;
	}

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

	/**
	 * @since 3.1
	 *
	 * @param string $message
	 */
	public function reportMessage( $message ) {

		if ( $this->messageReporter !== null ) {
			return $this->messageReporter->reportMessage( $message );
		}

		$this->output( $message );
	}

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

		if ( !Setup::isEnabled() ) {
			$this->reportMessage( "\nYou need to have Semantic MediaWiki enabled in order to run the maintenance script!\n" );
			exit;
		}

		$this->store = ApplicationFactory::getInstance()->getStore(
			'SMW\SQLStore\SQLStore'
		);

		$this->reportMessage( "\nChecking 'smw_hash' field consistency ...\n" );
		$this->populate();

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

		return true;
	}

	/**
	 * @since 3.1
	 *
	 * @return Iterator
	 */
	public function fetchRows() {

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

		return $connection->select(
			SQLStore::ID_TABLE,
			[
				'smw_id',
				'smw_title',
				'smw_namespace',
				'smw_iw',
				'smw_subobject'
			],
			[
				'smw_hash' => null,
				'smw_iw != ' . $connection->addQuotes( SMW_SQL3_SMWDELETEIW )
			],
			__METHOD__
		);
	}

	/**
	 * @since 3.1
	 *
	 * @param Iterator $rows
	 */
	public function populate( \Iterator $rows = null ) {

		if ( $rows === null ) {
			$rows = $this->fetchRows();
		}

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

		$count = 0;
		$i = 0;

		if ( $rows !== null ) {
			$count = $rows->numRows();
		}

		if ( $count == 0 ) {
			$this->reportMessage( "   ... all rows populated ...\n"  );
		} else {
			$this->reportMessage( "   ... missing $count rows ...\n"  );

			foreach ( $rows as $row ) {

				$hash = $idTable->computeSha1(
					[
						$row->smw_title,
						(int)$row->smw_namespace,
						$row->smw_iw,
						$row->smw_subobject
					]
				);

				$this->reportMessage(
					$this->progress( $row->smw_id, $i++, $count )
				);

				$connection->update(
					SQLStore::ID_TABLE,
					[
						'smw_hash' => $hash
					],
					[
						'smw_id' => $row->smw_id
					],
					__METHOD__
				);
			}
		}

		$this->reportMessage( "\n"  );
		$this->setComplete( true );
	}

	/**
	 * @see Maintenance::addDefaultParams
	 */
	protected function addDefaultParams() {
		parent::addDefaultParams();
	}

	private function progress( $id, $i, $count ) {
		return "\r". sprintf( "%-35s%s", "   ... updating document no.", sprintf( "%s (%1.0f%%)", $id, round( ( $i / $count ) * 100 ) ) );
	}

}

$maintClass = 'SMW\Maintenance\PopulateHashField';
require_once( RUN_MAINTENANCE_IF_MAIN );