[ Index ] |
|
Code source de Mantis 1.1.0rc3 |
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: version_api.php,v 1.24.2.1 2007-10-13 22:35:48 giallu Exp $ 22 # -------------------------------------------------------- 23 24 ### Version API ### 25 26 #========================================= 27 # Version Data Structure Definition 28 #=================================== 29 class VersionData { 30 var $id = 0; 31 var $project_id = 0; 32 var $version = ''; 33 var $description = ''; 34 var $released = 1; 35 var $date_order = ''; 36 } 37 38 #=================================== 39 # Boolean queries and ensures 40 #=================================== 41 42 $g_cache_versions = array(); 43 44 # -------------------- 45 # Cache a version row if necessary and return the cached copy 46 # If the second parameter is true (default), trigger an error 47 # if the version can't be found. If the second parameter is 48 # false, return false if the version can't be found. 49 function version_cache_row( $p_version_id, $p_trigger_errors = true ) { 50 global $g_cache_versions; 51 52 $c_version_id = db_prepare_int( $p_version_id ); 53 $t_project_version_table = config_get( 'mantis_project_version_table' ); 54 55 if ( isset( $g_cache_versions[$c_version_id] ) ) { 56 return $g_cache_versions[$c_version_id]; 57 } 58 59 $query = "SELECT * 60 FROM $t_project_version_table 61 WHERE id='$c_version_id'"; 62 $result = db_query( $query ); 63 64 if ( 0 == db_num_rows( $result ) ) { 65 $g_cache_versions[$c_version_id] = false; 66 67 if ( $p_trigger_errors ) { 68 error_parameters( $p_version_id ); 69 trigger_error( ERROR_VERSION_NOT_FOUND, ERROR ); 70 } else { 71 return false; 72 } 73 } 74 75 $row = db_fetch_array( $result ); 76 $row['date_order'] = db_unixtimestamp( $row['date_order'] ); 77 $g_cache_versions[$c_version_id] = $row; 78 79 return $row; 80 } 81 82 # -------------------- 83 # Check whether the version exists 84 # $p_project_id : null will use the current project, otherwise the specified project 85 # Returns true if the version exists, false otherwise 86 function version_exists( $p_version_id ) { 87 return version_cache_row( $p_version_id, false ) !== false; 88 } 89 90 # -------------------- 91 # Check whether the version name is unique 92 # Returns true if the name is unique, false otherwise 93 function version_is_unique( $p_version, $p_project_id = null ) { 94 return version_get_id( $p_version, $p_project_id ) === false; 95 } 96 97 # -------------------- 98 # Check whether the version exists 99 # Trigger an error if it does not 100 function version_ensure_exists( $p_version_id ) { 101 if ( !version_exists( $p_version_id ) ) { 102 error_parameters( $p_version_id ); 103 trigger_error( ERROR_VERSION_NOT_FOUND, ERROR ); 104 } 105 } 106 107 # -------------------- 108 # Check whether the version is unique within a project 109 # Trigger an error if it is not 110 function version_ensure_unique( $p_version, $p_project_id = null ) { 111 if ( !version_is_unique( $p_version, $p_project_id ) ) { 112 trigger_error( ERROR_VERSION_DUPLICATE, ERROR ); 113 } 114 } 115 116 117 #=================================== 118 # Creation / Deletion / Updating 119 #=================================== 120 121 # -------------------- 122 # Add a version to the project 123 function version_add( $p_project_id, $p_version, $p_released = VERSION_RELEASED, $p_description = '', $p_date_order = null) { 124 $c_project_id = db_prepare_int( $p_project_id ); 125 $c_released = db_prepare_int( $p_released ); 126 $c_version = db_prepare_string( $p_version ); 127 $c_description = db_prepare_string( $p_description ); 128 129 if ( null === $p_date_order ) { 130 $c_date_order = db_now(); 131 } else { 132 $c_date_order = db_timestamp( $p_date_order ); 133 } 134 135 version_ensure_unique( $p_version, $p_project_id ); 136 137 $t_project_version_table = config_get( 'mantis_project_version_table' ); 138 139 $query = "INSERT INTO $t_project_version_table 140 ( project_id, version, date_order, description, released ) 141 VALUES 142 ( '$c_project_id', '$c_version', " . $c_date_order . ", '$c_description', '$c_released' )"; 143 db_query( $query ); 144 145 # db_query() errors on failure so: 146 return true; 147 } 148 149 # -------------------- 150 # Update the definition of a version 151 function version_update( $p_version_info ) { 152 version_ensure_exists( $p_version_info->id ); 153 154 $t_old_version_name = version_get_field( $p_version_info->id, 'version' ); 155 156 # check for duplicates 157 if ( ( strtolower( $t_old_version_name ) != strtolower( $p_version_info->version ) ) && 158 !version_is_unique( $p_version_info->version, $p_version_info->project_id ) ) { 159 trigger_error( ERROR_VERSION_DUPLICATE, ERROR ); 160 } 161 162 $c_version_id = db_prepare_int( $p_version_info->id ); 163 $c_version_name = db_prepare_string( $p_version_info->version ); 164 $c_old_version_name = db_prepare_string( $t_old_version_name ); 165 $c_description = db_prepare_string( $p_version_info->description ); 166 $c_released = db_prepare_int( $p_version_info->released ); 167 $c_date_order = db_timestamp( $p_version_info->date_order ); 168 $c_project_id = db_prepare_int( $p_version_info->project_id ); 169 170 $t_project_version_table = config_get( 'mantis_project_version_table' ); 171 $t_bug_table = config_get( 'mantis_bug_table' ); 172 173 $query = "UPDATE $t_project_version_table 174 SET version='$c_version_name', 175 description='$c_description', 176 released='$c_released', 177 date_order=$c_date_order 178 WHERE id='$c_version_id'"; 179 db_query( $query ); 180 181 if ( $c_version_name != $c_old_version_name ) { 182 $query = "UPDATE $t_bug_table 183 SET version='$c_version_name' 184 WHERE ( project_id='$c_project_id' ) AND ( version='$c_old_version_name' )"; 185 db_query( $query ); 186 187 $query = "UPDATE $t_bug_table 188 SET fixed_in_version='$c_version_name' 189 WHERE ( project_id='$c_project_id' ) AND ( fixed_in_version='$c_old_version_name' )"; 190 db_query( $query ); 191 192 $query = "UPDATE $t_bug_table 193 SET target_version='$c_version_name' 194 WHERE ( project_id='$c_project_id' ) AND ( target_version='$c_old_version_name' )"; 195 db_query( $query ); 196 197 # @@@ We should consider using ids instead of names for foreign keys. The main advantage of using the names are: 198 # - for history the version history entries will still be valid even if the version is deleted in the future. -- we can ban deleting referenced versions. 199 # - when an issue is copied or moved from one project to another, we can keep the last version with the issue even if it doesn't exist in the new project. Also previous history entries remain valid. 200 # @@@ We should update the history for version, fixed_in_version, and target_version. 201 # @@@ We probably need to update the saved filters too? 202 } 203 204 # db_query() errors on failure so: 205 return true; 206 } 207 208 # -------------------- 209 # Remove a version from the project 210 function version_remove( $p_version_id, $p_new_version='' ) { 211 $c_version_id = db_prepare_int( $p_version_id ); 212 $c_new_version = db_prepare_string( $p_new_version ); 213 214 version_ensure_exists( $p_version_id ); 215 216 $t_old_version = version_get_field( $p_version_id, 'version' ); 217 $t_project_id = version_get_field( $p_version_id, 'project_id' ); 218 219 $c_old_version = db_prepare_string( $t_old_version ); 220 $c_project_id = db_prepare_int( $t_project_id ); 221 222 $t_project_version_table = config_get( 'mantis_project_version_table' ); 223 $t_bug_table = config_get( 'mantis_bug_table' ); 224 225 $query = "DELETE FROM $t_project_version_table 226 WHERE id='$c_version_id'"; 227 db_query( $query ); 228 229 $query = "UPDATE $t_bug_table 230 SET version='$c_new_version' 231 WHERE project_id='$c_project_id' AND version='$c_old_version'"; 232 db_query( $query ); 233 234 $query = "UPDATE $t_bug_table 235 SET fixed_in_version='$c_new_version' 236 WHERE ( project_id='$c_project_id' ) AND ( fixed_in_version='$c_old_version' )"; 237 db_query( $query ); 238 239 # db_query() errors on failure so: 240 return true; 241 } 242 243 # -------------------- 244 # Remove all versions associated with a project 245 function version_remove_all( $p_project_id ) { 246 $c_project_id = db_prepare_int( $p_project_id ); 247 248 $t_project_version_table = config_get( 'mantis_project_version_table' ); 249 $t_bug_table = config_get( 'mantis_bug_table' ); 250 251 $query = "DELETE FROM $t_project_version_table 252 WHERE project_id='$c_project_id'"; 253 254 db_query( $query ); 255 256 $query = "UPDATE $t_bug_table 257 SET version='' 258 WHERE project_id='$c_project_id'"; 259 db_query( $query ); 260 261 $query = "UPDATE $t_bug_table 262 SET fixed_in_version='' 263 WHERE project_id='$c_project_id'"; 264 db_query( $query ); 265 266 # db_query() errors on failure so: 267 return true; 268 } 269 270 271 #=================================== 272 # Data Access 273 #=================================== 274 275 # -------------------- 276 # Return all versions for the specified project 277 function version_get_all_rows( $p_project_id, $p_released = null ) { 278 $c_project_id = db_prepare_int( $p_project_id ); 279 280 if ( $p_released === null ) { 281 $t_released_where = ''; 282 } else { 283 $c_released = db_prepare_int( $p_released ); 284 $t_released_where = "AND ( released = $c_released )"; 285 } 286 287 $t_project_version_table = config_get( 'mantis_project_version_table' ); 288 289 $query = "SELECT * 290 FROM $t_project_version_table 291 WHERE project_id='$c_project_id' $t_released_where 292 ORDER BY date_order DESC"; 293 $result = db_query( $query ); 294 $count = db_num_rows( $result ); 295 $rows = array(); 296 for ( $i = 0 ; $i < $count ; $i++ ) { 297 $row = db_fetch_array( $result ); 298 $row['date_order'] = db_unixtimestamp( $row['date_order'] ); 299 $rows[] = $row; 300 } 301 return $rows; 302 } 303 304 # -------------------- 305 # Return all versions for the specified project, including subprojects 306 function version_get_all_rows_with_subs( $p_project_id, $p_released = null ) { 307 $t_project_where = helper_project_specific_where( $p_project_id ); 308 309 if ( $p_released === null ) { 310 $t_released_where = ''; 311 } else { 312 $c_released = db_prepare_int( $p_released ); 313 $t_released_where = "AND ( released = $c_released )"; 314 } 315 316 $t_project_version_table = config_get( 'mantis_project_version_table' ); 317 318 $query = "SELECT * 319 FROM $t_project_version_table 320 WHERE $t_project_where $t_released_where 321 ORDER BY date_order DESC"; 322 $result = db_query( $query ); 323 $count = db_num_rows( $result ); 324 $rows = array(); 325 for ( $i = 0 ; $i < $count ; $i++ ) { 326 $row = db_fetch_array( $result ); 327 $row['date_order'] = db_unixtimestamp( $row['date_order'] ); 328 $rows[] = $row; 329 } 330 return $rows; 331 } 332 333 # -------------------- 334 # Get the version_id, given the project_id and $p_version_id 335 # returns false if not found, otherwise returns the id. 336 function version_get_id( $p_version, $p_project_id = null ) { 337 $c_version = db_prepare_string( $p_version ); 338 339 if ( $p_project_id === null ) { 340 $c_project_id = helper_get_current_project(); 341 } else { 342 $c_project_id = db_prepare_int( $p_project_id ); 343 } 344 345 $t_project_version_table = config_get( 'mantis_project_version_table' ); 346 347 $query = "SELECT id 348 FROM $t_project_version_table 349 WHERE project_id='$c_project_id' AND 350 version='$c_version'"; 351 352 $result = db_query( $query ); 353 354 if ( 0 == db_num_rows( $result ) ) { 355 return false; 356 } else { 357 return db_result( $result ); 358 } 359 } 360 361 # -------------------- 362 # Get the specified field name for the specified version id. 363 # triggers an error if version not found, otherwise returns the field value. 364 function version_get_field( $p_version_id, $p_field_name ) { 365 $row = version_cache_row( $p_version_id ); 366 367 if ( isset( $row[$p_field_name] ) ) { 368 return $row[$p_field_name]; 369 } else { 370 error_parameters( $p_field_name ); 371 trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING ); 372 return ''; 373 } 374 } 375 376 # -------------------- 377 # get information about a version given its id 378 function version_get( $p_version_id ) { 379 $row = version_cache_row( $p_version_id ); 380 381 $t_version_data = new VersionData; 382 $t_row_keys = array_keys( $row ); 383 $t_vars = get_object_vars( $t_version_data ); 384 385 # Check each variable in the class 386 foreach ( $t_vars as $var => $val ) { 387 # If we got a field from the DB with the same name 388 if ( in_array( $var, $t_row_keys, true ) ) { 389 # Store that value in the object 390 $t_version_data->$var = $row[$var]; 391 } 392 } 393 394 return $t_version_data; 395 } 396 397 # -------------------- 398 # Return a copy of the version structure with all the instvars prepared for db insertion 399 function version_prepare_db( $p_version_info ) { 400 $p_version_info->id = db_prepare_int( $p_version_info->id ); 401 $p_version_info->project_id = db_prepare_int( $p_version_info->project_id ); 402 $p_version_info->version = db_prepare_string( $p_version_info->version ); 403 $p_version_info->description = db_prepare_string( $p_version_info->description ); 404 $p_version_info->released = db_prepare_int( $p_version_info->released ); 405 $p_version_info->date_order = db_prepare_string( $p_version_info->date_order ); 406 407 return $p_version_info; 408 } 409 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 09:42:17 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |