summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/maintenance/setupStore.php
blob: 59d6fe98673253d2153669655f7c89ecababf8b1 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

namespace SMW\Maintenance;

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

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

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

/**
 * Sets up the storage backend currently selected in LocalSettings.php
 * (or the default MySQL store if no other store was selected). This
 * is equivalent to clicking the respective button on the special page
 * Special:SMWAdmin. However, the latter may timeout if the setup involves
 * migrating a lot of existing data.
 *
 * Note: If SMW is not installed in its standard path under ./extensions
 *       then the MW_INSTALL_PATH environment variable must be set.
 *       See README in the maintenance directory.
 *
 * Usage:
 * php setupStore.php [options...]
 *
 * -password Password for user account
 * NOTE: specifying user credentials in a command line call will usually store them
 * within the shell history file. For security, provide credentials in Adminssetings.php
 * instead and ensure that your text editor does not create world-readable backup copies
 * when modifying this file.
 *
 * --delete   Delete all SMW data, uninstall the selected storage backend. This is useful
 *            when moving to a new storage engine, and in the rare case of unsinstalling
 *            SMW. Deleted data can be recreated using this script (setup) followed by the
 *            use of the rebuildhData.php script which may take some time.
 *
 * --backend  The backend to use, e.g. SMWSQLStore3.
 *
 * --skip-optimize Skips the table optimization process.
 *
 * --skip-import Skips the import process.
 *
 * --nochecks When specified, no prompts are provided. Deletion will thus happen
 *            without the need to provide any confirmation.
 *
 * @author Markus Krötzsch
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class SetupStore extends \Maintenance {

	/**
	 * Name of the store class configured in LocalSettings.php. Stored to
	 * be able to tell if the selected store is the currecnt default or not.
	 *
	 * @var string
	 */
	protected $originalStore;

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

	/**
	 * @since 2.0
	 */
	public function __construct() {
		parent::__construct();
	}

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

		$this->mDescription = 'Sets up the SMW storage backend currently selected in LocalSettings.php.';

		$this->addOption( 'backend', 'Execute the operation for the storage backend of the given name.', false, true, 'b' );

		$this->addOption( 'delete', 'Delete all SMW data, uninstall the selected storage backend.' );
		$this->addOption( 'skip-optimize', 'Skipping the table optimization process (not recommended).', false );
		$this->addOption( 'skip-import', 'Skipping the import process.', false );

		$this->addOption(
			'nochecks',
			'Run the script without providing prompts. Deletion will thus happen without the need to provide any confirmation.'
		);
	}

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

	/**
	 * @see Maintenance::getDbType
	 *
	 * @since 3.0
	 */
	public function getDbType() {
		return \Maintenance::DB_ADMIN;
	}

	/**
	 * @since 3.0
	 */
	public function getConnection() {
		return $this->getDB( DB_MASTER );
	}

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

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

		StoreFactory::clear();

		$this->loadGlobalFunctions();
		$store = $this->getStore();

		$connectionManager = ApplicationFactory::getInstance()->getConnectionManager();

		// #2963 Use the Maintenance DB connection instead and the DB_ADMIN request
		// to allow to use the admin user/pass, if set
		$connectionManager->registerCallbackConnection( DB_MASTER, [ $this, 'getConnection' ] );

		$store->setConnectionManager(
			$connectionManager
		);

		$store->setMessageReporter(
			$this->getMessageReporter()
		);

		$store->setOption( Installer::OPT_TABLE_OPTIMIZE, !$this->hasOption( 'skip-optimize' ) );
		$store->setOption( Installer::OPT_IMPORT, !$this->hasOption( 'skip-import' ) );
		$store->setOption( Installer::OPT_SUPPLEMENT_JOBS, true );

		if ( $this->hasOption( 'delete' ) ) {
			$this->dropStore( $store );
		} else {
			$store->setup();
		}

		// Avoid holding a reference
		StoreFactory::clear();
	}

	protected function getMessageReporter() {

		$messageReporterFactory = MessageReporterFactory::getInstance();

		if ( $this->messageReporter === null && $this->getOption( 'quiet' ) ) {
			$this->messageReporter = $messageReporterFactory->newNullMessageReporter();
		} elseif( $this->messageReporter === null ) {
			$this->messageReporter = $messageReporterFactory->newObservableMessageReporter();
			$this->messageReporter->registerReporterCallback( [ $this, 'reportMessage' ] );
		}

		return $this->messageReporter;
	}

	protected function loadGlobalFunctions() {
		global $smwgIP;

		if ( !isset( $smwgIP ) ) {
			$smwgIP = dirname( __FILE__ ) . '/../';
		}

		require_once ( $smwgIP . 'src/GlobalFunctions.php' );
	}

	protected function getStore() {
		global $smwgDefaultStore;

		$storeClass = $this->getOption( 'backend', $smwgDefaultStore );
		$this->originalStore = $smwgDefaultStore;

		if ( class_exists( $storeClass ) ) {
			$smwgDefaultStore = $storeClass;
		} else {
			$this->error( "\nError: There is no backend class \"$storeClass\". Aborting.", 1 );
		}

		return StoreFactory::getStore( $storeClass );
	}

	protected function dropStore( Store $store ) {
		$storeName = get_class( $store );

		$verification = $this->promptDeletionVerification( $storeName );

		if ( !$verification ) {
			return;
		}

		$store->drop( !$this->isQuiet() );

		// be sure to have some buffer, otherwise some PHPs complain
		while ( ob_get_level() > 0 ) {
			ob_end_flush();
		}

		$this->output( "\nYou can recreate them with this script followed by the use\n");
		$this->output( "of the rebuildData.php script to rebuild their contents.\n");
	}

	/**
	 * @param string $storeName
	 *
	 * @return boolean
	 */
	protected function promptDeletionVerification( $storeName ) {
		$this->output( "You are about to delete all data stored in the SMW backend $storeName.\n" );

		if ( $storeName === $this->originalStore ) {
			$this->output( "This backend is CURRENTLY IN USE. Deleting it is likely to BREAK YOUR WIKI.\n" );
		} else {
			$this->output( "This backend is not currently in use. Deleting it should not cause any problems.\n" );
		}
		$this->output( "To undo this operation later on, a complete refresh of the data will be needed.\n" );

		if ( !$this->hasOption( 'nochecks' ) ) {
			print ( "If you are sure you want to proceed, type DELETE.\n" );

			if ( $this->readconsole() !== 'DELETE' ) {
				print ( "Aborting.\n\n" );
				return false;
			}
		}
		return true;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $message
	 */
	public function reportMessage( $message ) {
		$this->output( $message );
	}

}

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