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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
<?php
/* OpenDb - Open Media Lending Database
Copyright (C) 2001,2002 by Jason Pell
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
include_once("./functions/utils.php");
include_once("./functions/address_type.php");
include_once("./functions/item_attribute.php");
function transfer_email()
{
global $HTTP_SESSION_VARS;
global $CONFIG_VARS;
if(is_array($CONFIG_VARS['email.user_address_attribute']) &&
isset($CONFIG_VARS['email.user_address_attribute']['s_attribute_type']))
{
$user_results = run_opendb_query("SELECT user_id, email, type FROM user");
if($user_results)
{
while($user_r = mysql_fetch_array($user_results))
{
echo ("<p class=\"querytitle\">Transfer email address for user ".$user_r['user_id']."</p>");
$addr_results = fetch_address_type_attribute_type_rs('EMAIL', $user_r['type'], 'update', TRUE);
if($addr_results)
{
// should be only one attribute.
while($addr_attribute_type_r = mysql_fetch_array($addr_results, MYSQL_ASSOC))
{
if($addr_attribute_type_r['s_attribute_type'] == $CONFIG_VARS['email.user_address_attribute']['s_attribute_type'])
{
if(!is_exists_valid_user_address($user_r['user_id'], $addr_attribute_type_r['s_address_type']))
{
$new_sequence_number = insert_user_address($user_r['user_id'], $addr_attribute_type_r['s_address_type']);
if($new_sequence_number !== FALSE)
{
if(insert_user_address_attribute($new_sequence_number, $addr_attribute_type_r['s_attribute_type'], $addr_attribute_type_r['order_no'], $user_r['email']))
{
echo("<div class=\"upgrade_success\">Email address transferred</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Email address not transferred</div>");
}
}
else
{
echo("<div class=\"upgrade_failure\">Email address not transferred</div>");
}
}
else
{
echo("<div class=\"upgrade_warning\">Email address already transferred</div>");
}
}
}
mysql_free_result($addr_results);
}
}
mysql_free_result($user_results);
}
}
}
function is_exists_old_user_email_column()
{
$result = run_opendb_query("SELECT user_id, email FROM user LIMIT 0,1");
if($result)
return TRUE;
else
return FALSE;
}
function is_exists_old_attribute_table()
{
$result = run_opendb_query("SELECT 'x' FROM item_attribute_old LIMIT 0,1");
if($result)
{
mysql_free_result($result);
return TRUE;
}
else
return FALSE;
}
function is_old_attribute_table()
{
$result = run_opendb_query("SELECT lookup_attribute_val FROM item_attribute LIMIT 0,1");
if($result)
{
mysql_free_result($result);
return FALSE;
}
else
{
// not old as it succeeded.
return TRUE;
}
}
function is_lookup_attribute_type_converted()
{
if(is_exists_old_attribute_table())
{
if(!is_old_attribute_table())
{
if(get_old_item_attribute_count()>0)
return get_item_attribute_count()>0;
else
return TRUE; // otherwise nothing to transfer
}
}
else
{
if(!is_old_attribute_table() && get_item_attribute_count()>0)
return TRUE;
else
return FALSE;
}
}
function is_old_user_addr_attr_table()
{
$result = run_opendb_query("SELECT lookup_attribute_val FROM user_address_attribute LIMIT 0,1");
if($result)
{
mysql_free_result($result);
return FALSE;
}
else
{
// not old as it succeeded.
return TRUE;
}
}
/*
* Check whether patch_before has been executed or not. Pick some
* table changes to base this test on, and make sure all table changes
* even those from most recent dev release are included, otherwise
* patch before will not be available.
*/
function is_patch_before_executed()
{
if(run_opendb_query("SELECT change_owner_ind FROM s_status_type LIMIT 0,1") &&
run_opendb_query("SELECT active_ind FROM user LIMIT 0,1"))
{
return TRUE;
}
else
{
return FALSE;
}
}
/*
* Check that these actions have been applied.
* ALTER TABLE user DROP email;
* ALTER TABLE user DROP location;
* DROP TABLE item_attribute_old;
*/
function is_patch_after_executed()
{
if(is_exists_old_user_email_column())
return FALSE;
if(is_exists_old_attribute_table())
return FALSE;
//else
return TRUE;
}
function is_address_type_executed()
{
$result = run_opendb_query("SELECT 'x' FROM s_address_type LIMIT 0,1");
if($result)
return TRUE;
else
return FALSE;
}
/*
* whether we need to run dev11+ patch for additional column in user_address_attribute
* table, that was introduced in dev11.
*/
function is_exists_dev11_user_address_attribute()
{
$result = run_opendb_query("SELECT lookup_attribute_val FROM user_address_attribute LIMIT 0,1");
if($result)
return TRUE;
else
return FALSE;
}
function is_exists_old_user_addr_attr_table()
{
$result = run_opendb_query("SELECT 'x' FROM user_address_attribute_old LIMIT 0,1");
if($result)
{
mysql_free_result($result);
return TRUE;
}
else
{
return FALSE;
}
}
/*
* Assumes s_address_type functionality already installed.
*/
function is_user_addresses_transferred()
{
// the easy way is to see if there are records in the table
if(get_user_addr_attr_count()>0)
return TRUE;
else
{
if(is_exists_old_user_email_column())
{
// we have to be more clever, in case there were no addresses to transfer.
$result = run_opendb_query("SELECT 'x' FROM user WHERE LENGTH(IFNULL(email,'')) > 0");
if($result && mysql_num_rows($result)>0)
{
// if there are records in user table, with a email address not null, then
// the transfer process has not occured.
mysql_free_result($result);
return FALSE;
}
else
{
// looks like we have nothing to transfer.
return TRUE;
}
}
else if(is_exists_old_user_addr_attr_table())
{
// the very first query does a count against the user_address_attribute table, so
// if its empty, then the transfer has not occurred.
return FALSE;
}
else
{
return TRUE;
}
}
}
function get_user_addr_attr_count()
{
$result = run_opendb_query("SELECT COUNT('x') as count FROM user_address_attribute");
if($result)
{
$found = mysql_fetch_array($result);
mysql_free_result($result);
return $found['count'];
}
else
return FALSE;
}
function get_item_attribute_count()
{
$result = run_opendb_query("SELECT COUNT('x') as count FROM item_attribute");
if($result)
{
$found = mysql_fetch_array($result);
mysql_free_result($result);
return $found['count'];
}
else
return FALSE;
}
function get_old_item_attribute_count()
{
$result = run_opendb_query("SELECT COUNT('x') as count FROM item_attribute_old");
if($result)
{
$found = mysql_fetch_array($result);
mysql_free_result($result);
return $found['count'];
}
else
return FALSE;
}
function convert_lookup_item_attributes()
{
// first of all get a list of all s_attribute_type_lookup s_attribute_type's
$lookup_attribute_type_r = array();
$results = run_opendb_query("SELECT DISTINCT s_attribute_type FROM s_attribute_type_lookup");
if($results)
{
while($attribute_type_r = mysql_fetch_array($results))
{
$lookup_attribute_type_r[] = $attribute_type_r['s_attribute_type'];
}
mysql_free_result($results);
}
// Get a list of s_attribute_type_lookups.
$results = run_opendb_query("SELECT item_id, s_attribute_type, order_no, attribute_val, update_on FROM item_attribute_old ORDER BY item_id, order_no");
if($results)
{
$item_id = NULL;
while($item_attribute_type_r = mysql_fetch_array($results))
{
if($item_id == NULL || $item_id != $item_attribute_type_r['item_id'])
{
$item_id = $item_attribute_type_r['item_id'];
echo("<div class=\"colortext\">Transferring attributes for item $item_id...</div>");
}
echo("<div class=\"colortext\">Attribute: ".$item_attribute_type_r['s_attribute_type'].".".$item_attribute_type_r['order_no']."</div>");
// is lookup attribute type
if(in_array($item_attribute_type_r['s_attribute_type'], $lookup_attribute_type_r))
{
$lookup_attribute_val_r = trim_explode(' ', trim($item_attribute_type_r['attribute_val']));
if(insert_lookup_item_attributes($item_attribute_type_r['item_id'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no'], $lookup_attribute_val_r))
{
echo("<div class=\"upgrade_success\">Lookup Attribute transferred successfully.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Lookup Attribute not transferred successfully.</div>");
}
}
else
{
if(insert_item_attribute($item_attribute_type_r['item_id'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no'], trim($item_attribute_type_r['attribute_val'])))
{
echo("<div class=\"upgrade_success\">Attribute transferred successfully.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Attribute not transferred successfully.</div>");
}
}
}
mysql_free_result($results);
echo( "<p><div class=\"success\">Done!</div></p>");
}
}
function check_item_attribute_transfer()
{
echo( "<h3>Lookup Attribute Transfer results...</h3>");
echo("<p>Please Note: <ul><li>The OLD and NEW Distinct Item Counts should be exactly the same.</li>".
"<li>The NEW Lookup Item Attribute Count should be higher than the OLD Lookup Item Attribute Count</p></li></ul>");
// first of all get a list of all s_attribute_type_lookup s_attribute_type's
$lookup_attribute_type_r = array();
$results = run_opendb_query("SELECT DISTINCT s_attribute_type FROM s_attribute_type_lookup");
if($results)
{
while($attribute_type_r = mysql_fetch_array($results))
{
$lookup_attribute_type_r[] = $attribute_type_r['s_attribute_type'];
}
mysql_free_result($results);
}
$in_clause = format_sql_in_clause($lookup_attribute_type_r);
echo("<h2>OLD Lookup Item Attribute, Distinct Item Count</h2>");
echo("<p class=\"querytitle\">SELECT COUNT(DISTINCT item_id) AS count FROM item_attribute_old WHERE s_attribute_type IN($in_clause)</p>");
$result = run_opendb_query("SELECT COUNT(DISTINCT item_id) AS count FROM item_attribute_old WHERE s_attribute_type IN($in_clause)");
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
echo("<div class=\"upgrade_success\">".$found['count']." records.</div>");
echo("<h2>OLD Lookup Item Attribute Count</h2>");
echo("<p class=\"querytitle\">SELECT COUNT(*) AS count FROM item_attribute_old WHERE s_attribute_type IN($in_clause)</p>");
$result = run_opendb_query("SELECT COUNT(*) AS count FROM item_attribute_old WHERE s_attribute_type IN($in_clause)");
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
echo("<div class=\"upgrade_success\">".$found['count']." records.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Count failed.</div>");
}
echo("<h2>NEW Lookup Item Attribute, Distinct Item Count</h2>");
echo("<p class=\"querytitle\">SELECT COUNT(DISTINCT item_id) AS count FROM item_attribute WHERE s_attribute_type IN($in_clause)</p>");
$result = run_opendb_query("SELECT COUNT(DISTINCT item_id) AS count FROM item_attribute WHERE s_attribute_type IN($in_clause)");
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
echo("<div class=\"upgrade_success\">".$found['count']." records.</div>");
echo("<h2>NEW Lookup Item Attribute Count</h2>");
echo("<p class=\"querytitle\">SELECT COUNT(*) AS count FROM item_attribute WHERE s_attribute_type IN($in_clause)</p>");
$result = run_opendb_query("SELECT COUNT(*) AS count FROM item_attribute WHERE s_attribute_type IN($in_clause)");
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
echo("<div class=\"upgrade_success\">".$found['count']." records.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Count failed.</div>");
}
}
else
{
echo("<div class=\"upgrade_failure\">Count failed.</div>");
}
}
else
{
echo("<div class=\"upgrade_failure\">Count failed.</div>");
}
}
/**
* From 0.80-dev11 onwards, we need to add a lookup_attribute_val to the user_address_attribute
* table.
*/
function convert_lookup_user_address_attributes()
{
// first of all get a list of all s_attribute_type_lookup s_attribute_type's
$lookup_attribute_type_r = array();
$results = run_opendb_query("SELECT s_attribute_type FROM s_attribute_type WHERE s_field_type = 'ADDRESS'");
if($results)
{
while($attribute_type_r = mysql_fetch_array($results))
{
if(is_lookup_attribute_type($attribute_type_r['s_attribute_type']))
{
$lookup_attribute_type_r[] = $attribute_type_r['s_attribute_type'];
}
}
mysql_free_result($results);
}
$results = run_opendb_query("SELECT ua_sequence_number, s_attribute_type, order_no, attribute_val FROM user_address_attribute_old ORDER BY ua_sequence_number, order_no");
if($results)
{
$sequence_number = NULL;
while($user_addr_attribute_type_r = mysql_fetch_array($results))
{
if($sequence_number == NULL || $sequence_number != $user_addr_attribute_type_r['ua_sequence_number'])
{
$sequence_number = $user_addr_attribute_type_r['ua_sequence_number'];
echo("<div class=\"colortext\">Transferring user address attributes for ua_sequence_number $sequence_number...</div>");
}
echo("<div class=\"colortext\">Attribute: ".$user_addr_attribute_type_r['s_attribute_type'].".".$user_addr_attribute_type_r['order_no']."</div>");
// is lookup attribute type
if(in_array($user_addr_attribute_type_r['s_attribute_type'], $lookup_attribute_type_r))
{
$lookup_attribute_val_r = trim_explode(' ', $user_addr_attribute_type_r['attribute_val']);
if(insert_lookup_user_address_attributes($user_addr_attribute_type_r['ua_sequence_number'], $user_addr_attribute_type_r['s_attribute_type'], $user_addr_attribute_type_r['order_no'], $lookup_attribute_val_r))
{
echo("<div class=\"upgrade_success\">Lookup Attribute transferred successfully.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Lookup Attribute not transferred successfully.</div>");
}
}
else
{
if(insert_user_address_attribute($user_addr_attribute_type_r['ua_sequence_number'], $user_addr_attribute_type_r['s_attribute_type'], $user_addr_attribute_type_r['order_no'], trim($user_addr_attribute_type_r['attribute_val'])))
{
echo("<div class=\"upgrade_success\">Attribute transferred successfully.</div>");
}
else
{
echo("<div class=\"upgrade_failure\">Attribute not transferred successfully.</div>");
}
}
}
mysql_free_result($results);
echo( "<p><div class=\"success\">Done!</div></p>");
}
}
if($CONFIG_VARS['patch.enable']===TRUE)
{
if($HTTP_VARS['op'] == 'menu')
{
echo("<p></p>");
echo("<DIV ALIGN=\"CENTER\">");
echo("<FORM action=\"$PHP_SELF\" method=\"get\" target=\"main\">");
print_patch_button("patch_before", "1. Execute patch_before.sql", !is_patch_before_executed());
echo(" ");
print_patch_button("address_type_sql", "2. Execute address_type.sql", is_patch_before_executed() && !is_address_type_executed());
echo(" ");
print_patch_button("transfer_email", "3. Transfer Email Addresses", is_patch_before_executed() && is_address_type_executed() && is_exists_old_user_email_column() && !is_user_addresses_transferred());
echo(" ");
print_patch_button("item_attribute_sql", "4. Execute item_attribute.sql", is_patch_before_executed() && is_address_type_executed() && is_user_addresses_transferred() && is_old_attribute_table());
echo(" ");
print_patch_button("convert_lookup_item_attributes", "5. Convert Lookup Item Attributes", is_patch_before_executed() && is_address_type_executed() && !is_lookup_attribute_type_converted() && is_exists_old_attribute_table());
echo(" ");
$next_step_no = '6';
// do not show these options for pre dev-11 patching
if(is_patch_before_executed() && is_address_type_executed() &&
(!is_exists_dev11_user_address_attribute() || is_exists_old_user_addr_attr_table()))
{
echo(" ");
print_patch_button("convert_user_addr_attributes1", "6. Convert User Address Attributes Part 1 (0.80-dev11+)", is_patch_before_executed() && is_address_type_executed() && !is_old_attribute_table() && is_lookup_attribute_type_converted() && !is_exists_old_user_addr_attr_table());
echo(" ");
print_patch_button("convert_user_addr_attributes2", "7. Convert User Address Attributes Part 2 (0.80-dev11+)", is_patch_before_executed() && is_address_type_executed() && !is_old_attribute_table() && is_lookup_attribute_type_converted() && is_exists_old_user_addr_attr_table() && !is_user_addresses_transferred());
echo(" ");
print_patch_button("convert_user_addr_attributes3", "8. Convert User Address Attributes Part 3 (0.80-dev11+)", is_patch_before_executed() && is_address_type_executed() && !is_old_attribute_table() && is_lookup_attribute_type_converted() && is_exists_old_user_addr_attr_table() && is_user_addresses_transferred());
echo(" ");
$next_step_no = '9';
}
print_patch_button("patch_after", $next_step_no.". Execute patch_after.sql", is_patch_before_executed() && is_address_type_executed() && is_lookup_attribute_type_converted() && is_user_addresses_transferred() && !is_patch_after_executed());
echo("<INPUT TYPE=\"hidden\" name=\"op\" value=\"result\">");
echo("<INPUT TYPE=\"hidden\" name=\"from\" value=\"".$HTTP_VARS['from']."\">");
echo("</FORM>");
echo("</DIV>");
echo("<p class=\"colortext\">Please ensure that you complete ALL steps. The steps MUST be completed
in order. If you experience problems with Adding/Editing Items, such as 'Invalid Item Type' errors,
then it is quite possible that this patch was <b>not</b> completed successfully.</b></p>");
}
else
{
if(isset($HTTP_VARS['patch_before'] ))
exec_patch_sql_file($HTTP_VARS['from'], "patch_before.sql", TRUE);
else if(isset($HTTP_VARS['address_type_sql']))
exec_patch_sql_file($HTTP_VARS['from'], "address_type.sql", TRUE);
else if(isset($HTTP_VARS['transfer_email']))
transfer_email();
else if(isset($HTTP_VARS['item_attribute_sql']))
exec_patch_sql_file($HTTP_VARS['from'], "item_attribute.sql", TRUE);
else if(isset($HTTP_VARS['convert_lookup_item_attributes']))
{
convert_lookup_item_attributes();
// now display the results of the transfer
check_item_attribute_transfer();
}
else if(isset($HTTP_VARS['convert_user_addr_attributes1']))
exec_patch_sql_file($HTTP_VARS['from'], "user_address_attribute.sql", TRUE);
else if(isset($HTTP_VARS['convert_user_addr_attributes2']))
convert_lookup_user_address_attributes();
else if(isset($HTTP_VARS['convert_user_addr_attributes3']))
exec_patch_sql_statement("DROP TABLE user_address_attribute_old", TRUE);
else if(isset($HTTP_VARS['patch_after'] ))
exec_patch_sql_file($HTTP_VARS['from'], "patch_after.sql", TRUE);
}
}
?>
|