trac.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. class TracTickets {
  3. /**
  4. * When open tickets for a Trac installation 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 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  15. if ( ! extension_loaded( 'openssl' ) ) {
  16. $trac_url = preg_replace( '/^https:/', 'http:', $trac_url );
  17. }
  18. if ( ! isset( self::$trac_ticket_cache[ $trac_url ] ) ) {
  19. // In case you're running the tests offline, keep track of open tickets.
  20. $file = DIR_TESTDATA . '/.trac-ticket-cache.' . str_replace( array( 'http://', 'https://', '/' ), array( '', '', '-' ), rtrim( $trac_url, '/' ) );
  21. $tickets = @file_get_contents( $trac_url . '/query?status=%21closed&format=csv&col=id' );
  22. // Check if our HTTP request failed.
  23. if ( false === $tickets ) {
  24. if ( file_exists( $file ) ) {
  25. register_shutdown_function( array( 'TracTickets', 'usingLocalCache' ) );
  26. $tickets = file_get_contents( $file );
  27. } else {
  28. register_shutdown_function( array( 'TracTickets', 'forcingKnownBugs' ) );
  29. self::$trac_ticket_cache[ $trac_url ] = array();
  30. return true; // Assume the ticket is closed, which means it gets run.
  31. }
  32. } else {
  33. $tickets = substr( $tickets, 2 ); // Remove 'id' column header.
  34. $tickets = trim( $tickets );
  35. file_put_contents( $file, $tickets );
  36. }
  37. $tickets = explode( "\r\n", $tickets );
  38. self::$trac_ticket_cache[ $trac_url ] = $tickets;
  39. }
  40. return ! in_array( $ticket_id, self::$trac_ticket_cache[ $trac_url ], true );
  41. }
  42. // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  43. public static function usingLocalCache() {
  44. echo PHP_EOL . "\x1b[0m\x1b[30;43m\x1b[2K";
  45. echo 'Info: Trac was inaccessible, so a local ticket status cache was used.' . PHP_EOL;
  46. echo "\x1b[0m\x1b[2K";
  47. }
  48. // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  49. public static function forcingKnownBugs() {
  50. echo PHP_EOL . "\x1b[0m\x1b[37;41m\x1b[2K";
  51. echo "Error: Trac was inaccessible, so known bugs weren't able to be skipped." . PHP_EOL;
  52. echo "\x1b[0m\x1b[2K";
  53. }
  54. }