Esempio n. 1
0
 /**
  */
 function diff($from_lines, $to_lines)
 {
     array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
     array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
     /* Convert the two input arrays into strings for xdiff processing. */
     $from_string = implode("\n", $from_lines);
     $to_string = implode("\n", $to_lines);
     /* Diff the two strings and convert the result to an array. */
     $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
     $diff = explode("\n", $diff);
     /* Walk through the diff one line at a time.  We build the $edits
      * array of diff operations by reading the first character of the
      * xdiff output (which is in the "unified diff" format).
      *
      * Note that we don't have enough information to detect "changed"
      * lines using this approach, so we can't add Text_Diff_Op_changed
      * instances to the $edits array.  The result is still perfectly
      * valid, albeit a little less descriptive and efficient. */
     $edits = array();
     foreach ($diff as $line) {
         switch ($line[0]) {
             case ' ':
                 $edits[] = new Text_Diff_Op_copy(array(substr($line, 1)));
                 break;
             case '+':
                 $edits[] = new Text_Diff_Op_add(array(substr($line, 1)));
                 break;
             case '-':
                 $edits[] = new Text_Diff_Op_delete(array(substr($line, 1)));
                 break;
         }
     }
     return $edits;
 }
Esempio n. 2
0
 /**
  * @inheritDoc
  */
 public function merge($base, $remote, $local)
 {
     // Skip merging if there is nothing to do.
     if ($merged = PhpMergeBase::simpleMerge($base, $remote, $local)) {
         return $merged;
     }
     // First try the built in xdiff_string_merge3.
     $merged = xdiff_string_merge3($base, $remote, $local, $error);
     //        if ($error) {
     //            // Try it the other way around.
     //            $error = null;
     //            $merged = xdiff_string_merge3($base, $local, $remote, $error);
     //        }
     if ($error) {
         // So there might be a merge conflict.
         $baseLines = $this->base = Line::createArray(array_map(function ($l) {
             return [$l, 0];
         }, explode("\n", $base)));
         // Get patch strings and transform them to Hunks.
         $remotePatch = xdiff_string_diff($base, $remote, 0);
         $localPatch = xdiff_string_diff($base, $local, 0);
         // Note that patching $remote with $localPatch will work only in
         // some cases because xdiff patches differently than git and will
         // apply the same change twice.
         $remote_hunks = self::interpretDiff($remotePatch);
         $local_hunks = self::interpretDiff($localPatch);
         // Merge Hunks and detect conflicts the same way PhpMerge does.
         $conflicts = [];
         $merged = PhpMerge::mergeHunks($baseLines, $remote_hunks, $local_hunks, $conflicts);
         if ($conflicts) {
             throw new MergeException('A merge conflict has occured.', $conflicts, $merged);
         }
     }
     return $merged;
 }
Esempio n. 3
0
 public static function compareStringToString($str1, $str2)
 {
     $compare_result = NULL;
     // wrapping the words
     $str1 = wordwrap($str1, 1);
     $str2 = wordwrap($str2, 1);
     // Diff
     $compare_result = xdiff_string_diff($str1, $str2);
     return $compare_result;
 }
Esempio n. 4
0
 public function diffString($stringFrom, $stringTo)
 {
     // fix "no new lineat the end" problem.
     if (!ereg("\n\$", $stringFrom)) {
         $stringFrom .= "\n";
     }
     if (!ereg("\n\$", $stringTo)) {
         $stringTo .= "\n";
     }
     return xdiff_string_diff($stringFrom, $stringTo);
 }
Esempio n. 5
0
function createDiff($source, $newfile, $id, $oldsource, $oldfile, $oldid)
{
    // Try different ways of diffing, in order of preference.
    if (function_exists('xdiff_string_diff')) {
        // The PECL xdiff PHP-extension.
        $difftext = xdiff_string_diff($oldsource['sourcecode'], $source['sourcecode'], 2, TRUE);
    } elseif (!(bool) ini_get('safe_mode') || strtolower(ini_get('safe_mode')) == 'off') {
        // Only try executing diff when safe_mode is off, otherwise
        // the shell exec will fail.
        if (is_readable($oldfile) && is_readable($newfile)) {
            // A direct diff on the sources in the SUBMITDIR.
            $difftext = systemDiff($oldfile, $newfile);
        } else {
            // Try generating temporary files for executing diff.
            $oldfile = tempnam(TMPDIR, "source-old-s{$oldid}-");
            $newfile = tempnam(TMPDIR, "source-new-s{$id}-");
            if (!$oldfile || !$newfile) {
                $difftext = "DOMjudge: error generating temporary files for diff.";
            } else {
                $oldhandle = fopen($oldfile, 'w');
                $newhandle = fopen($newfile, 'w');
                if (!$oldhandle || !$newhandle) {
                    $difftext = "DOMjudge: error opening temporary files for diff.";
                } else {
                    if (fwrite($oldhandle, $oldsource['sourcecode']) === FALSE || fwrite($newhandle, $source['sourcecode']) === FALSE) {
                        $difftext = "DOMjudge: error writing temporary files for diff.";
                    } else {
                        $difftext = systemDiff($oldfile, $newfile);
                    }
                }
                if ($oldhandle) {
                    fclose($oldhandle);
                }
                if ($newhandle) {
                    fclose($newhandle);
                }
            }
            if ($oldfile) {
                unlink($oldfile);
            }
            if ($newfile) {
                unlink($newfile);
            }
        }
    } else {
        $difftext = "DOMjudge: diff functionality not available in PHP or via shell exec.";
    }
    return $difftext;
}
Esempio n. 6
0
 /**
  */
 public function diff($from_lines, $to_lines)
 {
     if (!extension_loaded('xdiff')) {
         throw new Horde_Text_Diff_Exception('The xdiff extension is required for this diff engine');
     }
     array_walk($from_lines, ['Horde_Text_Diff', 'trimNewlines']);
     array_walk($to_lines, ['Horde_Text_Diff', 'trimNewlines']);
     /* Convert the two input arrays into strings for xdiff processing. */
     $from_string = implode("\n", $from_lines);
     $to_string = implode("\n", $to_lines);
     /* Diff the two strings and convert the result to an array. */
     $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
     $diff = explode("\n", $diff);
     /* Walk through the diff one line at a time.  We build the $edits
      * array of diff operations by reading the first character of the
      * xdiff output (which is in the "unified diff" format).
      *
      * Note that we don't have enough information to detect "changed"
      * lines using this approach, so we can't add Horde_Text_Diff_Op_Changed
      * instances to the $edits array.  The result is still perfectly
      * valid, albeit a little less descriptive and efficient. */
     $edits = [];
     foreach ($diff as $line) {
         if (!strlen($line)) {
             continue;
         }
         switch ($line[0]) {
             case ' ':
                 $edits[] = new Horde_Text_Diff_Op_Copy([substr($line, 1)]);
                 break;
             case '+':
                 $edits[] = new Horde_Text_Diff_Op_Add([substr($line, 1)]);
                 break;
             case '-':
                 $edits[] = new Horde_Text_Diff_Op_Delete([substr($line, 1)]);
                 break;
         }
     }
     return $edits;
 }
     I2CE::raiseError("WARNING: Cannot write to directory {$target_dir}", E_USER_ERROR);
 }
 foreach ($translated_files as $file => $contents) {
     $target_file = $target_dir . $file;
     if (is_file($target_file) && is_readable($target_file)) {
         $existing = file_get_contents($target_file);
         if (false === $existing) {
             I2CE::raiseError("WARNING: Translation to {$locale} for {$file} in {$module} exists but is not readable. -- Skipping");
             continue;
         }
         if ($contents == $existing) {
             continue;
         } else {
             $msg = "There is an existing translation to {$locale} for {$file} in the module {$module}.  Overwrite? ";
             if (function_exists('xdiff_string_diff')) {
                 $diff = xdiff_string_diff($existing, $contents, 1);
             } else {
                 if ($first_time) {
                     $msg .= "\nFor more information, run (on ubunutu):\n\tsudo apt-get install re2c  php5-dev build-essential wget\n\tmkdir -p /tmp/src\n\tcd /tmp/src\n\twget http://www.xmailserver.org/libxdiff-0.22.tar.gz\n\ttar -xzf libxdiff-0.22.tar.gz \n\tcd libxdiff-0.22\n\t./configure\n\tmake\n\tsudo make install\n\tsudo pecl install xdiff\n\techo \"extension=xdiff.so\" | sudo tee /etc/php5/conf.d/xdiff.ini \n";
                 }
                 $first_time = false;
                 $diff = false;
             }
             if (prompt($msg, $booleans['overwrite-all'], $diff)) {
                 unlink($target_file);
             } else {
                 continue;
             }
         }
     }
     if (false === file_put_contents($target_file, $contents)) {
Esempio n. 8
0
function diff($string1, $string2)
{
    if (!extension_loaded("xdiff")) {
        $result = @dl("xdiff.so");
        // avoid the E_WARNING
        if (!$result) {
            return "Note: xdiff not available for diffing. Outputting both strings:\nString1:\n{$string1}\nString2:\n{$string2}";
        }
    }
    if (strlen($string1) > 5000000 || strlen($string2) > 5000000) {
        return "Too big to xdiff. Outputting both strings:\nString1:\n{$string1}\nString2:\n{$string2}";
    }
    return xdiff_string_diff("{$string1}\n", "{$string2}\n");
}
function tftpd_db_write_config(&$db, &$data)
{
    /* Our $db object still contains the query results from the authentication
     * query earlier. Lets retrieve the device id so we update the proper
     * configuration file
     */
    $r = $db->get_row();
    $device = $r['id'];
    $filename = $r['filename'];
    /* Retrieve old configuration data
     * Only create patch if we have a previous config
     */
    $sql = "SELECT content FROM configurations WHERE device='" . $device . "'";
    tftpd_log('25', 'sql query string: "' . $sql . '"');
    if ($db->query($sql) > 0) {
        /* Create patch */
        $r = $db->get_row($sql);
        $old_data = $r['content'];
        $patch = xdiff_string_diff($data, $old_data);
        /* Don't save an empty patch */
        if ($patch != '') {
            /* Escape the data before accessing db */
            $patch = mysql_real_escape_string($patch);
            /* Insert the patch into the database */
            $sql = "INSERT INTO patches (id, device, ts, content) VALUES ('0','{$device}',NOW(),'{$patch}')";
            tftpd_log('25', 'sql query string: "' . $sql . '"');
            if ($db->query($sql) > 0) {
                tftpd_log('1', 'db: patch table updated');
            } else {
                tftpd_log('0', 'db: patch table update failed');
            }
        }
        /* Prepare query to update the configuration */
        $data = mysql_real_escape_string($data);
        $sql = "UPDATE configurations SET content='{$data}' WHERE device='{$device}'";
    } else {
        /* Prepare query to insert the configuration */
        $data = mysql_real_escape_string($data);
        $sql = "INSERT INTO configurations (device, content) VALUES ('{$device}', '{$data}')";
    }
    tftpd_log('25', 'sql query string: "' . $sql . '"');
    $db->query($sql);
    tftpd_log('1', 'db: configurations table updated');
    /* REPLACE the timestamp */
    $sql = "REPLACE INTO updated (device, ts) " . "VALUES ('{$device}',NOW())";
    tftpd_log('25', 'sql query string: "' . $sql . '"');
    $db->query($sql);
}
// $old_article = file_get_contents('./old_article.txt');
// $new_article = $_REQUEST['article']; /* Let's say that someone pasted a new article to html form */
//
// $diff = xdiff_string_diff($old_article, $new_article, 1);
// if (is_string($diff)) {
//    echo "Differences between two articles:\n";
//    echo $diff;
// }
$version_a = 1;
$version_b = 2;
$tcase_id = 88;
$va = $tcase_mgr->get_by_id($tcase_id, null, 'ALL', 'ALL', $version_a);
$vb = $tcase_mgr->get_by_id($tcase_id, null, 'ALL', 'ALL', $version_b);
new dBug($va);
new dBug($vb);
$diff = xdiff_string_diff($va[0]['summary'], $vb[0]['summary'], 1);
echo "Differences between two articles:\n";
echo $diff;
die;
// getByPathName
// function getByPathName($pathName,$pathSeparator='::')
$pathName = 'ZATHURA::Holodeck::Apollo 10 Simulation::Unload::Full speed unload';
$fname = 'getByPathName';
echo "<pre> testcase - {$fname}(\$pathName,\$pathSeparator='::')";
echo "</pre>";
echo "<pre>            {$fname}({$pathName})";
echo "</pre>";
$result = $tcase_mgr->{$fname}($pathName);
new dBug($result);
die;
$tcase_id = 318;
Esempio n. 11
0
 /**
  * Get diff using xdiff
  *
  * @param string $fromData from file data
  * @param string $toData to file data
  * @param integer $context context lines
  * @return string diff content
  */
 private function GetXDiff($fromData, $toData, $context = 3)
 {
     return xdiff_string_diff($fromData, $toData, $context);
 }
Esempio n. 12
0
 /**
  * GetXDiff
  *
  * Get diff using xdiff
  *
  * @access private
  * @param int $context number of context lines
  * @param boolean $header true to include standard diff header
  * @param string $file override the file name
  * @return string diff content
  */
 private function GetXDiff($context = 3, $header = true, $file = null)
 {
     if (!function_exists('xdiff_string_diff')) {
         return '';
     }
     $fromData = '';
     $toData = '';
     $isBinary = false;
     $fromName = '/dev/null';
     $toName = '/dev/null';
     if (empty($this->status) || $this->status == 'M' || $this->status == 'D') {
         $fromBlob = $this->GetFromBlob();
         $isBinary = $isBinary || $fromBlob->IsBinary();
         $fromData = $fromBlob->GetData(false);
         $fromName = 'a/';
         if (!empty($file)) {
             $fromName .= $file;
         } else {
             if (!empty($this->fromFile)) {
                 $fromName .= $this->fromFile;
             } else {
                 $fromName .= $this->fromHash;
             }
         }
     }
     if (empty($this->status) || $this->status == 'M' || $this->status == 'A') {
         $toBlob = $this->GetToBlob();
         $isBinary = $isBinary || $toBlob->IsBinary();
         $toData = $toBlob->GetData(false);
         $toName = 'b/';
         if (!empty($file)) {
             $toName .= $file;
         } else {
             if (!empty($this->toFile)) {
                 $toName .= $this->toFile;
             } else {
                 $toName .= $this->toHash;
             }
         }
     }
     $output = '';
     if ($isBinary) {
         $output = sprintf(__('Binary files %1$s and %2$s differ'), $fromName, $toName) . "\n";
     } else {
         if ($header) {
             $output = '--- ' . $fromName . "\n" . '+++ ' . $toName . "\n";
         }
         $output .= xdiff_string_diff($fromData, $toData, $context);
     }
     return $output;
 }