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