| [ PHPXref.com ] | [ Generated: Sun Jul 20 21:04:18 2008 ] | [ WikyBlog 0.9.2 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <? 2 //$lang checked 3 4 if ( !defined('WikyBlog') ){ 5 die("Not an entry point..."); 6 } 7 8 9 /////////////////////////////////////////////////////////////////////////////////////////////////// 10 /////////////////////////////////////////////////////////////////////////////////////////////////// 11 /////////////////////////////////////////////////////////////////////////////////////////////////// 12 // 13 // difference_main 14 // 15 16 17 class difference_main{ 18 19 var $instructions; 20 var $keys; 21 var $differences = 0; 22 23 function difference_main($source,$result,$show=false){ 24 $instruction = array(); 25 26 // 1) if Objects => arrays 27 if( is_object($result) ){ 28 $type = get_class( $result ); 29 $type2 = get_class( $source ); 30 if( $type !== $type2){ 31 trigger_error('Objects are not of the same type '.$type.' :: '.$type2); 32 } 33 34 $source = get_object_vars( $source ); 35 $result = get_object_vars( $result ); 36 } 37 38 // 2) Make instructions for each field 39 40 if( $show ){ 41 global $dbObject; 42 $source = $dbObject->translateToUser($source); 43 $result = $dbObject->translateToUser($result); 44 45 $workingClass = new showComparison(); 46 }else{ 47 $workingClass = new getInstructions(); 48 } 49 50 51 foreach($result as $key => $resultValue){ 52 $sourceValue = $source[$key]; 53 54 if( empty($resultValue) ){ 55 $resultValue = ''; 56 } 57 if( empty($sourceValue) ){ 58 $sourceValue = ''; 59 } 60 if( $show ){ 61 $workingClass->calcDifference($sourceValue, $resultValue,false); 62 $workingClass->show($key); 63 64 }else{ 65 $workingClass->calcDifference($sourceValue, $resultValue); 66 $workingClass->setInstructions(); 67 68 if( isset($workingClass->instructions) ){ 69 70 $instructions[$key] = $workingClass->instructions; 71 $this->keys[] = $key; 72 $this->differences += $workingClass->differences; 73 } 74 } 75 76 } 77 78 // 3) Serialize, Compress and return 79 if( !empty($instructions) ){ 80 //echo showArray($instructions); 81 $instructions = serialize($instructions); 82 $this->instructions = $instructions; 83 if( function_exists('gzdeflate') ){ 84 $this->instructions = gzdeflate($this->instructions); 85 } 86 } 87 } 88 89 } 90 91 92 93 94 // 95 // difference_main 96 // 97 /////////////////////////////////////////////////////////////////////////////////////////////////// 98 /////////////////////////////////////////////////////////////////////////////////////////////////// 99 /////////////////////////////////////////////////////////////////////////////////////////////////// 100 // 101 // difference 102 // 103 104 105 106 107 class difference{ 108 109 //////////////////////////////////////////////////////////////////////////// 110 //// 111 //// VARIABLES 112 //// 113 var $source; 114 var $result; 115 116 var $left; 117 var $leftIndex; 118 var $maxleft; 119 120 var $right; 121 var $rightIndex; 122 var $maxRight; 123 124 var $deleteLeft; 125 var $addRight; 126 var $instructions; 127 128 var $debugText; 129 var $differences; 130 131 132 function resetVars(){ 133 $array = get_class_vars( get_class($this) ); 134 foreach($array as $key => $value){ 135 unset($this->$key); 136 } 137 //$this->differences = 0; 138 $this->leftIndex = 0; 139 $this->rightIndex = 0; 140 $this->deleteLeft = array(); 141 $this->addRight = array(); 142 } 143 144 //////////////////////////////////////////////////////////////////////////// 145 //// 146 //// This Does the Work 147 //// $left = source; $right = result; 148 //// 149 150 function calcDifference($left,$right,$doBlankLines=true){ 151 152 // 0) don't bother getting instructions if result is very small 153 // this doesn't exactly work because of later comparison... 154 // if( wbStrlen($right) < 80 ){ 155 // $this->instructions = $right; 156 // return; 157 // } 158 $this->resetVars(); 159 160 $this->source = $left; 161 $this->result = $right; 162 163 $this->left = stringToArray($left); 164 $this->right = stringToArray($right); 165 166 $this->maxLeft = count($this->left); 167 $this->maxRight = count($this->right); 168 169 /// should use a foreach(..) here... I think they're faster too 170 171 while( ($this->leftIndex < $this->maxLeft) or ($this->rightIndex < $this->maxRight) ){ 172 //echo $this->debugText; 173 //$this->debugText = '<p>'; 174 175 //////////////////////////////////////////////////////////////////////////// 176 //// 177 //// I) End of left or right 178 //// 179 // left at max 180 if( $this->leftIndex == $this->maxLeft ){ 181 //$this->debugText .= '<p>LeftIndex = max = '.$this->maxLeft; 182 while($this->rightIndex < $this->maxRight){ 183 $this->addRight(); 184 } 185 break; 186 } 187 // right at max 188 if( $this->rightIndex == $this->maxRight ){ 189 //$this->debugText .= '<p>RightIndex = max = '.$this->maxRight; 190 while($this->leftIndex < $this->maxLeft){ 191 $this->deleteLeft(); 192 } 193 break; 194 } 195 196 197 //////////////////////////////////////////////////////////////////////////// 198 //// 199 //// II) left == right 200 //// 201 202 if( $this->left[$this->leftIndex] == $this->right[$this->rightIndex] ){ 203 //$this->debugText .= '<p>Equal at left: '.$this->leftIndex.' Right: '.$this->rightIndex; 204 unset($this->left[$this->leftIndex]); 205 unset($this->right[$this->rightIndex]); 206 $this->leftIndex++; 207 $this->rightIndex++; 208 continue; 209 } 210 211 //////////////////////////////////////////////////////////////////////////// 212 //// 213 //// III) left != right 214 //// 215 216 if($doBlankLines){ 217 // 2) Add or Delete Blank Lines 218 if( empty($this->left[$this->leftIndex]) || $this->left[$this->leftIndex] == ''){ 219 //$this->debugText .= '<br><b>Empty line on left at</b> '.$this->leftIndex; 220 $this->deleteLeft(); 221 continue; 222 } 223 if( empty($this->right[$this->rightIndex]) || $this->right[$this->rightIndex] == ''){ 224 //$this->debugText .= '<br><b>Empty line on right at</b> '.$this->rightIndex; 225 $this->addRight(); 226 continue; 227 } 228 } 229 230 231 // 3) Distance to next similarity 232 $rightMatchAt = false; 233 $leftMatchAt = false; 234 $rightMatchAt = array_search($this->left[$this->leftIndex],$this->right); 235 $leftMatchAt = array_search($this->right[$this->rightIndex],$this->left); 236 //$this->debugText .= '<p>Left: '.$this->leftIndex. ' Right-Match At: '.$rightMatchAt .' Right: '.$this->rightIndex. ' Left-Match at: '.$leftMatchAt; 237 238 // 4) left never equals right with either line: 239 // - this should prevent both differences from being negative below 240 if( ($rightMatchAt === false) && ($leftMatchAt === false)){ 241 $this->addRight(); 242 $this->deleteLeft(); 243 continue; 244 } 245 if($rightMatchAt === false){ 246 $this->deleteLeft(); 247 continue; 248 } 249 if($leftMatchAt === false){ 250 $this->addRight(); 251 continue; 252 } 253 254 $numAdds = ($rightMatchAt - $this->rightIndex); 255 $numDeletes = ($leftMatchAt - $this->leftIndex);; 256 257 //$this->debugText .= '<br> Number of Adds: '.$numAdds; 258 //$this->debugText .= '<br> Number of Deletes: '.$numDeletes; 259 260 if( (($numAdds>0) && ($numAdds<=$numDeletes)) || ($numDeletes<0) ){ 261 while($this->rightIndex < $rightMatchAt){ 262 $this->addRight(); 263 } 264 }elseif($numDeletes >= 0){ 265 while($this->leftIndex < $leftMatchAt){ 266 $this->deleteLeft(); 267 } 268 } 269 270 }// 1st while 271 272 //echo $this->debugText; 273 $this->differences = $this->numberOfDifferences(); 274 return; 275 276 }//end function findDifference 277 278 function addRight(){ 279 //$this->debugText .= '<br> Add right: (leftIndex) '.$this->leftIndex; 280 $this->addRight[$this->leftIndex][$this->rightIndex] = $this->right[$this->rightIndex]; 281 unset($this->right[$this->rightIndex]); 282 $this->rightIndex++; 283 //$this->differences++; 284 } 285 function deleteLeft(){ 286 //$this->debugText .= '<br> Delete left: (leftIndex) '.$this->leftIndex; 287 $this->deleteLeft[$this->leftIndex] = $this->left[$this->leftIndex]; 288 unset($this->left[$this->leftIndex]); 289 $this->leftIndex++; 290 //$this->differences++; 291 } 292 293 294 function numberOfDifferences(){ 295 $differences = 0; 296 $temp1 = array_keys($this->deleteLeft); 297 $temp2 = array_keys($this->addRight); 298 $intersection = count( array_intersect($temp1,$temp2) ); 299 $differences = count($this->deleteLeft) + (count($this->addRight,1)-count($this->addRight) ); 300 $differences -= $intersection; 301 return $differences; 302 } 303 304 } 305 306 307 // 308 // difference 309 // 310 /////////////////////////////////////////////////////////////////////////////////////////////////// 311 /////////////////////////////////////////////////////////////////////////////////////////////////// 312 /////////////////////////////////////////////////////////////////////////////////////////////////// 313 // 314 // getInstructions 315 // translates add/delete then serializes 316 317 318 319 class getInstructions extends difference{ 320 321 function setInstructions(){ 322 323 if( empty($this->deleteLeft) && empty($this->addRight) ){ 324 return; 325 } 326 327 // 1) Make Move Instructions 328 $deleteTemp = $this->deleteLeft; 329 $addTemp = $this->addRight; 330 331 $moveTemp = NULL; 332 foreach($this->addRight as $leftIndex => $addArray){ 333 foreach($addArray as $rightIndex => $addLine){ 334 335 if( ($addLine == '') || empty($addLine)){ 336 continue; 337 } 338 339 $moveFrom = false; 340 $moveFrom = array_search($addLine,$deleteTemp); 341 if( $moveFrom !== false){ 342 $moveTemp[$moveFrom]=$leftIndex.'.'.$rightIndex; 343 unset($deleteTemp[$moveFrom]); 344 unset($addTemp[$leftIndex][$rightIndex]); 345 } 346 } 347 } 348 if( !empty($moveTemp) ){ 349 $array['m'] = $moveTemp; 350 } 351 352 353 // 2) Replace Instructions 354 $replaceTemp = array(); 355 foreach($deleteTemp as $leftIndex => $deleteLine){ 356 if( isset($addTemp[$leftIndex]) ){ 357 foreach($addTemp[$leftIndex] as $addKey => $addLine){ 358 $replaceTemp[$leftIndex][$addKey] = $addLine; 359 //$replaceTemp[$leftIndex] = $addLine; // other options for saving space? only a few bytes here and there 360 //$replaceTemp[$leftIndex.'.'.$addKey] = $addLine; // 361 } 362 unset($addTemp[$leftIndex]); 363 unset($deleteTemp[$leftIndex]); 364 } 365 } 366 if( !empty($replaceTemp) ){ 367 $array['r'] = $replaceTemp; 368 } 369 370 // 3) Compress Delete Instrucions 371 foreach($deleteTemp as $key => $deleteLine){ 372 $array['d'][$key] = ''; 373 } 374 375 // 4) Add instructions 376 if( !empty($addTemp) ){ 377 $array['a'] = $addTemp; 378 } 379 380 // 5) Check Size of instructions vs just saving value 381 if( function_exists('gzdeflate') ){ 382 $resultSize = strlen(gzdeflate($this->result)); 383 $instructionSize = strlen(gzdeflate( serialize($array) )); 384 }else{ 385 $resultSize = $this->result; 386 $instructionSize = strlen(serialize($array)); 387 } 388 389 // echo '<p><b>Sizes</b> (compressed)'; 390 // echo '<br>Result Size: '.$resultSize; 391 // echo '<br>Size of Instructions: '.$instructionSize; 392 393 if($resultSize <= $instructionSize){ 394 $this->instructions = $this->result; 395 }else{ 396 $this->instructions = $array; 397 } 398 }// end setInstructions 399 } 400 401 402 403 404 // 405 // getInstructions 406 // 407 /////////////////////////////////////////////////////////////////////////////////////////////////// 408 /////////////////////////////////////////////////////////////////////////////////////////////////// 409 /////////////////////////////////////////////////////////////////////////////////////////////////// 410 // 411 // showComparison 412 // 413 414 class showComparison extends difference{ 415 416 function show($field){ 417 global $lang; 418 // echo 'This should just be a couple of arrays!'; 419 // echo '<table><tr><td>'; 420 // echo showArray($this->deleteLeft); 421 // echo '</td><td>'; 422 // echo showArray($this->addRight); 423 // echo '</td></table>'; 424 425 $source = stringToArray($this->source); 426 //$result = stringToArray($this->result); 427 428 429 $differences = $this->numberOfDifferences(); 430 if( $differences == 0){ 431 return; 432 } 433 434 $keys1 = array_keys($this->deleteLeft); 435 $keys2 = array_keys($this->addRight); 436 $keys = array_merge($keys1,$keys2); 437 $keys = array_unique($keys); 438 asort($keys); 439 440 $showedLine = false; 441 $nextLine = false; 442 443 echo '<h3 class="heading">'.toDisplay($field).'</h3><div class="underline"></div>'; 444 echo $differences. ' '.$lang['differences']; 445 446 echo '<table cellspacing="3" cellpadding="3" border="0" width="100%">'; 447 foreach($keys as $lineNum){ 448 449 450 //Line Number 451 if( ($showedLine !== $lineNum) && ($showedLine !== ($lineNum-1) )){ 452 453 if( $nextLine ){ 454 echo $nextLine; 455 } 456 if( isset($source[($lineNum-1)]) ){ 457 echo '<tr><td colspan="4"><strong>'.$lang['line_num'].($lineNum-1).'</strong></td></tr>'; 458 echo '<tr><td></td><td class="historyNeut">'.htmlspecialchars($source[($lineNum-1)]).'</td>'; 459 echo '<td></td><td class="historyNeut">'.htmlspecialchars($source[($lineNum-1)]).' </td>'; 460 echo '</tr>'; 461 }else{ 462 echo '<tr><td colspan="4"><strong>'.$lang['line_num'].$lineNum.'</strong></td></tr>'; 463 } 464 } 465 $showedLine = $lineNum; 466 467 468 if( isset($this->deleteLeft[$lineNum]) && isset($this->addRight[$lineNum]) ){ 469 470 471 472 473 $i =0; 474 foreach($this->addRight[$lineNum] as $rightLine){ 475 echo '<tr>'; 476 if( $i == 0){ 477 478 echo '<td style="text-align:right"> <strong>-</strong> </td>'; 479 echo '<td class="historyDelete">'; 480 echo htmlspecialchars($this->deleteLeft[$lineNum]); 481 echo '</td>'; 482 483 }else{ 484 echo '<td> </td><td> </td>'; 485 } 486 487 echo '<td style="text-align:right"> <strong>+</strong> </td>'; 488 echo '<td class="historyAdd">'; 489 echo htmlspecialchars($rightLine); 490 echo '</td></tr>'; 491 } 492 $nextLineNum = $lineNum+1; 493 494 }elseif( isset($this->addRight[$lineNum]) ){ 495 496 foreach($this->addRight[$lineNum] as $rightLine){ 497 echo '<tr><td> </td><td> </td>'; 498 echo '<td style="text-align:right"> <strong>+</strong> </td>'; 499 echo '<td class="historyAdd">'; 500 echo htmlspecialchars($rightLine); 501 echo '</td></tr>'; 502 } 503 504 if( isset($source[$lineNum]) ){ 505 echo '<tr><td> </td>'; 506 echo '<td class="historyNeut">'; 507 echo htmlspecialchars($source[$lineNum]); 508 echo ' </td><td> </td>'; 509 echo '<td class="historyNeut">'; 510 echo htmlspecialchars($source[$lineNum]); 511 echo '</td></tr>'; 512 } 513 $nextLineNum = $lineNum+1; 514 515 }elseif( isset($this->deleteLeft[$lineNum]) ){ 516 517 echo '<tr>'; 518 echo '<td style="text-align:right"> <strong>-</strong> </td>'; 519 echo '<td class="historyDelete">'; 520 echo htmlspecialchars($this->deleteLeft[$lineNum]); 521 echo '</td>'; 522 echo '<td></td><td>'; 523 echo ''; 524 echo '</td></tr>'; 525 526 $nextLineNum = $lineNum+1; 527 } 528 529 if( isset($source[$nextLineNum]) ){ 530 531 $nextLine = '<tr><td> </td><td class="historyNeut">'.htmlspecialchars($source[$nextLineNum]).'</td>'; 532 $nextLine .= '<td></td><td class="historyNeut">'.htmlspecialchars($source[$nextLineNum]).' </td>'; 533 $nextLine .= '</tr>'; 534 }else{ 535 $nextLine = ''; 536 } 537 538 539 540 } 541 echo $nextLine; 542 echo '</table><br />'; 543 544 }//end function 545 546 }//end class
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |