forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-cache2.php
125 lines (103 loc) · 4.11 KB
/
migrate-cache2.php
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
<?php
error_reporting(2147483647);
function cleanupDirs($dir, $curlevel) {
echo 'Scanning directory: ' . $dir . ' (level: ' . $curlevel . ')' . PHP_EOL;
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (strlen(basename($object)) == 3) {
if (is_dir($dir)) {
cleanupDirs($dir."/".$object, $curlevel+1);
}
} elseif ($curlevel > 2) {
echo 'Removing file: ' . $dir."/".$object . PHP_EOL;
unlink($dir."/".$object);
}
}
}
echo 'Trying to remove directory: ' . $dir . PHP_EOL;
@rmdir($dir);
} # cleanupDirs
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";
SpotClassAutoload::register();
require_once "lib/Bootstrap.php";
/*
* Create a DAO factory. We cannot use the bootstrapper here,
* because it validates for a valid settings etc. version.
*/
$bootstrap = new Bootstrap();
$daoFactory = $bootstrap->getDaoFactory();
$settings = $bootstrap->getSettings($daoFactory, true);
$dbSettings = $bootstrap->getDbSettings();
/*
* Try to create the directory, we hardcode it here because
* it cannot be made configurable in the database anyway
* and this is just the lazy way out, really
*/
$daoFactory->setCachePath('./cache/');
$cacheDao = $daoFactory->getCacheDao();
if (!is_dir('./cache')) {
mkdir('./cache', 0777);
} # if
/*
* Now try to get all current cache items
*/
$dbConnection = $daoFactory->getConnection();
# Update old blacklisttable
$schemaVer = $dbConnection->singleQuery("SELECT value FROM settings WHERE name = 'schemaversion'", array());
if ($schemaVer >= 0.63) {
throw new Exception("Your cache is already upgraded");
} # if
$counter = 0;
while(true) {
$counter++;
echo "Migrating cache content, items " . (($counter - 1) * 1000) . ' to ' . ($counter * 1000);
$results = $dbConnection->arrayQuery('SELECT id, cachetype, metadata FROM cache LIMIT 1001 OFFSET ' . (($counter - 1) * 1000) );
foreach($results as $cacheItem) {
$cacheItem['metadata'] = unserialize($cacheItem['metadata']);
$cacheDao->migrateCacheToNewStorage($cacheItem['id'], $cacheItem['cachetype'], $cacheItem['metadata']);
} # results
echo ", done. " . PHP_EOL;
if (count($results) == 0) {
break;
} # if
} # while
/*
* try to remove the directories
*/
echo "Removing old and empty cache directories (can take a while)..." . PHP_EOL;
/*
* Removing orphaned files
*/
$cacheBasePath = '.' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
cleanUpDirs($cacheBasePath . 'image' . DIRECTORY_SEPARATOR, 0);
cleanUpDirs($cacheBasePath . 'nzb' . DIRECTORY_SEPARATOR, 0);
cleanUpDirs($cacheBasePath . 'stats' . DIRECTORY_SEPARATOR, 0);
cleanUpDirs($cacheBasePath . 'web' . DIRECTORY_SEPARATOR, 0);
cleanUpDirs($cacheBasePath . 'translatedcomments' . DIRECTORY_SEPARATOR, 0);
cleanUpDirs($cacheBasePath . 'translatertoken' . DIRECTORY_SEPARATOR, 0);
/*
* And actually start updating or creating the schema and settings
*/
echo "Updating schema..(" . $dbSettings['engine'] . ")" . PHP_EOL;
# update the database with this specific schemaversion
$dbConnection->rawExec("DELETE FROM settings WHERE name = 'schemaversion'", array());
$dbConnection->rawExec("INSERT INTO settings(name, value) VALUES('schemaversion', '" . SPOTDB_SCHEMA_VERSION . "')");
}
catch(Exception $x) {
echo PHP_EOL . PHP_EOL;
echo 'SpotWeb crashed' . PHP_EOL . PHP_EOL;
echo "Cache migration failed:" . PHP_EOL;
echo " " . $x->getMessage() . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
die(1);
} # catch