trac.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. class TracTickets {
  3. /**
  4. * When open tickets for a Trac install is requested, the results are stored here.
  5. *
  6. * @var array
  7. */
  8. protected static $trac_ticket_cache = array();
  9. /**
  10. * Checks if track ticket #$ticket_id is resolved
  11. *
  12. * @return bool|null true if the ticket is resolved, false if not resolved, null on error
  13. */
  14. public static function isTracTicketClosed( $trac_url, $ticket_id ) {
  15. if ( ! isset( self::$trac_ticket_cache[ $trac_url ] ) ) {
  16. // In case you're running the tests offline, keep track of open tickets.
  17. $file = DIR_TESTDATA . '/.trac-ticket-cache.' . str_replace( array( 'http://', 'https://', '/' ), array( '', '', '-' ), rtrim( $trac_url, '/' ) );
  18. $tickets = @file_get_contents( $trac_url . '/query?status=%21closed&format=csv&col=id' );
  19. // Check if our HTTP request failed.
  20. if ( false === $tickets ) {
  21. if ( file_exists( $file ) ) {
  22. register_shutdown_function( array( 'TracTickets', 'usingLocalCache' ) );
  23. $tickets = file_get_contents( $file );
  24. } else {
  25. register_shutdown_function( array( 'TracTickets', 'forcingKnownBugs' ) );
  26. self::$trac_ticket_cache[ $trac_url ] = array();
  27. return true; // Assume the ticket is closed, which means it gets run.
  28. }
  29. } else {
  30. $tickets = substr( $tickets, 2 ); // remove 'id' column header
  31. $tickets = trim( $tickets );
  32. file_put_contents( $file, $tickets );
  33. }
  34. $tickets = explode( "\r\n", $tickets );
  35. self::$trac_ticket_cache[ $trac_url ] = $tickets;
  36. }
  37. return ! in_array( $ticket_id, self::$trac_ticket_cache[ $trac_url ] );
  38. }
  39. public static function usingLocalCache() {
  40. echo PHP_EOL . "\x1b[0m\x1b[30;43m\x1b[2K";
  41. echo 'INFO: Trac was inaccessible, so a local ticket status cache was used.' . PHP_EOL;
  42. echo "\x1b[0m\x1b[2K";
  43. }
  44. public static function forcingKnownBugs() {
  45. echo PHP_EOL . "\x1b[0m\x1b[37;41m\x1b[2K";
  46. echo "ERROR: Trac was inaccessible, so known bugs weren't able to be skipped." . PHP_EOL;
  47. echo "\x1b[0m\x1b[2K";
  48. }
  49. }