]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/diff/TableDiffFormatter.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / diff / TableDiffFormatter.php
1 <?php
2 /**
3  * Portions taken from phpwiki-1.3.3.
4  *
5  * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6  * You may copy this code freely under the conditions of the GPL.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @file
24  * @ingroup DifferenceEngine
25  */
26
27 /**
28  * MediaWiki default table style diff formatter
29  * @todo document
30  * @private
31  * @ingroup DifferenceEngine
32  */
33 class TableDiffFormatter extends DiffFormatter {
34
35         function __construct() {
36                 $this->leadingContextLines = 2;
37                 $this->trailingContextLines = 2;
38         }
39
40         /**
41          * @static
42          * @param string $msg
43          *
44          * @return mixed
45          */
46         public static function escapeWhiteSpace( $msg ) {
47                 $msg = preg_replace( '/^ /m', '&#160; ', $msg );
48                 $msg = preg_replace( '/ $/m', ' &#160;', $msg );
49                 $msg = preg_replace( '/  /', '&#160; ', $msg );
50
51                 return $msg;
52         }
53
54         /**
55          * @param int $xbeg
56          * @param int $xlen
57          * @param int $ybeg
58          * @param int $ylen
59          *
60          * @return string
61          */
62         protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
63                 // '<!--LINE \d+ -->' get replaced by a localised line number
64                 // in DifferenceEngine::localiseLineNumbers
65                 $r = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l' .
66                         $xbeg .
67                         '" ><!--LINE ' .
68                         $xbeg .
69                         "--></td>\n" .
70                         '<td colspan="2" class="diff-lineno"><!--LINE ' .
71                         $ybeg .
72                         "--></td></tr>\n";
73
74                 return $r;
75         }
76
77         /**
78          * Writes the header to the output buffer.
79          *
80          * @param string $header
81          */
82         protected function startBlock( $header ) {
83                 $this->writeOutput( $header );
84         }
85
86         protected function endBlock() {
87         }
88
89         /**
90          * @param string[] $lines
91          * @param string $prefix
92          * @param string $color
93          */
94         protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
95         }
96
97         /**
98          * HTML-escape parameter before calling this
99          *
100          * @param string $line
101          *
102          * @return string
103          */
104         protected function addedLine( $line ) {
105                 return $this->wrapLine( '+', 'diff-addedline', $line );
106         }
107
108         /**
109          * HTML-escape parameter before calling this
110          *
111          * @param string $line
112          *
113          * @return string
114          */
115         protected function deletedLine( $line ) {
116                 return $this->wrapLine( '−', 'diff-deletedline', $line );
117         }
118
119         /**
120          * HTML-escape parameter before calling this
121          *
122          * @param string $line
123          *
124          * @return string
125          */
126         protected function contextLine( $line ) {
127                 return $this->wrapLine( '&#160;', 'diff-context', $line );
128         }
129
130         /**
131          * @param string $marker
132          * @param string $class Unused
133          * @param string $line
134          *
135          * @return string
136          */
137         protected function wrapLine( $marker, $class, $line ) {
138                 if ( $line !== '' ) {
139                         // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
140                         $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
141                 }
142
143                 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
144         }
145
146         /**
147          * @return string
148          */
149         protected function emptyLine() {
150                 return '<td colspan="2">&#160;</td>';
151         }
152
153         /**
154          * Writes all lines to the output buffer, each enclosed in <tr>.
155          *
156          * @param string[] $lines
157          */
158         protected function added( $lines ) {
159                 foreach ( $lines as $line ) {
160                         $this->writeOutput( '<tr>' . $this->emptyLine() .
161                                 $this->addedLine( '<ins class="diffchange">' .
162                                         htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n" );
163                 }
164         }
165
166         /**
167          * Writes all lines to the output buffer, each enclosed in <tr>.
168          *
169          * @param string[] $lines
170          */
171         protected function deleted( $lines ) {
172                 foreach ( $lines as $line ) {
173                         $this->writeOutput( '<tr>' . $this->deletedLine( '<del class="diffchange">' .
174                                         htmlspecialchars( $line ) . '</del>' ) .
175                                 $this->emptyLine() . "</tr>\n" );
176                 }
177         }
178
179         /**
180          * Writes all lines to the output buffer, each enclosed in <tr>.
181          *
182          * @param string[] $lines
183          */
184         protected function context( $lines ) {
185                 foreach ( $lines as $line ) {
186                         $this->writeOutput( '<tr>' .
187                                 $this->contextLine( htmlspecialchars( $line ) ) .
188                                 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n" );
189                 }
190         }
191
192         /**
193          * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
194          *
195          * @param string[] $orig
196          * @param string[] $closing
197          */
198         protected function changed( $orig, $closing ) {
199                 $diff = new WordLevelDiff( $orig, $closing );
200                 $del = $diff->orig();
201                 $add = $diff->closing();
202
203                 # Notice that WordLevelDiff returns HTML-escaped output.
204                 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
205
206                 $ndel = count( $del );
207                 $nadd = count( $add );
208                 $n = max( $ndel, $nadd );
209                 for ( $i = 0; $i < $n; $i++ ) {
210                         $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
211                         $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
212                         $this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );
213                 }
214         }
215
216 }