summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/maintenance/rebuildConceptCache.php
blob: 86a77df506320ed663b8e5b8877e3e58c89ef858 (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
<?php

namespace SMW\Maintenance;

use SMW\ApplicationFactory;
use SMW\Setup;

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

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

/**
 * Manage concept caches
 *
 * This script is used to manage concept caches for Semantic MediaWiki. Concepts
 * are semantic queries stored on Concept: pages. The elements of concepts can be
 * computed online, or they can come from a pre-computed cache. The wiki may even
 * be configured to display certain concepts only if they are available cached.
 *
 * This script can create, delete and update these caches, or merely show their
 * status.
 *
 * Usage: php rebuildConceptCache.php <action> [<select concepts>] [<options>]
 *
 * Actions:
 * --help      Show this message.
 * --status    Show the cache status of the selected concepts.
 * --create    Rebuild caches for the selected concepts.
 * --delete    Remove all caches for the selected concepts.
 *
 * If no further options are given, all concepts in the wiki are processed.
 *
 * Select concepts:
 * --concept       'Concept name' Process only this one concept.
 * --hard          Process only concepts that are not allowed to be computed
 *                 online according to the current wiki settings.
 * --update        Process only concepts that already have some cache, i.e. do
 *                 not create any new caches. For the opposite (only concepts
 *                 without caches), use --old with a very high number.
 * --old <min>     Process only concepts with caches older than <min> minutes
 *                 or with no caches at all.
 * -s <startid>    Process only concepts with page id of at least <startid>
 * -e <endid>      Process only concepts with page id of at most <endid>
 *
 * Selection options can be combined to process only concepts that meet all the
 * requirements at once. If --concept is given, then -s and -e are ignored.
 *
 * Options:
 * --quiet         Do not give any output.
 * --verbose       Give additional output. No effect if --quiet is given.
 *
 * Use option --help for usage details.
 *
 * 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.
 *
 * @ingroup Maintenance
 *
 * @licence GNU GPL v2+
 * @since 1.9.2
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class RebuildConceptCache extends \Maintenance {

	public function __construct() {
		parent::__construct();

		$this->addDescription( "\n" .
			"This script is used to manage concept caches for Semantic MediaWiki. Concepts \n" .
			"are semantic queries stored on Concept: pages. The elements of concepts can be \n" .
			"computed online, or they can come from a pre-computed cache. The wiki may even \n" .
			"be configured to display certain concepts only if they are available cached. \n" .
			"\n" . "This script can create, delete and update these caches, or merely show their \n".
			"status. "
		);

		$this->addDefaultParams();
	}

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

		parent::addDefaultParams();

		// Actions
		$this->addOption( 'status', 'Show the cache status of the selected concepts' );
		$this->addOption( 'create', 'Rebuild caches for the selected concepts.' );
		$this->addOption( 'delete', 'Remove all caches for the selected concepts.' );

		// Options
		$this->addOption( 'concept', '"Concept name" Process only this one concept.', false, true );
		$this->addOption( 'hard', 'Process only concepts that are not allowed to be computed online according to the current wiki settings.' );

		$this->addOption( 'update', 'Process only concepts that already have some cache, i.e. do not create any new caches. ' .
								'For the opposite (only concepts without caches), use --old with a very high number.' );

		$this->addOption( 'old', '<min> Process only concepts with caches older than <min> minutes or with no caches at all.', false, true );
		$this->addOption( 's', '<startid> Process only concepts with page id of at least <startid>', false, true );
		$this->addOption( 'e', '<endid> Process only concepts with page id of at most <endid>', false, true );

		$this->addOption( 'with-maintenance-log', 'Add log entry to `Special:Log` about the maintenance run.', false );
		$this->addOption( 'report-runtime', 'Report execution time and memory usage', false );
		$this->addOption( 'debug', 'Sets global variables to support debug ouput while running the script', false );
		$this->addOption( 'quiet', 'Do not give any output', false );
		$this->addOption( 'verbose', 'Give additional output. No effect if --quiet is given.', false );
	}

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

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

		if ( !Setup::isValid( true ) ) {
			$this->reportMessage( "\nYou need to run `update.php` or `setupStore.php` first before continuing\nwith any maintenance tasks!\n" );
			exit;
		}

		$applicationFactory = ApplicationFactory::getInstance();
		$maintenanceFactory = $applicationFactory->newMaintenanceFactory();

		$maintenanceHelper = $maintenanceFactory->newMaintenanceHelper();
		$maintenanceHelper->initRuntimeValues();

		if ( $this->hasOption( 'debug' ) ) {
			$maintenanceHelper->setGlobalToValue( 'wgShowExceptionDetails', true );
			$maintenanceHelper->setGlobalToValue( 'wgShowSQLErrors', true );
			$maintenanceHelper->setGlobalToValue( 'wgShowDBErrorBacktrace', true );
		}

		$conceptCacheRebuilder = $maintenanceFactory->newConceptCacheRebuilder(
			$applicationFactory->getStore(),
			[ $this, 'reportMessage' ]
		);

		$conceptCacheRebuilder->setParameters( $this->mOptions );

		$result = $this->checkForRebuildState(
			$conceptCacheRebuilder->rebuild()
		);

		if ( $result && $this->hasOption( 'report-runtime' ) ) {
			$this->reportMessage( "\n" . "Runtime report ..." . "\n" );
			$this->reportMessage( $maintenanceHelper->getFormattedRuntimeValues( '   ...' ) . "\n" );
		}

		if ( $this->hasOption( 'with-maintenance-log' ) ) {
			$maintenanceLogger = $maintenanceFactory->newMaintenanceLogger( 'RebuildConceptCacheLogger' );
			$maintenanceLogger->log( $maintenanceHelper->getFormattedRuntimeValues() );
		}

		$maintenanceHelper->reset();

		return $result;
	}

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

	private function checkForRebuildState( $rebuildResult ) {

		if ( !$rebuildResult ) {
			$this->reportMessage( $this->mDescription . "\n\n" . 'Use option --help for usage details.' . "\n"  );
			return false;
		}

		return true;
	}

}

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