TableDiffFormatter.php 5.3 KB

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