-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresetDb.php
More file actions
103 lines (95 loc) · 3.93 KB
/
resetDb.php
File metadata and controls
103 lines (95 loc) · 3.93 KB
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
<?php
/**
* phpjobseeker
*
* Copyright (C) 2009, 2015, 2017, 2026 Kevin Benton - kbenton at bentonfam dot org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
namespace com\kbcmdba\pjs2;
require_once 'Libs/autoload.php';
// Be sure to specify these in apply order. The reset script will automatically
// reverse the order for safe removal.
$controllerNames = [
'\\com\\kbcmdba\\pjs2\\VersionController',
'\\com\\kbcmdba\\pjs2\\UserController',
'\\com\\kbcmdba\\pjs2\\AuthTicketController',
'\\com\\kbcmdba\\pjs2\\ApplicationStatusController',
'\\com\\kbcmdba\\pjs2\\ApplicationStatusSummaryController',
'\\com\\kbcmdba\\pjs2\\CompanyController',
'\\com\\kbcmdba\\pjs2\\ContactController',
'\\com\\kbcmdba\\pjs2\\JobController',
'\\com\\kbcmdba\\pjs2\\KeywordController',
'\\com\\kbcmdba\\pjs2\\NoteController',
'\\com\\kbcmdba\\pjs2\\SearchController',
'\\com\\kbcmdba\\pjs2\\JobKeywordMapController'
];
$controllers = [];
$config = new Config();
$page = new PJSWebPage($config->getTitle() . " - Reset DB", true);
$body = "<ul>\n";
try {
$dbc = new DBConnection("admin", null, null, null, null, null, 'mysqli', true);
$dbh = $dbc->getConnection();
$tablesResult = $dbh->query("SHOW TABLES");
$hasExistingTables = $tablesResult && $tablesResult->num_rows > 0;
if (! $dbc->getCreatedDb() && $hasExistingTables) {
// Database exists with tables. Don't allow reset if the user is not logged in.
$auth = new Auth();
if (! $auth->isAuthorized()) {
throw new \Exception("User must be logged in to reset the database!");
}
if (! $auth->hasRole('admin')) {
throw new \Exception("Only admins can reset the database!");
}
if ("1" !== $config->getResetOk()) {
throw new \Exception("Reset capability is turned off! See config.xml");
}
}
foreach (array_reverse($controllerNames) as $controllerName) {
$controller = new $controllerName('write');
$controllers[$controllerName] = $controller;
if (method_exists($controller, 'dropTriggers')) {
$body .= "<li>Dropping Triggers: $controllerName</li>\n";
$controller->dropTriggers();
}
}
foreach ($controllers as $controllerName => $controller) {
$body .= "<li>Dropping Tables: $controllerName</li>\n";
$controller->dropTable();
}
foreach (array_reverse($controllers) as $controllerName => $controller) {
$body .= "<li>Creating Tables: $controllerName</li>\n";
$controller->createTable();
}
foreach (array_reverse($controllers) as $controllerName => $controller) {
if (method_exists($controller, 'createTriggers')) {
$body .= "<li>Creating Triggers: $controllerName</li>\n";
$controller->createTriggers();
}
}
foreach (array_reverse($controllers) as $controllerName => $controller) {
if (method_exists($controller, 'preLoadData')) {
$body .= "<li>Pre-populating tables: $controllerName</li>\n";
$controller->preLoadData();
}
}
$body .= "</ul>\n<p>Done.</p>";
} catch (\Exception $e) {
$body .= "</ul>\n<p />Uncaught exception: " . $e->getMessage() . "\n";
}
$page->setBody($body);
$page->displayPage();