[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/admin/ -> upgrade_inc.php (source)

   1  <?php
   2  # Mantis - a php based bugtracking system
   3  
   4  # Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
   5  # Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
   6  
   7  # Mantis is free software: you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation, either version 2 of the License, or
  10  # (at your option) any later version.
  11  #
  12  # Mantis is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  #
  17  # You should have received a copy of the GNU General Public License
  18  # along with Mantis.  If not, see <http://www.gnu.org/licenses/>.
  19  
  20      # --------------------------------------------------------
  21      # $Id: upgrade_inc.php,v 1.21.2.1 2007-10-13 22:34:59 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      require_once ( 'db_table_names_inc.php' );
  25  
  26      # Compatibility function
  27      #
  28      # The old upgrade system used this logic to determine whether an upgrade
  29      #  had been done.  We use the same system to check and update the user's
  30      #  database with the appropriate notations to indicate they have been done.
  31  	function admin_check_applied( $p_table_name, $p_field_name='' ) {
  32          $c_table_name = db_prepare_string( $p_table_name );
  33          $c_field_name = db_prepare_string( $p_field_name );
  34  
  35          $result = db_query( "DESCRIBE $c_table_name $c_field_name" );
  36  
  37          if ( $result && ( 0 < db_num_rows( $result ) ) ) {
  38              return true;
  39          } else {
  40              return false;
  41          }
  42      }
  43      
  44  ?>
  45  <?php
  46      class Upgrade {
  47          var $id;
  48          var $description;
  49  
  50  		function Upgrade( $p_id, $p_description ) {
  51              if ( strlen( $p_id ) > 20 ) {
  52                  echo "Upgrade Id '$p_id' too long.  Maximum length is 20 characters";
  53                  exit;
  54              }
  55  
  56              $this->id = $p_id;
  57              $this->description = $p_description;
  58              $this->error = '';
  59          }
  60  
  61  		function is_applied() {
  62              $t_upgrade_table = config_get_global( 'mantis_upgrade_table' );
  63              
  64              if ( ! db_table_exists( $t_upgrade_table ) ) {
  65                  return false;
  66              }
  67  
  68              $query = "SELECT COUNT(*)
  69                        FROM $t_upgrade_table
  70                        WHERE upgrade_id = '$this->id'";
  71  
  72              $result = db_query( $query );
  73  
  74              if ( 0 < db_result( $result ) ) {
  75                  return true;
  76              } else {
  77                  return false;
  78              }
  79          }
  80  
  81  		function set_applied() {
  82              $t_upgrade_table = config_get_global( 'mantis_upgrade_table' );
  83  
  84              $query = "INSERT INTO $t_upgrade_table
  85                          (upgrade_id, description)
  86                        VALUES
  87                          ('$this->id', '$this->description')";
  88  
  89              db_query( $query );
  90          }
  91      }
  92  
  93      class SQLUpgrade extends Upgrade {
  94          var $query;
  95  
  96  		function SQLUpgrade( $p_id, $p_description, $p_query ) {
  97              Upgrade::Upgrade( $p_id, $p_description );
  98  
  99              $this->query = $p_query;
 100          }
 101  
 102  		function execute() {
 103              $result = @db_query( $this->query );
 104  
 105              if ( $result ) {
 106                  $this->set_applied();
 107              } else {
 108                  $this->error = db_error_msg();
 109              }
 110  
 111              return $result;
 112          }
 113  
 114  		function display() {
 115              $t_description = "# Upgrade $this->id: $this->description<br />";
 116              $t_description .= $this->query . "<br /><br />";
 117  
 118              return $t_description;
 119          }
 120      }
 121  
 122      class FunctionUpgrade extends Upgrade {
 123          var $function_name;
 124  
 125  		function FunctionUpgrade ( $p_id, $p_description, $p_function_name ) {
 126              Upgrade::Upgrade( $p_id, $p_description );
 127  
 128              $this->function_name = $p_function_name;
 129          }
 130  
 131  		function execute() {
 132              if ( ! function_exists( $this->function_name ) ) {
 133                  $this->error = "Function $this->function_name does not exist";
 134                  return false;
 135              }
 136  
 137              $result = call_user_func( $this->function_name );
 138  
 139              if ( $result ) {
 140                  $this->set_applied();
 141              } else {
 142                  $this->error = "Function $this->function_name() returned false<br />";
 143                  $t_db_error = db_error_msg();
 144                  if ( !is_blank( $t_db_error ) ) {
 145                      $this->error .= "Last database error (may not be applicable) was: "
 146                                      . $t_db_error;
 147                  }
 148              }
 149  
 150              return $result;
 151          }
 152  
 153  		function display() {
 154              return "# Upgrade $this->id: $this->description<br /># Execute function $this->function_name()<br /><br />";
 155          }
 156      }
 157  
 158      class ReleaseUpgrade extends Upgrade {
 159          var $release_name;
 160  
 161  		function ReleaseUpgrade ( $p_release ) {
 162              Upgrade::Upgrade( 'release_' . $p_release, 'Mark release for database version ' . $p_release );
 163  
 164              $this->release_name = $p_release;
 165          }
 166  
 167  		function execute() {
 168              config_set( 'database_version', $this->release_name );
 169              $this->set_applied();
 170  
 171              return true;
 172          }
 173  
 174  		function display() {
 175              return "# Upgrade $this->id: $this->description<br /><br />";
 176          }
 177      }
 178  
 179      class UpgradeSet {
 180          var $item_array;
 181          var $upgrade_name;
 182          var $upgrade_file;
 183  
 184  		function UpgradeSet( $p_name='Mantis Upgrade', $p_filename='mantis_upgrade' ) {
 185              $this->item_array = array();
 186              $this->upgrade_name = $p_name;
 187              $this->upgrade_file = $p_filename;
 188          }
 189  
 190  		function add_item( $p_item ) {
 191              array_push( $this->item_array, $p_item );
 192          }
 193  
 194  		function add_items( $p_items ) {
 195              foreach ( $p_items as $t_item ) {
 196                  $this->add_item( $t_item );
 197              }
 198          }
 199          
 200          # add items, and check if they can be marked as completed
 201  		function add_items_with_check( $p_upgrade_file, $p_table_check='' ) {
 202          
 203              $t_start_count = $this->count_items();
 204              $this->add_items( include( $p_upgrade_file ) );
 205              $t_end_count = $this->count_items();
 206              # check for table presence and db version and mark as applied if either the table
 207              #  is present, or our version stamp is lower than the actual db
 208              if ( ( ( $p_table_check != '' ) && admin_check_applied( $p_table_check ) ) ) {
 209                  for ( $i = $t_start_count; $i < $t_end_count; $i++ ) {
 210                      if ( ! $this->is_applied( $i ) ) {
 211                          $this->set_applied( $i );
 212                      }
 213                  }
 214              }
 215          }
 216  
 217  
 218          # return count of items in upgrade set. Used to flag applied items in old databases
 219  		function count_items() {
 220              if ( isset( $this->item_array ) ) {
 221                  return count( $this->item_array );
 222              } else {
 223                  return 0;
 224              }
 225          }
 226          
 227          # set a specific item in the set to applied
 228          function set_applied( $p_offset ) {
 229              $t_item = $this->item_array[$p_offset];
 230              $t_item->set_applied();
 231          }
 232  
 233          # check to see if a specific item in the set is applied
 234          function is_applied( $p_offset ) {
 235              $t_item = $this->item_array[$p_offset];
 236              return $t_item->is_applied();
 237          }
 238  
 239  
 240  		function process_post_data( $p_advanced=false ) {
 241              $f_execute_all        = gpc_get_bool( $this->upgrade_file . '_execute_all' );
 242              $f_execute_selected    = gpc_get_bool( $this->upgrade_file . '_execute_selected' );
 243              $f_print_all        = gpc_get_bool( $this->upgrade_file . '_print_all' );
 244              $f_print_selected    = gpc_get_bool( $this->upgrade_file . '_print_selected' );
 245  
 246              $f_execute_list        = gpc_get_string_array( $this->upgrade_file . '_execute_list', array() );
 247  
 248              if ( $f_execute_all ) {
 249                  $this->run( true, null, $p_advanced );
 250              } else if ( $f_execute_selected && $p_advanced ) {
 251                  $this->run( true, $f_execute_list, $p_advanced );
 252              } else if ( $f_print_all ) {
 253                  $this->output();
 254              } else if ( $f_print_selected && $p_advanced ) {
 255                  $this->output( $f_execute_list );
 256              } else {
 257                  $this->run( false, null, $p_advanced );
 258              }
 259          }
 260  
 261  		function run( $p_execute, $p_limit, $p_advanced ) {
 262              if ( ! php_version_at_least( '4.1.0' ) ) {
 263                  global $_SERVER;
 264              }
 265  
 266              if ( $p_execute ) {
 267                  # Mark this as a long process and ignore user aborts
 268                  helper_begin_long_process( true );
 269                  # Disable compression so we can stream
 270                  compress_disable();
 271                  # Flush the output buffer
 272                  @ob_end_flush();
 273                  echo '<b>Please be patient, this may take a while...</b>';
 274              }
 275  
 276              # Form
 277              echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
 278  
 279              # Execute All Button
 280              echo "<input type=\"submit\" name=\"{$this->upgrade_file}_execute_all\" value=\"Execute All\" />";
 281              # Print All Button
 282              echo "<input type=\"submit\" name=\"{$this->upgrade_file}_print_all\" value=\"Print All\" /><br /><br />";
 283  
 284              if ( $p_advanced ) {
 285                  # Execute Selected Button
 286                  echo "<input type=\"submit\" name=\"{$this->upgrade_file}_execute_selected\" value=\"Execute Selected\" />";
 287                  # Print Selected Button
 288                  echo "<input type=\"submit\" name=\"{$this->upgrade_file}_print_selected\" value=\"Print Selected\" />";
 289              }
 290  
 291              # Table
 292              echo '<table width="80%" bgcolor="#222222" border="0" cellpadding="10" cellspacing="1">';
 293  
 294              echo "<tr><td bgcolor=\"#e8e8e8\" colspan=\"3\"><span class=\"title\">$this->upgrade_name</span></td></tr>";
 295  
 296              # Headings
 297              echo '<tr bgcolor="#ffffff"><th width="70%">Description</th><th nowrap="nowrap">Upgrade ID</th><th width="30%">Status</th></tr>';
 298  
 299              $t_error = false;
 300  
 301              foreach ( $this->item_array as $item ) {
 302                  $t_state='';
 303  
 304                  if ( $item->is_applied() ) {
 305                      if ( !$p_advanced ) {
 306                          continue; #next one
 307                      }
 308  
 309                      $t_state    = 'disabled="disabled"';
 310                      $t_color    = '#00ff88';
 311                      $t_message    = 'Previously Applied';
 312                  } else if ( null !== $p_limit && is_array( $p_limit )
 313                              && ! in_array( $item->id, $p_limit ) ) {
 314                      $t_color    = '#ffff88';
 315                      $t_message    = 'Skipped';
 316                  } else if ( $p_execute ) {
 317                      if ( $t_error ) {
 318                          $t_state    = 'checked="checked"';
 319                          $t_color    = '#ff0088';
 320                          $t_message    = 'Skipped due to previous error';
 321                          continue;  # next one
 322                      }
 323  
 324                      if ( $item->execute() ) {
 325                          $t_state    = 'disabled="disabled"';
 326                          $t_color    = '#00ff88';
 327                          $t_message    = 'Applied';
 328                      } else {
 329                          $t_state    = 'checked="checked"';
 330                          $t_color    = '#ff0088';
 331                          $t_message    = 'ERROR: ' . $item->error;
 332                          $t_error = true;
 333                      }
 334                  } else {  # not applied but not executing
 335                      $t_color    = '#ff0088';
 336                      $t_message    = 'Not Applied';
 337                      $t_state    = 'checked="checked"';
 338                  }
 339  
 340                  echo '<tr bgcolor="#ffffff"><td>';
 341                  echo $item->description; # description
 342                  echo '</td>';
 343  
 344                  echo '<td nowrap="nowrap">';
 345                  if ( $p_advanced ) {
 346                      echo "<input type=\"checkbox\" name=\"{$this->upgrade_file}_execute_list[]\" value=\"$item->id\" $t_state /> ";
 347                  }
 348                  echo "$item->id</td>";
 349                  echo "<td bgcolor=\"$t_color\">$t_message</td>";
 350                  echo '</tr>';
 351              }
 352  
 353              echo '</table>';
 354  
 355              # Execute All Button
 356              echo "<br /><input type=\"submit\" name=\"{$this->upgrade_file}_execute_all\" value=\"Execute All\" />";
 357              # Print All Button
 358              echo "<input type=\"submit\" name=\"{$this->upgrade_file}_print_all\" value=\"Print All\" />";
 359  
 360              if ( $p_advanced ) {
 361                  # Execute Selected Button
 362                  echo "<input type=\"submit\" name=\"{$this->upgrade_file}_execute_selected\" value=\"Execute Selected\" />";
 363                  # Print Selected Button
 364                  echo "<input type=\"submit\" name=\"{$this->upgrade_file}_print_selected\" value=\"Print Selected\" />";
 365              }
 366  
 367              echo '</form>';
 368          }
 369  
 370          # Runs the upgrade steps without user intervention - used by Linux distributions to automate the upgrade process.
 371  		function run_all_unattended() {
 372              $t_error = false;
 373  
 374              foreach ( $this->item_array as $item ) {
 375                  $t_state='';
 376  
 377                  if ( $item->is_applied() ) {
 378                      $t_message    = 'Previously Applied:';
 379                  } else {
 380                      if ( $t_error ) {
 381                          $t_message    = 'Skipped due to previous error';
 382                          continue;  # next one
 383                      }
 384  
 385                      if ( $item->execute() ) {
 386                          $t_message    = 'Applied:';
 387                      } else {
 388                          $t_message    = 'ERROR - ' . $item->error . ':';
 389                          $t_error = true;
 390                      }
 391                  } 
 392  
 393                  echo $t_message, ' ', $item->description, "\n";
 394              }
 395  
 396              return $t_error;
 397          }
 398  
 399  		function output( $p_limit=null ) {
 400              # @@@ The generated file is in UNIX format, should it be in Windows format?
 401              $t_filename = $this->upgrade_file . '.sql';
 402              #header( "Content-Type: text/plain; name=$t_filename" );
 403              #header( 'Content-Transfer-Encoding: BASE64;' );
 404              #header( "Content-Disposition: attachment; filename=$t_filename" );
 405  
 406              $t_upgrade_table = config_get_global( 'mantis_upgrade_table' );
 407  
 408              foreach ( $this->item_array as $item ) {
 409                  if ( $item->is_applied()  #already applied or...
 410                       || ( null !== $p_limit && is_array( $p_limit )  #limit list included
 411                            && ! in_array( $item->id, $p_limit ) ) ) {  #and not in limit list
 412                      continue; #then skip to the next one
 413                  } else {
 414                      echo $item->display();
 415                  }
 416              }
 417          }
 418      }
 419      
 420      $upgrade_set = new UpgradeSet();
 421  
 422      $upgrade_set->add_items_with_check( 'upgrades/0_13_inc.php', $t_project_table );
 423      $upgrade_set->add_items_with_check( 'upgrades/0_14_inc.php', $t_bug_file_table );
 424      $upgrade_set->add_items_with_check( 'upgrades/0_15_inc.php', $t_bug_history_table );
 425      $upgrade_set->add_items_with_check( 'upgrades/0_16_inc.php', $t_bug_monitor_table );
 426  
 427      # this upgrade process was introduced in 0.17.x, so beyond here, the 
 428      #  process of checking the upgrade_table to see if updates are applied should work
 429      $upgrade_set->add_items_with_check( 'upgrades/0_17_inc.php', '', '0.17.0' );
 430      $upgrade_set->add_items_with_check( 'upgrades/0_18_inc.php', '', '0.18.0' );
 431      $upgrade_set->add_items_with_check( 'upgrades/0_19_inc.php', '', '0.19.0' );
 432      $upgrade_set->add_items_with_check( 'upgrades/1_00_inc.php', '', '1.0.0' );    
 433  
 434  ?>


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics