Bump and prep 1.34.1
[lhc/web/wiklou.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 * @param string $msg
42 *
43 * @return mixed
44 */
45 public static function escapeWhiteSpace( $msg ) {
46 $msg = preg_replace( '/^ /m', "\u{00A0} ", $msg );
47 $msg = preg_replace( '/ $/m', " \u{00A0}", $msg );
48 $msg = preg_replace( '/ /', "\u{00A0} ", $msg );
49
50 return $msg;
51 }
52
53 /**
54 * @param int $xbeg
55 * @param int $xlen
56 * @param int $ybeg
57 * @param int $ylen
58 *
59 * @return string
60 */
61 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
62 // '<!--LINE \d+ -->' get replaced by a localised line number
63 // in DifferenceEngine::localiseLineNumbers
64 $r = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l' .
65 $xbeg .
66 '" ><!--LINE ' .
67 $xbeg .
68 "--></td>\n" .
69 '<td colspan="2" class="diff-lineno"><!--LINE ' .
70 $ybeg .
71 "--></td></tr>\n";
72
73 return $r;
74 }
75
76 /**
77 * Writes the header to the output buffer.
78 *
79 * @param string $header
80 */
81 protected function startBlock( $header ) {
82 $this->writeOutput( $header );
83 }
84
85 protected function endBlock() {
86 }
87
88 /**
89 * @param string[] $lines
90 * @param string $prefix
91 * @param string $color
92 */
93 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
94 }
95
96 /**
97 * HTML-escape parameter before calling this
98 *
99 * @param string $line
100 *
101 * @return string
102 */
103 protected function addedLine( $line ) {
104 return $this->wrapLine( '+', 'diff-addedline', $line );
105 }
106
107 /**
108 * HTML-escape parameter before calling this
109 *
110 * @param string $line
111 *
112 * @return string
113 */
114 protected function deletedLine( $line ) {
115 return $this->wrapLine( '−', 'diff-deletedline', $line );
116 }
117
118 /**
119 * HTML-escape parameter before calling this
120 *
121 * @param string $line
122 *
123 * @return string
124 */
125 protected function contextLine( $line ) {
126 return $this->wrapLine( "\u{00A0}", 'diff-context', $line );
127 }
128
129 /**
130 * @param string $marker
131 * @param string $class Unused
132 * @param string $line
133 *
134 * @return string
135 */
136 protected function wrapLine( $marker, $class, $line ) {
137 if ( $line !== '' ) {
138 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
139 $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
140 }
141
142 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
143 }
144
145 /**
146 * @return string
147 */
148 protected function emptyLine() {
149 return "<td colspan=\"2\">\u{00A0}</td>";
150 }
151
152 /**
153 * Writes all lines to the output buffer, each enclosed in <tr>.
154 *
155 * @param string[] $lines
156 */
157 protected function added( $lines ) {
158 foreach ( $lines as $line ) {
159 $this->writeOutput( '<tr>' . $this->emptyLine() .
160 $this->addedLine( '<ins class="diffchange">' .
161 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n" );
162 }
163 }
164
165 /**
166 * Writes all lines to the output buffer, each enclosed in <tr>.
167 *
168 * @param string[] $lines
169 */
170 protected function deleted( $lines ) {
171 foreach ( $lines as $line ) {
172 $this->writeOutput( '<tr>' . $this->deletedLine( '<del class="diffchange">' .
173 htmlspecialchars( $line ) . '</del>' ) .
174 $this->emptyLine() . "</tr>\n" );
175 }
176 }
177
178 /**
179 * Writes all lines to the output buffer, each enclosed in <tr>.
180 *
181 * @param string[] $lines
182 */
183 protected function context( $lines ) {
184 foreach ( $lines as $line ) {
185 $this->writeOutput( '<tr>' .
186 $this->contextLine( htmlspecialchars( $line ) ) .
187 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n" );
188 }
189 }
190
191 /**
192 * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
193 *
194 * @param string[] $orig
195 * @param string[] $closing
196 */
197 protected function changed( $orig, $closing ) {
198 $diff = new WordLevelDiff( $orig, $closing );
199 $del = $diff->orig();
200 $add = $diff->closing();
201
202 # Notice that WordLevelDiff returns HTML-escaped output.
203 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
204
205 $ndel = count( $del );
206 $nadd = count( $add );
207 $n = max( $ndel, $nadd );
208 for ( $i = 0; $i < $n; $i++ ) {
209 $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
210 $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
211 $this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );
212 }
213 }
214
215 }