PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mrbs-1.4.8/web/upgrade/24/post.inc

#
PHP | 154 lines | 92 code | 20 blank | 42 comment | 27 complexity | 0fdf633791c6c4073fec7346b2bb81e3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // $Id: post.inc 1742 2011-01-07 17:00:33Z cimorrison $
  3. // This upgrade assings a UID to all existing bookings and a RECURRENCE-ID to all
  4. // existing members of a series. However the RECURRENCE-ID is not necessarily going
  5. // to be correct if the entry is a modified entry (ie entry_type=ENTRY_RPT_CHANGED).
  6. // That's because the RECURRENCE_ID is supposed to be the *original* start date and
  7. // time of that entry, and this is information that we no longer have. (We do know
  8. // the time, as we have the start time in the repeat table, but it is not trivial to
  9. // take advantage of it as we'd have to account for DST changes and we still don't
  10. // have the date. So I've taken the view here that it's not worth bothering about
  11. // as this upgrade procedure is only used once for existing entries).
  12. function get_microtime()
  13. {
  14. if (function_exists('microtime'))
  15. {
  16. list($usec, $sec) = explode(" ", microtime());
  17. return ((float)$usec + (float)$sec);
  18. }
  19. else
  20. {
  21. return time();
  22. }
  23. }
  24. function do_sql_command($sql, $step)
  25. {
  26. if (sql_command($sql) < 0)
  27. {
  28. // echo "$sql<br>\n"; // Debug line
  29. // echo sql_error() . "<br>"; // Debug line
  30. echo "<span class=\"error\">Step $step failed</span><br>";
  31. // exit; // Debug line
  32. }
  33. }
  34. global $dbsys, $tbl_entry, $tbl_repeat;
  35. // There's the option here to display some timing information as this upgrade
  36. // could take a while
  37. $display_timing = FALSE;
  38. $n_entry = sql_query1("SELECT COUNT(*) FROM $tbl_entry");
  39. $n_repeat = sql_query1("SELECT COUNT(*) FROM $tbl_repeat");
  40. if ($display_timing)
  41. {
  42. echo "Upgrade 24: $n_entry entry rows and $n_repeat repeat rows";
  43. }
  44. $start_clock = get_microtime();
  45. // MySQL (mysql and mysqli)
  46. // ------------------------
  47. if ($dbsys != "pgsql")
  48. {
  49. // Give all the individual entries, that haven't already got one, an ical uid
  50. $sql = "UPDATE $tbl_entry SET ical_uid=CONCAT(CAST(id AS char), '-', UUID()) WHERE ical_uid='' AND repeat_id=0";
  51. do_sql_command($sql, 1);
  52. // Give everything in the repeat table, that doesn't already have one, an ical uid
  53. $sql = "UPDATE $tbl_repeat SET ical_uid=CONCAT(CAST(id AS char), '-', UUID()) WHERE ical_uid=''";
  54. do_sql_command($sql, 2);
  55. // Now go through the entry table and give all entries, that don't have an ical uid
  56. // and are members of a series, the ical_uid from the corresponding uid from the
  57. // repeat table
  58. $sql = "UPDATE $tbl_entry E, $tbl_repeat R
  59. SET E.ical_uid=R.ical_uid
  60. WHERE E.ical_uid=''
  61. AND E.repeat_id=R.id";
  62. do_sql_command($sql, 3);
  63. // Finally give a recurrence id to any entry in the entry table that hasn't got one
  64. // and should have one - ie if it is a member of a series
  65. $sql = "UPDATE $tbl_entry
  66. SET ical_recur_id=DATE_FORMAT(CONVERT_TZ(FROM_UNIXTIME(start_time), @@session.time_zone, '+0:00'), '%Y%m%dT%H%i%sZ')
  67. WHERE repeat_id!=0
  68. AND ical_recur_id=''";
  69. do_sql_command($sql, 4);
  70. }
  71. // PostgreSQL
  72. // ----------
  73. else
  74. {
  75. // PostgreSQL doesn't have a UUID() function as standard, so we have to construct
  76. // our own UUID.
  77. //
  78. // We will generate a uid of the form "MRBS-uniqid-MD5hash@domain_name"
  79. // where uniqid is time based and is generated by uniqid() and the
  80. // MD5hash is the first 8 characters of the MD5 hash of $str concatenated
  81. // with a random number. [This is the same process used by the MRBS function
  82. // generate_global_uid()]
  83. if (empty($_SERVER['SERVER_NAME']))
  84. {
  85. $domain_name .= 'MRBS';
  86. }
  87. elseif (strpos($_SERVER['SERVER_NAME'], 'www.') === 0)
  88. {
  89. $domain_name .= substr($_SERVER['SERVER_NAME'], 4);
  90. }
  91. else
  92. {
  93. $domain_name .= $_SERVER['SERVER_NAME'];
  94. }
  95. // Give all the individual entries, that haven't already got one, an ical uid
  96. $sql = "UPDATE $tbl_entry
  97. SET ical_uid='MRBS-' || CAST(id AS varchar(255)) || '-' || CURRENT_DATE || CURRENT_TIME || '-' || SUBSTRING((MD5(name || CAST(RANDOM() AS varchar(255)))) from 1 for 8) || '@$domain_name'
  98. WHERE ical_uid=''
  99. AND repeat_id=0";
  100. do_sql_command($sql, 1);
  101. // Give everything in the repeat table, that doesn't already have one, an ical uid
  102. $sql = "UPDATE $tbl_repeat
  103. SET ical_uid='MRBS-' || CAST(id AS varchar(255)) || '-' || CURRENT_DATE || CURRENT_TIME || '-' || SUBSTRING((MD5(name || CAST(RANDOM() AS varchar(255)))) from 1 for 8) || '@$domain_name'
  104. WHERE ical_uid=''";
  105. do_sql_command($sql, 2);
  106. // Now go through the entry table and give all entries, that don't have an ical uid
  107. // and are members of a series, the ical_uid from the corresponding uid from the
  108. // repeat table. (The SQL is slightly different from the MySQL case)
  109. $sql = "UPDATE $tbl_entry
  110. SET ical_uid=R.ical_uid
  111. FROM $tbl_repeat AS R
  112. WHERE $tbl_entry.ical_uid=''
  113. AND $tbl_entry.repeat_id=R.id";
  114. do_sql_command($sql, 3);
  115. // Finally give a recurrence id to any entry in the entry table that hasn't got one
  116. // and should have one - ie if it is a member of a series (The SQL is slightly
  117. // different from the MySQL case)
  118. $sql = "UPDATE $tbl_entry
  119. SET ical_recur_id=TO_CHAR(TIMESTAMP 'epoch' + start_time * INTERVAL '1 second', 'YYYYMMDD\"T\"HH24MISS\"Z\"')
  120. WHERE repeat_id!=0
  121. AND ical_recur_id=''";
  122. do_sql_command($sql, 4);
  123. }
  124. $stop_clock = get_microtime();
  125. $clock_diff = $stop_clock - $start_clock;
  126. if (is_float($start_clock))
  127. {
  128. $clock_diff = sprintf('%.3f', $clock_diff);
  129. }
  130. if ($display_timing)
  131. {
  132. echo " processed in $clock_diff seconds<br>\n";
  133. }
  134. ?>