MediaWiki  REL1_23
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         $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
00064             '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
00065 
00066         return $r;
00067     }
00068 
00074     protected function startBlock( $header ) {
00075         echo $header;
00076     }
00077 
00078     protected function endBlock() {
00079     }
00080 
00086     protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
00087     }
00088 
00096     protected function addedLine( $line ) {
00097         return $this->wrapLine( '+', 'diff-addedline', $line );
00098     }
00099 
00107     protected function deletedLine( $line ) {
00108         return $this->wrapLine( '−', 'diff-deletedline', $line );
00109     }
00110 
00118     protected function contextLine( $line ) {
00119         return $this->wrapLine( '&#160;', 'diff-context', $line );
00120     }
00121 
00129     protected function wrapLine( $marker, $class, $line ) {
00130         if ( $line !== '' ) {
00131             // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
00132             $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
00133         }
00134 
00135         return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
00136     }
00137 
00141     protected function emptyLine() {
00142         return '<td colspan="2">&#160;</td>';
00143     }
00144 
00150     protected function added( $lines ) {
00151         foreach ( $lines as $line ) {
00152             echo '<tr>' . $this->emptyLine() .
00153                 $this->addedLine( '<ins class="diffchange">' .
00154                     htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
00155         }
00156     }
00157 
00163     protected function deleted( $lines ) {
00164         foreach ( $lines as $line ) {
00165             echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
00166                     htmlspecialchars( $line ) . '</del>' ) .
00167                 $this->emptyLine() . "</tr>\n";
00168         }
00169     }
00170 
00176     protected function context( $lines ) {
00177         foreach ( $lines as $line ) {
00178             echo '<tr>' .
00179                 $this->contextLine( htmlspecialchars( $line ) ) .
00180                 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
00181         }
00182     }
00183 
00190     protected function changed( $orig, $closing ) {
00191         wfProfileIn( __METHOD__ );
00192 
00193         $diff = new WordLevelDiff( $orig, $closing );
00194         $del = $diff->orig();
00195         $add = $diff->closing();
00196 
00197         # Notice that WordLevelDiff returns HTML-escaped output.
00198         # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
00199 
00200         while ( $line = array_shift( $del ) ) {
00201             $aline = array_shift( $add );
00202             echo '<tr>' . $this->deletedLine( $line ) .
00203                 $this->addedLine( $aline ) . "</tr>\n";
00204         }
00205         foreach ( $add as $line ) { # If any leftovers
00206             echo '<tr>' . $this->emptyLine() .
00207                 $this->addedLine( $line ) . "</tr>\n";
00208         }
00209         wfProfileOut( __METHOD__ );
00210     }
00211 
00212 }