Diff.php
[Pman.MTrack] / Diff.php
1 <?php
2 class Pman_MTrack_Diff extends Pman {
3     
4     function getAuth()
5     {
6         die("nothing here yet");
7     }
8    
9    
10     static function toHTML($input)
11     {
12          
13         $nlines = 0;
14
15         if (is_resource($input)) {
16             $lines = array();
17             while (($line = fgets($input)) !== false) {
18               $lines[] = rtrim($line, "\r\n");
19             }
20             $input = $lines;
21         }
22         
23         // abase???
24         if (is_string($input)) {
25             $abase = md5($input);
26             $input = preg_split("/\r?\n/", $input);
27         } else {
28             $abase = md5(join("\n", $input));
29         }
30         
31         $ret = array();
32            
33         while (true) {
34              
35             
36             if (!count($input)) {
37                 break;
38             }
39             
40             $line = array_shift($input);
41             $nlines++;
42             
43             if (!strncmp($line, '@@ ', 3)) {
44                 /* done with preamble */
45                 break;
46             }
47             $ret[] = (object)array(
48                 
49                 'line' => $line,
50                 'meta' => true,
51                 'cls' =>'meta'
52                 
53             );
54              
55         }
56         $class = 'meta';
57           
58     $lines = array(0, 0);
59         $first = false;
60         
61         $in_meta = false;
62         
63         while (true) {
64             $class = 'unmod';
65             
66             if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
67                   $line, $M)) {
68                 $lines[0] = (int)$M[1] - 1;
69                 $lines[1] = (int)$M[2] - 1;
70                 $class = 'meta';
71                 $first = true;
72                 $in_meta = false;
73             } else if (preg_match("/^diff /", $line)) {
74                 $class = 'meta';
75                 $in_meta=true;
76             
77             } elseif (strlen($line)) {
78                 if ($line[0] == '-') {
79                     $lines[0]++;
80                     $class = 'removed';
81                 } elseif ($line[0] == '+') {
82                     $lines[1]++;
83                     $class = 'added';
84                 } else {
85                     $lines[0]++;
86                     $lines[1]++;
87                 }
88             } else {
89                 $lines[0]++;
90                 $lines[1]++;
91             }
92             
93             if ($in_meta) {
94                 $class = 'meta';
95             }
96             
97             
98             $row = "<tr class='$class";
99             if ($first) {
100                 $row .= ' first';
101             }
102             if ($class != 'meta' && $first) {
103                 $first = false;
104             }
105             $row .= "'>";
106
107             $anchor = $abase . '.' . $nlines;
108             $anchor_row = "<td class='linelink'><a name='$anchor'></a>".
109                     "<a href='#$anchor' title='link to this line'>#</a></td>";
110           
111             $txt_data = '<td class="line" width="100%">'.
112                 htmlspecialchars(substr($line, 1), ENT_QUOTES, 'utf-8') .
113                 "</td></tr>\n";
114                 
115               
116           
117             switch ($class) {
118                 
119                 case 'meta':
120                      $row .= '<td colspan="4" class="line" width="100%">' .
121                         htmlspecialchars($line, ENT_QUOTES, 'utf-8') . "</td></tr>\n";
122                     $html .= $row;
123                     break;
124                 
125                 case 'added':
126                     $row .= "<td class='lineno'></td><td class='lineno'>" .
127                         $lines[1] . "</td>" .
128                         $anchor_row . $txt_data   ;
129                     $html .= $row;
130
131                     break;
132                 
133                 case 'removed':
134                     $row .= "<td class='lineno'>" . $lines[0] .
135                             "</td><td class='lineno'></td>" .
136                         $anchor_row   . $txt_data ;
137                     $html .= $row;
138
139                     break;
140                 
141                 default:
142                     $row .= "<td class='lineno'>" . $lines[0] .
143                         "</td><td class='lineno'>" . $lines[1] . "</td>" .
144                         $anchor_row   . $txt_data ;
145                        $html .= $row;
146             }
147             
148            
149         
150             if (!count($input)) {
151                 break;
152             }
153             $line = array_shift($input);
154             $nlines++;
155         }
156           
157         if ($nlines == 0) {
158           return null;
159         }
160       
161         $html .= "</table>";
162         return $html;
163     }
164 }