[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/admin/ -> move_db2disk.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This upgrade moves attachments from the database to the disk
  19   * @package MantisBT
  20   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  21   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  22   * @link http://www.mantisbt.org
  23   */
  24  /**
  25   * MantisBT Core API's
  26   */
  27  require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );
  28  
  29  access_ensure_global_level( config_get_global( 'admin_site_threshold' ) );
  30  
  31  // Move type should be attachment or project.
  32  $f_move_type = gpc_get( 'doc' );
  33  
  34  function get_prefix( $file_path ) {
  35      if( substr( $file_path, 0, 1 ) == '/' ) {
  36  
  37          # Unix absolute
  38          return '';
  39      }
  40      if( substr( $file_path, 0, 1 ) == '\\' ) {
  41  
  42          # Windows absolute
  43          return '';
  44      }
  45      if( substr( $file_path, 1, 2 ) == ':\\' ) {
  46  
  47          # Windows absolute
  48          return '';
  49      }
  50      return dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR;
  51  }
  52  
  53  # ------ move file attachments to issues from database to disk
  54  # select non-empty data fields
  55  # match with the project to get the file path
  56  # store the file in the correct folder
  57  #
  58  # Assumptions: only supports storage in local file system (not FTP)
  59  #              file paths are set up and working
  60  #
  61  # Re-running this is safe because the data
  62  # is not removed from the database until it is successfully copied.
  63  #
  64  function upgrade_move_att2disk( $p_source ) {
  65  
  66      # $p_source is the string "attachment" or "project"
  67      if( $p_source == 'attachment' ) {
  68          $t_file_table = db_get_table( 'bug_file' );
  69          $t_bug_label = "Bug";
  70      }
  71      if( $p_source == 'project' ) {
  72          $t_file_table = db_get_table( 'project_file' );
  73          $t_bug_label = "Project";
  74      }
  75  
  76      # check that the source was valid
  77      if( !isset( $t_file_table ) ) {
  78          echo 'Failure: Internal Error: File source not set';
  79          return;
  80      }
  81  
  82      # check that the destination is set up properly
  83      $t_upload_method = config_get_global( 'file_upload_method' );
  84      if( $t_upload_method <> DISK ) {
  85          echo 'Failure: Upload Method is not DISK';
  86          return;
  87      }
  88  
  89      $query = 'SELECT * FROM ' . $t_file_table . ' WHERE content <> \'\'';
  90  
  91      $result = @db_query_bound( $query );
  92  
  93      if( false == $result ) {
  94          echo '<p>No attachments need to be moved.</p>';
  95          return;
  96      }
  97  
  98      $count = db_num_rows( $result );
  99      echo '<p>Found ' . $count . ' attachments to be moved.</p>';
 100      $t_failures = 0;
 101  
 102      if( $count > 0 ) {
 103          echo '<table width="80%" bgcolor="#222222" cellpadding="10" cellspacing="1">';
 104  
 105          # Headings
 106          echo '<tr bgcolor="#ffffff"><th width="10%">' . $t_bug_label . '</th><th width="20%">Attachment</th><th width="70%">Status</th></tr>';
 107      }
 108  
 109      for( $i = 0;$i < $count;$i++ ) {
 110          $t_row = db_fetch_array( $result );
 111  
 112          // trace bug id back to project to determine the proper file path
 113          if( $p_source == 'attachment' ) {
 114              $t_project_id = bug_get_field( $t_row['bug_id'], 'project_id' );
 115              $t_bug_id = $t_row['bug_id'];
 116          } else {
 117              $t_project_id = (int) $t_row['project_id'];
 118              $t_bug_id = $t_project_id;
 119          }
 120  
 121          $t_file_path = project_get_field( $t_project_id, 'file_path' );
 122          $prefix = get_prefix( $t_file_path );
 123          $t_real_file_path = $prefix . $t_file_path;
 124          $c_filename = file_clean_name( $t_row['filename'] );
 125  
 126          printf( "\n<tr %s><td>%8d</td><td>%s</td><td>", helper_alternate_class(), $t_bug_id, $t_row['filename'] );
 127  
 128          if( is_blank( $t_real_file_path ) || !file_exists( $t_real_file_path ) || !is_dir( $t_real_file_path ) || !is_writable( $t_real_file_path ) ) {
 129              echo 'Destination ' . $t_real_file_path . ' not writable';
 130              $t_failures++;
 131          } else {
 132              $t_file_name = $t_real_file_path . $c_filename;
 133  
 134              // write file to disk store after adjusting the path
 135              if( file_put_contents( $t_file_name, $t_row['content'] ) ) {
 136                  // successful, update database
 137                  /** @todo do we want to check the size of data transfer matches here? */
 138                  $c_new_file_name = $t_file_path . $c_filename;
 139                  $query2 = "UPDATE $t_file_table SET diskfile = " . db_param() . ",
 140                          folder = " . db_param() . ", content = '' WHERE id = " . db_param();
 141                  $update = @db_query_bound( $query2, Array( $c_new_file_name, $t_file_path, $t_row['id'] ) );
 142                  if( !$update ) {
 143                      echo 'database update failed';
 144                      $t_failures++;
 145                  } else {
 146                      echo 'moved to ' . $t_file_name;
 147                  }
 148              } else {
 149                  echo 'copy to ' . $t_file_name . ' failed';
 150                  $t_failures++;
 151              }
 152          }
 153  
 154          echo '</td></tr>';
 155      }
 156  
 157      echo '</table><br />' . $count . ' attachments processed, ' . $t_failures . ' failures';
 158  }
 159  
 160  # ---------------------
 161  # main code
 162  #
 163  if( $f_move_type == 'attachment' ) {
 164      $t_type = 'Attachments';
 165  } else {
 166      if( $f_move_type == 'project' ) {
 167          $t_type = 'Project Files';
 168      } else {
 169          echo "<p>Invalid value '$f_move_type' for parameter 'doc'.</p>";
 170          exit;
 171      }
 172  }
 173  ?>
 174  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 175  <html>
 176  <head>
 177  <title>MantisBT Administration - Move <?php echo $t_type?> to Disk</title>
 178  <link rel="stylesheet" type="text/css" href="admin.css" />
 179  </head>
 180  <body>
 181  
 182  <table width="100%" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
 183      <tr class="top-bar">
 184          <td class="links">
 185              [ <a href="system_utils.php">Back to System Utilities</a> ]
 186              [ <a href="move_db2disk.php">Refresh view</a> ]
 187          </td>
 188          <td class="title">
 189              Move <?php echo $t_type?> to Disk
 190          </td>
 191      </tr>
 192  </table>
 193  <br /><br />
 194  
 195  <?php
 196      upgrade_move_att2disk( $f_move_type );
 197  echo '<p>Completed...</p>';
 198  ?>
 199  </body>
 200  </html>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7