summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Hooks/ExtensionSchemaUpdates.php
blob: 02c7ba1cd66d5c4cd39ab5aa79faef95914a417c (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
<?php

namespace SMW\MediaWiki\Hooks;

use DatabaseUpdater;
use Maintenance;
use ReflectionProperty;
use SMW\Options;
use SMW\SQLStore\Installer;

/**
 * Schema update to set up the needed database tables
 *
 * @see https://www.mediawiki.org/wiki/Manual:Hooks/LoadExtensionSchemaUpdates
 *
 * @license GNU GPL v2+
 * @since 2.0
 *
 * @author mwjames
 */
class ExtensionSchemaUpdates {

	/**
	 * @var DatabaseUpdater
	 */
	protected $updater = null;

	/**
	 * @since  2.0
	 *
	 * @param DatabaseUpdater $updater = null
	 */
	public function __construct( DatabaseUpdater $updater = null ) {
		$this->updater = $updater;
	}

	/**
	 * @since 2.0
	 *
	 * @return true
	 */
	public function process() {

		$verbose = true;

		$options = new Options(
			[
				Installer::OPT_SCHEMA_UPDATE => true,
				Installer::OPT_TABLE_OPTIMIZE => true,
				Installer::OPT_IMPORT => true,
				Installer::OPT_SUPPLEMENT_JOBS => true
			]
		);

		if ( $this->hasMaintenanceArg( 'skip-optimize' ) ) {
			$options->set( Installer::OPT_TABLE_OPTIMIZE, false );
		}

		// Needs a static caller otherwise the DatabaseUpdater returns with:
		// "Warning: call_user_func_array() expects parameter 1 to be a
		// valid callback ..."
		//
		// DatabaseUpdater notes "... $callback is the method to call; either a
		// DatabaseUpdater method name or a callable. Must be serializable (ie.
		// no anonymous functions allowed). The rest of the parameters (if any)
		// will be passed to the callback. ..."
		$this->updater->addExtensionUpdate(
			[
				'SMWStore::setupStore',
				[
					'verbose' => $verbose,
					'options' => $options
				]
			]
		);

		return true;
	}

	private function hasMaintenanceArg( $key ) {

		$maintenance = null;

		// We don't have access to the `update.php` internals due to lack
		// of public methods ... it is far from a clean approach but the only
		// way to fetch arguments invoked during the execution of `update.php`
		// Check required due to missing property in MW 1.29-
		if ( property_exists( $this->updater, 'maintenance' ) ) {
			$reflectionProperty = new ReflectionProperty( $this->updater, 'maintenance' );
			$reflectionProperty->setAccessible( true );
			$maintenance = $reflectionProperty->getValue( $this->updater );
		}

		if ( $maintenance instanceof Maintenance ) {
			$reflectionProperty = new ReflectionProperty( $maintenance, 'mOptions' );
			$reflectionProperty->setAccessible( true );
			$options = $reflectionProperty->getValue( $maintenance );
			return isset( $options[$key] );
		}

		return false;
	}

}