MediaWiki  REL1_24
TableDiffFormatter.php
Go to the documentation of this file.
00001 <?php
00033 class TableDiffFormatter extends DiffFormatter {
00034 
00035     function __construct() {
00036         $this->leadingContextLines = 2;
00037         $this->trailingContextLines = 2;
00038     }
00039 
00046     public static function escapeWhiteSpace( $msg ) {
00047         $msg = preg_replace( '/^ /m', '&#160; ', $msg );
00048         $msg = preg_replace( '/ $/m', ' &#160;', $msg );
00049         $msg = preg_replace( '/  /', '&#160; ', $msg );
00050 
00051         return $msg;
00052     }
00053 
00062     protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
00063         // '<!--LINE \d+ -->' get replaced by a localised line number
00064         // in DifferenceEngine::localiseLineNumbers
00065         $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
00066             '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
00067 
00068         return $r;
00069     }
00070 
00076     protected function startBlock( $header ) {
00077         echo $header;
00078     }
00079 
00080     protected function endBlock() {
00081     }
00082 
00088     protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
00089     }
00090 
00098     protected function addedLine( $line ) {
00099         return $this->wrapLine( '+', 'diff-addedline', $line );
00100     }
00101 
00109     protected function deletedLine( $line ) {
00110         return $this->wrapLine( '−', 'diff-deletedline', $line );
00111     }
00112 
00120     protected function contextLine( $line ) {
00121         return $this->wrapLine( '&#160;', 'diff-context', $line );
00122     }
00123 
00131     protected function wrapLine( $marker, $class, $line ) {
00132         if ( $line !== '' ) {
00133             // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
00134             $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
00135         }
00136 
00137         return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
00138     }
00139 
00143     protected function emptyLine() {
00144         return '<td colspan="2">&#160;</td>';
00145     }
00146 
00152     protected function added( $lines ) {
00153         foreach ( $lines as $line ) {
00154             echo '<tr>' . $this->emptyLine() .
00155                 $this->addedLine( '<ins class="diffchange">' .
00156                     htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
00157         }
00158     }
00159 
00165     protected function deleted( $lines ) {
00166         foreach ( $lines as $line ) {
00167             echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
00168                     htmlspecialchars( $line ) . '</del>' ) .
00169                 $this->emptyLine() . "</tr>\n";
00170         }
00171     }
00172 
00178     protected function context( $lines ) {
00179         foreach ( $lines as $line ) {
00180             echo '<tr>' .
00181                 $this->contextLine( htmlspecialchars( $line ) ) .
00182                 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
00183         }
00184     }
00185 
00192     protected function changed( $orig, $closing ) {
00193         wfProfileIn( __METHOD__ );
00194 
00195         $diff = new WordLevelDiff( $orig, $closing );
00196         $del = $diff->orig();
00197         $add = $diff->closing();
00198 
00199         # Notice that WordLevelDiff returns HTML-escaped output.
00200         # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
00201 
00202         while ( $line = array_shift( $del ) ) {
00203             $aline = array_shift( $add );
00204             echo '<tr>' . $this->deletedLine( $line ) .
00205                 $this->addedLine( $aline ) . "</tr>\n";
00206         }
00207         foreach ( $add as $line ) { # If any leftovers
00208             echo '<tr>' . $this->emptyLine() .
00209                 $this->addedLine( $line ) . "</tr>\n";
00210         }
00211         wfProfileOut( __METHOD__ );
00212     }
00213 
00214 }