PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/bug_revision_api.php

https://github.com/fusenigk/mantisbt-1
PHP | 360 lines | 215 code | 56 blank | 89 comment | 32 complexity | b93b5913a768d906b93cd0697e44dee7 MD5 | raw file
  1. <?php
  2. # Mantis - a php based bugtracking system
  3. # Mantis is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # Mantis is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Mantis. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Bug Revision API
  17. *
  18. * @package CoreAPI
  19. * @subpackage BugRevisionAPI
  20. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  21. * @link http://www.mantisbt.org
  22. *
  23. * @uses constant_inc.php
  24. * @uses database_api.php
  25. */
  26. require_api( 'constant_inc.php' );
  27. require_api( 'database_api.php' );
  28. /**
  29. * Add a new revision to a bug history.
  30. * @param int $p_bug_id Bug ID
  31. * @param int $p_user_id User ID
  32. * @param int $p_type Revision Type
  33. * @param string $p_value Value
  34. * @param int $p_bugnote_id Bugnote ID
  35. * @param int $p_timestamp Timestamp(int)
  36. * @return int Revision ID
  37. */
  38. function bug_revision_add( $p_bug_id, $p_user_id, $p_type, $p_value, $p_bugnote_id=0, $p_timestamp = null ) {
  39. if ( $p_type <= REV_ANY ) {
  40. return null;
  41. }
  42. $t_bug_rev_table = db_get_table( 'bug_revision' );
  43. $t_last = bug_revision_last( $p_bug_id, $p_type );
  44. # Don't save a revision twice if nothing has changed
  45. if ( !is_null( $t_last ) &&
  46. $p_value == $t_last['value'] ) {
  47. return $t_last['id'];
  48. }
  49. if ( $p_timestamp === null ) {
  50. $t_timestamp = db_now();
  51. } else {
  52. $t_timestamp = $p_timestamp;
  53. }
  54. $t_query = "INSERT INTO $t_bug_rev_table (
  55. bug_id,
  56. bugnote_id,
  57. user_id,
  58. timestamp,
  59. type,
  60. value
  61. ) VALUES ( " .
  62. db_param() . ', ' .
  63. db_param() . ', ' .
  64. db_param() . ', ' .
  65. db_param() . ', ' .
  66. db_param() . ', ' .
  67. db_param() .
  68. ' )';
  69. db_query_bound( $t_query, array(
  70. $p_bug_id,
  71. $p_bugnote_id,
  72. $p_user_id,
  73. $t_timestamp,
  74. $p_type,
  75. $p_value
  76. ) );
  77. return db_insert_id( $t_bug_rev_table );
  78. }
  79. /**
  80. * Check if a bug revision exists
  81. * @param int $p_revision_id Revision ID
  82. * @return bool Whether or not the bug revision exists
  83. */
  84. function bug_revision_exists( $p_revision_id ) {
  85. $t_bug_rev_table = db_get_table( 'bug_revision' );
  86. $t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
  87. $t_result = db_query_bound( $t_query, array( $p_revision_id ) );
  88. if ( db_num_rows( $t_result ) < 1 ) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Get a row of data for a given revision ID.
  95. * @param int $p_revision_id Revision ID
  96. * @return array Revision data row
  97. */
  98. function bug_revision_get( $p_revision_id ) {
  99. $t_bug_rev_table = db_get_table( 'bug_revision' );
  100. $t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
  101. $t_result = db_query_bound( $t_query, array( $p_revision_id ) );
  102. if ( db_num_rows( $t_result ) < 1 ) {
  103. trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
  104. }
  105. return db_fetch_array( $t_result );
  106. }
  107. /**
  108. * Get the name of the type of a bug revision.
  109. * @param int $p_revision_id Revision type ID (see constant_inc.php for possible values)
  110. * @return string Name of the type of the bug revision
  111. */
  112. function bug_revision_get_type_name( $p_revision_type_id ) {
  113. $t_type_name = '';
  114. switch( $p_revision_type_id ) {
  115. case REV_DESCRIPTION:
  116. $t_type_name = lang_get( 'description' );
  117. break;
  118. case REV_STEPS_TO_REPRODUCE:
  119. $t_type_name = lang_get( 'steps_to_reproduce' );
  120. break;
  121. case REV_ADDITIONAL_INFO:
  122. $t_type_name = lang_get( 'additional_information' );
  123. break;
  124. case REV_BUGNOTE:
  125. $t_type_name = lang_get( 'bugnote' );
  126. break;
  127. }
  128. return $t_type_name;
  129. }
  130. /**
  131. * Remove one or more bug revisions from the bug history.
  132. * @param int $p_revision_id Revision ID, or array of revision IDs
  133. * @return null
  134. */
  135. function bug_revision_drop( $p_revision_id ) {
  136. $t_bug_rev_table = db_get_table( 'bug_revision' );
  137. if ( is_array( $p_revision_id ) ) {
  138. $t_revisions = array();
  139. $t_first = true;
  140. $t_query = "DELETE FROM $t_bug_rev_table WHERE id IN ( ";
  141. # TODO: Fetch bug revisions in one query (and cache them)
  142. foreach( $p_revision_id as $t_rev_id ) {
  143. $t_query .= ( $t_first ? db_param() : ', ' . db_param() );
  144. $t_revisions[$t_rev_id] = bug_revision_get( $t_rev_id );
  145. }
  146. $t_query .= ' )';
  147. db_query_bound( $t_query, $p_revision_id );
  148. foreach( $p_revision_id as $t_rev_id ) {
  149. if ( $t_revisions[$t_rev_id]['type'] == REV_BUGNOTE ) {
  150. 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'] );
  151. } else {
  152. 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'] );
  153. }
  154. }
  155. } else {
  156. $t_revision = bug_revision_get( $p_revision_id );
  157. $t_query = "DELETE FROM $t_bug_rev_table WHERE id=" . db_param();
  158. db_query_bound( $t_query, array( $p_revision_id ) );
  159. if ( $t_revision['type'] == REV_BUGNOTE ) {
  160. history_log_event_special( $t_revision['bug_id'], BUGNOTE_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['bugnote_id'] );
  161. } else {
  162. history_log_event_special( $t_revision['bug_id'], BUG_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['type'] );
  163. }
  164. }
  165. }
  166. /**
  167. * Retrieve a count of revisions to the bug's information.
  168. * @param int $p_bug_id Bug ID
  169. * @param int $p_type Revision Type (optional)
  170. * @param int $p_bugnote_id Bugnote ID (optional)
  171. * @return array|null Array of Revision rows
  172. */
  173. function bug_revision_count( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
  174. $t_bug_rev_table = db_get_table( 'bug_revision' );
  175. $t_params = array( $p_bug_id );
  176. $t_query = "SELECT COUNT(id) FROM $t_bug_rev_table
  177. WHERE bug_id=" . db_param();
  178. if ( REV_ANY < $p_type ) {
  179. $t_query .= ' AND type=' . db_param();
  180. $t_params[] = $p_type;
  181. }
  182. if ( $p_bugnote_id > 0 ) {
  183. $t_query .= ' AND bugnote_id=' . db_param();
  184. $t_params[] = $p_bugnote_id;
  185. } else {
  186. $t_query .= ' AND bugnote_id=0';
  187. }
  188. $t_result = db_query_bound( $t_query, $t_params );
  189. return db_result( $t_result );
  190. }
  191. /**
  192. * Delete all revision history for a bug.
  193. * @param int $p_bug_id Bug ID
  194. * @param int $p_bugnote_id Bugnote ID (optional)
  195. * @return null
  196. */
  197. function bug_revision_delete( $p_bug_id, $p_bugnote_id=0 ) {
  198. $t_bug_rev_table = db_get_table( 'bug_revision' );
  199. if ( $p_bugnote_id < 1 ) {
  200. $t_query = "DELETE FROM $t_bug_rev_table WHERE bug_id=" . db_param();
  201. db_query_bound( $t_query, array( $p_bug_id ) );
  202. } else {
  203. $t_query = "DELETE FROM $t_bug_rev_table WHERE bugnote_id=" . db_param();
  204. db_query_bound( $t_query, array( $p_bugnote_id ) );
  205. }
  206. }
  207. /**
  208. * Retrieve the last change to the bug's information.
  209. * @param int $p_bug_id Bug ID
  210. * @param int $p_type Revision Type (optional)
  211. * @param int $p_bugnote_id Bugnote ID (optional)
  212. * @return null|array Revision row
  213. */
  214. function bug_revision_last( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
  215. $t_bug_rev_table = db_get_table( 'bug_revision' );
  216. $t_params = array( $p_bug_id );
  217. $t_query = "SELECT * FROM $t_bug_rev_table
  218. WHERE bug_id=" . db_param();
  219. if ( REV_ANY < $p_type ) {
  220. $t_query .= ' AND type=' . db_param();
  221. $t_params[] = $p_type;
  222. }
  223. if ( $p_bugnote_id > 0 ) {
  224. $t_query .= ' AND bugnote_id=' . db_param();
  225. $t_params[] = $p_bugnote_id;
  226. } else {
  227. $t_query .= ' AND bugnote_id=0';
  228. }
  229. $t_query .= ' ORDER BY timestamp DESC';
  230. $t_result = db_query_bound( $t_query, $t_params, 1 );
  231. if ( db_num_rows( $t_result ) > 0 ) {
  232. return db_fetch_array( $t_result );
  233. } else {
  234. return null;
  235. }
  236. }
  237. /**
  238. * Retrieve a full list of changes to the bug's information.
  239. * @param int $p_bug_id Bug ID
  240. * @param int $p_type Revision Type
  241. * @param int $p_bugnote_id Bugnote ID
  242. * @return array/null Array of Revision rows
  243. */
  244. function bug_revision_list( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
  245. $t_bug_rev_table = db_get_table( 'bug_revision' );
  246. $t_params = array( $p_bug_id );
  247. $t_query = "SELECT * FROM $t_bug_rev_table
  248. WHERE bug_id=" . db_param();
  249. if ( REV_ANY < $p_type ) {
  250. $t_query .= ' AND type=' . db_param();
  251. $t_params[] = $p_type;
  252. }
  253. if ( $p_bugnote_id > 0 ) {
  254. $t_query .= ' AND bugnote_id=' . db_param();
  255. $t_params[] = $p_bugnote_id;
  256. } else {
  257. $t_query .= ' AND bugnote_id=0';
  258. }
  259. $t_query .= ' ORDER BY timestamp ASC';
  260. $t_result = db_query_bound( $t_query, $t_params );
  261. $t_revisions = array();
  262. while( $t_row = db_fetch_array( $t_result ) ) {
  263. $t_revisions[$t_row['id']] = $t_row;
  264. }
  265. return $t_revisions;
  266. }
  267. /**
  268. * Retrieve a list of changes to a bug of the same type as the
  269. * given revision ID.
  270. * @param int $p_rev_id Revision ID
  271. * @return array|null Array of Revision rows
  272. */
  273. function bug_revision_like( $p_rev_id ) {
  274. $t_bug_rev_table = db_get_table( 'bug_revision' );
  275. $t_query = "SELECT bug_id, bugnote_id, type FROM $t_bug_rev_table WHERE id=" . db_param();
  276. $t_result = db_query_bound( $t_query, array( $p_rev_id ) );
  277. if ( db_num_rows( $t_result ) < 1 ) {
  278. trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
  279. }
  280. $t_row = db_fetch_array( $t_result );
  281. $t_bug_id = $t_row['bug_id'];
  282. $t_bugnote_id = $t_row['bugnote_id'];
  283. $t_type = $t_row['type'];
  284. $t_params = array( $t_bug_id );
  285. $t_query = "SELECT * FROM $t_bug_rev_table
  286. WHERE bug_id=" . db_param();
  287. if ( REV_ANY < $t_type ) {
  288. $t_query .= ' AND type=' . db_param();
  289. $t_params[] = $t_type;
  290. }
  291. if ( $t_bugnote_id > 0 ) {
  292. $t_query .= ' AND bugnote_id=' . db_param();
  293. $t_params[] = $t_bugnote_id;
  294. } else {
  295. $t_query .= ' AND bugnote_id=0';
  296. }
  297. $t_query .= ' ORDER BY timestamp ASC';
  298. $t_result = db_query_bound( $t_query, $t_params );
  299. $t_revisions = array();
  300. while( $t_row = db_fetch_array( $t_result ) ) {
  301. $t_revisions[$t_row['id']] = $t_row;
  302. }
  303. return $t_revisions;
  304. }