├── modx_gen ├── CHANGELOG ├── constants.php ├── generator_config.php ├── README ├── diff ├── inline.php ├── Renderer.php ├── Diff.php └── native.php ├── modx_writer.php ├── modx_gen.php ├── functions.php ├── COPYING └── modx_diff.php /modx_gen: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | php "$(dirname $0)/modx_gen.php" "$@" 4 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | Changes since 1.0.0-b1 2 | Improved the README file. 3 | config.php is renamed to generator_config.php to avoid confusion with other config files. 4 | Some more file names added to the default ignore list. 5 | Ignore version is a parameter to the script, so it's removed from the config. 6 | Parses meta tags from other generators and puts them in the generated MODX file. 7 | Changed the generator meta tag. 8 | There are now default settings in generator_config.php for the script parameters. 9 | Contextual finds, except for in-line finds. 10 | Renamed -f --outfile to -m --modxfile. 11 | Added -r, --root = Creates a root directory containing the files missing in old. 12 | Added -f, --force = Replaces the root directory if it exists. 13 | Added exit values so calling apps knows if it was successful. 14 | Removed the third parameter to check_missing(), $args are global here. 15 | old and new are now main parameters to the app so -o and -n are not needed. old needs to be first. 16 | -o are still needed if there is a default setting that needs to be overridden. 17 | -------------------------------------------------------------------------------- /constants.php: -------------------------------------------------------------------------------- 1 | '', 54 | // 'new' for the modified files, path can be absolute or relative. 55 | 'new' => '', 56 | //'modxfile' for -m, --modxfile = path and name of file to generate. Defautls to stdout. 57 | // You need to specify path and file name if you want to use this. 58 | 'modxfile' => '', 59 | // 'root' for -r, --root = Creates a root directory containing the files missing in old. 60 | 'root' => '', 61 | 62 | // The following are just on or off switches (true or false). 63 | // 'force' for -f, --force = Replaces the root directory if it exists. 64 | 'force' => false, 65 | // 'verbose' for -v, --verbose = Tell what happens. 66 | 'verbose' => false, 67 | // 'custom' for -c, --custom = This is an install file for a addition style or language. 68 | 'custom' => false, 69 | // 'ignore_version' for -i, --ignore-version = ignore SVN version info at the top of files. 70 | 'ignore_version' => true, 71 | ); 72 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | MODX Generator will take an original phpBB folder and compare it to a copy with applied modifications. The resulting diff is then converted to the MODX format. Copy, edit and inline actions are automatically generated. 2 | 3 | MODX Generator can also be used to upgrade modifications and handles both directories and files. 4 | 5 | The resulted MODX file can be imported to the MODX Creator to generate a MODX install file. 6 | This is an automated tool, you need to carefully check through the generated edits before submitting for validation. 7 | 8 | AutoMOD RC2 has limited support for multiple in-line finds, if your modification has edits with multiple in-line finds you should mention that in the author notes. 9 | AutoMOD r208 has full support for multiple in-line finds. 10 | 11 | Do note that this tool is still in beta stage. 12 | 13 | Authors: Jari Kanerva (tumba25) 14 | 15 | INSTALLATION: 16 | To install this tool simply extract the MODX Generator package somewhere on your computer. You will need all files. 17 | 18 | The MODX Generator uses Pear Text_Diff for the initial file comparison. 19 | The Text_Diff files in the modx_generator directory are slightly modified and can't be replaced. 20 | 21 | You will need the CLI version of PHP 4.2.0 or higher. 22 | 23 | USAGE: 24 | To run the generator, open a new shell (command-line interface / command prompt). 25 | Then call the PHP executable, followed by the path to modx_gen.php. And after that come the arguments. 26 | 27 | php modx_gen.php [switches] path/to/unchanged/dir path/to/changed/dir [-r path | -m path] 28 | php modx_gen.php [switches] path/to/unchanged/file path/to/changed/file [-r path | -m path] 29 | 30 | The path to the original and unchanged files can be absolute or relative. 31 | Both old and new need to be either files or dirs. 32 | The key -o is only needed if you have specified old in generator_config.php and want to override that setting. 33 | 34 | The parameters can be given in any order as long as old is before new if you don't specify the -o and -f keys. 35 | 36 | The parameters to modx_gen.php needs to be after "php modx_gen.php". 37 | -o, --old = Original files, path can be absolute or relative. 38 | -n, --new = Modified files, path can be absolute or relative. 39 | 40 | -r, --root = Creates a root directory containing the files missing in old. 41 | You need to specify a path to where to place the root directory. 42 | -m, --modxfile = path and name of MODX file to generate. Defautls to stdout. 43 | 44 | Switches 45 | -c, --custom = This is an install file for a addition style or language (subsilver2 is a additional style). 46 | Without --custom only the prosilver style and English language will be compared. 47 | Additional languages and styles need separate install files. 48 | -f, --force = Replaces the root directory if it exists. 49 | -h, --help = print this text. 50 | -v, --verbose = tell what happens. 51 | -i, --ignore-version = ignore SVN version info at the top of files. 52 | 53 | Note that most of the parameters have a default value that can be set in generator_config.php 54 | 55 | CONFIGURATION 56 | generator_config.php contains two lists. 57 | $ignore contains file and directories to totally ignore. 58 | Files in this array are not added to the copy list if they are missing and not compared. 59 | 60 | $ignore_ext contains file extensions to not compare. 61 | Files with these extensions are added to copy and (if selected) copied to root. But they are not compared. 62 | 63 | It also contains the $defaults array. Look in generator_config.php for more informaion on this array. 64 | -------------------------------------------------------------------------------- /diff/inline.php: -------------------------------------------------------------------------------- 1 | '; 43 | 44 | /** 45 | * Suffix for inserted text. 46 | */ 47 | var $_ins_suffix = ''; 48 | 49 | /** 50 | * Prefix for deleted text. 51 | */ 52 | var $_del_prefix = ''; 53 | 54 | /** 55 | * Suffix for deleted text. 56 | */ 57 | var $_del_suffix = ''; 58 | 59 | /** 60 | * Header for each change block. 61 | */ 62 | var $_block_header = ''; 63 | 64 | /** 65 | * Header for each change block. 66 | */ 67 | var $_block_footer = ''; 68 | 69 | /** 70 | * What are we currently splitting on? Used to recurse to show word-level 71 | * changes. 72 | */ 73 | var $_split_level = 'lines'; 74 | 75 | function _blockHeader($xbeg, $xlen, $ybeg, $ylen) 76 | { 77 | return $this->_block_header; 78 | } 79 | 80 | function _startBlock($header) 81 | { 82 | return $header; 83 | } 84 | 85 | function _lines($lines, $prefix = ' ', $encode = true) 86 | { 87 | // if ($encode) { 88 | // array_walk($lines, array(&$this, '_encode')); 89 | // } 90 | 91 | if ($this->_split_level == 'words') { 92 | return implode('', $lines); 93 | } else { 94 | return implode("\n", $lines) . "\n"; 95 | } 96 | } 97 | 98 | function _added($lines) 99 | { 100 | // array_walk($lines, array(&$this, '_encode')); 101 | $lines[0] = $this->_ins_prefix . $lines[0]; 102 | $lines[count($lines) - 1] .= $this->_ins_suffix; 103 | return $this->_lines($lines, ' ', false); 104 | } 105 | 106 | function _deleted($lines, $words = false) 107 | { 108 | // array_walk($lines, array(&$this, '_encode')); 109 | $lines[0] = $this->_del_prefix . $lines[0]; 110 | $lines[count($lines) - 1] .= $this->_del_suffix; 111 | return $this->_lines($lines, ' ', false); 112 | } 113 | 114 | function _changed($orig, $final) 115 | { 116 | /* If we've already split on words, don't try to do so again - just 117 | * display. */ 118 | if ($this->_split_level == 'words') { 119 | $prefix = ''; 120 | while ($orig[0] !== false && $final[0] !== false && 121 | substr($orig[0], 0, 1) == ' ' && 122 | substr($final[0], 0, 1) == ' ') { 123 | $prefix .= substr($orig[0], 0, 1); 124 | $orig[0] = substr($orig[0], 1); 125 | $final[0] = substr($final[0], 1); 126 | } 127 | return $prefix . $this->_deleted($orig) . $this->_added($final); 128 | } 129 | 130 | $text1 = implode("\n", $orig); 131 | $text2 = implode("\n", $final); 132 | 133 | /* Non-printing newline marker. */ 134 | $nl = "\0"; 135 | 136 | /* We want to split on word boundaries, but we need to 137 | * preserve whitespace as well. Therefore we split on words, 138 | * but include all blocks of whitespace in the wordlist. */ 139 | $diff = new Text_Diff('native', 140 | array($this->_splitOnWords($text1, $nl), 141 | $this->_splitOnWords($text2, $nl))); 142 | 143 | /* Get the diff in inline format. */ 144 | $renderer = new Text_Diff_Renderer_inline 145 | (array_merge($this->getParams(), 146 | array('split_level' => 'words'))); 147 | 148 | /* Run the diff and get the output. */ 149 | return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; 150 | } 151 | 152 | function _splitOnWords($string, $newlineEscape = "\n") 153 | { 154 | // Ignore \0; otherwise the while loop will never finish. 155 | $string = str_replace("\0", '', $string); 156 | 157 | $words = array(); 158 | $length = strlen($string); 159 | $pos = 0; 160 | 161 | while ($pos < $length) { 162 | // Eat a word with any preceding whitespace. 163 | $spaces = strspn(substr($string, $pos), " \n"); 164 | $nextpos = strcspn(substr($string, $pos + $spaces), " \n"); 165 | $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos)); 166 | $pos += $spaces + $nextpos; 167 | } 168 | 169 | return $words; 170 | } 171 | 172 | function _encode(&$string) 173 | { 174 | // $string = htmlspecialchars($string); 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /modx_writer.php: -------------------------------------------------------------------------------- 1 | openMemory(); 27 | $this->setIndent(true); 28 | $this->setIndentString("\t"); 29 | 30 | // The header 31 | $this->startDocument('1.0', 'UTF-8', 'yes'); 32 | $this->writePi('xml-stylesheet', 'type="text/xsl" href="modx.prosilver.en.xsl"'); 33 | $this->writeComment('NOTICE: Please open this file in your web browser. If presented with a security warning, you may safely tell it to allow the blocked content.'); 34 | $this->writeComment('For security purposes, please check: http://www.phpbb.com/mods/ for the latest version of this MOD.\nAlthough MODs are checked before being allowed in the MODs Database there is no guarantee that there are no security problems within the MOD.\nNo support will be given for MODs not found within the MODs Database which can be found at http://www.phpbb.com/mods/'); 35 | 36 | // 37 | $this->startElement('mod'); 38 | $this->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 39 | $this->writeAttribute('xmlns', 'http://www.phpbb.com/mods/xml/' . $this->modx_version); 40 | 41 | //
42 | $this->startElement('header'); 43 | 44 | // 45 | $this->write_element('meta', '', array( 46 | 'name' => 'generator', 47 | 'content' => 'MODX file generated by MODX Generator by tumba25', 48 | ), false, false); 49 | 50 | //
51 | $this->endElement(); 52 | $this->startElement('action-group'); 53 | } 54 | 55 | /** 56 | * Writes the edits to xml 57 | * 58 | * @param $name, the tag name. 59 | * @param $text, text for the tag. 60 | * @param $attributes, attributes if the are any. 61 | */ 62 | public function write_element($name, $text, $attributes = false) 63 | { 64 | if ($text == '' && $attributes == false) 65 | { 66 | // nothing to write 67 | return; 68 | } 69 | 70 | $this->startElement($name); 71 | 72 | if ($attributes != false) 73 | { 74 | foreach ($attributes as $key => $value) 75 | { 76 | if ($value != '') 77 | { 78 | $this->writeAttribute($key, $value); 79 | } 80 | } 81 | } 82 | 83 | if ($text != '') 84 | { 85 | // We need to check for in the strings. 86 | // Accoring to DavidIQ this should work. 87 | // Can't use a regular expresion for this, there might be only ]]> in the files. 88 | $find = array( 89 | '', 91 | '<![CDATA[', 92 | ']]>'); 93 | $replace = array( 94 | '<![CDATA[', 95 | ']]>', 96 | ']]><![CDATA[]]>writeCdata($text); 101 | } 102 | $this->endElement(); 103 | } 104 | 105 | /** 106 | * Write the diff to the modx file. 107 | * 108 | * @param $file, file name. 109 | * @param $file_diff, the famous diff- 110 | */ 111 | public function generate_xml($file, $file_diff) 112 | { 113 | $last_change = 0; 114 | 115 | // On Windows the directory separator will be \, so we need to replace that. 116 | $file = str_replace('\\', '/', $file); 117 | 118 | $this->startElement('open'); 119 | $this->writeAttribute('src', $file); 120 | 121 | foreach ($file_diff as $line => $value) 122 | { 123 | if (is_array($value)) 124 | { 125 | if (isset($value['find'])) 126 | { 127 | $this->startElement('edit'); 128 | 129 | // Both inlines and normal edits needs to start with a FIND 130 | $this->write_element('find', $value['find'][0]); 131 | if (isset($value['find'][1])) 132 | { 133 | $this->write_element('find', $value['find'][1]); 134 | } 135 | } 136 | 137 | if ($value['type'] == INLINE) 138 | { 139 | // Inline edit 140 | foreach ($value['changes'] as $change) 141 | { 142 | switch($change['add-type']) 143 | { 144 | case ADD_BEFORE: 145 | $change_type = 'before-add'; 146 | break; 147 | 148 | case REPLACE: 149 | $change_type = 'replace-with'; 150 | break; 151 | 152 | default: 153 | $change_type = 'after-add'; 154 | break; 155 | } 156 | if (isset($change['inline-find'])) 157 | { 158 | $this->startElement('inline-edit'); 159 | $this->write_element('inline-find', $change['inline-find'][0]); 160 | if (isset($change['inline-find'][1])) 161 | { 162 | // In-line replaces has two finds. 163 | $this->write_element('inline-find', $change['inline-find'][1]); 164 | } 165 | } 166 | $this->write_element('inline-action', $change['add'], array('type' => $change_type)); 167 | if (isset($change['close'])) 168 | { 169 | $this->endElement(); 170 | } 171 | } 172 | } 173 | else if ($value['type'] == EDIT) 174 | { 175 | // Edit 176 | if (isset($value['add'])) 177 | { 178 | switch($value['add-type']) 179 | { 180 | case ADD_BEFORE: 181 | $change_type = 'before-add'; 182 | break; 183 | 184 | case REPLACE: 185 | $change_type = 'replace-with'; 186 | break; 187 | 188 | default: 189 | $change_type = 'after-add'; 190 | break; 191 | } 192 | $this->write_element('action', $value['add'], array('type' => $change_type)); 193 | } 194 | else if (!empty($value['del'])) 195 | { 196 | $this->write_element('action', '', array('type' => 'replace-with')); 197 | } 198 | } 199 | else 200 | { 201 | // Eh? 202 | } 203 | 204 | if (isset($value['close'])) 205 | { 206 | $this->endElement(); 207 | } 208 | } 209 | } 210 | 211 | $this->endElement(); 212 | } 213 | 214 | /** 215 | * Closes the file and either returns the modx or writes it to file 216 | */ 217 | public function modx_close($out_file = '') 218 | { 219 | $this->endElement(); 220 | $this->endElement(); 221 | 222 | $out_string = $this->outputMemory(); 223 | 224 | if ($out_file) 225 | { 226 | file_put_contents($out_file, $out_string); 227 | } 228 | else 229 | { 230 | echo $out_string . "\n"; 231 | } 232 | } 233 | 234 | } 235 | 236 | ?> -------------------------------------------------------------------------------- /modx_gen.php: -------------------------------------------------------------------------------- 1 | render($diff); 107 | unset($renderer, $diff); 108 | 109 | $parser = new modx_diff(); 110 | $file_diff = $parser->parse($file_diff); 111 | 112 | if (!empty($file_diff)) 113 | { 114 | $xml = new modx_writer(); 115 | 116 | $where_changes = true; 117 | $xml->generate_xml($file, $file_diff); 118 | } 119 | } 120 | else 121 | { 122 | // Add / to the end of the paths if it's not already there 123 | $args['old'] .= (substr($args['old'], -1) != $dir_separator) ? $dir_separator : ''; 124 | $args['new'] .= (substr($args['new'], -1) != $dir_separator) ? $dir_separator : ''; 125 | 126 | // Get the files 127 | if ($args['verbose']) 128 | { 129 | echo 'Getting files' . "\n"; 130 | } 131 | get_dir_contents($args['old'], $old_arr); 132 | get_dir_contents($args['new'], $new_arr); 133 | 134 | // Sort them alphabetically 135 | if ($args['verbose']) 136 | { 137 | echo 'Sorting files' . "\n"; 138 | } 139 | $old_arr = directory_sort($old_arr); 140 | $new_arr = directory_sort($new_arr); 141 | 142 | $xml = new modx_writer(); 143 | $parser = new modx_diff(); 144 | 145 | // Start with a check for new files 146 | if ($args['verbose']) 147 | { 148 | echo 'Checking for missing files' . "\n"; 149 | } 150 | $where_changes = check_missing($old_arr, $new_arr); 151 | 152 | foreach ($old_arr as $file) 153 | { 154 | $ext = substr(strrchr($file, '.'), 1); 155 | 156 | if (in_array($ext, $ignore_ext) || !file_exists($args['new'] . $file)) 157 | { 158 | continue; 159 | } 160 | if ($args['verbose']) 161 | { 162 | echo 'Comparing file: ' . $file; 163 | } 164 | 165 | $old_file = file($args['old'] . $file); 166 | $new_file = file($args['new'] . $file); 167 | 168 | if (!empty($args['ignore_version'])) 169 | { 170 | // Ignore the version strings. 171 | rem_ignore($old_file, $new_file); 172 | } 173 | 174 | if ($old_file != $new_file) 175 | { 176 | if (filesize($args['old'] . $file) == 0) 177 | { 178 | echo $args['old'] . $file . ' is empty, can\'t create a find in an empty file.' . "\n"; 179 | continue; 180 | } 181 | 182 | if ($args['verbose']) 183 | { 184 | echo '... Differences found.' . "\n"; 185 | } 186 | 187 | $diff = new Text_Diff('native', array($old_file, $new_file)); 188 | unset($old_file, $new_file); 189 | 190 | $renderer = new Text_Diff_Renderer_inline(); 191 | $file_diff = $renderer->render($diff); 192 | unset($renderer, $diff); 193 | 194 | $file_diff = $parser->parse($file_diff); 195 | 196 | if ($file_diff) 197 | { 198 | $where_changes = true; 199 | $xml->generate_xml($file, $file_diff); 200 | } 201 | } 202 | else if ($args['verbose']) 203 | { 204 | echo '... Identical.' . "\n"; 205 | } 206 | } 207 | } 208 | 209 | if (isset($xml) && $where_changes) 210 | { 211 | $out_file = (!empty($args['modxfile'])) ? $args['modxfile'] : ''; 212 | $xml->modx_close($out_file); 213 | } 214 | else 215 | { 216 | echo 'There where no changes' . "\n"; 217 | } 218 | // Should be removed before final release. 219 | if ($args['verbose']) 220 | { 221 | echo 'Memory peak: ' . memory_get_peak_usage(true) . "\n"; 222 | $mtime = microtime(); 223 | $mtime = explode(" ",$mtime); 224 | $mtime = $mtime[1] + $mtime[0]; 225 | $endtime = $mtime; 226 | $totaltime = ($endtime - $starttime); 227 | echo 'Execution time: ' . $totaltime . ' seconds' . "\n"; 228 | } 229 | exit(0); 230 | -------------------------------------------------------------------------------- /diff/Renderer.php: -------------------------------------------------------------------------------- 1 | $value) { 41 | $v = '_' . $param; 42 | if (isset($this->$v)) { 43 | $this->$v = $value; 44 | } 45 | } 46 | } 47 | 48 | /** 49 | * Get any renderer parameters. 50 | * 51 | * @return array All parameters of this renderer object. 52 | */ 53 | function getParams() 54 | { 55 | $params = array(); 56 | foreach (get_object_vars($this) as $k => $v) { 57 | if ($k[0] == '_') { 58 | $params[substr($k, 1)] = $v; 59 | } 60 | } 61 | 62 | return $params; 63 | } 64 | 65 | /** 66 | * Renders a diff. 67 | * 68 | * @param Text_Diff $diff A Text_Diff object. 69 | * 70 | * @return string The formatted output. 71 | */ 72 | function render($diff) 73 | { 74 | $xi = $yi = 1; 75 | $block = false; 76 | $context = array(); 77 | 78 | $nlead = $this->_leading_context_lines; 79 | $ntrail = $this->_trailing_context_lines; 80 | 81 | $output = $this->_startDiff(); 82 | 83 | $diffs = $diff->getDiff(); 84 | foreach ($diffs as $i => $edit) { 85 | /* If these are unchanged (copied) lines, and we want to keep 86 | * leading or trailing context lines, extract them from the copy 87 | * block. */ 88 | if (@is_a($edit, 'Text_Diff_Op_copy')) { 89 | /* Do we have any diff blocks yet? */ 90 | if (is_array($block)) { 91 | /* How many lines to keep as context from the copy 92 | * block. */ 93 | $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail; 94 | if (count($edit->orig) <= $keep) { 95 | /* We have less lines in the block than we want for 96 | * context => keep the whole block. */ 97 | $block[] = $edit; 98 | } else { 99 | if ($ntrail) { 100 | /* Create a new block with as many lines as we need 101 | * for the trailing context. */ 102 | $context = array_slice($edit->orig, 0, $ntrail); 103 | $block[] = new Text_Diff_Op_copy($context); 104 | } 105 | /* @todo */ 106 | $output .= $this->_block($x0, $ntrail + $xi - $x0, 107 | $y0, $ntrail + $yi - $y0, 108 | $block); 109 | $block = false; 110 | } 111 | } 112 | /* Keep the copy block as the context for the next block. */ 113 | $context = $edit->orig; 114 | } else { 115 | /* Don't we have any diff blocks yet? */ 116 | if (!is_array($block)) { 117 | /* Extract context lines from the preceding copy block. */ 118 | $context = array_slice($context, count($context) - $nlead); 119 | $x0 = $xi - count($context); 120 | $y0 = $yi - count($context); 121 | $block = array(); 122 | if ($context) { 123 | $block[] = new Text_Diff_Op_copy($context); 124 | } 125 | } 126 | $block[] = $edit; 127 | } 128 | 129 | if ($edit->orig) { 130 | $xi += count($edit->orig); 131 | } 132 | if ($edit->final) { 133 | $yi += count($edit->final); 134 | } 135 | } 136 | 137 | if (is_array($block)) { 138 | $output .= $this->_block($x0, $xi - $x0, 139 | $y0, $yi - $y0, 140 | $block); 141 | } 142 | 143 | return $output . $this->_endDiff(); 144 | } 145 | 146 | function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) 147 | { 148 | $output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen)); 149 | 150 | foreach ($edits as $edit) { 151 | switch (strtolower(get_class($edit))) { 152 | case 'text_diff_op_copy': 153 | $output .= $this->_context($edit->orig); 154 | break; 155 | 156 | case 'text_diff_op_add': 157 | $output .= $this->_added($edit->final); 158 | break; 159 | 160 | case 'text_diff_op_delete': 161 | $output .= $this->_deleted($edit->orig); 162 | break; 163 | 164 | case 'text_diff_op_change': 165 | $output .= $this->_changed($edit->orig, $edit->final); 166 | break; 167 | } 168 | } 169 | return $output . $this->_endBlock(); 170 | } 171 | 172 | function _startDiff() 173 | { 174 | return ''; 175 | } 176 | 177 | function _endDiff() 178 | { 179 | return ''; 180 | } 181 | 182 | function _blockHeader($xbeg, $xlen, $ybeg, $ylen) 183 | { 184 | if ($xlen > 1) { 185 | $xbeg .= ',' . ($xbeg + $xlen - 1); 186 | } 187 | if ($ylen > 1) { 188 | $ybeg .= ',' . ($ybeg + $ylen - 1); 189 | } 190 | 191 | // this matches the GNU Diff behaviour 192 | if ($xlen && !$ylen) { 193 | $ybeg--; 194 | } elseif (!$xlen) { 195 | $xbeg--; 196 | } 197 | 198 | return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; 199 | } 200 | 201 | function _startBlock($header) 202 | { 203 | return $header . "\n"; 204 | } 205 | 206 | function _endBlock() 207 | { 208 | return $this->_block_footer; 209 | } 210 | 211 | function _lines($lines, $prefix = ' ') 212 | { 213 | return $prefix . implode("\n$prefix", $lines) . "\n"; 214 | } 215 | 216 | function _context($lines) 217 | { 218 | return $this->_lines($lines, ' '); 219 | } 220 | 221 | function _added($lines) 222 | { 223 | return $this->_lines($lines, '> '); 224 | } 225 | 226 | function _deleted($lines) 227 | { 228 | return $this->_lines($lines, '< '); 229 | } 230 | 231 | function _changed($orig, $final) 232 | { 233 | return $this->_deleted($orig) . "---\n" . $this->_added($final); 234 | } 235 | 236 | } 237 | -------------------------------------------------------------------------------- /functions.php: -------------------------------------------------------------------------------- 1 | startElement('copy'); 189 | } 190 | 191 | if ($copy) 192 | { 193 | // Copy files to root/. 194 | if ($args['verbose']) 195 | { 196 | echo 'Copying: ' . $file . "\n"; 197 | } 198 | $dir = dirname($file); 199 | $dir_arr = explode($dir_separator, $dir); 200 | $dir = ''; 201 | foreach ($dir_arr as $value) 202 | { 203 | $dir .= (($dir == '') ? '' : $dir_separator) . $value; 204 | 205 | // Check that the target directory exists. 206 | if(!file_exists($copy . $dir)) 207 | { 208 | if (!@mkdir($copy . $dir)) 209 | { 210 | echo 'Could not create: ' . $copy . $dir . "\n"; 211 | } 212 | } 213 | } 214 | if (!@copy($args['new'] . $file, $copy . $file)) 215 | { 216 | echo 'Could not copy: ' . $file . "\n"; 217 | exit(20); 218 | } 219 | } 220 | // On Windows the directory separator will be \, so we need to handle that. 221 | $file = str_replace('\\', '/', $file); 222 | $xml->write_element('file', '', array('from' => 'root/' . $file, 'to' => $file)); 223 | } 224 | } 225 | if ($missing) 226 | { 227 | $xml->endElement(); 228 | return(true); 229 | } 230 | 231 | return(false); 232 | } 233 | 234 | /** 235 | * Recursive delete a directory. 236 | */ 237 | function delete_dir($dir, $base = '') 238 | { 239 | global $dir_separator; 240 | 241 | $dir .= (substr($dir, -1) != $dir_separator) ? $dir_separator : ''; 242 | 243 | $handle = opendir($dir); 244 | while (($file = readdir($handle)) !== false) 245 | { 246 | if ($file != '.' && $file != '..') 247 | { 248 | $path = $dir . $file; 249 | if (is_file($path)) 250 | { 251 | unlink($path); 252 | } 253 | else if (is_dir($path)) 254 | { 255 | delete_dir($path, $base . $file); 256 | } 257 | } 258 | } 259 | closedir($handle); 260 | 261 | rmdir($dir); 262 | } 263 | 264 | /** 265 | * Sorts the directory in alphabetical order, files first. 266 | */ 267 | function directory_sort($dir) 268 | { 269 | $filenames = $directories = $files = array(); 270 | foreach ($dir as $key => $value) 271 | { 272 | $files[$key] = $value; 273 | $filenames[$key] = basename($value); 274 | $directories[$key] = dirname($value); 275 | } 276 | array_multisort($directories, SORT_STRING, $filenames, SORT_STRING, $files); 277 | 278 | return($files); 279 | } 280 | 281 | /** 282 | * Reads a directory recursive and puts the result in a array. 283 | * From evil<3s diff_tools 284 | * Slightly modified. 285 | */ 286 | function get_dir_contents($dir, &$dir_arr, $base = '') 287 | { 288 | global $ignore, $args, $dir_separator; 289 | 290 | $ignore = array_merge(array('.', '..'), $ignore); 291 | 292 | $handle = opendir($dir); 293 | while (($file = readdir($handle)) !== false) 294 | { 295 | if (!in_array($file, $ignore)) 296 | { 297 | $path = $dir . $dir_separator . $file; 298 | if (!$args['custom'] && is_dir($path)) 299 | { 300 | // Only diff Prosilver and English 301 | // Other styles and languages require their own install files. 302 | if ((basename($dir) == 'styles' && $file != 'prosilver') || (basename($dir) == 'language' && $file != 'en')) 303 | { 304 | continue; 305 | } 306 | } 307 | if (is_file($path)) 308 | { 309 | $dir_arr[] = $base . $file; 310 | } 311 | else if (is_dir($path)) 312 | { 313 | get_dir_contents($path, $dir_arr, $base . $file . $dir_separator); 314 | } 315 | } 316 | } 317 | closedir($handle); 318 | } 319 | -------------------------------------------------------------------------------- /diff/Diff.php: -------------------------------------------------------------------------------- 1 | , and is used/adapted with his permission. 8 | * 9 | * $Horde: framework/Text_Diff/Diff.php,v 1.11.2.12 2009/01/06 15:23:41 jan Exp $ 10 | * 11 | * Copyright 2004 Geoffrey T. Dairiki 12 | * Copyright 2004-2009 The Horde Project (http://www.horde.org/) 13 | * 14 | * See the enclosed file COPYING for license information (LGPL). If you did 15 | * not receive this file, see http://opensource.org/licenses/lgpl-license.php. 16 | * 17 | * @package Text_Diff 18 | * @author Geoffrey T. Dairiki 19 | */ 20 | class Text_Diff { 21 | 22 | /** 23 | * Array of changes. 24 | * 25 | * @var array 26 | */ 27 | var $_edits; 28 | 29 | /** 30 | * Computes diffs between sequences of strings. 31 | * 32 | * @param string $engine Name of the diffing engine to use. 'auto' 33 | * will automatically select the best. 34 | * @param array $params Parameters to pass to the diffing engine. 35 | * Normally an array of two arrays, each 36 | * containing the lines from a file. 37 | */ 38 | function Text_Diff($engine, $params) 39 | { 40 | // Backward compatibility workaround. 41 | if (!is_string($engine)) { 42 | $params = array($engine, $params); 43 | $engine = 'auto'; 44 | } 45 | 46 | if ($engine == 'auto') { 47 | $engine = extension_loaded('xdiff') ? 'xdiff' : 'native'; 48 | } else { 49 | $engine = basename($engine); 50 | } 51 | 52 | global $script_path, $dir_separator; 53 | require_once($script_path . 'diff' . $dir_separator . $engine . '.php'); 54 | $class = 'Text_Diff_Engine_' . $engine; 55 | $diff_engine = new $class(); 56 | 57 | $this->_edits = call_user_func_array(array($diff_engine, 'diff'), $params); 58 | } 59 | 60 | /** 61 | * Returns the array of differences. 62 | */ 63 | function getDiff() 64 | { 65 | return $this->_edits; 66 | } 67 | 68 | /** 69 | * returns the number of new (added) lines in a given diff. 70 | * 71 | * @since Text_Diff 1.1.0 72 | * @since Horde 3.2 73 | * 74 | * @return integer The number of new lines 75 | */ 76 | function countAddedLines() 77 | { 78 | $count = 0; 79 | foreach ($this->_edits as $edit) { 80 | if (is_a($edit, 'Text_Diff_Op_add') || 81 | is_a($edit, 'Text_Diff_Op_change')) { 82 | $count += $edit->nfinal(); 83 | } 84 | } 85 | return $count; 86 | } 87 | 88 | /** 89 | * Returns the number of deleted (removed) lines in a given diff. 90 | * 91 | * @since Text_Diff 1.1.0 92 | * @since Horde 3.2 93 | * 94 | * @return integer The number of deleted lines 95 | */ 96 | function countDeletedLines() 97 | { 98 | $count = 0; 99 | foreach ($this->_edits as $edit) { 100 | if (is_a($edit, 'Text_Diff_Op_delete') || 101 | is_a($edit, 'Text_Diff_Op_change')) { 102 | $count += $edit->norig(); 103 | } 104 | } 105 | return $count; 106 | } 107 | 108 | /** 109 | * Computes a reversed diff. 110 | * 111 | * Example: 112 | * 113 | * $diff = new Text_Diff($lines1, $lines2); 114 | * $rev = $diff->reverse(); 115 | * 116 | * 117 | * @return Text_Diff A Diff object representing the inverse of the 118 | * original diff. Note that we purposely don't return a 119 | * reference here, since this essentially is a clone() 120 | * method. 121 | */ 122 | function reverse() 123 | { 124 | if (version_compare(zend_version(), '2', '>')) { 125 | $rev = clone($this); 126 | } else { 127 | $rev = $this; 128 | } 129 | $rev->_edits = array(); 130 | foreach ($this->_edits as $edit) { 131 | $rev->_edits[] = $edit->reverse(); 132 | } 133 | return $rev; 134 | } 135 | 136 | /** 137 | * Checks for an empty diff. 138 | * 139 | * @return boolean True if two sequences were identical. 140 | */ 141 | function isEmpty() 142 | { 143 | foreach ($this->_edits as $edit) { 144 | if (!is_a($edit, 'Text_Diff_Op_copy')) { 145 | return false; 146 | } 147 | } 148 | return true; 149 | } 150 | 151 | /** 152 | * Computes the length of the Longest Common Subsequence (LCS). 153 | * 154 | * This is mostly for diagnostic purposes. 155 | * 156 | * @return integer The length of the LCS. 157 | */ 158 | function lcs() 159 | { 160 | $lcs = 0; 161 | foreach ($this->_edits as $edit) { 162 | if (is_a($edit, 'Text_Diff_Op_copy')) { 163 | $lcs += count($edit->orig); 164 | } 165 | } 166 | return $lcs; 167 | } 168 | 169 | /** 170 | * Gets the original set of lines. 171 | * 172 | * This reconstructs the $from_lines parameter passed to the constructor. 173 | * 174 | * @return array The original sequence of strings. 175 | */ 176 | function getOriginal() 177 | { 178 | $lines = array(); 179 | foreach ($this->_edits as $edit) { 180 | if ($edit->orig) { 181 | array_splice($lines, count($lines), 0, $edit->orig); 182 | } 183 | } 184 | return $lines; 185 | } 186 | 187 | /** 188 | * Gets the final set of lines. 189 | * 190 | * This reconstructs the $to_lines parameter passed to the constructor. 191 | * 192 | * @return array The sequence of strings. 193 | */ 194 | function getFinal() 195 | { 196 | $lines = array(); 197 | foreach ($this->_edits as $edit) { 198 | if ($edit->final) { 199 | array_splice($lines, count($lines), 0, $edit->final); 200 | } 201 | } 202 | return $lines; 203 | } 204 | 205 | /** 206 | * Removes trailing newlines from a line of text. This is meant to be used 207 | * with array_walk(). 208 | * 209 | * @param string $line The line to trim. 210 | * @param integer $key The index of the line in the array. Not used. 211 | */ 212 | static function trimNewlines(&$line, $key) 213 | { 214 | $line = str_replace(array("\n", "\r"), '', $line); 215 | } 216 | 217 | /** 218 | * Determines the location of the system temporary directory. 219 | * 220 | * @static 221 | * 222 | * @access protected 223 | * 224 | * @return string A directory name which can be used for temp files. 225 | * Returns false if one could not be found. 226 | */ 227 | function _getTempDir() 228 | { 229 | $tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp', 230 | 'c:\windows\temp', 'c:\winnt\temp'); 231 | 232 | /* Try PHP's upload_tmp_dir directive. */ 233 | $tmp = ini_get('upload_tmp_dir'); 234 | 235 | /* Otherwise, try to determine the TMPDIR environment variable. */ 236 | if (!strlen($tmp)) { 237 | $tmp = getenv('TMPDIR'); 238 | } 239 | 240 | /* If we still cannot determine a value, then cycle through a list of 241 | * preset possibilities. */ 242 | while (!strlen($tmp) && count($tmp_locations)) { 243 | $tmp_check = array_shift($tmp_locations); 244 | if (@is_dir($tmp_check)) { 245 | $tmp = $tmp_check; 246 | } 247 | } 248 | 249 | /* If it is still empty, we have failed, so return false; otherwise 250 | * return the directory determined. */ 251 | return strlen($tmp) ? $tmp : false; 252 | } 253 | 254 | /** 255 | * Checks a diff for validity. 256 | * 257 | * This is here only for debugging purposes. 258 | */ 259 | function _check($from_lines, $to_lines) 260 | { 261 | if (serialize($from_lines) != serialize($this->getOriginal())) { 262 | trigger_error("Reconstructed original doesn't match", E_USER_ERROR); 263 | } 264 | if (serialize($to_lines) != serialize($this->getFinal())) { 265 | trigger_error("Reconstructed final doesn't match", E_USER_ERROR); 266 | } 267 | 268 | $rev = $this->reverse(); 269 | if (serialize($to_lines) != serialize($rev->getOriginal())) { 270 | trigger_error("Reversed original doesn't match", E_USER_ERROR); 271 | } 272 | if (serialize($from_lines) != serialize($rev->getFinal())) { 273 | trigger_error("Reversed final doesn't match", E_USER_ERROR); 274 | } 275 | 276 | $prevtype = null; 277 | foreach ($this->_edits as $edit) { 278 | if ($prevtype == get_class($edit)) { 279 | trigger_error("Edit sequence is non-optimal", E_USER_ERROR); 280 | } 281 | $prevtype = get_class($edit); 282 | } 283 | 284 | return true; 285 | } 286 | 287 | } 288 | 289 | /** 290 | * @package Text_Diff 291 | * @author Geoffrey T. Dairiki 292 | */ 293 | class Text_MappedDiff extends Text_Diff { 294 | 295 | /** 296 | * Computes a diff between sequences of strings. 297 | * 298 | * This can be used to compute things like case-insensitve diffs, or diffs 299 | * which ignore changes in white-space. 300 | * 301 | * @param array $from_lines An array of strings. 302 | * @param array $to_lines An array of strings. 303 | * @param array $mapped_from_lines This array should have the same size 304 | * number of elements as $from_lines. The 305 | * elements in $mapped_from_lines and 306 | * $mapped_to_lines are what is actually 307 | * compared when computing the diff. 308 | * @param array $mapped_to_lines This array should have the same number 309 | * of elements as $to_lines. 310 | */ 311 | function Text_MappedDiff($from_lines, $to_lines, 312 | $mapped_from_lines, $mapped_to_lines) 313 | { 314 | assert(count($from_lines) == count($mapped_from_lines)); 315 | assert(count($to_lines) == count($mapped_to_lines)); 316 | 317 | parent::Text_Diff($mapped_from_lines, $mapped_to_lines); 318 | 319 | $xi = $yi = 0; 320 | for ($i = 0; $i < count($this->_edits); $i++) { 321 | $orig = &$this->_edits[$i]->orig; 322 | if (is_array($orig)) { 323 | $orig = array_slice($from_lines, $xi, count($orig)); 324 | $xi += count($orig); 325 | } 326 | 327 | $final = &$this->_edits[$i]->final; 328 | if (is_array($final)) { 329 | $final = array_slice($to_lines, $yi, count($final)); 330 | $yi += count($final); 331 | } 332 | } 333 | } 334 | 335 | } 336 | 337 | /** 338 | * @package Text_Diff 339 | * @author Geoffrey T. Dairiki 340 | * 341 | * @access private 342 | */ 343 | class Text_Diff_Op { 344 | 345 | var $orig; 346 | var $final; 347 | 348 | function &reverse() 349 | { 350 | trigger_error('Abstract method', E_USER_ERROR); 351 | } 352 | 353 | function norig() 354 | { 355 | return $this->orig ? count($this->orig) : 0; 356 | } 357 | 358 | function nfinal() 359 | { 360 | return $this->final ? count($this->final) : 0; 361 | } 362 | 363 | } 364 | 365 | /** 366 | * @package Text_Diff 367 | * @author Geoffrey T. Dairiki 368 | * 369 | * @access private 370 | */ 371 | class Text_Diff_Op_copy extends Text_Diff_Op { 372 | 373 | function Text_Diff_Op_copy($orig, $final = false) 374 | { 375 | if (!is_array($final)) { 376 | $final = $orig; 377 | } 378 | $this->orig = $orig; 379 | $this->final = $final; 380 | } 381 | 382 | function &reverse() 383 | { 384 | $reverse = new Text_Diff_Op_copy($this->final, $this->orig); 385 | return $reverse; 386 | } 387 | 388 | } 389 | 390 | /** 391 | * @package Text_Diff 392 | * @author Geoffrey T. Dairiki 393 | * 394 | * @access private 395 | */ 396 | class Text_Diff_Op_delete extends Text_Diff_Op { 397 | 398 | function Text_Diff_Op_delete($lines) 399 | { 400 | $this->orig = $lines; 401 | $this->final = false; 402 | } 403 | 404 | function &reverse() 405 | { 406 | $reverse = new Text_Diff_Op_add($this->orig); 407 | return $reverse; 408 | } 409 | 410 | } 411 | 412 | /** 413 | * @package Text_Diff 414 | * @author Geoffrey T. Dairiki 415 | * 416 | * @access private 417 | */ 418 | class Text_Diff_Op_add extends Text_Diff_Op { 419 | 420 | function Text_Diff_Op_add($lines) 421 | { 422 | $this->final = $lines; 423 | $this->orig = false; 424 | } 425 | 426 | function &reverse() 427 | { 428 | $reverse = new Text_Diff_Op_delete($this->final); 429 | return $reverse; 430 | } 431 | 432 | } 433 | 434 | /** 435 | * @package Text_Diff 436 | * @author Geoffrey T. Dairiki 437 | * 438 | * @access private 439 | */ 440 | class Text_Diff_Op_change extends Text_Diff_Op { 441 | 442 | function Text_Diff_Op_change($orig, $final) 443 | { 444 | $this->orig = $orig; 445 | $this->final = $final; 446 | } 447 | 448 | function &reverse() 449 | { 450 | $reverse = new Text_Diff_Op_change($this->final, $this->orig); 451 | return $reverse; 452 | } 453 | 454 | } 455 | -------------------------------------------------------------------------------- /diff/native.php: -------------------------------------------------------------------------------- 1 | 2, and some optimizations) are from 18 | * Geoffrey T. Dairiki . The original PHP version of this 19 | * code was written by him, and is used/adapted with his permission. 20 | * 21 | * $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.7.2.5 2009/01/06 15:23:41 jan Exp $ 22 | * 23 | * Copyright 2004-2009 The Horde Project (http://www.horde.org/) 24 | * 25 | * See the enclosed file COPYING for license information (LGPL). If you did 26 | * not receive this file, see http://opensource.org/licenses/lgpl-license.php. 27 | * 28 | * @author Geoffrey T. Dairiki 29 | * @package Text_Diff 30 | */ 31 | class Text_Diff_Engine_native { 32 | 33 | function diff($from_lines, $to_lines) 34 | { 35 | array_walk($from_lines, array('Text_Diff', 'trimNewlines')); 36 | array_walk($to_lines, array('Text_Diff', 'trimNewlines')); 37 | 38 | $n_from = count($from_lines); 39 | $n_to = count($to_lines); 40 | 41 | $this->xchanged = $this->ychanged = array(); 42 | $this->xv = $this->yv = array(); 43 | $this->xind = $this->yind = array(); 44 | unset($this->seq); 45 | unset($this->in_seq); 46 | unset($this->lcs); 47 | 48 | // Skip leading common lines. 49 | for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) { 50 | if ($from_lines[$skip] !== $to_lines[$skip]) { 51 | break; 52 | } 53 | $this->xchanged[$skip] = $this->ychanged[$skip] = false; 54 | } 55 | 56 | // Skip trailing common lines. 57 | $xi = $n_from; $yi = $n_to; 58 | for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) { 59 | if ($from_lines[$xi] !== $to_lines[$yi]) { 60 | break; 61 | } 62 | $this->xchanged[$xi] = $this->ychanged[$yi] = false; 63 | } 64 | 65 | // Ignore lines which do not exist in both files. 66 | for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { 67 | $xhash[$from_lines[$xi]] = 1; 68 | } 69 | for ($yi = $skip; $yi < $n_to - $endskip; $yi++) { 70 | $line = $to_lines[$yi]; 71 | if (($this->ychanged[$yi] = empty($xhash[$line]))) { 72 | continue; 73 | } 74 | $yhash[$line] = 1; 75 | $this->yv[] = $line; 76 | $this->yind[] = $yi; 77 | } 78 | for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { 79 | $line = $from_lines[$xi]; 80 | if (($this->xchanged[$xi] = empty($yhash[$line]))) { 81 | continue; 82 | } 83 | $this->xv[] = $line; 84 | $this->xind[] = $xi; 85 | } 86 | 87 | // Find the LCS. 88 | $this->_compareseq(0, count($this->xv), 0, count($this->yv)); 89 | 90 | // Merge edits when possible. 91 | $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged); 92 | $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged); 93 | 94 | // Compute the edit operations. 95 | $edits = array(); 96 | $xi = $yi = 0; 97 | while ($xi < $n_from || $yi < $n_to) { 98 | assert($yi < $n_to || $this->xchanged[$xi]); 99 | assert($xi < $n_from || $this->ychanged[$yi]); 100 | 101 | // Skip matching "snake". 102 | $copy = array(); 103 | while ($xi < $n_from && $yi < $n_to 104 | && !$this->xchanged[$xi] && !$this->ychanged[$yi]) { 105 | $copy[] = $from_lines[$xi++]; 106 | ++$yi; 107 | } 108 | if ($copy) { 109 | $edits[] = new Text_Diff_Op_copy($copy); 110 | } 111 | 112 | // Find deletes & adds. 113 | $delete = array(); 114 | while ($xi < $n_from && $this->xchanged[$xi]) { 115 | $delete[] = $from_lines[$xi++]; 116 | } 117 | 118 | $add = array(); 119 | while ($yi < $n_to && $this->ychanged[$yi]) { 120 | $add[] = $to_lines[$yi++]; 121 | } 122 | 123 | if ($delete && $add) { 124 | $edits[] = new Text_Diff_Op_change($delete, $add); 125 | } elseif ($delete) { 126 | $edits[] = new Text_Diff_Op_delete($delete); 127 | } elseif ($add) { 128 | $edits[] = new Text_Diff_Op_add($add); 129 | } 130 | } 131 | 132 | return $edits; 133 | } 134 | 135 | /** 136 | * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF, 137 | * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized 138 | * segments. 139 | * 140 | * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of 141 | * NCHUNKS+1 (X, Y) indexes giving the diving points between sub 142 | * sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1), 143 | * the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) == 144 | * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM). 145 | * 146 | * This function assumes that the first lines of the specified portions of 147 | * the two files do not match, and likewise that the last lines do not 148 | * match. The caller must trim matching lines from the beginning and end 149 | * of the portions it is going to specify. 150 | */ 151 | function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) 152 | { 153 | $flip = false; 154 | 155 | if ($xlim - $xoff > $ylim - $yoff) { 156 | /* Things seems faster (I'm not sure I understand why) when the 157 | * shortest sequence is in X. */ 158 | $flip = true; 159 | list ($xoff, $xlim, $yoff, $ylim) 160 | = array($yoff, $ylim, $xoff, $xlim); 161 | } 162 | 163 | if ($flip) { 164 | for ($i = $ylim - 1; $i >= $yoff; $i--) { 165 | $ymatches[$this->xv[$i]][] = $i; 166 | } 167 | } else { 168 | for ($i = $ylim - 1; $i >= $yoff; $i--) { 169 | $ymatches[$this->yv[$i]][] = $i; 170 | } 171 | } 172 | 173 | $this->lcs = 0; 174 | $this->seq[0]= $yoff - 1; 175 | $this->in_seq = array(); 176 | $ymids[0] = array(); 177 | 178 | $numer = $xlim - $xoff + $nchunks - 1; 179 | $x = $xoff; 180 | for ($chunk = 0; $chunk < $nchunks; $chunk++) { 181 | if ($chunk > 0) { 182 | for ($i = 0; $i <= $this->lcs; $i++) { 183 | $ymids[$i][$chunk - 1] = $this->seq[$i]; 184 | } 185 | } 186 | 187 | $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks); 188 | for (; $x < $x1; $x++) { 189 | $line = $flip ? $this->yv[$x] : $this->xv[$x]; 190 | if (empty($ymatches[$line])) { 191 | continue; 192 | } 193 | $matches = $ymatches[$line]; 194 | reset($matches); 195 | while (list(, $y) = each($matches)) { 196 | if (empty($this->in_seq[$y])) { 197 | $k = $this->_lcsPos($y); 198 | assert($k > 0); 199 | $ymids[$k] = $ymids[$k - 1]; 200 | break; 201 | } 202 | } 203 | while (list(, $y) = each($matches)) { 204 | if ($y > $this->seq[$k - 1]) { 205 | assert($y <= $this->seq[$k]); 206 | /* Optimization: this is a common case: next match is 207 | * just replacing previous match. */ 208 | $this->in_seq[$this->seq[$k]] = false; 209 | $this->seq[$k] = $y; 210 | $this->in_seq[$y] = 1; 211 | } elseif (empty($this->in_seq[$y])) { 212 | $k = $this->_lcsPos($y); 213 | assert($k > 0); 214 | $ymids[$k] = $ymids[$k - 1]; 215 | } 216 | } 217 | } 218 | } 219 | 220 | $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff); 221 | $ymid = $ymids[$this->lcs]; 222 | for ($n = 0; $n < $nchunks - 1; $n++) { 223 | $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks); 224 | $y1 = $ymid[$n] + 1; 225 | $seps[] = $flip ? array($y1, $x1) : array($x1, $y1); 226 | } 227 | $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim); 228 | 229 | return array($this->lcs, $seps); 230 | } 231 | 232 | function _lcsPos($ypos) 233 | { 234 | $end = $this->lcs; 235 | if ($end == 0 || $ypos > $this->seq[$end]) { 236 | $this->seq[++$this->lcs] = $ypos; 237 | $this->in_seq[$ypos] = 1; 238 | return $this->lcs; 239 | } 240 | 241 | $beg = 1; 242 | while ($beg < $end) { 243 | $mid = (int)(($beg + $end) / 2); 244 | if ($ypos > $this->seq[$mid]) { 245 | $beg = $mid + 1; 246 | } else { 247 | $end = $mid; 248 | } 249 | } 250 | 251 | assert($ypos != $this->seq[$end]); 252 | 253 | $this->in_seq[$this->seq[$end]] = false; 254 | $this->seq[$end] = $ypos; 255 | $this->in_seq[$ypos] = 1; 256 | return $end; 257 | } 258 | 259 | /** 260 | * Finds LCS of two sequences. 261 | * 262 | * The results are recorded in the vectors $this->{x,y}changed[], by 263 | * storing a 1 in the element for each line that is an insertion or 264 | * deletion (ie. is not in the LCS). 265 | * 266 | * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1. 267 | * 268 | * Note that XLIM, YLIM are exclusive bounds. All line numbers are 269 | * origin-0 and discarded lines are not counted. 270 | */ 271 | function _compareseq ($xoff, $xlim, $yoff, $ylim) 272 | { 273 | /* Slide down the bottom initial diagonal. */ 274 | while ($xoff < $xlim && $yoff < $ylim 275 | && $this->xv[$xoff] == $this->yv[$yoff]) { 276 | ++$xoff; 277 | ++$yoff; 278 | } 279 | 280 | /* Slide up the top initial diagonal. */ 281 | while ($xlim > $xoff && $ylim > $yoff 282 | && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) { 283 | --$xlim; 284 | --$ylim; 285 | } 286 | 287 | if ($xoff == $xlim || $yoff == $ylim) { 288 | $lcs = 0; 289 | } else { 290 | /* This is ad hoc but seems to work well. $nchunks = 291 | * sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); $nchunks = 292 | * max(2,min(8,(int)$nchunks)); */ 293 | $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1; 294 | list($lcs, $seps) 295 | = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks); 296 | } 297 | 298 | if ($lcs == 0) { 299 | /* X and Y sequences have no common subsequence: mark all 300 | * changed. */ 301 | while ($yoff < $ylim) { 302 | $this->ychanged[$this->yind[$yoff++]] = 1; 303 | } 304 | while ($xoff < $xlim) { 305 | $this->xchanged[$this->xind[$xoff++]] = 1; 306 | } 307 | } else { 308 | /* Use the partitions to split this problem into subproblems. */ 309 | reset($seps); 310 | $pt1 = $seps[0]; 311 | while ($pt2 = next($seps)) { 312 | $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]); 313 | $pt1 = $pt2; 314 | } 315 | } 316 | } 317 | 318 | /** 319 | * Adjusts inserts/deletes of identical lines to join changes as much as 320 | * possible. 321 | * 322 | * We do something when a run of changed lines include a line at one end 323 | * and has an excluded, identical line at the other. We are free to 324 | * choose which identical line is included. `compareseq' usually chooses 325 | * the one at the beginning, but usually it is cleaner to consider the 326 | * following identical line to be the "change". 327 | * 328 | * This is extracted verbatim from analyze.c (GNU diffutils-2.7). 329 | */ 330 | function _shiftBoundaries($lines, &$changed, $other_changed) 331 | { 332 | $i = 0; 333 | $j = 0; 334 | 335 | assert('count($lines) == count($changed)'); 336 | $len = count($lines); 337 | $other_len = count($other_changed); 338 | 339 | while (1) { 340 | /* Scan forward to find the beginning of another run of 341 | * changes. Also keep track of the corresponding point in the 342 | * other file. 343 | * 344 | * Throughout this code, $i and $j are adjusted together so that 345 | * the first $i elements of $changed and the first $j elements of 346 | * $other_changed both contain the same number of zeros (unchanged 347 | * lines). 348 | * 349 | * Furthermore, $j is always kept so that $j == $other_len or 350 | * $other_changed[$j] == false. */ 351 | while ($j < $other_len && $other_changed[$j]) { 352 | $j++; 353 | } 354 | 355 | while ($i < $len && ! $changed[$i]) { 356 | assert('$j < $other_len && ! $other_changed[$j]'); 357 | $i++; $j++; 358 | while ($j < $other_len && $other_changed[$j]) { 359 | $j++; 360 | } 361 | } 362 | 363 | if ($i == $len) { 364 | break; 365 | } 366 | 367 | $start = $i; 368 | 369 | /* Find the end of this run of changes. */ 370 | while (++$i < $len && $changed[$i]) { 371 | continue; 372 | } 373 | 374 | do { 375 | /* Record the length of this run of changes, so that we can 376 | * later determine whether the run has grown. */ 377 | $runlength = $i - $start; 378 | 379 | /* Move the changed region back, so long as the previous 380 | * unchanged line matches the last changed one. This merges 381 | * with previous changed regions. */ 382 | while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) { 383 | $changed[--$start] = 1; 384 | $changed[--$i] = false; 385 | while ($start > 0 && $changed[$start - 1]) { 386 | $start--; 387 | } 388 | assert('$j > 0'); 389 | while ($other_changed[--$j]) { 390 | continue; 391 | } 392 | assert('$j >= 0 && !$other_changed[$j]'); 393 | } 394 | 395 | /* Set CORRESPONDING to the end of the changed run, at the 396 | * last point where it corresponds to a changed run in the 397 | * other file. CORRESPONDING == LEN means no such point has 398 | * been found. */ 399 | $corresponding = $j < $other_len ? $i : $len; 400 | 401 | /* Move the changed region forward, so long as the first 402 | * changed line matches the following unchanged one. This 403 | * merges with following changed regions. Do this second, so 404 | * that if there are no merges, the changed region is moved 405 | * forward as far as possible. */ 406 | while ($i < $len && $lines[$start] == $lines[$i]) { 407 | $changed[$start++] = false; 408 | $changed[$i++] = 1; 409 | while ($i < $len && $changed[$i]) { 410 | $i++; 411 | } 412 | 413 | assert('$j < $other_len && ! $other_changed[$j]'); 414 | $j++; 415 | if ($j < $other_len && $other_changed[$j]) { 416 | $corresponding = $i; 417 | while ($j < $other_len && $other_changed[$j]) { 418 | $j++; 419 | } 420 | } 421 | } 422 | } while ($runlength != $i - $start); 423 | 424 | /* If possible, move the fully-merged run of changes back to a 425 | * corresponding run in the other file. */ 426 | while ($corresponding < $i) { 427 | $changed[--$start] = 1; 428 | $changed[--$i] = 0; 429 | assert('$j > 0'); 430 | while ($other_changed[--$j]) { 431 | continue; 432 | } 433 | assert('$j >= 0 && !$other_changed[$j]'); 434 | } 435 | } 436 | } 437 | 438 | } 439 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /modx_diff.php: -------------------------------------------------------------------------------- 1 | render($diff)); 16 | */ 17 | class modx_diff 18 | { 19 | /** 20 | * The chars that serparate words. 21 | * These will be used for the in-line edits. 22 | * Need to figure out what chars should be counted as separators. 23 | */ 24 | private $separator = array( 25 | ' ', // Space 26 | ' ', // tab 27 | '(', 28 | ')', 29 | '[', 30 | ']', 31 | '{', 32 | '}', 33 | '->', 34 | '<', 35 | '>', 36 | '\'', 37 | '"', 38 | ',', 39 | '.', 40 | ';', 41 | '!', 42 | '\\', 43 | '/', 44 | '=', 45 | '?', 46 | '@', 47 | // '$', 48 | // '_', 49 | ); 50 | /** 51 | * Runs the diff 52 | * 53 | * @param $diff, the raw diff from Text_diff. 54 | * @param $f, string, the file to diff, will be removed before release. 55 | */ 56 | public function parse($diff, $f = '') 57 | { 58 | if (!empty($f)) 59 | { 60 | $this->file = $f; 61 | } 62 | 63 | // We need a array to parse 64 | $file_diff = explode("\n", $diff); 65 | 66 | // Send the diff for the first run 67 | $file_diff = $this->parse_diff_first_pass($file_diff); 68 | 69 | // And the second run 70 | $file_diff = $this->parse_diff_second_pass($file_diff); 71 | 72 | return($file_diff); 73 | } 74 | 75 | /** 76 | * parse_diff_first_pass 77 | * Parse the diff first run 78 | * 79 | * @param $file_diff, array, the raw diff. 80 | * @return $diff_arr, array, contains the parsed diff for the next run. 81 | */ 82 | private function parse_diff_first_pass($file_diff) 83 | { 84 | // A temporary array for the diff 85 | $diff_arr = array(); 86 | 87 | $end = count($file_diff); 88 | // The first and last thing in the array is and . 89 | // Let's remove them, they will only confuse later. 90 | if (substr($file_diff[0], 0, 13) == '') 91 | { 92 | $file_diff[0] = substr($file_diff[0], 13); 93 | } 94 | $key = $end - 1; 95 | $len = strlen($file_diff[$key]); 96 | if ($len >= 14) 97 | { 98 | if (substr($file_diff[$key], $len - 14) == '') 99 | { 100 | $file_diff[$key] = substr($file_diff[$key], 0, $len - 14); 101 | } 102 | } 103 | 104 | // Let's step trough the array and arrange it to something more handy. 105 | $cnt = 0; 106 | 107 | for ($key = 0; $key < $end; $key++) 108 | { 109 | if (strpos($file_diff[$key], '') !== false) 110 | { 111 | // This is a change with possible inline edits. Let's find out. 112 | // Start by turning it back to a string. 113 | $str = $file_diff[$key]; 114 | while (strpos($file_diff[$key], '') === false) 115 | { 116 | $str .= "\n" . $file_diff[++$key]; 117 | if ($key >= $end) 118 | { 119 | break; 120 | } 121 | } 122 | // Strip and 123 | $str = $this->strip_tag_group($str, 'modx-change'); 124 | // Get the original and changed lines. 125 | $orig = $this->rem_tag_data($str, 'modx-add'); 126 | $orig = $this->strip_tag_group($orig, 'modx-del'); 127 | $new = $this->rem_tag_data($str, 'modx-del'); 128 | $new = $this->strip_tag_group($new, 'modx-add'); 129 | 130 | // Now explode them with the new and old separated. 131 | $orig_arr = explode("\n", $orig); 132 | $new_arr = explode("\n", $new); 133 | $orig_size = sizeof($orig_arr); 134 | $new_size = sizeof($new_arr); 135 | 136 | if ($orig_size == $new_size) 137 | { 138 | // We have equal number of lines. Mostly one. 139 | for ($i = 0; $i < $orig_size; $i++) 140 | { 141 | if (trim($orig_arr[$i]) == trim($new_arr[$i])) 142 | { 143 | // We don't care about leading or trailing whitespace changes. 144 | $diff_arr[$cnt] = $orig_arr[$i]; 145 | } 146 | else if (substr(trim($orig_arr[$i]), 0, 1) == '*' && substr(trim($new_arr[$i]), 0, 1) == '*' || (substr(trim($orig_arr[$i]), 0, 2) == '//' && substr(trim($new_arr[$i]), 0, 2) == '//')) 147 | { 148 | // If the line starts with * or // we don't care about inline stuff. Just replace the whole line. 149 | // These lines are typically comments or file headers. 150 | $diff_arr[$cnt]['del'] = $orig_arr[$i]; 151 | $diff_arr[$cnt]['add'] = $new_arr[$i]; 152 | $diff_arr[$cnt]['type'] = EDIT; 153 | } 154 | else 155 | { 156 | // These are true inlines. Lets save them for the next run. 157 | $diff_arr[$cnt]['del'] = $orig_arr[$i]; 158 | $diff_arr[$cnt]['add'] = $new_arr[$i]; 159 | $diff_arr[$cnt]['type'] = INLINE; 160 | } 161 | $cnt++; 162 | } 163 | } 164 | else if ($new_size && $orig_size && $new_size > $orig_size) 165 | { 166 | 167 | // Contains both in-line edits and add-after or add-before. 168 | // Let's start with guessing which are most likely the in-lines. 169 | $inlines = array(); 170 | $prev_hit = -1; 171 | for ($i = 0; $i < $orig_size; $i++) 172 | { 173 | // The first edit can't be matched with the last if there are more to come. 174 | $preserved = $new_size - ($orig_size - $i - 1); 175 | $hit = $top_proc = 0; 176 | foreach ($new_arr as $line => $value) 177 | { 178 | // No need to check preserved lines. 179 | if ($line >= $preserved) 180 | { 181 | break; 182 | } 183 | // $precent is the difference between the lines. 184 | $percent = similar_text($orig_arr[$i], $value); 185 | 186 | // $top_proc contains the highest match for this line so far. So $percent must be higher for $hit to change. 187 | // $prev_hit contains the last match so the inline edits comes in the right order. 188 | // $preserved contains the number of lines that must be preserved for the edits to come. The first edit can't match with the last line 189 | $hit = ($percent > $top_proc && $line > $prev_hit && $line < $preserved) ? $line : $hit; 190 | // Can't fill $top_proc before the previous hit is passed. 191 | $top_proc = ($line > $prev_hit) ? (($percent > $top_proc) ? $percent : $top_proc) : 0; 192 | } 193 | 194 | // If we didn't get any hits on this one let's put it in the last possible place. Don't ask why. 195 | $hit = (!$hit && !$top_proc) ? $preserved - 1 : $hit; 196 | 197 | $inlines[$i] = $hit; 198 | $prev_hit = $hit; 199 | } 200 | unset($prev_hit, $preserved, $hit, $top_proc, $line, $value, $percent); 201 | // Got the inlines. Now put them in the right places... 202 | $j = 0; 203 | $cnt--; 204 | $in_edit = false; 205 | 206 | for ($i = 0; $i < $new_size; $i++) 207 | { 208 | if (isset($inlines[$j]) && $inlines[$j] == $i) 209 | { 210 | $cnt++; 211 | if (isset($new_arr[$i]) && isset($orig_arr[$j]) && trim($orig_arr[$j]) != trim($new_arr[$i])) 212 | { 213 | $diff_arr[$cnt]['del'] = $orig_arr[$j++]; 214 | $diff_arr[$cnt]['add'] = $new_arr[$i]; 215 | $diff_arr[$cnt]['type'] = INLINE; 216 | } 217 | else 218 | { 219 | $diff_arr[$cnt] = $orig_arr[$j++]; 220 | } 221 | $in_edit = false; 222 | } 223 | else 224 | { 225 | if (!$in_edit) 226 | { 227 | $cnt++; 228 | $diff_arr[$cnt]['type'] = EDIT; 229 | $diff_arr[$cnt]['add'] = ''; 230 | $in_edit = true; 231 | } 232 | $diff_arr[$cnt]['add'] .= (($diff_arr[$cnt]['add'] == '') ? '' : "\n") . $new_arr[$i]; 233 | } 234 | } 235 | 236 | $cnt++; 237 | } 238 | else if ($new_size && $orig_size && $new_size < $orig_size) 239 | { 240 | // Contains both in-line edits and lines to delete. 241 | // We need to guess the in-lines. 242 | $inlines = array(); 243 | $prev_hit = 0; 244 | for ($i = 0; $i < $new_size; $i++) 245 | { 246 | // $orig_arr[] will have more lines than $new_arr[] 247 | // We need to save space for all in-lines. 248 | $preserved = $orig_size - ($new_size - $i - 1); 249 | $hit = $top_proc = 0; 250 | foreach ($orig_arr as $line => $value) 251 | { 252 | // No need to check preserved lines. 253 | if ($line >= $preserved) 254 | { 255 | break; 256 | } 257 | $percent = similar_text($new_arr[$i], $value); 258 | 259 | // $top_proc contains the highest match for this line so far. So $percent must be higher for $hit to change. 260 | // $prev_hit contains the last match so the inline edits comes in the right order. 261 | // $preserved contains the number of lines that must be preserved for the edits to come. The first edit can't match with the last line 262 | $hit = ($percent > $top_proc && $line > $prev_hit && $line < $preserved) ? $line : $hit; 263 | 264 | // Can't fill $top_proc before the previous hit is passed. 265 | $top_proc = ($line > $prev_hit) ? (($percent > $top_proc) ? $percent : $top_proc) : 0; 266 | } 267 | $hit = (!$hit && !$top_proc) ? $preserved - 1 : $hit; 268 | $inlines[$i] = $hit; 269 | $prev_hit = $hit; 270 | } 271 | unset($prev_hit, $preserved, $hit, $top_proc, $line, $value, $percent); 272 | // Got the inlines. Now put them in the right places... 273 | $j = 0; 274 | $cnt--; 275 | $in_edit = false; 276 | for ($i = 0; $i < $orig_size; $i++) 277 | { 278 | if (isset($inlines[$j]) && $inlines[$j] == $i) 279 | { 280 | $cnt++; 281 | if (isset($new_arr[$j]) && isset($orig_arr[$i]) && trim($orig_arr[$i]) != trim($new_arr[$j])) 282 | { 283 | $diff_arr[$cnt]['del'] = $orig_arr[$i]; 284 | $diff_arr[$cnt]['add'] = $new_arr[$j++]; 285 | $diff_arr[$cnt]['type'] = INLINE; 286 | } 287 | else 288 | { 289 | $diff_arr[$cnt] = $orig_arr[$i]; 290 | } 291 | $in_edit = false; 292 | } 293 | else 294 | { 295 | if (!$in_edit) 296 | { 297 | $cnt++; 298 | $diff_arr[$cnt]['type'] = EDIT; 299 | $diff_arr[$cnt]['del'] = ''; 300 | $in_edit = true; 301 | } 302 | $diff_arr[$cnt]['del'] .= (($diff_arr[$cnt]['del'] == '') ? '' : "\n") . $orig_arr[$i]; 303 | } 304 | 305 | } 306 | $cnt++; 307 | } 308 | } 309 | 310 | else if (strpos($file_diff[$key], '') !== false) 311 | { 312 | // These are simple adds, just needs to be copied to the diff_arr 313 | $diff_arr[$cnt] = array(); 314 | $diff_arr[$cnt]['type'] = EDIT; 315 | $diff_arr[$cnt]['add'] = $file_diff[$key]; 316 | while (strpos($file_diff[$key], '') === false) 317 | { 318 | $diff_arr[$cnt]['add'] .= "\n" . $file_diff[++$key]; 319 | if ($key >= $end) 320 | { 321 | break; 322 | } 323 | } 324 | // Strip and 325 | $diff_arr[$cnt]['add'] = $this->strip_tag_group($diff_arr[$cnt]['add'], 'modx-add'); 326 | $cnt++; 327 | } 328 | 329 | else if (strpos($file_diff[$key], '') !== false) 330 | { 331 | // These are deletes. Lines to remove. 332 | // They usually stops in the in-line section but some might end up here. 333 | $diff_arr[$cnt] = array(); 334 | $diff_arr[$cnt]['type'] = EDIT; 335 | $diff_arr[$cnt]['del'] = $file_diff[$key]; 336 | while (strpos($file_diff[$key], '') === false) 337 | { 338 | $diff_arr[$cnt]['del'] .= "\n" . $file_diff[++$key]; 339 | if ($key >= $end) 340 | { 341 | break; 342 | } 343 | } 344 | // Strip and 345 | $diff_arr[$cnt]['del'] = $this->strip_tag_group($diff_arr[$cnt]['del'], 'modx-del'); 346 | $cnt++; 347 | } 348 | 349 | else 350 | { 351 | // Unchanged lines. Needed later for the finds. 352 | $diff_arr[$cnt++] = $file_diff[$key]; 353 | } 354 | } 355 | 356 | return($diff_arr); 357 | } 358 | 359 | /** 360 | * parse_diff_second_pass 361 | * Parse the diff a second time. 362 | * @param $file_diff, array containing the diff to parse. 363 | * @return $file_diff, array the diff ready to be written. 364 | */ 365 | private function parse_diff_second_pass($file_diff) 366 | { 367 | $last_change = -1; 368 | $find = ''; 369 | 370 | // Some of the inlines might need to be converted to normal edits. 371 | $file_diff = $this->check_inlines($file_diff); 372 | 373 | foreach ($file_diff as $num => &$row) 374 | { 375 | // Let's make sure this element has not been removed. 376 | if(!isset($row)) 377 | { 378 | continue; 379 | } 380 | // If there is any changes it's a array. Otherwise a string. 381 | if (is_array($row) && empty($row['add-type'])) 382 | { 383 | if (empty($row['add']) && empty($row['del'])) 384 | { 385 | // Some edits only wants to add or remove a newline. Let's ignore them. 386 | if (isset($row['add'])) 387 | { 388 | // Where adding a LF, just remove the element 389 | unset($file_diff[$num]); 390 | } 391 | if (isset($row['del'])) 392 | { 393 | // Someone wants to remove a newline. We might need it for a find. 394 | unset($file_diff[$num]); 395 | $file_diff[$num] = ''; 396 | } 397 | continue; 398 | } 399 | 400 | // These are either INLINE or EDIT. 401 | if ($row['type'] == INLINE) 402 | { 403 | // All inlines have both del and add set. 404 | $this->gen_find($file_diff, $num, $last_change, $row['find'], $row['del']); 405 | 406 | // $num_edits is not used now. There was a plan so I'll leave it for now it the plan returns. 407 | // $row['changes'] contains all in-line edits for this line. 408 | $num_edits = $this->gen_inline_find($row['del'], $row['add'], $row['changes']); 409 | 410 | // Need to keep track of the last change for the finds. 411 | $last_change = $num; 412 | } 413 | else if ($row['type'] == EDIT) 414 | { 415 | if (!empty($row['add']) && !empty($row['del'])) 416 | { 417 | // These should only be comments 418 | // All comments gets replaced. We don't care about inline stuff for them. 419 | // They need two finds to make sure we replace the right line. 420 | $this->gen_find($file_diff, $num, $last_change, $row['find'], $row['del']); 421 | $row['add-type'] = REPLACE; 422 | } 423 | else 424 | { 425 | // Edits have only add or del set. 426 | // If this is a del it might just be a move to the other side of a LF 427 | if (!empty($row['del'])) 428 | { 429 | // Let's check to be sure. 430 | $i = $num + 1; 431 | while (isset($file_diff[$i]) && $file_diff[$i] == '') 432 | { 433 | $i++; 434 | } 435 | if (!empty($file_diff[$i]['add']) && $file_diff[$i]['add'] == $row['del']) 436 | { 437 | // They are identical. Only a LF that's in the wrong place 438 | // We'll ignore this. Place back the old and remove the new. 439 | $old_string = $row['del']; 440 | unset($file_diff[$i], $file_diff[$num]); 441 | $file_diff[$num] = $old_string; 442 | continue; 443 | } 444 | } 445 | 446 | if (empty($row['add']) && !empty($row['del'])) 447 | { 448 | // This is a delete. We need to pass the deleted string to gen_find. 449 | $row['add-type'] = $this->gen_find($file_diff, $num, $last_change, $row['find'], $row['del']); 450 | } 451 | else 452 | { 453 | $row['add-type'] = $this->gen_find($file_diff, $num, $last_change, $row['find']); 454 | } 455 | if ($row['find'][0] == chr(0)) 456 | { 457 | unset($row['find']); 458 | } 459 | } 460 | $last_change = $num; 461 | } 462 | else 463 | { 464 | // Huh? 465 | // Should be imposible to get here. But if somebody succeeds, I want to hear about it. 466 | echo 'Tell somebody you got here' . "\n"; 467 | } 468 | } 469 | } 470 | unset($num, $row); 471 | 472 | // There might be some deleted keys so lets get them in order again. 473 | $file_diff = $this->reset_keys($file_diff); 474 | 475 | // Double adds needs to be merged to one. 476 | $file_diff = $this->merge_addafter($file_diff); 477 | 478 | $file_diff = $this->mark_finds($file_diff); 479 | 480 | return($file_diff); 481 | } 482 | 483 | /** 484 | * merge_adds 485 | * 486 | * Sometimes when rows are added with just empty lines between them they get 487 | * their own add-after with no find in the last one. 488 | * That would result in - - 489 | * This function takes care of that. 490 | */ 491 | private function merge_addafter($file_diff) 492 | { 493 | $temp_arr = array(); 494 | 495 | foreach ($file_diff as $key => &$value) 496 | { 497 | // If the next $value gets merged to this one it also is unset. 498 | if (isset($value)) 499 | { 500 | // This only affect edits. Not in-lines. 501 | if (is_array($value) && $value['type'] == EDIT && $value['add-type'] == ADD_AFTER) 502 | { 503 | // We have a edit that's add-after. 504 | // There might be some empty lines between. They cant be used as find, but we need to get past them 505 | $i = $key + 1; 506 | while (isset($file_diff[$i]) && is_string($file_diff[$i]) && $file_diff[$i] == '') 507 | { 508 | $i++; 509 | } 510 | 511 | // Is the next $value also a edit wiht add-after but no find? 512 | if (isset($file_diff[$i]) && is_array($file_diff[$i]) && $file_diff[$i]['type'] == EDIT && $file_diff[$i]['add-type'] == ADD_AFTER && !isset($file_diff[$i]['find'])) 513 | { 514 | // The next $value is a add-after with no find. 515 | // Lets merge them together with a empty line between. 516 | $value['add'] .= "\n\n" . $file_diff[$i]['add']; 517 | $temp_arr[] = $value; 518 | unset($file_diff[$i]); 519 | } 520 | else 521 | { 522 | $temp_arr[] = $value; 523 | } 524 | } 525 | else 526 | { 527 | $temp_arr[] = $value; 528 | } 529 | } 530 | } 531 | unset($key, $value); 532 | 533 | return($temp_arr); 534 | } 535 | 536 | /** 537 | * check_inlines 538 | * 539 | * Converts some in-lines to normal edits. 540 | * In-lines resulting in a commet or where both old and new are comments gets converted. 541 | * In-lines where old only contains whitespace needs also to be converted. 542 | */ 543 | private function check_inlines($file_diff) 544 | { 545 | $diff_arr = array(); 546 | $cnt = 0; 547 | foreach ($file_diff as $key => $row) 548 | { 549 | // We'll only check in-lines. 550 | if (is_array($row) && $row['type'] == INLINE) 551 | { 552 | $temp_add = trim($row['add']); 553 | $temp_del = trim($row['del']); 554 | 555 | // With comments the whole line gets replaced. No inline for them. 556 | if (substr($temp_add, 0, 1) == '*' || substr($temp_add, 0, 2) == '//') 557 | { 558 | $row['type'] = EDIT; 559 | $diff_arr[$cnt++] = $row; 560 | } 561 | 562 | // We make in-line edits where the find only contains whitespace to a edit. 563 | // There will not be anything in the line to in-line find. 564 | else if ($temp_del == '') 565 | { 566 | // We'll put the new line first 567 | $diff_arr[$cnt]['type'] = EDIT; 568 | $diff_arr[$cnt]['add'] = $row['add']; 569 | $cnt++; 570 | 571 | // The old line needs also to be there 572 | $diff_arr[$cnt++] = $row['del']; 573 | } 574 | else 575 | { 576 | $diff_arr[$cnt++] = $row; 577 | } 578 | } 579 | else 580 | { 581 | $diff_arr[$cnt++] = $row; 582 | } 583 | unset($file_diff[$key]); 584 | } 585 | 586 | return($diff_arr); 587 | } 588 | 589 | /** 590 | * Resets the keys in a array to get rid off missing keys. 591 | * 592 | * @param $arr, array to reset. 593 | */ 594 | private function reset_keys($arr) 595 | { 596 | $temp_arr = array(); 597 | 598 | $end = sizeof($arr); 599 | for ($i = 0, $j = 0; $i < $end; $i++) 600 | { 601 | if (isset($arr[$i])) 602 | { 603 | $temp_arr[$j++] = $arr[$i]; 604 | } 605 | } 606 | return($temp_arr); 607 | } 608 | 609 | /** 610 | * strip_tags 611 | * 612 | * Strips unvanted stuff from fields... 613 | * , and so on. 614 | * @param $data, string or array, what to remove tags from. 615 | * @param $tag, string, the tag to remove. 616 | * @return $data, string, with the tag removed. 617 | */ 618 | private function strip_tag_group($data, $tag) 619 | { 620 | $data = preg_replace('<<' . $tag . '>>', '', $data); 621 | $data = preg_replace('<>', '', $data); 622 | return($data); 623 | } 624 | 625 | /** 626 | * rem_tag_data 627 | * 628 | * Removes tags and their data 629 | * @param $data, string or array, what to remove tags and data from. 630 | * @param $tag, string, the tag to remove. 631 | * @return $data, string, with the tag and data removed. 632 | */ 633 | private function rem_tag_data($data, $tag) 634 | { 635 | $data = preg_replace('/<' . $tag . '\b[^>]*>(.*?)<\/' . $tag . '>/s', '', $data); 636 | return($data); 637 | } 638 | 639 | /** 640 | * gen_find 641 | * Generate the finds 642 | * @param $file_diff, array, the diff to get the finds from. 643 | * @param $num, int, the line number. 644 | * @param $last_change, int, the linenumber for the last change, can't use that for our find. 645 | * @param $find, array, the array to put the finds in. 646 | * @param $inline, string, if this is a inline edit this contains the target line. We'll add it to its own find. 647 | * @return $return, int. Is it add-after or add-before (if not in-line)? 648 | */ 649 | private function gen_find(&$file_diff, $num, $last_change, &$find, $inline = false) 650 | { 651 | $find = array(); 652 | $find[0] = ''; 653 | $rows = $return = 0; 654 | 655 | // $search_before = ($num - $last_change > 1) ? true : false; 656 | // Should this be a add-after or before. 657 | $search_before = ($num > 1) ? true : false; 658 | 659 | // If this is a inline and the line can't be mixed with any other line 660 | // between the last edit and this line, let's go with it. 661 | if ($inline && $this->is_unique($file_diff, $num, $last_change, $inline, true)) 662 | { 663 | $find[0] = $inline; 664 | // Inlines don't care about any return value 665 | return; 666 | } 667 | 668 | // With deletes and in-line edits we always need to search befor the edit. 669 | $search_before = ($inline) ? true : $search_before; 670 | 671 | if ($search_before) 672 | { 673 | // This is a add after. 674 | // Need a temporary array to check if the find is unique. 675 | $find_arr = array(); 676 | $cnt = 0; 677 | for ($i = $num - 1; $i > $last_change; $i--) 678 | { 679 | if ((isset($file_diff[$i]) && !is_string($file_diff[$i])) || !isset($file_diff[$i]) || $rows > MAX_SEARCH_ROWS - 1) 680 | { 681 | break; 682 | } 683 | $rows++; 684 | 685 | $find_arr[$cnt++] = $file_diff[$i]; 686 | 687 | // Check if we need more in the find. 688 | if (isset($file_diff[$i]) && $this->is_unique($file_diff, $num, $last_change, $find_arr)) 689 | { 690 | // If this find is uniqe, let's generate the find and get out of here. 691 | foreach ($find_arr as $line) 692 | { 693 | $find[0] = $line . (($find[0] == '') ? '' : "\n") . $find[0]; 694 | } 695 | break; 696 | } 697 | } 698 | $return = ADD_AFTER; 699 | } 700 | 701 | if ($inline) 702 | { 703 | // If it's a inline edit we add the line in its own find. 704 | // That way we can be sure that the find starts in the right line. 705 | $i = (empty($find[0])) ? 0 : 1; 706 | $find[$i] = $inline; 707 | $rows = ($i == 1) ? MAX_SEARCH_ROWS : $rows + 1; 708 | } 709 | 710 | if (!$search_before) // || ($inline && $rows < MAX_SEARCH_ROWS)) 711 | { 712 | // A add-before 713 | for ($i = $num + 1, $end = $num + 4; $i < $end; $i++) 714 | { 715 | if ((isset($file_diff[$i]) && !is_string($file_diff[$i])) || !isset($file_diff[$i]) || $rows > MAX_SEARCH_ROWS - 1) 716 | { 717 | break; 718 | } 719 | $rows++; 720 | // We don't check for unique finds in add before since they onlu occur at the beginning of files. 721 | $find[0] .= (($find[0] == '') ? '' : "\n") . $file_diff[$i]; 722 | } 723 | $return = ADD_BEFORE; 724 | } 725 | 726 | // Recheck if we got a workable find or only a empty row. 727 | // Set to 0x00 and it will get remove later. 728 | if (empty($find[0])) 729 | { 730 | $find[0] = chr(0); 731 | } 732 | 733 | return($return); 734 | } 735 | 736 | /** 737 | * is_unique 738 | * 739 | * For contextual finds. Don't make the FINDs bigger than they need to be. 740 | * Checks if the find is unique between the last change and the line to find. 741 | * @param $file_diff, the huge diff array. 742 | * @param $num, the postition for the line to find. 743 | * @param $last_change, the position for the last change. 744 | * @param $find, the string to check if it's unique. 745 | * @return bool true if the find is uniqe, otherwise false. 746 | */ 747 | private function is_unique($file_diff, $num, $last_change, $find, $inline = false) 748 | { 749 | // A inline is a string. And those are easy to check. 750 | if ($inline) 751 | { 752 | for ($i = $last_change + 1; $i < $num; $i++) 753 | { 754 | // If $file_diff[$i] is a array, something has gone terribly wrong. 755 | if (isset($file_diff[$i]) && $find == $file_diff[$i]) 756 | { 757 | return(false); 758 | } 759 | } 760 | } 761 | else 762 | { 763 | // A array needs more magic. 764 | $size = sizeof($find); 765 | 766 | $last_change = ($last_change < 0) ? 0 : $last_change + 1; 767 | // Need to trim and reverse $find. 768 | foreach ($find as &$line) 769 | { 770 | $line = trim($line); 771 | } 772 | unset($line); 773 | 774 | // We also need to remove empty lines from the beginning. 775 | $i = $size - 1; 776 | while ($i >= 0 && $find[$i] == '') 777 | { 778 | unset($find[$i--]); 779 | } 780 | 781 | $find = array_reverse($find); 782 | if (empty($find)) 783 | { 784 | // We can't have finds only containing empty lines. 785 | return(false); 786 | } 787 | 788 | // Stop the search when we have FIND lines left. Those should match anyway. 789 | for ($i = $last_change, $end = $num - $size; $i < $end; $i++) 790 | { 791 | // If the first line in $find don't match, there is no need to check the rest. 792 | if (isset($file_diff[$i])) 793 | { 794 | if ($find[0] == trim($file_diff[$i])) 795 | { 796 | if ($size == 1) 797 | { 798 | // If find is only 1 line, we can return here. 799 | return(false); 800 | } 801 | 802 | // If the find contains more than one line we need to check the rest to. 803 | $match = true; 804 | $j = $i; 805 | foreach ($find as $line) 806 | { 807 | if ($line != trim($file_diff[$j])) 808 | { 809 | $match = false; 810 | break; 811 | } 812 | $j++; 813 | } 814 | 815 | // If we have a match, let's return telling so. 816 | if ($match) 817 | { 818 | return(false); 819 | } 820 | } 821 | } 822 | } 823 | } 824 | 825 | // The find is unique and can be used. 826 | return(true); 827 | } 828 | 829 | /** 830 | * gen_inline_find 831 | * Generate inline finds 832 | * @param $del, the original string 833 | * @param $add, the string with the new stuff 834 | * @param $changes, the to put the changes in. There can be more than one in-line change per line 835 | * @return $cnt, int, the number of edits in this line. 836 | */ 837 | private function gen_inline_find($del, $add, &$changes) 838 | { 839 | // str_split messes up the non English chars. 840 | $add_arr = $this->split_string($add); 841 | $del_arr = $this->split_string($del); 842 | 843 | // Let's run a diff and see what we get. 844 | $row_diff = $this->line_diff($del_arr, $add_arr); 845 | unset($del_arr, $add_arr); 846 | 847 | $cnt = 0; 848 | 849 | // Need to remember the last find so we don't try to reuse it. 850 | $last_find = -1; 851 | 852 | // We need to keep track of the last post in the array. We can't use isset() for this. 853 | $end = sizeof($row_diff); 854 | 855 | foreach ($row_diff as $key => &$value) 856 | { 857 | if (is_array($value)) 858 | { 859 | if (empty($value['del']) && empty($value['add'])) 860 | { 861 | // Nothing to add or remove. 862 | unset($value); 863 | continue; 864 | } 865 | 866 | // Is this a replace or a add? 867 | else if (!empty($value['add']) && empty($value['del'])) 868 | { 869 | // This is a add 870 | // We want at least 6 chars for the inline find. 871 | $changes[$cnt]['inline-find'][0] = ''; 872 | if ($key <= 2) 873 | { 874 | // The change is in the beginning of the line. 875 | $i = ($key + 1 > $last_find) ? $key + 1 : $last_find + 1; 876 | while ($i < $end && !@is_array($row_diff[$i])) 877 | { 878 | // $row_diff[$i] might have been removed. 879 | $changes[$cnt]['inline-find'][0] .= (isset($row_diff[$i])) ? $row_diff[$i] : ''; 880 | $i++; 881 | } 882 | $last_find = $i - 1; 883 | $changes[$cnt]['add-type'] = ADD_BEFORE; 884 | } 885 | else 886 | { 887 | $i = $key - 1; 888 | while ($i >= 0 && !@is_array($row_diff[$i])) 889 | { 890 | // We have enough finds to do a add after. 891 | $changes[$cnt]['inline-find'][0] = (isset($row_diff[$i]) && $i > $last_find) ? $row_diff[$i] . $changes[$cnt]['inline-find'][0] : $changes[$cnt]['inline-find'][0]; 892 | $i--; 893 | } 894 | $last_find = $key - 1; 895 | // The string comes in backwards so we need to turn it right. 896 | $changes[$cnt]['add-type'] = ADD_AFTER; 897 | } 898 | $changes[$cnt]['add'] = ''; 899 | foreach ($value['add'] as $char) 900 | { 901 | // What to add before or after 902 | $changes[$cnt]['add'] .= $char; 903 | } 904 | $cnt++; 905 | } 906 | else 907 | { 908 | // This is a replace 909 | $inline_find = ''; 910 | foreach ($value['del'] as $char) 911 | { 912 | $inline_find .= $char; 913 | } 914 | $inline_add = ''; 915 | if (isset($value['add'])) 916 | { 917 | foreach ($value['add'] as $char) 918 | { 919 | $inline_add .= $char; 920 | } 921 | } 922 | 923 | // In-line replaces needs two finds. 924 | $i = $key - 1; 925 | while ($i >= 0 && !@is_array($row_diff[$i])) 926 | { 927 | // The replaces are always add after. 928 | // This find is just to make sure the replace searches for the right string to replace. 929 | $str = (isset($changes[$cnt]['inline-find'][0])) ? $changes[$cnt]['inline-find'][0] : ''; 930 | $changes[$cnt]['inline-find'][0] = (isset($row_diff[$i]) && $i > $last_find) ? $row_diff[$i] . $str : $str; 931 | $i--; 932 | } 933 | $last_find = $key - 1; 934 | 935 | // We'll ignore the first find if it only contains whitespace 936 | $i = (trim($changes[$cnt]['inline-find'][0]) == '') ? 0 : 1; 937 | $changes[$cnt]['inline-find'][$i] = $inline_find; 938 | $changes[$cnt]['add'] = $inline_add; 939 | $changes[$cnt]['add-type'] = REPLACE; 940 | $cnt++; 941 | } 942 | 943 | // Unset empty finds so mark_finds get a easier job. 944 | // Can't use empty() here because that removes strings containing only a zero. 945 | if (isset($changes[$cnt - 1]['inline-find'][0]) && $changes[$cnt - 1]['inline-find'][0] == '') 946 | { 947 | unset($changes[$cnt-1]['inline-find']); 948 | } 949 | } 950 | } 951 | unset($key, $value,$char); 952 | 953 | // Before returning, let's get sure the right in-line edits are closed. 954 | $changes = $this->mark_finds($changes, true); 955 | 956 | return($cnt); 957 | } 958 | 959 | /** 960 | * Saved this for now so we can go back to char diff if we want 961 | private function gen_inline_find($del, $add, &$changes) 962 | { 963 | // str_split messes up the non English chars. 964 | $add_arr = $this->split_string($add); 965 | $del_arr = $this->split_string($del); 966 | 967 | // Let's run a diff and see what we get. 968 | $row_diff = $this->line_diff($del_arr, $add_arr); 969 | unset($del_arr, $add_arr); 970 | 971 | $cnt = 0; 972 | 973 | // Need to remember the last find so we don't try to reuse it. 974 | $last_find = -1; 975 | 976 | // We need to keep track of the last post in the array. We can't use isset() for this. 977 | $end = sizeof($row_diff); 978 | 979 | foreach ($row_diff as $key => &$value) 980 | { 981 | if (is_array($value)) 982 | { 983 | if (empty($value['del']) && empty($value['add'])) 984 | { 985 | // Nothing to add or remove. 986 | unset($value); 987 | continue; 988 | } 989 | 990 | // Is this a replace or a add? 991 | else if (!empty($value['add']) && empty($value['del'])) 992 | { 993 | // This is a add 994 | // We want at least 6 chars for the inline find. 995 | $changes[$cnt]['inline-find'][0] = ''; 996 | if ($key <= 6) 997 | { 998 | // The change is in the beginning of the line. 999 | $i = ($key + 1 > $last_find) ? $key + 1 : $last_find + 1; 1000 | while ($i < $end && !@is_array($row_diff[$i])) 1001 | { 1002 | // $row_diff[$i] might have been removed. 1003 | $changes[$cnt]['inline-find'][0] .= (isset($row_diff[$i])) ? $row_diff[$i] : ''; 1004 | $i++; 1005 | } 1006 | $last_find = $i - 1; 1007 | $changes[$cnt]['add-type'] = ADD_BEFORE; 1008 | } 1009 | else 1010 | { 1011 | $i = $key - 1; 1012 | while ($i >= 0 && !@is_array($row_diff[$i])) 1013 | { 1014 | // We have enough finds to do a add after. 1015 | $changes[$cnt]['inline-find'][0] = (isset($row_diff[$i]) && $i > $last_find) ? $row_diff[$i] . $changes[$cnt]['inline-find'][0] : $changes[$cnt]['inline-find'][0]; 1016 | $i--; 1017 | } 1018 | $last_find = $key - 1; 1019 | // The string comes in backwards so we need to turn it right. 1020 | $changes[$cnt]['add-type'] = ADD_AFTER; 1021 | } 1022 | $changes[$cnt]['add'] = ''; 1023 | foreach ($value['add'] as $char) 1024 | { 1025 | // What to add before or after 1026 | $changes[$cnt]['add'] .= $char; 1027 | } 1028 | $cnt++; 1029 | } 1030 | else if (!empty($value['add']) && !empty($value['del'])) 1031 | { 1032 | // This is a replace 1033 | $inline_find = ''; 1034 | foreach ($value['del'] as $char) 1035 | { 1036 | $inline_find .= $char; 1037 | } 1038 | $inline_add = ''; 1039 | foreach ($value['add'] as $char) 1040 | { 1041 | $inline_add .= $char; 1042 | } 1043 | 1044 | $changes[$cnt]['inline-find'][0] = $inline_find; 1045 | $changes[$cnt]['add'] = $inline_add; 1046 | $changes[$cnt]['add-type'] = REPLACE; 1047 | $cnt++; 1048 | } 1049 | else if (empty($value['add']) && !empty($value['del'])) 1050 | { 1051 | // This is a delete 1052 | $inline_find = ''; 1053 | foreach ($value['del'] as $char) 1054 | { 1055 | $inline_find .= $char; 1056 | } 1057 | 1058 | $changes[$cnt]['inline-find'][0] = ''; 1059 | foreach ($value['del'] as $char) 1060 | { 1061 | $changes[$cnt]['inline-find'][0] .= $char; 1062 | } 1063 | $changes[$cnt]['add'] = ''; 1064 | $changes[$cnt]['add-type'] = REPLACE; 1065 | $cnt++; 1066 | } 1067 | // Unset empty finds so mark_finds get a easier job. 1068 | // Can't use empty() here because that removes strings containing only a zero. 1069 | if (isset($changes[$cnt - 1]['inline-find'][0]) && $changes[$cnt - 1]['inline-find'][0] == '') 1070 | { 1071 | unset($changes[$cnt-1]['inline-find'][0]); 1072 | } 1073 | } 1074 | } 1075 | unset($key, $value,$char); 1076 | 1077 | // Before returning, let's get sure the right in-line edits are closed. 1078 | $changes = $this->mark_finds($changes, true); 1079 | 1080 | return($cnt); 1081 | } 1082 | **/ 1083 | 1084 | /** 1085 | * split_string 1086 | * splits a string to a array. str_split messes up multibyte chars. 1087 | * @param $str, string to split 1088 | * @return $arr, the array 1089 | */ 1090 | private function split_string($str) 1091 | { 1092 | $arr = array(); 1093 | 1094 | $in_word = false; 1095 | $cnt = -1; 1096 | 1097 | for ($i = 0, $end = strlen($str); $i < $end; $i++) 1098 | { 1099 | $char = substr($str, $i, 1); 1100 | if (in_array($char, $this->separator)) 1101 | { 1102 | // This is a separator char. 1103 | $cnt++; 1104 | $in_word = false; 1105 | $arr[$cnt] = $char; 1106 | } 1107 | else 1108 | { 1109 | // Here we build words. 1110 | if ($in_word) 1111 | { 1112 | $arr[$cnt] .= $char; 1113 | } 1114 | else 1115 | { 1116 | $cnt++; 1117 | $in_word = true; 1118 | $arr[$cnt] = $char; 1119 | } 1120 | } 1121 | } 1122 | 1123 | /** 1124 | * Saved for the char diff. 1125 | for ($i = 0, $end = strlen($str); $i < $end; $i++) 1126 | { 1127 | $arr[] = substr($str, $i, 1); 1128 | } 1129 | **/ 1130 | 1131 | return($arr); 1132 | } 1133 | 1134 | /** 1135 | * Generate the inline diffs. 1136 | * From http://compsci.ca/v3/viewtopic.php?p=142539 slightly modified. 1137 | * @param $old, the original array 1138 | * @param $new, the modified array 1139 | * @return array with the diff 1140 | */ 1141 | function line_diff($old, $new) 1142 | { 1143 | $maxlen = 0; 1144 | foreach ($old as $oindex => $ovalue) 1145 | { 1146 | $nkeys = array_keys($new, $ovalue); 1147 | foreach ($nkeys as $nindex) 1148 | { 1149 | $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1; 1150 | if ($matrix[$oindex][$nindex] > $maxlen) 1151 | { 1152 | $maxlen = $matrix[$oindex][$nindex]; 1153 | $omax = $oindex + 1 - $maxlen; 1154 | $nmax = $nindex + 1 - $maxlen; 1155 | } 1156 | } 1157 | } 1158 | if ($maxlen == 0) 1159 | { 1160 | return array(array('del'=>$old, 'add'=>$new)); 1161 | } 1162 | 1163 | return array_merge($this->line_diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)), 1164 | array_slice($new, $nmax, $maxlen), 1165 | $this->line_diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))); 1166 | } 1167 | 1168 | /** 1169 | * mark_finds 1170 | * Check if the next edit has a find or not. 1171 | * This is needed to know if the edit tag should be closed or not. 1172 | * Steps backwards trough the array and sets 'close' if the next change has a find in it. 1173 | * If not, the edit tag should not be closed. 1174 | * 1175 | * @param $diff_arr, The well known diff 1176 | * @return $diff_arr. The same array with the 1177 | */ 1178 | private function mark_finds($diff_arr, $inline = false) 1179 | { 1180 | // The last change always needs ot have close on. 1181 | $close = true; 1182 | // Step backwards trough the array 1183 | $find = ($inline) ? 'inline-find' : 'find'; 1184 | for ($i = sizeof($diff_arr) - 1; $i > -1; $i--) 1185 | { 1186 | if (is_array($diff_arr[$i])) 1187 | { 1188 | if ($close) 1189 | { 1190 | $diff_arr[$i]['close'] = true; 1191 | } 1192 | 1193 | // Now to check if this edit has any finds or not. 1194 | if (isset($diff_arr[$i][$find])) 1195 | { 1196 | // This element has a find so the previous change should close its edit. 1197 | $close = true; 1198 | } 1199 | else 1200 | { 1201 | // This element has not a find so the previous change should leave the edit open. 1202 | $close = false; 1203 | } 1204 | } 1205 | } 1206 | 1207 | return($diff_arr); 1208 | } 1209 | 1210 | } 1211 | ?> --------------------------------------------------------------------------------