PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/public/upgrade/scripts/AddressBookUpgradeScript.class.php

https://gitlab.com/x33n/ProjectPier-Core
PHP | 220 lines | 120 code | 43 blank | 57 comment | 23 complexity | c3e1e4e9412fe9f7e983cefb35c226cb MD5 | raw file
  1. <?php
  2. /**
  3. * Upgrade for Address Book patch for ProjectPier 0.8.x
  4. *
  5. * @package ScriptUpgrader.scripts
  6. * @http://www.projectpier.org/
  7. */
  8. class AddressBookUpgradeScript extends ScriptUpgraderScript {
  9. /**
  10. * Database connection link
  11. *
  12. * @var resource
  13. */
  14. private $database_connection = null;
  15. /**
  16. * Construct the AddressBookUpgradeScript
  17. *
  18. * @param Output $output
  19. * @return AddressBookUpgradeScript
  20. */
  21. function __construct(Output $output) {
  22. parent::__construct($output);
  23. $this->setVersionFrom('0.8.0');
  24. $this->setVersionTo('0.8.0');
  25. } // __construct
  26. /**
  27. * Execute the script
  28. *
  29. * @param void
  30. * @return boolean
  31. */
  32. function execute() {
  33. define('ROOT', realpath(dirname(__FILE__) . '/../../../'));
  34. // ---------------------------------------------------
  35. // Load config
  36. // ---------------------------------------------------
  37. $config_is_set = require_once INSTALLATION_PATH . '/config/config.php';
  38. if (!$config_is_set) {
  39. $this->printMessage('Valid config files was not found!', true);
  40. return false;
  41. } else {
  42. $this->printMessage('Config file found and loaded.');
  43. } // if
  44. if (substr(PRODUCT_VERSION, 0, 3) !== '0.8') {
  45. $this->printMessage('This upgrade script is intended for version 0.8.x. You\'re running ProjectPier v.'.PRODUCT_VERSION.'.', true);
  46. return false;
  47. } // if
  48. // ---------------------------------------------------
  49. // Connect to database
  50. // ---------------------------------------------------
  51. if ($this->database_connection = mysql_connect(DB_HOST, DB_USER, DB_PASS)) {
  52. if (mysql_select_db(DB_NAME, $this->database_connection)) {
  53. $this->printMessage('Upgrade script has connected to the database.');
  54. } else {
  55. $this->printMessage('Failed to select database ' . DB_NAME);
  56. return false;
  57. } // if
  58. } else {
  59. $this->printMessage('Failed to connect to database', true);
  60. return false;
  61. } // if
  62. // ---------------------------------------------------
  63. // Check existence of tables for Address Book
  64. // ---------------------------------------------------
  65. $tables_to_check = array('contacts');
  66. foreach ($tables_to_check as $table) {
  67. $test_table_exists_sql = "SHOW TABLES LIKE '".TABLE_PREFIX."$table';";
  68. if (mysql_num_rows(mysql_query($test_table_exists_sql, $this->database_connection))) {
  69. $this->printMessage("Table ".TABLE_PREFIX."$table already exists. You might have done that upgrade already. It is recommended to proceed with the upgrade manually.", true);
  70. return false;
  71. }
  72. } // foreach
  73. $this->printMessage('The tables that need to be created do not exist already. It is safe to proceed with the database migration.');
  74. // ---------------------------------------------------
  75. // Check MySQL version
  76. // ---------------------------------------------------
  77. $mysql_version = mysql_get_server_info($this->database_connection);
  78. if ($mysql_version && version_compare($mysql_version, '4.1', '>=')) {
  79. $constants['DB_CHARSET'] = 'utf8';
  80. mysql_query("SET NAMES 'utf8'", $this->database_connection);
  81. tpl_assign('default_collation', $default_collation = 'collate utf8_unicode_ci');
  82. tpl_assign('default_charset', $default_charset = 'DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');
  83. } else {
  84. tpl_assign('default_collation', $default_collation = '');
  85. tpl_assign('default_charset', $default_charset = '');
  86. } // if
  87. tpl_assign('table_prefix', TABLE_PREFIX);
  88. // ---------------------------------------------------
  89. // Check test query
  90. // ---------------------------------------------------
  91. $test_table_name = TABLE_PREFIX . 'test_table';
  92. $test_table_sql = "CREATE TABLE `$test_table_name` (
  93. `id` int(10) unsigned NOT NULL auto_increment,
  94. `name` varchar(50) $default_collation NOT NULL default '',
  95. PRIMARY KEY (`id`)
  96. ) ENGINE=InnoDB $default_charset;";
  97. if (mysql_query($test_table_sql, $this->database_connection)) {
  98. $this->printMessage('Test query has been executed. It\'s safe to proceed with database migration.');
  99. mysql_query("DROP TABLE `$test_table_name`", $this->database_connection);
  100. } else {
  101. $this->printMessage('Failed to executed test query. MySQL said: ' . mysql_error($this->database_connection), true);
  102. return false;
  103. } // if
  104. mysql_query('BEGIN WORK');
  105. // ---------------------------------------------------
  106. // Contact table creation
  107. // ---------------------------------------------------
  108. $total_queries = 0;
  109. $executed_queries = 0;
  110. $upgrade_script = tpl_fetch(get_template_path('db_migration/addressbook_contactcreation'));
  111. if ($this->executeMultipleQueries($upgrade_script, $total_queries, $executed_queries, $this->database_connection)) {
  112. $this->printMessage("'Contacts' table correctly executed (total queries: $total_queries)");
  113. } else {
  114. $this->printMessage('Failed to execute DB schema transformations. MySQL said: ' . mysql_error(), true);
  115. mysql_query('ROLLBACK');
  116. return false;
  117. } // if
  118. // ---------------------------------------------------------------
  119. // Migration of existing users to contacts with associated users.
  120. // ---------------------------------------------------------------
  121. $contacts_created = 0;
  122. $users_table = TABLE_PREFIX.'users';
  123. $contacts_table = TABLE_PREFIX.'contacts';
  124. $rows = mysql_query("SELECT * FROM `$users_table`");
  125. while ($row = mysql_fetch_assoc($rows)) {
  126. if (!mysql_query("INSERT INTO $contacts_table (`company_id` , `user_id` , `email` , `display_name` , `title` , `avatar_file` , `office_number` , `fax_number` , `mobile_number` , `home_number` , `created_on` , `created_by_id` , `updated_on`) VALUES ('".$row['company_id']."', '".$row['id']."', '".$row['email']."', '".$row['display_name']."', '".$row['title']."', '".$row['avatar_file']."', '".$row['office_number']."', '".$row['fax_number']."' , '".$row['mobile_number']."' , '".$row['home_number']."' , '".$row['created_on']."', '".$row['created_by_id']."', '".$row['updated_on']."');")) {
  127. $this->printMessage("Error while creating contact. Operation aborted. MySQL said: ".mysql_error($this->database_connection), true);
  128. mysql_query('ROLLBACK');
  129. return false;
  130. }
  131. $contacts_created++;
  132. } // while
  133. $this->printMessage("$contacts_created contacts properly imported.");
  134. // ---------------------------------------------------------------
  135. // Modification of existing User table
  136. // ---------------------------------------------------------------
  137. $total_queries = 0;
  138. $executed_queries = 0;
  139. $upgrade_script = tpl_fetch(get_template_path('db_migration/addressbook_dbalteration'));
  140. if ($this->executeMultipleQueries($upgrade_script, $total_queries, $executed_queries, $this->database_connection)) {
  141. $this->printMessage("Database tables correctly modified. (total queries: $total_queries)");
  142. } else {
  143. $this->printMessage('Failed to execute DB schema transformations. MySQL said: ' . mysql_error(), true);
  144. mysql_query('ROLLBACK');
  145. return false;
  146. } // if
  147. // ---------------------------------------------------------------
  148. // Migration of UserIm to ContactIm (change user_id to contact_id)
  149. // ---------------------------------------------------------------
  150. $contact_im_changed = 0;
  151. $contacts_table = TABLE_PREFIX.'contacts';
  152. $contact_im_table = TABLE_PREFIX.'contact_im_values';
  153. // NB: user_id was renamed contact_id in the previous step
  154. $rows = mysql_query("SELECT `$contacts_table`.`id`, `$contact_im_table`.`contact_id`, `$contact_im_table`.`im_type_id` FROM `$contacts_table`, `$contact_im_table` WHERE `$contacts_table`.`user_id` = `$contact_im_table`.`contact_id`");
  155. while ($row = mysql_fetch_assoc($rows)) {
  156. if (!mysql_query("UPDATE `$contact_im_table` SET `contact_id` = '".$row['id']."' WHERE `contact_id` = '".$row['contact_id']."' AND `im_type_id` = '".$row['im_type_id']."'")) {
  157. $this->printMessage("Error while updating Contact-IM table. Upgrade aborted. MySQL said: ".mysql_error($this->database_connection), true);
  158. mysql_query('ROLLBACK');
  159. return false;
  160. } // if
  161. $contact_im_changed++;
  162. } // while
  163. $this->printMessage("$contact_im_changed contact-IM associations properly imported.");
  164. mysql_query('COMMIT');
  165. $this->printMessage('ProjectPier has been patched for Address Book. Enjoy!');
  166. } // execute
  167. /**
  168. * Return script name.
  169. *
  170. * @param void
  171. * @return string
  172. */
  173. function getScriptName() {
  174. return 'Upgrade of DB for Address Book patch (separation of contacts and users)';
  175. } // getName
  176. } // AddressBookUpgradeScript
  177. ?>