Patcher.php
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Filesystem;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Language\Text;
/**
* A Unified Diff Format Patcher class
*
* @link http://sourceforge.net/projects/phppatcher/ This has been derived from the PhpPatcher version 0.1.1 written by Giuseppe Mazzotta
* @since 3.0.0
*/
class Patcher
{
/**
* Regular expression for searching source files
*/
const SRC_FILE = '/^---\\s+(\\S+)\s+\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}(\\.\\d+)?\\s+(\+|-)\\d{4}/A';
/**
* Regular expression for searching destination files
*/
const DST_FILE = '/^\\+\\+\\+\\s+(\\S+)\s+\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}(\\.\\d+)?\\s+(\+|-)\\d{4}/A';
/**
* Regular expression for searching hunks of differences
*/
const HUNK = '/@@ -(\\d+)(,(\\d+))?\\s+\\+(\\d+)(,(\\d+))?\\s+@@($)/A';
/**
* Regular expression for splitting lines
*/
const SPLIT = '/(\r\n)|(\r)|(\n)/';
/**
* @var array sources files
* @since 3.0.0
*/
protected $sources = array();
/**
* @var array destination files
* @since 3.0.0
*/
protected $destinations = array();
/**
* @var array removal files
* @since 3.0.0
*/
protected $removals = array();
/**
* @var array patches
* @since 3.0.0
*/
protected $patches = array();
/**
* @var array instance of this class
* @since 3.0.0
*/
protected static $instance;
/**
* Constructor
*
* The constructor is protected to force the use of FilesystemPatcher::getInstance()
*
* @since 3.0.0
*/
protected function __construct()
{
}
/**
* Method to get a patcher
*
* @return FilesystemPatcher an instance of the patcher
*
* @since 3.0.0
*/
public static function getInstance()
{
if (!isset(static::$instance))
{
static::$instance = new static;
}
return static::$instance;
}
/**
* Reset the pacher
*
* @return FilesystemPatcher This object for chaining
*
* @since 3.0.0
*/
public function reset()
{
$this->sources = array();
$this->destinations = array();
$this->removals = array();
$this->patches = array();
return $this;
}
/**
* Apply the patches
*
* @return integer The number of files patched
*
* @since 3.0.0
* @throws \RuntimeException
*/
public function apply()
{
foreach ($this->patches as $patch)
{
// Separate the input into lines
$lines = self::splitLines($patch['udiff']);
// Loop for each header
while (self::findHeader($lines, $src, $dst))
{
$done = false;
$regex = '#^([^/]*/)*#';
if ($patch['strip'] !== null)
{
$regex = '#^([^/]*/){' . (int) $patch['strip'] . '}#';
}
$src = $patch['root'] . preg_replace($regex, '', $src);
$dst = $patch['root'] . preg_replace($regex, '', $dst);
// Loop for each hunk of differences
while (self::findHunk($lines, $src_line, $src_size, $dst_line, $dst_size))
{
$done = true;
// Apply the hunk of differences
$this->applyHunk($lines, $src, $dst, $src_line, $src_size, $dst_line, $dst_size);
}
// If no modifications were found, throw an exception
if (!$done)
{
throw new \RuntimeException('Invalid Diff');
}
}
}
// Initialize the counter
$done = 0;
// Patch each destination file
foreach ($this->destinations as $file => $content)
{
$buffer = implode("\n", $content);
if (File::write($file, $buffer))
{
if (isset($this->sources[$file]))
{
$this->sources[$file] = $content;
}
$done++;
}
}
// Remove each removed file
foreach ($this->removals as $file)
{
if (File::delete($file))
{
if (isset($this->sources[$file]))
{
unset($this->sources[$file]);
}
$done++;
}
}
// Clear the destinations cache
$this->destinations = array();
// Clear the removals
$this->removals = array();
// Clear the patches
$this->patches = array();
return $done;
}
/**
* Add a unified diff file to the patcher
*
* @param string $filename Path to the unified diff file
* @param string $root The files root path
* @param string $strip The number of '/' to strip
*
* @return FilesystemPatcher $this for chaining
*
* @since 3.0.0
*/
public function addFile($filename, $root = JPATH_BASE, $strip = 0)
{
return $this->add(file_get_contents($filename), $root, $strip);
}
/**
* Add a unified diff string to the patcher
*
* @param string $udiff Unified diff input string
* @param string $root The files root path
* @param string $strip The number of '/' to strip
*
* @return FilesystemPatcher $this for chaining
*
* @since 3.0.0
*/
public function add($udiff, $root = JPATH_BASE, $strip = 0)
{
$this->patches[] = array(
'udiff' => $udiff,
'root' => isset($root) ? rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : '',
'strip' => $strip,
);
return $this;
}
/**
* Separate CR or CRLF lines
*
* @param string $data Input string
*
* @return array The lines of the inputdestination file
*
* @since 3.0.0
*/
protected static function splitLines($data)
{
return preg_split(self::SPLIT, $data);
}
/**
* Find the diff header
*
* The internal array pointer of $lines is on the next line after the finding
*
* @param array &$lines The udiff array of lines
* @param string &$src The source file
* @param string &$dst The destination file
*
* @return boolean TRUE in case of success, FALSE in case of failure
*
* @since 3.0.0
* @throws \RuntimeException
*/
protected static function findHeader(&$lines, &$src, &$dst)
{
// Get the current line
$line = current($lines);
// Search for the header
while ($line !== false && !preg_match(self::SRC_FILE, $line, $m))
{
$line = next($lines);
}
if ($line === false)
{
// No header found, return false
return false;
}
// Set the source file
$src = $m[1];
// Advance to the next line
$line = next($lines);
if ($line === false)
{
throw new \RuntimeException('Unexpected EOF');
}
// Search the destination file
if (!preg_match(self::DST_FILE, $line, $m))
{
throw new \RuntimeException('Invalid Diff file');
}
// Set the destination file
$dst = $m[1];
// Advance to the next line
if (next($lines) === false)
{
throw new \RuntimeException('Unexpected EOF');
}
return true;
}
/**
* Find the next hunk of difference
*
* The internal array pointer of $lines is on the next line after the finding
*
* @param array &$lines The udiff array of lines
* @param string &$src_line The beginning of the patch for the source file
* @param string &$src_size The size of the patch for the source file
* @param string &$dst_line The beginning of the patch for the destination file
* @param string &$dst_size The size of the patch for the destination file
*
* @return boolean TRUE in case of success, false in case of failure
*
* @since 3.0.0
* @throws \RuntimeException
*/
protected static function findHunk(&$lines, &$src_line, &$src_size, &$dst_line, &$dst_size)
{
$line = current($lines);
if (preg_match(self::HUNK, $line, $m))
{
$src_line = (int) $m[1];
$src_size = 1;
if ($m[3] !== '')
{
$src_size = (int) $m[3];
}
$dst_line = (int) $m[4];
$dst_size = 1;
if ($m[6] !== '')
{
$dst_size = (int) $m[6];
}
if (next($lines) === false)
{
throw new \RuntimeException('Unexpected EOF');
}
return true;
}
return false;
}
/**
* Apply the patch
*
* @param array &$lines The udiff array of lines
* @param string $src The source file
* @param string $dst The destination file
* @param string $src_line The beginning of the patch for the source file
* @param string $src_size The size of the patch for the source file
* @param string $dst_line The beginning of the patch for the destination file
* @param string $dst_size The size of the patch for the destination file
*
* @return void
*
* @since 3.0.0
* @throws \RuntimeException
*/
protected function applyHunk(&$lines, $src, $dst, $src_line, $src_size, $dst_line, $dst_size)
{
$src_line--;
$dst_line--;
$line = current($lines);
// Source lines (old file)
$source = array();
// New lines (new file)
$destin = array();
$src_left = $src_size;
$dst_left = $dst_size;
do
{
if (!isset($line[0]))
{
$source[] = '';
$destin[] = '';
$src_left--;
$dst_left--;
}
elseif ($line[0] == '-')
{
if ($src_left == 0)
{
throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXPECTED_REMOVE_LINE', key($lines)));
}
$source[] = substr($line, 1);
$src_left--;
}
elseif ($line[0] == '+')
{
if ($dst_left == 0)
{
throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXPECTED_ADD_LINE', key($lines)));
}
$destin[] = substr($line, 1);
$dst_left--;
}
elseif ($line != '\\ No newline at end of file')
{
$line = substr($line, 1);
$source[] = $line;
$destin[] = $line;
$src_left--;
$dst_left--;
}
if ($src_left == 0 && $dst_left == 0)
{
// Now apply the patch, finally!
if ($src_size > 0)
{
$src_lines = & $this->getSource($src);
if (!isset($src_lines))
{
throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXISING_SOURCE', $src));
}
}
if ($dst_size > 0)
{
if ($src_size > 0)
{
$dst_lines = & $this->getDestination($dst, $src);
$src_bottom = $src_line + count($source);
for ($l = $src_line;$l < $src_bottom;$l++)
{
if ($src_lines[$l] != $source[$l - $src_line])
{
throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_FAILED_VERIFY', $src, $l));
}
}
array_splice($dst_lines, $dst_line, count($source), $destin);
}
else
{
$this->destinations[$dst] = $destin;
}
}
else
{
$this->removals[] = $src;
}
next($lines);
return;
}
$line = next($lines);
}
while ($line !== false);
throw new \RuntimeException('Unexpected EOF');
}
/**
* Get the lines of a source file
*
* @param string $src The path of a file
*
* @return array The lines of the source file
*
* @since 3.0.0
*/
protected function &getSource($src)
{
if (!isset($this->sources[$src]))
{
$this->sources[$src] = null;
if (is_readable($src))
{
$this->sources[$src] = self::splitLines(file_get_contents($src));
}
}
return $this->sources[$src];
}
/**
* Get the lines of a destination file
*
* @param string $dst The path of a destination file
* @param string $src The path of a source file
*
* @return array The lines of the destination file
*
* @since 3.0.0
*/
protected function &getDestination($dst, $src)
{
if (!isset($this->destinations[$dst]))
{
$this->destinations[$dst] = $this->getSource($src);
}
return $this->destinations[$dst];
}
}