PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mantisbt-1.2.8/core/bug_revision_api.php

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