forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-cache.php
121 lines (102 loc) · 4.21 KB
/
migrate-cache.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
<?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";
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.60) {
throw new Exception("Your schemaversion is already upgraded");
} # if
/*
* Remove any serialized caches as we don't support them anymore
*/
echo "Removing serialized entries from database";
$dbConnection->modify("DELETE FROM cache WHERE cachetype = 4");
$dbConnection->modify("DELETE FROM cache WHERE cachetype = 5");
echo ", done. " . PHP_EOL;
$counter = 1;
while(true) {
$counter++;
echo "Migrating cache content, items " . (($counter - 1) * 100) . ' to ' . ($counter * 100);
$results = null;
switch ($dbSettings['engine']) {
case 'mysql' :
case 'pdo_mysql' : {
$results = $dbConnection->arrayQuery(
'SELECT resourceid, stamp, metadata, serialized, cachetype, UNCOMPRESS(content) AS content FROM cache WHERE content IS NOT NULL LIMIT 100');
break;
} # mysql
case 'pgsql' :
case 'pdo_pgsql' : {
$results = $dbConnection->arrayQuery(
"SELECT resourceid, stamp, metadata, serialized, cachetype, content FROM cache WHERE content IS NOT NULL LIMIT 100");
foreach($results as &$v) {
$v['content'] = stream_get_contents($v['content']);
} # foreach
break;
} # case Postgresql
case 'pdo_sqlite' : {
$results = $dbConnection>arrayQuery(
'SELECT resourceid, stamp, metadata, serialized, cachetype, content FROM cache WHERE content IS NOT NULL LIMIT 100');
break;
} # mysql
}
foreach($results as $cacheItem) {
if ($cacheItem['metadata']) {
$cacheItem['metadata'] = unserialize($cacheItem['metadata']);
} # if
echo '.';
$cacheDao->putCacheContent($cacheItem['resourceid'], $cacheItem['cachetype'], $cacheItem['content'], $cacheItem['metadata'], 0);
/*
* Actually invalidate the cache content
*/
$dbConnection->modify("UPDATE cache SET content = NULL where resourceid = :resourceid AND cachetype = :cachetype",
array(':resourceid' => array($cacheItem['resourceid'], PDO::PARAM_STR),
':cachetype' => array($cacheItem['cachetype'], PDO::PARAM_INT)));
} # results
echo ", done. " . PHP_EOL;
if (count($results) == 0) {
break;
} # if
} # while
}
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