[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Portions taken from phpwiki-1.3.3. 4 * 5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <[email protected]> 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 35 function __construct() { 36 $this->leadingContextLines = 2; 37 $this->trailingContextLines = 2; 38 } 39 40 /** 41 * @static 42 * @param string $msg 43 * 44 * @return mixed 45 */ 46 public static function escapeWhiteSpace( $msg ) { 47 $msg = preg_replace( '/^ /m', '  ', $msg ); 48 $msg = preg_replace( '/ $/m', '  ', $msg ); 49 $msg = preg_replace( '/ /', '  ', $msg ); 50 51 return $msg; 52 } 53 54 /** 55 * @param int $xbeg 56 * @param int $xlen 57 * @param int $ybeg 58 * @param int $ylen 59 * 60 * @return string 61 */ 62 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) { 63 // '<!--LINE \d+ -->' get replaced by a localised line number 64 // in DifferenceEngine::localiseLineNumbers 65 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" . 66 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n"; 67 68 return $r; 69 } 70 71 /** 72 * Writes the header to the output buffer. 73 * 74 * @param string $header 75 */ 76 protected function startBlock( $header ) { 77 echo $header; 78 } 79 80 protected function endBlock() { 81 } 82 83 /** 84 * @param string[] $lines 85 * @param string $prefix 86 * @param string $color 87 */ 88 protected function lines( $lines, $prefix = ' ', $color = 'white' ) { 89 } 90 91 /** 92 * HTML-escape parameter before calling this 93 * 94 * @param string $line 95 * 96 * @return string 97 */ 98 protected function addedLine( $line ) { 99 return $this->wrapLine( '+', 'diff-addedline', $line ); 100 } 101 102 /** 103 * HTML-escape parameter before calling this 104 * 105 * @param string $line 106 * 107 * @return string 108 */ 109 protected function deletedLine( $line ) { 110 return $this->wrapLine( '−', 'diff-deletedline', $line ); 111 } 112 113 /** 114 * HTML-escape parameter before calling this 115 * 116 * @param string $line 117 * 118 * @return string 119 */ 120 protected function contextLine( $line ) { 121 return $this->wrapLine( ' ', 'diff-context', $line ); 122 } 123 124 /** 125 * @param string $marker 126 * @param string $class Unused 127 * @param string $line 128 * 129 * @return string 130 */ 131 protected function wrapLine( $marker, $class, $line ) { 132 if ( $line !== '' ) { 133 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly 134 $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) ); 135 } 136 137 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>"; 138 } 139 140 /** 141 * @return string 142 */ 143 protected function emptyLine() { 144 return '<td colspan="2"> </td>'; 145 } 146 147 /** 148 * Writes all lines to the output buffer, each enclosed in <tr>. 149 * 150 * @param string[] $lines 151 */ 152 protected function added( $lines ) { 153 foreach ( $lines as $line ) { 154 echo '<tr>' . $this->emptyLine() . 155 $this->addedLine( '<ins class="diffchange">' . 156 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n"; 157 } 158 } 159 160 /** 161 * Writes all lines to the output buffer, each enclosed in <tr>. 162 * 163 * @param string[] $lines 164 */ 165 protected function deleted( $lines ) { 166 foreach ( $lines as $line ) { 167 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' . 168 htmlspecialchars( $line ) . '</del>' ) . 169 $this->emptyLine() . "</tr>\n"; 170 } 171 } 172 173 /** 174 * Writes all lines to the output buffer, each enclosed in <tr>. 175 * 176 * @param string[] $lines 177 */ 178 protected function context( $lines ) { 179 foreach ( $lines as $line ) { 180 echo '<tr>' . 181 $this->contextLine( htmlspecialchars( $line ) ) . 182 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n"; 183 } 184 } 185 186 /** 187 * Writes the two sets of lines to the output buffer, each enclosed in <tr>. 188 * 189 * @param string[] $orig 190 * @param string[] $closing 191 */ 192 protected function changed( $orig, $closing ) { 193 wfProfileIn( __METHOD__ ); 194 195 $diff = new WordLevelDiff( $orig, $closing ); 196 $del = $diff->orig(); 197 $add = $diff->closing(); 198 199 # Notice that WordLevelDiff returns HTML-escaped output. 200 # Hence, we will be calling addedLine/deletedLine without HTML-escaping. 201 202 while ( $line = array_shift( $del ) ) { 203 $aline = array_shift( $add ); 204 echo '<tr>' . $this->deletedLine( $line ) . 205 $this->addedLine( $aline ) . "</tr>\n"; 206 } 207 foreach ( $add as $line ) { # If any leftovers 208 echo '<tr>' . $this->emptyLine() . 209 $this->addedLine( $line ) . "</tr>\n"; 210 } 211 wfProfileOut( __METHOD__ ); 212 } 213 214 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |