File: upgrade-db.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (123 lines) | stat: -rwxr-xr-x 4,678 bytes parent folder | download | duplicates (2)
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
<?php
error_reporting(2147483647);

try {
	/*
	 * If we are run from another directory, try to change the current
	 * working directory to a directory the script is in
	 */
	if (@!file_exists(getcwd() . '/' . basename($argv[0]))) {
		chdir(dirname(__FILE__));
	} # if

	require_once "lib/SpotClassAutoload.php";
	require_once "settings.php";

	/*
	 * Make sure we are not run from the server, an db upgrade can take too much time and
	 * will easily be aborted by either a database, apache or browser timeout
	 */
	SpotCommandline::initialize(array('reset-groupmembership', 'reset-securitygroups', 'reset-filters'), 
								array('reset-groupmembership' => false, 'reset-securitygroups' => false, 'reset-filters' => false,
									  'set-systemtype' => false, 'reset-password' => false, 'mass-userprefchange' => false));
	if (!SpotCommandline::isCommandline()) {
		die("upgrade-db.php can only be run from the console, it cannot be run from the web browser");
	} # if

	echo "Updating schema..(" . $settings['db']['engine'] . ")" . PHP_EOL;
	
	$spotUpgrader = new SpotUpgrader($settings['db'], $settings);
	$spotUpgrader->database();
	echo "Schema update done" . PHP_EOL;
	echo "Updating settings" . PHP_EOL;
	$spotUpgrader->settings();
	echo "Settings update done" . PHP_EOL;
	$spotUpgrader->users($settings);
	echo "Updating users" . PHP_EOL;
	echo "Users' update done" . PHP_EOL;

	/* 
	 * If the user asked to change the system type..
	 */
	if (SpotCommandline::get('set-systemtype')) {
		echo "Resetting the system type of Spotweb to " . SpotCommandline::get('set-systemtype') . PHP_EOL;
		$spotUpgrader->resetSystemType(SpotCommandline::get('set-systemtype'));
		echo "System type changed" . PHP_EOL;
	} # if

	/* 
	 * If the user asked to change the preference of all users..
	 */
	if (SpotCommandline::get('mass-userprefchange')) {
		$prefToChange = explode("=", SpotCommandline::get('mass-userprefchange'));
		if (count($prefToChange) != 2) {
			throw new Exception("Please specify new preference as follows: perpage=10 or count_newspots=off");
		} # if

		echo "Mass changing a users' preference " . $prefToChange[0] .  " to a value of " . $prefToChange[1] . PHP_EOL;
		$spotUpgrader->massChangeUserPreferences($prefToChange[0], $prefToChange[1]);
		echo "Users' preferences changed" . PHP_EOL;
	} # if


	/* 
	 * If the user asked to reset the password of a user
	 */
	if (SpotCommandline::get('reset-password')) {
		echo "Resetting the password of '". SpotCommandline::get('reset-password') . "' to 'spotweb'" . PHP_EOL;
		$spotUpgrader->resetPassword(SpotCommandline::get('reset-password'));
		echo "Password changed" . PHP_EOL;
	} # if

	/* If the user asked to reset group membership, reset all group memberships */
	if (SpotCommandline::get('reset-securitygroups')) {
		echo "Resetting security groups to their default settings" . PHP_EOL;
		$spotUpgrader->resetSecurityGroups();
		echo "Reset security groups to their default settings done" . PHP_EOL;
	} # if


	/* 
	 * If the user asked to reset group membership, reset all group memberships.
	 */
	if (SpotCommandline::get('reset-groupmembership')) {
		echo "Resetting users' group membeship to the default" . PHP_EOL;
		$spotUpgrader->resetUserGroupMembership();
		echo "Reset of users' group membership done" . PHP_EOL;
	} # if

	/* 
	 * If the user asked to reset filters, do so
	 */
	if (SpotCommandline::get('reset-filters')) {
		echo "Resetting users' filters to the default" . PHP_EOL;
		$spotUpgrader->resetFilters();
		echo "Reset of users' filters done" . PHP_EOL;
	} # if

	echo "Performing basic analysis of database tables" . PHP_EOL;
	$spotUpgrader->analyze($settings);
	echo "Basic database optimalisation done" . PHP_EOL;
} 

catch(SpotwebCannotBeUpgradedToooldException $x) {
	die("Your current Spotweb installation is too old to be upgraded to this current version of Spotweb. " . PHP_EOL . 
		"Please download an earlier version of Spotweb (https://github.com/spotweb/spotweb/zipball/" . $x->getMessage() . "), " . PHP_EOL .
		"run upgrade-db.php using that version and then upgrade back to this version to run upgrade-db.php once more.");
} # SpotwebCannotBeUpgradedToooldException

catch(InvalidOwnSettingsSettingException $x) {
	echo "There is an error in your settings. Please open install.php to configure Spotweb" . PHP_EOL . PHP_EOL;
	echo $x->getMessage() . PHP_EOL;
} # InvalidOwnSettingsSettingException

catch(Exception $x) {
	echo PHP_EOL . PHP_EOL;
	echo 'SpotWeb crashed' . PHP_EOL . PHP_EOL;
	echo "Database schema or settings upgrade failed:" . PHP_EOL;
	echo "   " . $x->getMessage() . PHP_EOL;
	echo PHP_EOL . PHP_EOL;
	echo $x->getTraceAsString();
	die(1);
} # catch