/**
  * Executes the specified modifications.
  *
  * @param	boolean	Whether to close the list tags at the end of exeuction
  */
 function execute($close_list = true)
 {
     if (!$this->inside_list) {
         echo "<ul>\n";
         $this->inside_list = true;
     }
     foreach ($this->modifications as $modification) {
         if (!empty($modification['message'])) {
             echo "\t<li>{$modification['message']}</li>\n";
         }
         $data =& $modification['data'];
         if (!empty($modification['alter'])) {
             $db_alter =& $this->setup_db_alter_class($data['table']);
         } else {
             unset($db_alter);
         }
         $alter_result = null;
         switch ($modification['modification_type']) {
             case 'add_field':
                 $alter_result = $db_alter->add_field($data);
                 break;
             case 'drop_field':
                 $alter_result = $db_alter->drop_field($data['name']);
                 break;
             case 'add_index':
                 $alter_result = $db_alter->add_index($data['name'], $data['fields'], $data['type']);
                 break;
             case 'drop_index':
                 $alter_result = $db_alter->drop_index($data['name']);
                 break;
             case 'run_query':
                 $error_state = $this->db->reporterror;
                 if ($error_state) {
                     $this->db->hide_errors();
                 }
                 $query_result = $this->db->query_write($data['query']);
                 if ($errno = $this->db->errno()) {
                     if (!in_array($errno, $data['ignorable_errors'])) {
                         echo "</ul>";
                         $this->db->show_errors();
                         $this->db->sql = $data['query'];
                         $this->db->halt();
                     } else {
                         // error occurred, but was ignorable
                         $this->db->errno = 0;
                     }
                 }
                 if ($error_state) {
                     $this->db->show_errors();
                 }
                 break;
             case 'show_message':
                 // do nothing -- just show the message
                 break;
             case 'debug_break':
                 echo "</ul><div>Debug break point. Stopping execution.</div>";
                 exit;
             default:
                 trigger_error("<strong>vB_UpgradeQueries</strong>: Invalid modification type '{$modification['modification_type']}' specified.", E_USER_ERROR);
         }
         if ($alter_result === false) {
             if ($db_alter->error_no == ERRDB_MYSQL) {
                 echo "</ul>";
                 $this->db->show_errors();
                 $this->db->sql = $db_alter->sql;
                 $this->db->connection_recent = null;
                 $this->db->error = $db_alter->error_desc;
                 $this->db->errno = -1;
                 $this->db->halt();
             } else {
                 if (ob_start()) {
                     print_r($modification);
                     $results = ob_get_contents();
                     ob_end_clean();
                 } else {
                     $results = serialize($modification);
                 }
                 echo "\t\t<!-- {$results}\n\t\t" . "Error information: " . $db_alter->error_no . " / " . htmlspecialchars($db_alter->error_desc) . " -->\n\n";
             }
         }
     }
     if ($close_list) {
         echo "</ul>\n";
         $this->inside_list = false;
     }
     $this->modifications = array();
 }
Exemple #2
0
 /**
  * Executes the specified modifications.
  *
  * @param	boolen	Check if table exists for create table commands
  * @param	array		return value from step execution
  *
  * @return	mixed	Return array upon error
  */
 public function execute($check_table = true, $result = null)
 {
     $this->response['returnvalue'] = $result;
     if ($check_table and !$this->check_table_conflict()) {
         $this->add_message($this->phrase['core']['table_conflict']);
         $this->modifications = array();
         return $this->response;
     }
     foreach ($this->modifications as $modification) {
         $this->add_message($modification['message'], 'STANDARD', $modification['replace'] ? $modification['replace'] : false);
         $data =& $modification['data'];
         if (!empty($modification['alter'])) {
             $db_alter =& $this->setup_db_alter_class($data['table']);
         } else {
             unset($db_alter);
         }
         $alter_result = null;
         switch ($modification['modification_type']) {
             case 'add_field':
                 $alter_result = $db_alter->add_field($data);
                 if ($errno = $this->db->errno() and in_array($errno, $data['ignorable_errors'])) {
                     $alter_result = true;
                 }
                 break;
             case 'drop_field':
                 $alter_result = $db_alter->drop_field($data['name']);
                 if ($errno = $this->db->errno() and in_array($errno, $data['ignorable_errors'])) {
                     $alter_result = true;
                 }
                 break;
             case 'add_index':
                 $alter_result = $db_alter->add_index($data['name'], $data['fields'], $data['type']);
                 if ($errno = $this->db->errno() and in_array($errno, $data['ignorable_errors'])) {
                     $alter_result = true;
                 }
                 break;
             case 'drop_index':
                 $alter_result = $db_alter->drop_index($data['name']);
                 if ($errno = $this->db->errno() and in_array($errno, $data['ignorable_errors'])) {
                     $alter_result = true;
                 }
                 break;
             case 'run_query':
                 $error_state = $this->db->reporterror;
                 if ($error_state) {
                     $this->db->hide_errors();
                 }
                 $query_result = $this->db->query_write("### vBulletin Database Alter ###\r\n" . $data['query']);
                 if ($errno = $this->db->errno()) {
                     if (!in_array($errno, $data['ignorable_errors'])) {
                         if ($errno == self::MYSQL_ERROR_CANT_CREATE_TABLE) {
                             if (stripos($this->db->error, 'errno: 121') !== false and stripos($data['query'], 'engine=innodb')) {
                                 preg_match('#CREATE TABLE ([a-z0-9_]+)#si', $data['query'], $matches);
                                 $this->add_error(sprintf($this->phrase['core']['table_creation_x_failed'], $matches[1]), self::PHP_TRIGGER_ERROR, true);
                                 $this->modifications = array();
                                 return $this->response;
                             }
                         }
                         $this->add_error(array('message' => $data['query'], 'error' => $this->db->error, 'errno' => $this->db->errno), self::MYSQL_HALT, true);
                         $this->modifications = array();
                         return $this->response;
                     } else {
                         // error occurred, but was ignorable
                         $this->db->errno = 0;
                     }
                 }
                 if ($error_state) {
                     $this->db->show_errors();
                 }
                 break;
             case 'show_message':
                 // do nothing -- just show the message
                 break;
             case 'debug_break':
                 //	echo "</ul><div>Debug break point. Stopping execution.</div>";
                 //	exit;
             //	echo "</ul><div>Debug break point. Stopping execution.</div>";
             //	exit;
             default:
                 $this->add_error(sprintf($this->phrase['core']['invalid_modification_type_x'], $modification['modification_type']), self::PHP_TRIGGER_ERROR, true);
                 $this->modifications = array();
                 return $this->response;
         }
         if ($alter_result === false) {
             if ($db_alter->error_no == ERRDB_MYSQL) {
                 $this->db->show_errors();
                 $this->db->sql = $db_alter->sql;
                 $this->db->connection_recent = null;
                 $this->db->error = $db_alter->error_desc;
                 $this->db->errno = -1;
                 $this->add_error(array('message' => $this->db->sql, 'error' => $this->db->error, 'errno' => $this->db->errno), self::MYSQL_HALT, true);
                 $this->modifications = array();
                 return $this->response;
             } else {
                 if (ob_start()) {
                     print_r($modification);
                     $results = ob_get_contents();
                     ob_end_clean();
                 } else {
                     $results = serialize($modification);
                 }
                 $this->add_error(array('message' => $results, 'error' => $db_alter->error_desc, 'errno' => $db_alter->error_no), self::MYSQL_ERROR, false);
                 //$this->modifications = array();
                 //return $this->response;
             }
         }
     }
     $this->modifications = array();
     return $this->response;
 }