r82489 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82488‎ | r82489 | r82490 >
Date:21:34, 19 February 2011
Author:reedy
Status:ok
Tags:
Comment:
Explicitally define some variables

Bracify file
Modified paths:
  • /trunk/phase3/includes/diff/WikiDiff.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/diff/WikiDiff.php
@@ -135,6 +135,8 @@
136136
137137 const MAX_XREF_LENGTH = 10000;
138138
 139+ protected $xchanged, $ychanged;
 140+
139141 function diff ( $from_lines, $to_lines ) {
140142 wfProfileIn( __METHOD__ );
141143
@@ -162,24 +164,28 @@
163165 $copy[] = $from_lines[$xi++];
164166 ++$yi;
165167 }
166 - if ( $copy )
167 - $edits[] = new _DiffOp_Copy( $copy );
 168+ if ( $copy ) {
 169+ $edits[] = new _DiffOp_Copy( $copy );
 170+ }
168171
169172 // Find deletes & adds.
170173 $delete = array();
171 - while ( $xi < $n_from && $this->xchanged[$xi] )
172 - $delete[] = $from_lines[$xi++];
 174+ while ( $xi < $n_from && $this->xchanged[$xi] ) {
 175+ $delete[] = $from_lines[$xi++];
 176+ }
173177
174178 $add = array();
175 - while ( $yi < $n_to && $this->ychanged[$yi] )
176 - $add[] = $to_lines[$yi++];
 179+ while ( $yi < $n_to && $this->ychanged[$yi] ) {
 180+ $add[] = $to_lines[$yi++];
 181+ }
177182
178 - if ( $delete && $add )
179 - $edits[] = new _DiffOp_Change( $delete, $add );
180 - elseif ( $delete )
181 - $edits[] = new _DiffOp_Delete( $delete );
182 - elseif ( $add )
183 - $edits[] = new _DiffOp_Add( $add );
 183+ if ( $delete && $add ) {
 184+ $edits[] = new _DiffOp_Change( $delete, $add );
 185+ } elseif ( $delete ) {
 186+ $edits[] = new _DiffOp_Delete( $delete );
 187+ } elseif ( $add ) {
 188+ $edits[] = new _DiffOp_Add( $add );
 189+ }
184190 }
185191 wfProfileOut( __METHOD__ );
186192 return $edits;
@@ -282,16 +288,18 @@
283289 // Things seems faster (I'm not sure I understand why)
284290 // when the shortest sequence in X.
285291 $flip = true;
286 - list ( $xoff, $xlim, $yoff, $ylim )
287 - = array( $yoff, $ylim, $xoff, $xlim );
 292+ list ( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
288293 }
289294
290 - if ( $flip )
291 - for ( $i = $ylim - 1; $i >= $yoff; $i-- )
292 - $ymatches[$this->xv[$i]][] = $i;
293 - else
294 - for ( $i = $ylim - 1; $i >= $yoff; $i-- )
295 - $ymatches[$this->yv[$i]][] = $i;
 295+ if ( $flip ) {
 296+ for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
 297+ $ymatches[$this->xv[$i]][] = $i;
 298+ }
 299+ } else {
 300+ for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
 301+ $ymatches[$this->yv[$i]][] = $i;
 302+ }
 303+ }
296304
297305 $this->lcs = 0;
298306 $this->seq[0] = $yoff - 1;
@@ -301,9 +309,11 @@
302310 $numer = $xlim - $xoff + $nchunks - 1;
303311 $x = $xoff;
304312 for ( $chunk = 0; $chunk < $nchunks; $chunk++ ) {
305 - if ( $chunk > 0 )
306 - for ( $i = 0; $i <= $this->lcs; $i++ )
307 - $ymids[$i][$chunk -1] = $this->seq[$i];
 313+ if ( $chunk > 0 ) {
 314+ for ( $i = 0; $i <= $this->lcs; $i++ ) {
 315+ $ymids[$i][$chunk -1] = $this->seq[$i];
 316+ }
 317+ }
308318
309319 $x1 = $xoff + (int)( ( $numer + ( $xlim -$xoff ) * $chunk ) / $nchunks );
310320 for ( ; $x < $x1; $x++ ) {
@@ -401,9 +411,9 @@
402412 --$ylim;
403413 }
404414
405 - if ( $xoff == $xlim || $yoff == $ylim )
406 - $lcs = 0;
407 - else {
 415+ if ( $xoff == $xlim || $yoff == $ylim ) {
 416+ $lcs = 0;
 417+ } else {
408418 // This is ad hoc but seems to work well.
409419 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
410420 // $nchunks = max(2,min(8,(int)$nchunks));
@@ -415,10 +425,12 @@
416426 if ( $lcs == 0 ) {
417427 // X and Y sequences have no common subsequence:
418428 // mark all changed.
419 - while ( $yoff < $ylim )
420 - $this->ychanged[$this->yind[$yoff++]] = 1;
421 - while ( $xoff < $xlim )
422 - $this->xchanged[$this->xind[$xoff++]] = 1;
 429+ while ( $yoff < $ylim ) {
 430+ $this->ychanged[$this->yind[$yoff++]] = 1;
 431+ }
 432+ while ( $xoff < $xlim ) {
 433+ $this->xchanged[$this->xind[$xoff++]] = 1;
 434+ }
423435 } else {
424436 // Use the partitions to split this problem into subproblems.
425437 reset( $seps );
@@ -463,8 +475,9 @@
464476 * Furthermore, $j is always kept so that $j == $other_len or
465477 * $other_changed[$j] == false.
466478 */
467 - while ( $j < $other_len && $other_changed[$j] )
468 - $j++;
 479+ while ( $j < $other_len && $other_changed[$j] ) {
 480+ $j++;
 481+ }
469482
470483 while ( $i < $len && ! $changed[$i] ) {
471484 assert( '$j < $other_len && ! $other_changed[$j]' );
@@ -473,14 +486,16 @@
474487 $j++;
475488 }
476489
477 - if ( $i == $len )
478 - break;
 490+ if ( $i == $len ) {
 491+ break;
 492+ }
479493
480494 $start = $i;
481495
482496 // Find the end of this run of changes.
483 - while ( ++$i < $len && $changed[$i] )
484 - continue;
 497+ while ( ++$i < $len && $changed[$i] ) {
 498+ continue;
 499+ }
485500
486501 do {
487502 /*
@@ -497,11 +512,13 @@
498513 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
499514 $changed[--$start] = 1;
500515 $changed[--$i] = false;
501 - while ( $start > 0 && $changed[$start - 1] )
502 - $start--;
 516+ while ( $start > 0 && $changed[$start - 1] ) {
 517+ $start--;
 518+ }
503519 assert( '$j > 0' );
504 - while ( $other_changed[--$j] )
505 - continue;
 520+ while ( $other_changed[--$j] ) {
 521+ continue;
 522+ }
506523 assert( '$j >= 0 && !$other_changed[$j]' );
507524 }
508525
@@ -522,15 +539,17 @@
523540 while ( $i < $len && $lines[$start] == $lines[$i] ) {
524541 $changed[$start++] = false;
525542 $changed[$i++] = 1;
526 - while ( $i < $len && $changed[$i] )
527 - $i++;
 543+ while ( $i < $len && $changed[$i] ) {
 544+ $i++;
 545+ }
528546
529547 assert( '$j < $other_len && ! $other_changed[$j]' );
530548 $j++;
531549 if ( $j < $other_len && $other_changed[$j] ) {
532550 $corresponding = $i;
533 - while ( $j < $other_len && $other_changed[$j] )
534 - $j++;
 551+ while ( $j < $other_len && $other_changed[$j] ) {
 552+ $j++;
 553+ }
535554 }
536555 }
537556 } while ( $runlength != $i - $start );
@@ -543,8 +562,9 @@
544563 $changed[--$start] = 1;
545564 $changed[--$i] = 0;
546565 assert( '$j > 0' );
547 - while ( $other_changed[--$j] )
548 - continue;
 566+ while ( $other_changed[--$j] ) {
 567+ continue;
 568+ }
549569 assert( '$j >= 0 && !$other_changed[$j]' );
550570 }
551571 }
@@ -602,8 +622,9 @@
603623 */
604624 function isEmpty () {
605625 foreach ( $this->edits as $edit ) {
606 - if ( $edit->type != 'copy' )
607 - return false;
 626+ if ( $edit->type != 'copy' ) {
 627+ return false;
 628+ }
608629 }
609630 return true;
610631 }
@@ -618,8 +639,9 @@
619640 function lcs () {
620641 $lcs = 0;
621642 foreach ( $this->edits as $edit ) {
622 - if ( $edit->type == 'copy' )
623 - $lcs += sizeof( $edit->orig );
 643+ if ( $edit->type == 'copy' ) {
 644+ $lcs += sizeof( $edit->orig );
 645+ }
624646 }
625647 return $lcs;
626648 }
@@ -636,8 +658,9 @@
637659 $lines = array();
638660
639661 foreach ( $this->edits as $edit ) {
640 - if ( $edit->orig )
641 - array_splice( $lines, sizeof( $lines ), 0, $edit->orig );
 662+ if ( $edit->orig ) {
 663+ array_splice( $lines, sizeof( $lines ), 0, $edit->orig );
 664+ }
642665 }
643666 return $lines;
644667 }
@@ -654,8 +677,9 @@
655678 $lines = array();
656679
657680 foreach ( $this->edits as $edit ) {
658 - if ( $edit->closing )
659 - array_splice( $lines, sizeof( $lines ), 0, $edit->closing );
 681+ if ( $edit->closing ) {
 682+ array_splice( $lines, sizeof( $lines ), 0, $edit->closing );
 683+ }
660684 }
661685 return $lines;
662686 }
@@ -667,22 +691,27 @@
668692 */
669693 function _check ( $from_lines, $to_lines ) {
670694 wfProfileIn( __METHOD__ );
671 - if ( serialize( $from_lines ) != serialize( $this->orig() ) )
672 - trigger_error( "Reconstructed original doesn't match", E_USER_ERROR );
673 - if ( serialize( $to_lines ) != serialize( $this->closing() ) )
674 - trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR );
 695+ if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
 696+ trigger_error( "Reconstructed original doesn't match", E_USER_ERROR );
 697+ }
 698+ if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
 699+ trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR );
 700+ }
675701
676702 $rev = $this->reverse();
677 - if ( serialize( $to_lines ) != serialize( $rev->orig() ) )
678 - trigger_error( "Reversed original doesn't match", E_USER_ERROR );
679 - if ( serialize( $from_lines ) != serialize( $rev->closing() ) )
680 - trigger_error( "Reversed closing doesn't match", E_USER_ERROR );
 703+ if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
 704+ trigger_error( "Reversed original doesn't match", E_USER_ERROR );
 705+ }
 706+ if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
 707+ trigger_error( "Reversed closing doesn't match", E_USER_ERROR );
 708+ }
681709
682710
683711 $prevtype = 'none';
684712 foreach ( $this->edits as $edit ) {
685 - if ( $prevtype == $edit->type )
686 - trigger_error( "Edit sequence is non-optimal", E_USER_ERROR );
 713+ if ( $prevtype == $edit->type ) {
 714+ trigger_error( "Edit sequence is non-optimal", E_USER_ERROR );
 715+ }
687716 $prevtype = $edit->type;
688717 }
689718
@@ -723,7 +752,7 @@
724753 * have the same number of elements as $to_lines.
725754 */
726755 function __construct( $from_lines, $to_lines,
727 - $mapped_from_lines, $mapped_to_lines ) {
 756+ $mapped_from_lines, $mapped_to_lines ) {
728757 wfProfileIn( __METHOD__ );
729758
730759 assert( sizeof( $from_lines ) == sizeof( $mapped_from_lines ) );
@@ -779,7 +808,7 @@
780809 /**
781810 * Format a diff.
782811 *
783 - * @param $diff object A Diff object.
 812+ * @param $diff Diff A Diff object.
784813 * @return string The formatted output.
785814 */
786815 function format( $diff ) {
@@ -806,8 +835,8 @@
807836 $block[] = new _DiffOp_Copy( $context );
808837 }
809838 $this->_block( $x0, $ntrail + $xi - $x0,
810 - $y0, $ntrail + $yi - $y0,
811 - $block );
 839+ $y0, $ntrail + $yi - $y0,
 840+ $block );
812841 $block = false;
813842 }
814843 }
@@ -819,22 +848,26 @@
820849 $x0 = $xi - sizeof( $context );
821850 $y0 = $yi - sizeof( $context );
822851 $block = array();
823 - if ( $context )
824 - $block[] = new _DiffOp_Copy( $context );
 852+ if ( $context ) {
 853+ $block[] = new _DiffOp_Copy( $context );
 854+ }
825855 }
826856 $block[] = $edit;
827857 }
828858
829 - if ( $edit->orig )
830 - $xi += sizeof( $edit->orig );
831 - if ( $edit->closing )
832 - $yi += sizeof( $edit->closing );
 859+ if ( $edit->orig ) {
 860+ $xi += sizeof( $edit->orig );
 861+ }
 862+ if ( $edit->closing ) {
 863+ $yi += sizeof( $edit->closing );
 864+ }
833865 }
834866
835 - if ( is_array( $block ) )
836 - $this->_block( $x0, $xi - $x0,
837 - $y0, $yi - $y0,
838 - $block );
 867+ if ( is_array( $block ) ) {
 868+ $this->_block( $x0, $xi - $x0,
 869+ $y0, $yi - $y0,
 870+ $block );
 871+ }
839872
840873 $end = $this->_end_diff();
841874 wfProfileOut( __METHOD__ );
@@ -845,16 +878,17 @@
846879 wfProfileIn( __METHOD__ );
847880 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
848881 foreach ( $edits as $edit ) {
849 - if ( $edit->type == 'copy' )
850 - $this->_context( $edit->orig );
851 - elseif ( $edit->type == 'add' )
852 - $this->_added( $edit->closing );
853 - elseif ( $edit->type == 'delete' )
854 - $this->_deleted( $edit->orig );
855 - elseif ( $edit->type == 'change' )
856 - $this->_changed( $edit->orig, $edit->closing );
857 - else
858 - trigger_error( 'Unknown edit type', E_USER_ERROR );
 882+ if ( $edit->type == 'copy' ) {
 883+ $this->_context( $edit->orig );
 884+ } elseif ( $edit->type == 'add' ) {
 885+ $this->_added( $edit->closing );
 886+ } elseif ( $edit->type == 'delete' ) {
 887+ $this->_deleted( $edit->orig );
 888+ } elseif ( $edit->type == 'change' ) {
 889+ $this->_changed( $edit->orig, $edit->closing );
 890+ } else {
 891+ trigger_error( 'Unknown edit type', E_USER_ERROR );
 892+ }
859893 }
860894 $this->_end_block();
861895 wfProfileOut( __METHOD__ );
@@ -871,10 +905,12 @@
872906 }
873907
874908 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
875 - if ( $xlen > 1 )
876 - $xbeg .= "," . ( $xbeg + $xlen - 1 );
877 - if ( $ylen > 1 )
878 - $ybeg .= "," . ( $ybeg + $ylen - 1 );
 909+ if ( $xlen > 1 ) {
 910+ $xbeg .= "," . ( $xbeg + $xlen - 1 );
 911+ }
 912+ if ( $ylen > 1 ) {
 913+ $ybeg .= "," . ( $ybeg + $ylen - 1 );
 914+ }
879915
880916 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
881917 }
@@ -887,8 +923,9 @@
888924 }
889925
890926 function _lines( $lines, $prefix = ' ' ) {
891 - foreach ( $lines as $line )
892 - echo "$prefix $line\n";
 927+ foreach ( $lines as $line ) {
 928+ echo "$prefix $line\n";
 929+ }
893930 }
894931
895932 function _context( $lines ) {
@@ -942,40 +979,41 @@
943980 $oldline = 1;
944981 $newline = 1;
945982 $retval = array();
946 - foreach ( $diff->edits as $edit )
947 - switch( $edit->type ) {
948 - case 'add':
949 - foreach ( $edit->closing as $l ) {
950 - $retval[] = array(
951 - 'action' => 'add',
952 - 'new' => $l,
953 - 'newline' => $newline++
954 - );
955 - }
956 - break;
957 - case 'delete':
958 - foreach ( $edit->orig as $l ) {
959 - $retval[] = array(
960 - 'action' => 'delete',
961 - 'old' => $l,
962 - 'oldline' => $oldline++,
963 - );
964 - }
965 - break;
966 - case 'change':
967 - foreach ( $edit->orig as $i => $l ) {
968 - $retval[] = array(
969 - 'action' => 'change',
970 - 'old' => $l,
971 - 'new' => @$edit->closing[$i],
972 - 'oldline' => $oldline++,
973 - 'newline' => $newline++,
974 - );
975 - }
976 - break;
977 - case 'copy':
978 - $oldline += count( $edit->orig );
979 - $newline += count( $edit->orig );
 983+ foreach ( $diff->edits as $edit ) {
 984+ switch( $edit->type ) {
 985+ case 'add':
 986+ foreach ( $edit->closing as $l ) {
 987+ $retval[] = array(
 988+ 'action' => 'add',
 989+ 'new' => $l,
 990+ 'newline' => $newline++
 991+ );
 992+ }
 993+ break;
 994+ case 'delete':
 995+ foreach ( $edit->orig as $l ) {
 996+ $retval[] = array(
 997+ 'action' => 'delete',
 998+ 'old' => $l,
 999+ 'oldline' => $oldline++,
 1000+ );
 1001+ }
 1002+ break;
 1003+ case 'change':
 1004+ foreach ( $edit->orig as $i => $l ) {
 1005+ $retval[] = array(
 1006+ 'action' => 'change',
 1007+ 'old' => $l,
 1008+ 'new' => @$edit->closing[$i],
 1009+ 'oldline' => $oldline++,
 1010+ 'newline' => $newline++,
 1011+ );
 1012+ }
 1013+ break;
 1014+ case 'copy':
 1015+ $oldline += count( $edit->orig );
 1016+ $newline += count( $edit->orig );
 1017+ }
9801018 }
9811019 return $retval;
9821020 }
@@ -1003,14 +1041,15 @@
10041042
10051043 function _flushGroup ( $new_tag ) {
10061044 if ( $this->_group !== '' ) {
1007 - if ( $this->_tag == 'ins' )
1008 - $this->_line .= '<ins class="diffchange diffchange-inline">' .
1009 - htmlspecialchars ( $this->_group ) . '</ins>';
1010 - elseif ( $this->_tag == 'del' )
1011 - $this->_line .= '<del class="diffchange diffchange-inline">' .
1012 - htmlspecialchars ( $this->_group ) . '</del>';
1013 - else
1014 - $this->_line .= htmlspecialchars ( $this->_group );
 1045+ if ( $this->_tag == 'ins' ) {
 1046+ $this->_line .= '<ins class="diffchange diffchange-inline">' .
 1047+ htmlspecialchars ( $this->_group ) . '</ins>';
 1048+ } elseif ( $this->_tag == 'del' ) {
 1049+ $this->_line .= '<del class="diffchange diffchange-inline">' .
 1050+ htmlspecialchars ( $this->_group ) . '</del>';
 1051+ } else {
 1052+ $this->_line .= htmlspecialchars ( $this->_group );
 1053+ }
10151054 }
10161055 $this->_group = '';
10171056 $this->_tag = $new_tag;
@@ -1018,22 +1057,25 @@
10191058
10201059 function _flushLine ( $new_tag ) {
10211060 $this->_flushGroup( $new_tag );
1022 - if ( $this->_line != '' )
1023 - array_push ( $this->_lines, $this->_line );
1024 - else
1025 - # make empty lines visible by inserting an NBSP
1026 - array_push ( $this->_lines, NBSP );
 1061+ if ( $this->_line != '' ) {
 1062+ array_push ( $this->_lines, $this->_line );
 1063+ } else {
 1064+ # make empty lines visible by inserting an NBSP
 1065+ array_push ( $this->_lines, NBSP );
 1066+ }
10271067 $this->_line = '';
10281068 }
10291069
10301070 function addWords ( $words, $tag = '' ) {
1031 - if ( $tag != $this->_tag )
1032 - $this->_flushGroup( $tag );
 1071+ if ( $tag != $this->_tag ) {
 1072+ $this->_flushGroup( $tag );
 1073+ }
10331074
10341075 foreach ( $words as $word ) {
10351076 // new-line should only come as first char of word.
1036 - if ( $word == '' )
1037 - continue;
 1077+ if ( $word == '' ) {
 1078+ continue;
 1079+ }
10381080 if ( $word[0] == "\n" ) {
10391081 $this->_flushLine( $tag );
10401082 $word = substr( $word, 1 );
@@ -1089,7 +1131,7 @@
10901132 } else {
10911133 $m = array();
10921134 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1093 - $line, $m ) )
 1135+ $line, $m ) )
10941136 {
10951137 $words = array_merge( $words, $m[0] );
10961138 $stripped = array_merge( $stripped, $m[1] );
@@ -1105,10 +1147,11 @@
11061148 $orig = new _HWLDF_WordAccumulator;
11071149
11081150 foreach ( $this->edits as $edit ) {
1109 - if ( $edit->type == 'copy' )
1110 - $orig->addWords( $edit->orig );
1111 - elseif ( $edit->orig )
1112 - $orig->addWords( $edit->orig, 'del' );
 1151+ if ( $edit->type == 'copy' ) {
 1152+ $orig->addWords( $edit->orig );
 1153+ } elseif ( $edit->orig ) {
 1154+ $orig->addWords( $edit->orig, 'del' );
 1155+ }
11131156 }
11141157 $lines = $orig->getLines();
11151158 wfProfileOut( __METHOD__ );
@@ -1120,10 +1163,11 @@
11211164 $closing = new _HWLDF_WordAccumulator;
11221165
11231166 foreach ( $this->edits as $edit ) {
1124 - if ( $edit->type == 'copy' )
1125 - $closing->addWords( $edit->closing );
1126 - elseif ( $edit->closing )
1127 - $closing->addWords( $edit->closing, 'ins' );
 1167+ if ( $edit->type == 'copy' ) {
 1168+ $closing->addWords( $edit->closing );
 1169+ } elseif ( $edit->closing ) {
 1170+ $closing->addWords( $edit->closing, 'ins' );
 1171+ }
11281172 }
11291173 $lines = $closing->getLines();
11301174 wfProfileOut( __METHOD__ );

Sign-offs

UserFlagDate
Hasharinspected09:35, 1 March 2011

Status & tagging log