[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> bug_revision_api.php (source)

   1  <?php
   2  # Mantis - a php based bugtracking system
   3  
   4  # Mantis 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  # Mantis 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 Mantis.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Bug Revision API
  19   *
  20   * @package CoreAPI
  21   * @subpackage BugRevisionAPI
  22   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  23   * @link http://www.mantisbt.org
  24   *
  25   * @uses constant_inc.php
  26   * @uses database_api.php
  27   */
  28  
  29  require_api( 'constant_inc.php' );
  30  require_api( 'database_api.php' );
  31  
  32  /**
  33   * Add a new revision to a bug history.
  34   * @param int $p_bug_id Bug ID
  35   * @param int $p_user_id User ID
  36   * @param int $p_type Revision Type
  37   * @param string $p_value Value
  38   * @param int $p_bugnote_id Bugnote ID
  39   * @param int $p_timestamp Timestamp(int)
  40   * @return int Revision ID
  41   */
  42  function bug_revision_add( $p_bug_id, $p_user_id, $p_type, $p_value, $p_bugnote_id=0, $p_timestamp = null ) {
  43      if ( $p_type <= REV_ANY ) {
  44          return null;
  45      }
  46  
  47      $t_bug_rev_table = db_get_table( 'bug_revision' );
  48  
  49      $t_last = bug_revision_last( $p_bug_id, $p_type );
  50  
  51      # Don't save a revision twice if nothing has changed
  52      if ( !is_null( $t_last ) &&
  53          $p_value == $t_last['value'] ) {
  54  
  55          return $t_last['id'];
  56      }
  57  
  58      if ( $p_timestamp === null ) {
  59          $t_timestamp = db_now();
  60      } else {
  61          $t_timestamp = $p_timestamp;
  62      }
  63  
  64      $t_query = "INSERT INTO $t_bug_rev_table (
  65              bug_id,
  66              bugnote_id,
  67              user_id,
  68              timestamp,
  69              type,
  70              value
  71          ) VALUES ( " .
  72              db_param() . ', ' .
  73              db_param() . ', ' .
  74              db_param() . ', ' .
  75              db_param() . ', ' .
  76              db_param() . ', ' .
  77              db_param() .
  78          ' )';
  79      db_query_bound( $t_query, array(
  80              $p_bug_id,
  81              $p_bugnote_id,
  82              $p_user_id,
  83              $t_timestamp,
  84              $p_type,
  85              $p_value
  86          ) );
  87  
  88      return db_insert_id( $t_bug_rev_table );
  89  }
  90  
  91  /**
  92   * Check if a bug revision exists
  93   * @param int $p_revision_id Revision ID
  94   * @return bool Whether or not the bug revision exists
  95   */
  96  function bug_revision_exists( $p_revision_id ) {
  97      $t_bug_rev_table = db_get_table( 'bug_revision' );
  98  
  99      $t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
 100      $t_result = db_query_bound( $t_query, array( $p_revision_id ) );
 101  
 102      if ( db_num_rows( $t_result ) < 1 ) {
 103          return false;
 104      }
 105  
 106      return true;
 107  }
 108  
 109  /**
 110   * Get a row of data for a given revision ID.
 111   * @param int $p_revision_id Revision ID
 112   * @return array Revision data row
 113   */
 114  function bug_revision_get( $p_revision_id ) {
 115      $t_bug_rev_table = db_get_table( 'bug_revision' );
 116  
 117      $t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
 118      $t_result = db_query_bound( $t_query, array( $p_revision_id ) );
 119  
 120      if ( db_num_rows( $t_result ) < 1 ) {
 121          trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
 122      }
 123  
 124      return db_fetch_array( $t_result );
 125  }
 126  
 127  /**
 128   * Get the name of the type of a bug revision.
 129   * @param int $p_revision_id Revision type ID (see constant_inc.php for possible values)
 130   * @return string Name of the type of the bug revision
 131   */
 132  function bug_revision_get_type_name( $p_revision_type_id ) {
 133      $t_type_name = '';
 134      switch( $p_revision_type_id ) {
 135          case REV_DESCRIPTION:
 136              $t_type_name = lang_get( 'description' );
 137              break;
 138          case REV_STEPS_TO_REPRODUCE:
 139              $t_type_name = lang_get( 'steps_to_reproduce' );
 140              break;
 141          case REV_ADDITIONAL_INFO:
 142              $t_type_name = lang_get( 'additional_information' );
 143              break;
 144          case REV_BUGNOTE:
 145              $t_type_name = lang_get( 'bugnote' );
 146              break;
 147      }
 148      return $t_type_name;
 149  }
 150  
 151  /**
 152   * Remove one or more bug revisions from the bug history.
 153   * @param int $p_revision_id Revision ID, or array of revision IDs
 154   * @return null
 155   */
 156  function bug_revision_drop( $p_revision_id ) {
 157      $t_bug_rev_table = db_get_table( 'bug_revision' );
 158  
 159      if ( is_array( $p_revision_id ) ) {
 160          $t_revisions = array();
 161          $t_first = true;
 162          $t_query = "DELETE FROM $t_bug_rev_table WHERE id IN ( ";
 163  
 164          # TODO: Fetch bug revisions in one query (and cache them)
 165          foreach( $p_revision_id as $t_rev_id ) {
 166              $t_query .= ( $t_first ? db_param() : ', ' . db_param() );
 167              $t_revisions[$t_rev_id] = bug_revision_get( $t_rev_id );
 168          }
 169  
 170          $t_query .= ' )';
 171          db_query_bound( $t_query, $p_revision_id );
 172          foreach( $p_revision_id as $t_rev_id ) {
 173              if ( $t_revisions[$t_rev_id]['type'] == REV_BUGNOTE ) {
 174                  history_log_event_special( $t_revisions[$t_rev_id]['bug_id'], BUGNOTE_REVISION_DROPPED, bugnote_format_id( $t_rev_id ), $t_revisions[$t_rev_id]['bugnote_id'] );
 175              } else {
 176                  history_log_event_special( $t_revisions[$t_rev_id]['bug_id'], BUG_REVISION_DROPPED, bugnote_format_id( $t_rev_id ), $t_revisions[$t_rev_id]['type'] );
 177              }
 178          }
 179      } else {
 180          $t_revision = bug_revision_get( $p_revision_id );
 181          $t_query = "DELETE FROM $t_bug_rev_table WHERE id=" . db_param();
 182          db_query_bound( $t_query, array( $p_revision_id ) );
 183          if ( $t_revision['type'] == REV_BUGNOTE ) {
 184              history_log_event_special( $t_revision['bug_id'], BUGNOTE_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['bugnote_id'] );
 185          } else {
 186              history_log_event_special( $t_revision['bug_id'], BUG_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['type'] );
 187          }
 188      }
 189  }
 190  
 191  /**
 192   * Retrieve a count of revisions to the bug's information.
 193   * @param int $p_bug_id Bug ID
 194   * @param int $p_type Revision Type (optional)
 195   * @param int $p_bugnote_id Bugnote ID (optional)
 196   * @return array|null Array of Revision rows
 197   */
 198  function bug_revision_count( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
 199      $t_bug_rev_table = db_get_table( 'bug_revision' );
 200  
 201      $t_params = array( $p_bug_id );
 202      $t_query = "SELECT COUNT(id) FROM $t_bug_rev_table
 203          WHERE bug_id=" . db_param();
 204  
 205      if ( REV_ANY < $p_type ) {
 206          $t_query .= ' AND type=' . db_param();
 207          $t_params[] = $p_type;
 208      }
 209  
 210      if ( $p_bugnote_id > 0 ) {
 211          $t_query .= ' AND bugnote_id=' . db_param();
 212          $t_params[] = $p_bugnote_id;
 213      } else {
 214          $t_query .= ' AND bugnote_id=0';
 215      }
 216  
 217      $t_result = db_query_bound( $t_query, $t_params );
 218  
 219      return db_result( $t_result );
 220  }
 221  
 222  /**
 223   * Delete all revision history for a bug.
 224   * @param int $p_bug_id Bug ID
 225   * @param int $p_bugnote_id Bugnote ID (optional)
 226   * @return null
 227   */
 228  function bug_revision_delete( $p_bug_id, $p_bugnote_id=0 ) {
 229      $t_bug_rev_table = db_get_table( 'bug_revision' );
 230  
 231      if ( $p_bugnote_id < 1 ) {
 232          $t_query = "DELETE FROM $t_bug_rev_table WHERE bug_id=" . db_param();
 233          db_query_bound( $t_query, array( $p_bug_id ) );
 234      } else {
 235          $t_query = "DELETE FROM $t_bug_rev_table WHERE bugnote_id=" . db_param();
 236          db_query_bound( $t_query, array( $p_bugnote_id ) );
 237      }
 238  }
 239  
 240  /**
 241   * Retrieve the last change to the bug's information.
 242   * @param int $p_bug_id Bug ID
 243   * @param int $p_type Revision Type (optional)
 244   * @param int $p_bugnote_id Bugnote ID (optional)
 245   * @return null|array Revision row
 246   */
 247  function bug_revision_last( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
 248      $t_bug_rev_table = db_get_table( 'bug_revision' );
 249  
 250      $t_params = array( $p_bug_id );
 251      $t_query = "SELECT * FROM $t_bug_rev_table
 252          WHERE bug_id=" . db_param();
 253  
 254      if ( REV_ANY < $p_type ) {
 255          $t_query .= ' AND type=' . db_param();
 256          $t_params[] = $p_type;
 257      }
 258  
 259      if ( $p_bugnote_id > 0 ) {
 260          $t_query .= ' AND bugnote_id=' . db_param();
 261          $t_params[] = $p_bugnote_id;
 262      } else {
 263          $t_query .= ' AND bugnote_id=0';
 264      }
 265  
 266      $t_query .= ' ORDER BY timestamp DESC';
 267      $t_result = db_query_bound( $t_query, $t_params, 1 );
 268  
 269      if ( db_num_rows( $t_result ) > 0 ) {
 270          return db_fetch_array( $t_result );
 271      } else {
 272          return null;
 273      }
 274  }
 275  
 276  /**
 277   * Retrieve a full list of changes to the bug's information.
 278   * @param int $p_bug_id Bug ID
 279   * @param int $p_type Revision Type
 280   * @param int $p_bugnote_id Bugnote ID
 281   * @return array/null Array of Revision rows
 282   */
 283  function bug_revision_list( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
 284      $t_bug_rev_table = db_get_table( 'bug_revision' );
 285  
 286      $t_params = array( $p_bug_id );
 287      $t_query = "SELECT * FROM $t_bug_rev_table
 288          WHERE bug_id=" . db_param();
 289  
 290      if ( REV_ANY < $p_type ) {
 291          $t_query .= ' AND type=' . db_param();
 292          $t_params[] = $p_type;
 293      }
 294  
 295      if ( $p_bugnote_id > 0 ) {
 296          $t_query .= ' AND bugnote_id=' . db_param();
 297          $t_params[] = $p_bugnote_id;
 298      } else {
 299          $t_query .= ' AND bugnote_id=0';
 300      }
 301  
 302      $t_query .= ' ORDER BY timestamp ASC';
 303      $t_result = db_query_bound( $t_query, $t_params );
 304  
 305      $t_revisions = array();
 306      while( $t_row = db_fetch_array( $t_result ) ) {
 307          $t_revisions[$t_row['id']] = $t_row;
 308      }
 309  
 310      return $t_revisions;
 311  }
 312  
 313  /**
 314   * Retrieve a list of changes to a bug of the same type as the
 315   * given revision ID.
 316   * @param int $p_rev_id Revision ID
 317   * @return array|null Array of Revision rows
 318   */
 319  function bug_revision_like( $p_rev_id ) {
 320      $t_bug_rev_table = db_get_table( 'bug_revision' );
 321  
 322      $t_query = "SELECT bug_id, bugnote_id, type FROM $t_bug_rev_table WHERE id=" . db_param();
 323      $t_result = db_query_bound( $t_query, array( $p_rev_id ) );
 324  
 325      if ( db_num_rows( $t_result ) < 1 ) {
 326          trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
 327      }
 328  
 329      $t_row = db_fetch_array( $t_result );
 330      $t_bug_id = $t_row['bug_id'];
 331      $t_bugnote_id = $t_row['bugnote_id'];
 332      $t_type = $t_row['type'];
 333  
 334      $t_params = array( $t_bug_id );
 335      $t_query = "SELECT * FROM $t_bug_rev_table
 336          WHERE bug_id=" . db_param();
 337  
 338      if ( REV_ANY < $t_type ) {
 339          $t_query .= ' AND type=' . db_param();
 340          $t_params[] = $t_type;
 341      }
 342  
 343      if ( $t_bugnote_id > 0 ) {
 344          $t_query .= ' AND bugnote_id=' . db_param();
 345          $t_params[] = $t_bugnote_id;
 346      } else {
 347          $t_query .= ' AND bugnote_id=0';
 348      }
 349  
 350      $t_query .= ' ORDER BY timestamp ASC';
 351      $t_result = db_query_bound( $t_query, $t_params );
 352  
 353      $t_revisions = array();
 354      while( $t_row = db_fetch_array( $t_result ) ) {
 355          $t_revisions[$t_row['id']] = $t_row;
 356      }
 357  
 358      return $t_revisions;
 359  }
 360  


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