-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrator.php
More file actions
149 lines (129 loc) · 3.61 KB
/
migrator.php
File metadata and controls
149 lines (129 loc) · 3.61 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
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
<?php
/**
* Script for the migration of the PHP sessions
*/
/**
* Include session storage managers
*/
require_once 'FileSessionManager.php';
require_once 'MemcacheSessionManager.php';
require_once 'RedisSessionManager.php';
/**
* Params of the session managers
*/
$params = array(
'session_managers' => array(
'files' => array(
'class_name' => 'FileSessionManager',
'prefix' => 'sess_',
'params' => array('path' => '/var/lib/php5/'),
),
'memcache' => array(
'class_name' => 'MemcacheSessionManager',
'prefix' => 'memc.sess.key.',
'params' => array(
'host' => 'localhost',
'port' => 11211,
)
),
'redis' => array(
'class_name' => 'RedisSessionManager',
'prefix' => 'PHPREDIS_SESSION:',
'params' => array(
'host' => 'localhost',
'port' => 6379,
)
),
)
);
/**
* Get list of the supported session storages
* @return array
*/
function get_supported_storages()
{
global $params;
return array_keys($params['session_managers']);
}
/**
* Print help message for user
*/
function echo_usage()
{
global $argv;
$s = "Usage: php " . $argv[0] . " --from=<source> --to=<destination>\n\n";
$s .= "Options:\n\n";
$s .= "--clean-destination - remove all sessions from the destination storage";
$s .= "\n\nSupported storages: " . implode(', ', get_supported_storages());
echo $s;
}
/**
* Move php sessions from one storage to other
* @param SessionManager $src source
* @param SessionManager $dest destination
* @param boolean $verbose show verbose output
* @return integer count of the moved sessions
*/
function move_sessions(BaseSessionManager $src, BaseSessionManager $dest, $verbose = true)
{
$count = 0;
$keys = $src->getAllKeys();
if ($verbose) {
echo "Moving sessions...\n\n";
}
foreach ($keys as $k) {
$session_contents = $src->get($k);
$dest->set($k, $session_contents);
$count++;
if ($verbose) {
echo $k . "\n";
}
}
if ($verbose) {
echo "\nDone.\n";
echo "Session moved: $count\n";
}
return $count;
}
/**
* Run session migration process
*/
function main()
{
global $params;
$longopts = array('from:', 'to:', 'clean-destination::');
$opts = getopt('', $longopts);
$supported_storages = get_supported_storages();
if (empty($opts['from']) || empty($opts['to'])) {
echo_usage();
exit(1);
}
if (!in_array($opts['from'], $supported_storages)) {
echo_usage();
exit(1);
}
if (!in_array($opts['to'], $supported_storages)) {
echo_usage();
exit(1);
}
if ($opts['to'] === $opts['from']) {
echo "Source cannot be destination\n";
exit(1);
}
$managers = $params['session_managers'];
$src_params = $managers[$opts['from']]['params'];
$src_cls = $managers[$opts['from']]['class_name'];
$src = new $src_cls($managers[$opts['from']]['prefix'], $src_params);
$dest_params = $managers[$opts['to']]['params'];
$dest_cls = $managers[$opts['to']]['class_name'];
$dest = new $dest_cls($managers[$opts['to']]['prefix'], $dest_params);
if (isset($opts['clean-destination'])) {
echo "Removing all sessions from the destination storage...\n";
$deleted_count = $dest->deleteAll();
echo "Removed items: $deleted_count\n";
echo "Done.\n\n";
}
move_sessions($src, $dest);
}
ini_set('register_argc_argv', 1);
main();