Menu

[r9]: / trunk / BSVN / parser.php  Maximize  Restore  History

Download this file

539 lines (489 with data), 13.7 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
534
535
536
537
538
<?php
/**
* BSVN Parsers
* @copyright (c) 2010 Kamil Kamiński
* @license GPL v3 (http://gplv3.fsf.org/)
* @author Kamil Kamiński
* @package bsvn
*/
/**
* Parser for svn info output
* @package bsvn
*/
class BSVN_InfoParser {
public $url;
public $root;
public $uuid;
public $entry;
/**
* Creates new parser instance for provided XML
* @param string $xmlstring
*/
public function __construct($xmlstring) {
$dom = new DomDocument();
$dom->loadXml($xmlstring);
$xpath = new DomXPath($dom);
$this->uuid = self::getstring( $xpath->evaluate('//uuid') );
$this->root = self::getstring( $xpath->evaluate('//root') );
$this->url = self::getstring( $xpath->evaluate('//url') );
$this->entry = new BSVN_Entry( $xpath->evaluate('/info/entry')->item(0));
}
private static function getstring(DOMNodeList $nodes) {
foreach($nodes as $n) {
//var_dump($n);
//printf("type: %s\n", $n->nodeType);
//printf("value: %s\n", $n->nodeValue);
if ($n->nodeType == XML_ELEMENT_NODE)
return trim($n->nodeValue);
}
}
}
/**
* XML "entry" node
*/
class BSVN_Entry {
public $kind;
public $name;
public $path;
public $revision;
/**
* @var BSVN_Commit
*/
public $commit;
function __construct(DOMNode $item = null) {
if ($item !== null)
$this->parse($item);
}
/**
* Parses provided DOMNode
* @param DOMNode $item
*/
public function parse(DOMNode $item) {
if ($item->hasAttributes()) {
$this->kind = $item->getAttribute('kind');
$this->path = $item->getAttribute('path');
$this->revision = $item->getAttribute('revision');
}
foreach ($item->childNodes as $node) {
if ($node->nodeType != XML_ELEMENT_NODE)
continue;
switch ($node->nodeName) {
case 'commit': $this->commit = new BSVN_Commit($node); continue;
case 'name': $this->name = $node->nodeValue; continue;
default: continue;//bsvn_verbose(sprintf("Unknown node: %s (%s)\n", $node->nodeName, trim($node->nodeValue)));
}
}
}
}
/**
* XML "commit" node
*/
class BSVN_Commit {
public $revision;
public $author;
public $date;
function __construct(DOMNode $item=null) {
if ($item !== null) {
$this->parse($item);
}
}
/**
* Parses provided DOMNode
* @param DOMNode $item
*/
public function parse(DOMNode $item) {
if ($item->hasAttributes())
$this->revision = $item->getAttribute('revision');
foreach($item->childNodes as $node) {
if ($node->nodeType != XML_ELEMENT_NODE)
continue;
switch ($node->nodeName) {
case 'author': $this->author = $node->nodeValue; continue;
case 'date': $this->date = new DateTime($node->nodeValue); continue;
default: bsvn_verbose(sprintf("Unknown node: %s (%s)\n", $node->nodeName, trim($node->nodeValue)));
}
}
}
}
/**
* Parser for svn list output
*/
class BSVN_ListParser {
public $path;
/**
* Listed entries
* @var BSVN_Entry array
*/
public $entries = null;
function __construct($xmlstring='') {
if ($xmlstring != '') {
$dom = new DomDocument();
$dom->loadXml($xmlstring);
$xpath = new DomXPath($dom);
$this->entries = array();
$this->parse($xpath->evaluate( '/lists/list')->item(0));
}
}
/**
* Parses provided DOMNode
* @param DOMNode $item
*/
public function parse(DOMNode $item) {
if ($item->hasAttributes())
$this->path = $item->getAttribute('path');
foreach ($item->childNodes as $node) {
if ($node->nodeType != XML_ELEMENT_NODE)
continue;
if ($node->nodeName == 'entry') {
$e = new BSVN_Entry($node);
$this->entries[$e->name] = $e;
}
}
}
}
/**
* Parser for svn log output
*/
class BSVN_LogParser {
function __construct($xmlstream, $isFile=false) {
if ($xmlstream != '') {
BSVN_Database::get()->beginTransaction();
try {
$dom = new DomDocument();
if ($isFile)
$load = $dom->load($xmlstream);
else $load = $dom->loadXml($xmlstream);
if (!$load)
throw new Exception('Cannot load XML!');
unset($load);
foreach ($dom->childNodes as $masternode) {
if ($masternode->nodeType != XML_ELEMENT_NODE)
continue;
if ($masternode->nodeName == 'log') {
foreach ($masternode->childNodes as $node) {
if ($node->nodeType != XML_ELEMENT_NODE)
continue;
if ($node->nodeName == 'logentry') {
$logentry = new BSVN_LogEntry($node);
$logentry->save();
$sql = BSVN_Database::prepare('UPDATE authors SET active=?,commits=commits+1, a=a+?, m=m+?, d=d+?, r=r+? WHERE id=?');
$sql->bindValue(1, $logentry->date->format('U'), PDO::PARAM_STR);
$sql->bindValue(2, $logentry->a, PDO::PARAM_INT);
$sql->bindValue(3, $logentry->m, PDO::PARAM_INT);
$sql->bindValue(4, $logentry->d, PDO::PARAM_INT);
$sql->bindValue(5, $logentry->r, PDO::PARAM_INT);
$sql->bindValue(6, $logentry->author, PDO::PARAM_INT);
$sql->execute();
}
}
}
}
} catch (Exception $ex) {
@file_put_contents('php://stderr', $ex->getMessage());
if (BSVN_Commandline::hasSwitch('verbose'))
@file_put_contents('php://stderr', $ex->getTraceAsString ());
}
BSVN_Database::get()->commit();
}
}
public function __destruct() {
$this->repo = null;
}
}
/**
* Single log entry instannce
*/
class BSVN_LogEntry {
/**
* Revision number
* @var int
*/
public $revision;
/**
* Revision author's id
* @var id
*/
public $author;
/**
* Commit date
* @var DateTime
*/
public $date=null;
/**
* Revision message
* @var string
*/
public $msg;
/**
* Files added in this commit
* @var int
*/
public $a;
/**
* Files modified in this commit
* @var int
*/
public $m;
/**
* Fies deleted in this commit
* @var int
*/
public $d;
/**
*
* @var int
*/
public $r;
/**
*
* @var int
*/
public $branch = null;
function __construct(DOMNode $node=null) {
if ($node !== null) {
$this->parse($node);
}
else {
if ($this->date !== null) // this->date mogło już być DateTime, ze względu na wywołanie parse
$this->date = new DateTime('@'.$this->date);
}
}
/**
* Parses provided node
* @param DOMNode $node
*/
public function parse(DOMNode &$node) {
$this->m = 0;
$this->d = 0;
$this->r = 0;
$this->a = 0;
if ($node->hasAttributes())
$this->revision = $node->getAttribute('revision');
foreach ($node->childNodes as $cn) {
if ($cn->nodeType != XML_ELEMENT_NODE)
continue;
switch ($cn->nodeName) {
case 'author':
$this->author = BSVN_Repository::get()->requestAuthorId(trim($cn->nodeValue));
continue;
case 'date': $this->date = new DateTime($cn->nodeValue); $this->date->setTimezone( BSVN_Repository::get()->getTimezone() ); continue;
case 'msg': $this->msg = trim($cn->nodeValue); continue;
case 'paths':
foreach($cn->childNodes as $pathnode) {
if ($pathnode->nodeType != XML_ELEMENT_NODE)
continue;
if ($pathnode->nodeName == 'path') {
$o = new BSVN_LogEntryPath($pathnode);
$o->revision = $this->revision;
$o->save();
switch($o->action) {
case 'm': $this->m++; break;
case 'd': $this->d++; break;
case 'r': $this->r++; break;
case 'a': $this->a++; break;
default: file_put_contents('php://stderr', 'Unknown action: '.$o->action."\n");
}
if ($this->branch === null)
$this->detectBranch($o->path);
}
}
}
}
}
private function detectBranch($path) {
$m = array();
if (preg_match('/'.BSVN_Repository::get()->branchesDir.'\/([^\0\/]+)/i', $path, $m)) {
$this->branch = BSVN_Repository::get()->getBranchId($m[1]);
}
}
/**
* Saves revision onto database
*/
public function save() {
$branchid = $this->branch===null?0:$this->branch;
$sql = BSVN_Database::prepare('REPLACE INTO revisions (revision, branch, author, msg, date, a, m, d, r) VALUES(?,?,?,?,?,?,?,?,?)');
$sql->bindValue(1, $this->revision, PDO::PARAM_INT);
$sql->bindValue(2, $branchid, PDO::PARAM_INT);
$sql->bindValue(3, $this->author, PDO::PARAM_INT);
$sql->bindValue(4, $this->msg, PDO::PARAM_STR);
$sql->bindValue(5, $this->date->format('U'), PDO::PARAM_STR);
$sql->bindValue(6, $this->a, PDO::PARAM_INT);
$sql->bindValue(7, $this->m, PDO::PARAM_INT);
$sql->bindValue(8, $this->d, PDO::PARAM_INT);
$sql->bindValue(9, $this->r, PDO::PARAM_INT);
$sql->execute();
}
}
class BSVN_LogEntryPath {
public $id = null;
public $kind;
public $action;
public $path;
public $revision = null;
public function __construct(DOMNode $pathnode=null) {
if ($pathnode !== null) {
$this->kind = $pathnode->getAttribute('kind');
$this->action = strtolower($pathnode->getAttribute('action'));
$this->path = trim($pathnode->nodeValue);
}
}
public function save() {
$dir = '';
$fname = '';
if ($this->revision === null)
throw new Exception('Cannot store unversioned file into database! Set $revision first!');
if ($this->kind != '') {
// The easy way :]
if ($this->kind == 'dir') {
$dir = $this->path;
} else {
$dir = pathinfo($this->path, PATHINFO_DIRNAME);
$fname = pathinfo($this->path, PATHINFO_BASENAME);
}
} else {
// WARNING: Fuzzy autodetection
$dir = pathinfo($this->path, PATHINFO_DIRNAME);
$fname = pathinfo($this->path, PATHINFO_BASENAME);
if (strpos($fname, '.')===false) {
// No dot in filename, maybe this is directory??
$dir .= $fname;
$fname = '';
} else {
// UpperCase extensions are uncommon, try to filter out .Net's directory names
$ext = pathinfo($fname, PATHINFO_EXTENSION);
if ($ext == ucfirst($ext)) {
$dir .= $fname;
$fname = '';
}
}
bsvn_verbose('Fuzzy kind detection: '.$this->path.' : '.($fname==''?'dir':'file'));
}
// update database entry, if it exists, set modCount++
$sql = BSVN_Database::prepare('UPDATE files SET revision=?, modCount=modCount+1 WHERE dir=? AND fname=?');
$sql->bindValue(1, $this->revision, PDO::PARAM_INT);
$sql->bindValue(2, $dir, PDO::PARAM_STR);
$sql->bindValue(3, $fname, PDO::PARAM_STR);
$sql->execute();
if (!$sql->rowCount()) {
$sql = BSVN_Database::prepare('INSERT INTO files (dir, fname, type, revision, modCount, createdRev) VALUES(?,?,?,?,0,?)');
$sql->bindValue(1, $dir, PDO::PARAM_STR);
$sql->bindValue(2, $fname, PDO::PARAM_STR);
$sql->bindValue(3, self::getType($fname), PDO::PARAM_STR);
$sql->bindValue(4, $this->revision, PDO::PARAM_INT);
$sql->bindValue(5, $this->revision, PDO::PARAM_INT);
$sql->execute();
$this->id = BSVN_Database::get()->lastInsertId();
}
if ($this->action == 'd') {
$sql = BSVN_Database::prepare('UPDATE files SET deletedRev=? WHERE dir=? AND fname=?');
$sql->bindValue(1, $this->revision, PDO::PARAM_INT);
$sql->bindValue(2, $dir, PDO::PARAM_STR);
$sql->bindValue(3, $fname, PDO::PARAM_STR);
$sql->execute();
}
}
public static function getType($fname) {
if ($fname == '')
return 'dir';
return pathinfo($fname, PATHINFO_EXTENSION);
}
}
class BSVN_DiffParser {
private $file = '';
public function __construct($file) {
$this->file = $file;
}
/**
* Parses diff file
* @return BSVN_DiffFileStats Associative array of file-stats
*/
public function parse() {
$out = array();
$fh = fopen($this->file, 'rt');
if ($fh === false)
throw new Exception('Cannot open diff for reading.');
$obj = null;
$section = null;
while (!feof($fh)) {
$line = trim(fgets($fh, 4096));
if (empty($line))
continue;
// Find "Index: "
if (strcasecmp(substr($line, 0, 7), 'Index: ') == 0) {
if ($obj != null)
$out[$obj->fpath] = $obj;
$obj = new BSVN_DiffFileStats();
$obj->fpath = substr($line, 8);
continue;
}
if ($obj == null)
continue;
// Skip '='
switch (trim($line[0])) {
case '' :
case '=' : continue;
case '@' :
if ($section !== null)
$obj->sections[] = $section;
$obj->diffsections++;
$section = new BSVN_DiffSection();
$section->id = $line;
// codebase changed
$matches = array();
if (!preg_match('/@@\s*(?<range11>[\-\+0-9]+)(,(?<range12>[\-\+0-9]+))?\s*(?<range21>[\-\+0-9]+)(,(?<range22>[\-\+0-9]+))?\s*@@/', $line, $matches)) {
bsvn_verbose('WARNING: Cannot parse section data: '.$line);
$section = null; //skip section
continue;
}
$r12 = 1;
$r22 = 1;
if (isset($matches['range22']))
$r22 = intval($matches['range22']);
if (isset($matches['range12']))
$r12 = intval($matches['range12']);
$section->codebaseChange = $r22-$r12;
break;
case '\\': continue; // Comment
case '+' : if ($section !==null) $section->linesAdded++; break;
case '-' : if ($section !== null) $section->linesDeleted++; break;
}
}
if ($obj != null && $section !== null)
$obj->sections[] = $section;
if ($obj != null)
$out[$obj->fpath] = $obj;
fclose($fh);
return $out;
}
}
class BSVN_DiffFileStats {
public $fpath = '';
public $revision = 0;
public $author = '';
public $diffsections = 0;
public $sections = array();
public function getCodebaseChange() {
$sum = 0;
foreach ($this->sections as $section) {
$sum += $section->codebaseChange;
}
return $sum;
}
public function getLinesChanged() {
$sum = 0;
foreach($this->sections as $section) {
$sum += $section->getLinesChanged();
}
return $sum;
}
}
class BSVN_DiffSection {
public $id = '';
public $codebaseChange = 0;
public $linesAdded = 0;
public $linesDeleted = 0;
public function getLinesChanged() {
return max($this->linesAdded, $this->linesDeleted);
}
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.