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     function toHTML($input)
11     {
12         
13     
14     
15         $nlines = 0;
16
17         if (is_resource($input)) {
18             $lines = array();
19             while (($line = fgets($input)) !== false) {
20               $lines[] = rtrim($line, "\r\n");
21             }
22             $input = $lines;
23         }
24         
25         // abase???
26         if (is_string($input)) {
27             $abase = md5($input);
28             $input = preg_split("/\r?\n/", $input);
29         } else {
30             $abase = md5(join("\n", $input));
31         }
32
33         $html = "<table class='code diff'>";
34             //$html = "<pre class='code diff'>";
35           
36         while (true) {
37             if (!count($input)) {
38                 break;
39             }
40             $line = array_shift($input);
41             $nlines++;
42             if (!strncmp($line, '@@ ', 3)) {
43                 /* done with preamble */
44                 break;
45             }
46             $html .=  "<tr class='meta'><td class='lineno'></td>".
47                     "<td class='lineno'></td><td class='lineno'></td><td width='100%'>". 
48                     htmlspecialchars($line, ENT_QUOTES, 'utf-8') .
49                     '</tr>';
50         }
51           
52         $lines = array(0, 0);
53         $first = false;
54         while (true) {
55             $class = 'unmod';
56           
57             if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
58                   $line, $M)) {
59                 $lines[0] = (int)$M[1] - 1;
60                 $lines[1] = (int)$M[2] - 1;
61                 $class = 'meta';
62                 $first = true;
63             } elseif (strlen($line)) {
64                 if ($line[0] == '-') {
65                     $lines[0]++;
66                     $class = 'removed';
67                 } elseif ($line[0] == '+') {
68                     $lines[1]++;
69                     $class = 'added';
70                 } else {
71                     $lines[0]++;
72                     $lines[1]++;
73                 }
74             } else {
75                 $lines[0]++;
76                 $lines[1]++;
77             }
78             
79             
80             $row = "<tr class='$class";
81             if ($first) {
82                 $row .= ' first';
83             }
84             if ($class != 'meta' && $first) {
85                 $first = false;
86             }
87             $row .= "'>";
88           
89             switch ($class) {
90                 case 'meta':
91                     $line_info = '';
92                     $row .= "<td class='lineno'></td><td class='lineno'></td>";
93                     break;
94                 
95                 case 'added':
96                     $row .= "<td class='lineno'></td><td class='lineno'>" .
97                         $lines[1] . "</td>";
98                     break;
99                 
100                 case 'removed':
101                     $row .= "<td class='lineno'>" . $lines[0] .
102                             "</td><td class='lineno'></td>";
103                     break;
104                 
105                 default:
106                     $row .= "<td class='lineno'>" . $lines[0] .
107                         "</td><td class='lineno'>" . $lines[1] . "</td>";
108             }
109             
110             $anchor = $abase . '.' . $nlines;
111            $row .= "<td class='linelink'><a name='$anchor'></a>".
112                     "<a href='#$anchor' title='link to this line'>#</a></td>";
113           
114               $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
115               $row .= "<td class='line' width='100%'>$line</td></tr>\n";
116               $html .= $row;
117           
118               if (!count($input)) {
119                 break;
120               }
121               $line = array_shift($input);
122               $nlines++;
123             }
124           
125             if ($nlines == 0) {
126               return null;
127             }
128           
129             $html .= "</table>";
130             return $html;
131 }