@@ -153,8 +153,6 @@ protected function diffTableContent()
153153 $ oldRows = $ this ->oldTable ->getRows ();
154154 $ newRows = $ this ->newTable ->getRows ();
155155
156- $ appliedRowSpans = array ();
157-
158156 $ oldMatchData = array ();
159157 $ newMatchData = array ();
160158
@@ -173,23 +171,117 @@ protected function diffTableContent()
173171 $ newText = $ newRow ->getInnerHtml ();
174172
175173 // similar_text
176- $ percentage = null ;
177- similar_text ($ oldText , $ newText , $ percentage );
174+ // $percentage = null;
175+ // similar_text($oldText, $newText, $percentage);
178176 $ percentage = $ this ->getMatchPercentage ($ oldRow , $ newRow );
179177
180178 $ oldMatchData [$ oldIndex ][$ newIndex ] = $ percentage ;
181179 $ newMatchData [$ newIndex ][$ oldIndex ] = $ percentage ;
182180 }
183181 }
184182
183+ $ matches = $ this ->getRowMatches ($ oldMatchData , $ newMatchData );
184+
185+ addDebugOutput ($ matches , __METHOD__ );
186+
187+ $ this ->diffTableRows ($ oldRows , $ newRows , $ oldMatchData );
188+
189+ $ this ->content = $ this ->htmlFromNode ($ this ->diffTable );
190+ }
191+
192+ protected function getRowMatches ($ oldMatchData , $ newMatchData )
193+ {
194+ $ matches = array ();
195+
196+ $ startInOld = 0 ;
197+ $ startInNew = 0 ;
198+ $ endInOld = count ($ oldMatchData );
199+ $ endInNew = count ($ newMatchData );
200+
201+ $ this ->findRowMatches ($ newMatchData , $ startInOld , $ endInOld , $ startInNew , $ endInNew , $ matches );
202+
203+ return $ matches ;
204+ }
205+
206+ protected function findRowMatches ($ newMatchData , $ startInOld , $ endInOld , $ startInNew , $ endInNew , &$ matches )
207+ {
208+ $ match = $ this ->findRowMatch ($ newMatchData , $ startInOld , $ endInOld , $ startInNew , $ endInNew );
209+ if ($ match !== null ) {
210+ if ($ startInOld < $ match ->getStartInOld () &&
211+ $ startInNew < $ match ->getStartInNew ()
212+ ) {
213+ $ this ->findRowMatches (
214+ $ newMatchData ,
215+ $ startInOld ,
216+ $ match ->getStartInOld (),
217+ $ startInNew ,
218+ $ match ->getStartInNew (),
219+ $ matches
220+ );
221+ }
222+
223+ $ matches [] = $ match ;
224+
225+ if ($ match ->getEndInOld () < $ endInOld &&
226+ $ match ->getEndInNew () < $ endInNew
227+ ) {
228+ $ this ->findRowMatches ($ newMatchData , $ match ->getEndInOld (), $ endInOld , $ match ->getEndInNew (), $ endInNew , $ matches );
229+ }
230+ }
231+ }
232+
233+ protected function findRowMatch ($ newMatchData , $ startInOld , $ endInOld , $ startInNew , $ endInNew )
234+ {
235+ $ bestMatch = null ;
236+ $ bestPercentage = 0 ;
237+
238+ foreach ($ newMatchData as $ newIndex => $ oldMatches ) {
239+ if ($ newIndex < $ startInNew ) {
240+ continue ;
241+ }
242+
243+ if ($ newIndex > $ endInNew ) {
244+ break ;
245+ }
246+ foreach ($ oldMatches as $ oldIndex => $ percentage ) {
247+ if ($ oldIndex < $ startInOld ) {
248+ continue ;
249+ }
250+
251+ if ($ oldIndex > $ endInOld ) {
252+ break ;
253+ }
254+
255+ if ($ percentage > $ bestPercentage ) {
256+ $ bestPercentage = $ percentage ;
257+ $ bestMatch = array (
258+ 'oldIndex ' => $ oldIndex ,
259+ 'newIndex ' => $ newIndex ,
260+ );
261+ }
262+ }
263+ }
264+
265+ if ($ bestMatch !== null ) {
266+ return new RowMatch ($ bestMatch ['newIndex ' ], $ bestMatch ['oldIndex ' ], $ bestMatch ['newIndex ' ] + 1 , $ bestMatch ['oldIndex ' ] + 1 );
267+ }
268+
269+ return null ;
270+ }
271+
272+ /**
273+ * @param $oldRows
274+ * @param $newRows
275+ * @param $oldMatchData
276+ */
277+ protected function diffTableRows ($ oldRows , $ newRows , $ oldMatchData )
278+ {
279+ $ appliedRowSpans = array ();
185280 $ currentIndexInOld = 0 ;
186- $ currentIndexInNew = 0 ;
187281 $ oldCount = count ($ oldRows );
188282 $ newCount = count ($ newRows );
189283 $ difference = max ($ oldCount , $ newCount ) - min ($ oldCount , $ newCount );
190284
191- // $this->matchThreshold = ($this->matchThreshold * 0.80);
192-
193285 foreach ($ newRows as $ newIndex => $ row ) {
194286 $ oldRow = $ this ->oldTable ->getRow ($ currentIndexInOld );
195287
@@ -240,8 +332,6 @@ protected function diffTableContent()
240332 $ this ->diffAndAppendRows ($ row , null , $ appliedRowSpans );
241333 }
242334 }
243-
244- $ this ->content = $ this ->htmlFromNode ($ this ->diffTable );
245335 }
246336
247337 /**
@@ -738,6 +828,7 @@ protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow)
738828 {
739829 $ matches = array ();
740830 $ thresholdCount = 0 ;
831+ $ firstCellMatch = false ;
741832 foreach ($ newRow ->getCells () as $ newIndex => $ newCell ) {
742833 $ oldCell = $ oldRow ->getCell ($ newIndex );
743834
@@ -748,11 +839,21 @@ protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow)
748839 $ matches [$ newIndex ] = $ percentage ;
749840
750841 if ($ percentage > ($ this ->matchThreshold * 0.50 )) {
842+ if ($ newIndex === 0 && $ percentage > 0.95 ) {
843+ $ firstCellMatch = true ;
844+ }
751845 $ thresholdCount ++;
752846 }
753847 }
754848 }
755849
756- return (count ($ matches ) > 0 ) ? $ thresholdCount / count ($ matches ) : 0 ;
850+ $ matchPercentage = (count ($ matches ) > 0 ) ? ($ thresholdCount / count ($ matches )) : 0 ;
851+
852+ if ($ firstCellMatch ) {
853+ // @todo: Weight the first cell match higher
854+ $ matchPercentage = $ matchPercentage * 1.50 ;
855+ }
856+
857+ return $ matchPercentage ;
757858 }
758859}
0 commit comments