Merge "Further expand Status unit tests"
[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 function __construct() {
35 $this->leadingContextLines = 2;
36 $this->trailingContextLines = 2;
37 }
38
39 /**
40 * @static
41 * @param $msg
42 * @return mixed
43 */
44 public static function escapeWhiteSpace( $msg ) {
45 $msg = preg_replace( '/^ /m', '&#160; ', $msg );
46 $msg = preg_replace( '/ $/m', ' &#160;', $msg );
47 $msg = preg_replace( '/ /', '&#160; ', $msg );
48
49 return $msg;
50 }
51
52 /**
53 * @param $xbeg
54 * @param $xlen
55 * @param $ybeg
56 * @param $ylen
57 * @return string
58 */
59 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
60 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
61 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
62
63 return $r;
64 }
65
66 /**
67 * @param $header
68 */
69 protected function startBlock( $header ) {
70 echo $header;
71 }
72
73 protected function endBlock() {
74 }
75
76 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
77 }
78
79 /**
80 * HTML-escape parameter before calling this
81 * @param $line
82 * @return string
83 */
84 protected function addedLine( $line ) {
85 return $this->wrapLine( '+', 'diff-addedline', $line );
86 }
87
88 /**
89 * HTML-escape parameter before calling this
90 * @param $line
91 * @return string
92 */
93 protected function deletedLine( $line ) {
94 return $this->wrapLine( '−', 'diff-deletedline', $line );
95 }
96
97 /**
98 * HTML-escape parameter before calling this
99 * @param $line
100 * @return string
101 */
102 protected function contextLine( $line ) {
103 return $this->wrapLine( '&#160;', 'diff-context', $line );
104 }
105
106 /**
107 * @param $marker
108 * @param $class
109 * @param $line
110 * @return string
111 */
112 protected function wrapLine( $marker, $class, $line ) {
113 if ( $line !== '' ) {
114 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
115 $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
116 }
117
118 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
119 }
120
121 /**
122 * @return string
123 */
124 protected function emptyLine() {
125 return '<td colspan="2">&#160;</td>';
126 }
127
128 /**
129 * @param $lines array
130 */
131 protected function added( $lines ) {
132 foreach ( $lines as $line ) {
133 echo '<tr>' . $this->emptyLine() .
134 $this->addedLine( '<ins class="diffchange">' .
135 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
136 }
137 }
138
139 /**
140 * @param $lines
141 */
142 protected function deleted( $lines ) {
143 foreach ( $lines as $line ) {
144 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
145 htmlspecialchars( $line ) . '</del>' ) .
146 $this->emptyLine() . "</tr>\n";
147 }
148 }
149
150 /**
151 * @param $lines
152 */
153 protected function context( $lines ) {
154 foreach ( $lines as $line ) {
155 echo '<tr>' .
156 $this->contextLine( htmlspecialchars( $line ) ) .
157 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
158 }
159 }
160
161 /**
162 * @param $orig
163 * @param $closing
164 */
165 protected function changed( $orig, $closing ) {
166 wfProfileIn( __METHOD__ );
167
168 $diff = new WordLevelDiff( $orig, $closing );
169 $del = $diff->orig();
170 $add = $diff->closing();
171
172 # Notice that WordLevelDiff returns HTML-escaped output.
173 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
174
175 while ( $line = array_shift( $del ) ) {
176 $aline = array_shift( $add );
177 echo '<tr>' . $this->deletedLine( $line ) .
178 $this->addedLine( $aline ) . "</tr>\n";
179 }
180 foreach ( $add as $line ) { # If any leftovers
181 echo '<tr>' . $this->emptyLine() .
182 $this->addedLine( $line ) . "</tr>\n";
183 }
184 wfProfileOut( __METHOD__ );
185 }
186 }