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