File: 2.1_to_2.2_sql_schema.php

package info (click to toggle)
turba2 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,332 kB
  • ctags: 2,927
  • sloc: php: 11,046; xml: 1,690; sql: 507; makefile: 62; perl: 17; sh: 1
file content (306 lines) | stat: -rwxr-xr-x 12,113 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/php
<?php
/**
 * Very basic migration script for moving to the Turba 2.2 default sql schema.
 *
 * Note: This is NOT complete yet, but will get your Turba 2.1 data into
 * enough shape to run with the new default sql schema in Turba 2.2.
 *
 * It is HIGHLY RECOMMENDED to back up your current Turba tables BEFORE
 * attempting this upgrade!
 *
 * $Horde: turba/scripts/upgrades/2.1_to_2.2_sql_schema.php,v 1.18.2.7 2008/06/03 16:42:39 mrubinsk Exp $
 *
 * Copyright 2007-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 */

/* Set this variable to 'true' to activate the script. */
$for_real = false;

/* You must set these to an appropriate user/pass/table for your server. */
$db_user = 'root';
$db_pass = '';
$db_table = 'turba_objects';

/* Allow skipping of parsing certain fields.
 * You can force fields to not be parsed by setting the field to false
 * below. */
$do_name = true;
$do_home = true;
$do_work = true;
$do_email = true;

/* YOU SHOULD NOT HAVE TO TOUCH ANYTHING BELOW THIS LINE */
@define('AUTH_HANDLER', true);
@define('HORDE_BASE', dirname(__FILE__) . '/../../..');

/* Set up the CLI environment */
require_once HORDE_BASE . '/lib/core.php';
require_once 'Horde/CLI.php';
if (!Horde_CLI::runningFromCli()) {
    exit("Must be run from the command line\n");
}
$cli = &Horde_CLI::singleton();
$cli->init();

/* Grab what we need to steal the DB config */
require_once HORDE_BASE . '/config/conf.php';
require_once 'Horde/Form.php';
require 'Horde/NLS/carsigns.php';
require_once 'DB.php';

$config = $GLOBALS['conf']['sql'];
$config['username'] = $db_user;
$config['password'] = $db_pass;
unset($config['charset']);
$db = DB::connect($config);
if (is_a($db, 'PEAR_Error')) {
    Horde::fatal($db, __FILE__, __LINE__);
}
if (!$for_real) {
    $cli->message('No changes will done to the existing data. Please read the comments in the code, then set the $for_real flag to true before running.', 'cli.message');
}

/* Define how to transform the address book table */
$queries = array(
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_firstname VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_lastname VARCHAR(255)',
    'UPDATE ' . $db_table . ' SET object_lastname = object_name',
    'ALTER TABLE ' . $db_table . ' DROP COLUMN object_name',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_middlenames VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_nameprefix VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_namesuffix VARCHAR(32)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_phototype VARCHAR(10)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_bday VARCHAR(10)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homestreet VARCHAR(255)',
    'UPDATE ' . $db_table . ' SET object_homestreet = object_homeaddress',
    'ALTER TABLE ' . $db_table . ' DROP COLUMN object_homeaddress',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homepob VARCHAR(10)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homecity VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homeprovince VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homepostalcode VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_homecountry VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workstreet VARCHAR(255)',
    'UPDATE ' . $db_table . ' SET object_workstreet = object_workaddress',
    'ALTER TABLE ' . $db_table . ' DROP COLUMN object_workaddress',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workpob VARCHAR(10)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workcity VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workprovince VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workpostalcode VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_workcountry VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_tz VARCHAR(32)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_geo VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_pager VARCHAR(25)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_role VARCHAR(255)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_logotype VARCHAR(10)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_category VARCHAR(80)',
    'ALTER TABLE ' . $db_table . ' ADD COLUMN object_url VARCHAR(255)',
    'CREATE INDEX turba_email_idx ON ' . $db_table . ' (object_email)',
    'CREATE INDEX turba_firstname_idx ON ' . $db_table . ' (object_firstname)',
    'CREATE INDEX turba_lastname_idx ON ' . $db_table . ' (object_lastname)',
);
switch ($config['phptype']) {
case 'mssql':
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_photo VARBINARY(MAX)';
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_logo VARBINARY(MAX)';
    break;
case 'pgsql':
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_photo TEXT';
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_logo TEXT';
    break;
default:
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_photo BLOB';
    $queries[] = 'ALTER TABLE ' . $db_table . ' ADD COLUMN object_logo BLOB';
    break;
}

/* Perform the queries */
/* @TODO - Better error handling */
$error = false;
foreach ($queries as $query) {
    if ($for_real) {
        $results = $db->query($query);
        if (is_a($results, 'PEAR_Error')) {
            $cli->message($results->toString(), 'cli.error');
            $error = true;
            continue;
        }
    }
    $cli->message($query, 'cli.success');
}
if ($error &&
    $cli->prompt('Continue?', array('y' => 'Yes', 'n' => 'No'), 'n') != 'y') {
    exit(1);
}

/* Attempt to transform the fullname into lastname and firstname */
if ($do_name) {
    require_once HORDE_BASE . '/turba/lib/Turba.php';
    $sql = 'SELECT object_id, ' . ($for_real ? 'object_lastname' : 'object_name') . ' FROM ' . $db_table;
    $names = $db->getAssoc($sql);
    if (is_a($names, 'PEAR_Error')) {
        $cli->message($names->toString(), 'cli.error');
        exit(1);
    }
    $insert_query = 'UPDATE ' . $db_table . ' SET object_firstname = ?, object_lastname = ? WHERE object_id = ?';
    if (!$for_real) {
        $cli->writeln($insert_query);
    }
    $insert = $db->prepare($insert_query);
    foreach ($names as $id => $name ) {
        $lastname = Turba::guessLastName($name);
        $firstname = '';
        if (strpos($name, ',') !== false) {
            $firstname = preg_replace('/' . preg_quote($lastname, '/') . ',\s*/', '', $name);
        } elseif ($name != $lastname) {
            $firstname = preg_replace('/\s+' . preg_quote($lastname, '/') . '/', '', $name);
        }
        if ($for_real) {
            $db->execute($insert, array($firstname, $lastname, $id));
        } else {
            $cli->writeln("ID=$id\nFirst name: $firstname; Last name: $lastname; Name: $name\n");
        }
    }
    $cli->message('Contact name fields parsed.', 'cli.success');
} else {
    $cli->message('Contact name fields SKIPPED.', 'cli.success');
}

if ($do_home) {
    $sql = 'SELECT object_id, ' . ($for_real ? 'object_homestreet' : 'object_homeaddress') . ' FROM ' . $db_table;
    $addresses = $db->getAssoc($sql);
    if (is_a($addresses, 'PEAR_Error')) {
        $cli->message($addresses->toString(), 'cli.error');
        exit(1);
    }
    $insert_query = 'UPDATE ' . $db_table . ' SET object_homestreet = ?, object_homecity = ?, object_homeprovince = ?, object_homepostalcode = ?, object_homecountry = ? WHERE object_id = ?';
    if (!$for_real) {
        $cli->writeln($insert_query);
    }
    $insert = $db->prepare($insert_query);
    parseAddress($addresses, $insert, $for_real);
    $cli->message('Home address fields parsed.', 'cli.success');
} else {
    $cli->message('Home address fields SKIPPED.', 'cli.success');
}

if ($do_work) {
    $sql = 'SELECT object_id, ' . ($for_real ? 'object_workstreet' : 'object_workaddress') . ' FROM ' . $db_table;
    $addresses = $db->getAssoc($sql);
    if (is_a($addresses, 'PEAR_Error')) {
        $cli->message($addresses->toString(), 'cli.error');
        exit(1);
    }
    $insert_query = 'UPDATE ' . $db_table . ' SET object_workstreet = ?, object_workcity = ?, object_workprovince = ?, object_workpostalcode = ?, object_workcountry = ? WHERE object_id = ?';
    if (!$for_real) {
        $cli->writeln($insert_query);
    }
    $insert = $db->prepare($insert_query);
    parseAddress($addresses, $insert, $for_real);
    $cli->message('Work address fields parsed.', 'cli.success');
} else {
    $cli->message('Work address fields SKIPPED.', 'cli.success');
}

if ($do_email) {
    $sql = 'SELECT object_id, object_email FROM ' . $db_table;
    $emails = $db->getAssoc($sql);
    if (is_a($emails, 'PEAR_Error')) {
        $cli->message($emails->toString(), 'cli.error');
        exit(1);
    }
   $insert_query = 'UPDATE ' . $db_table . ' SET object_email = ? WHERE object_id = ?';
    if (!$for_real) {
        $cli->writeln($insert_query);
    }
    if ($for_real) {
        $insert = $db->prepare($insert_query);
        foreach ($emails as $id => $email) {
            $db->execute($insert, array(getBareEmail($email), $id));
        }
    } else {
        $cli->writeln($insert_query);
    }
}

/**
 * Helper function to parse out freeform addresses
 *
 * Try to parse out the free form addresses.
 * Assumptions we make to fit into our schema:
 * - Postal code is on the same line as state/province information
 * - If there is a line following the state/province/postal code line,
 *   it is taken as a country.
 * - Any lines before the postal code are treated as street address.
 *
 * @param array $addresses   An array of addresses to parse.
 * @param object $insert     A prepared update query to write the results.
 * @param boolean $for_real  Whether to really change any data.
 */
function parseAddress($addresses, $insert, $for_real)
{
    global $countries;

    foreach ($addresses as $id => $address) {
        if (empty($address)) {
            continue;
        }
        $city = $state = $postalCode = $street = $country = '';
        $p_address = Horde_Form_Type_address::parse($address);
        if (!count($p_address)) {
            $street = $address;
        } else {
            if (!empty($p_address['street'])) {
                $street = $p_address['street'];
            }
            if (!empty($p_address['city'])) {
                $city = $p_address['city'];
            }
            if (!empty($p_address['state'])) {
                $state = $p_address['state'];
            }
            if (!empty($p_address['zip'])) {
                $postalCode = $p_address['zip'];
            }
            if (!empty($p_address['country'])) {
                $country = isset($countries[String::upper($p_address['country'])])
                    ? $countries[String::upper($p_address['country'])]
                    : String::upper($p_address['country']);
            }
        }
        if ($for_real) {
            $GLOBALS['db']->execute($insert, array($street, $city, $state, $postalCode, $country, $id));
        } else {
            $GLOBALS['cli']->writeln("ID: $id\nStreet: $street\nCity: $city\nState: $state\nPostal Code: $postalCode\nCountry: $country\nAddress:\n$address\n");
        }
    }
}

/**
 * Static function to make a given email address rfc822 compliant.
 *
 * @param string $address  An email address.
 *
 * @return string  The RFC822-formatted email address.
 */
function getBareEmail($address)
{
    // Empty values are still empty.
    if (!$address) {
        return $address;
    }

    require_once 'Mail/RFC822.php';
    require_once 'Horde/MIME.php';

    static $rfc822;
    if (is_null($rfc822)) {
        $rfc822 = new Mail_RFC822();
    }

    $rfc822->validateMailbox($address);
    return MIME::rfc822WriteAddress($address->mailbox, $address->host);
}