final move of files
[web.mtrack] / MTrackWeb / DataObjects / Changes.php
1 <?php
2 /**
3  * Table Definition for changes
4  * 
5  * -- it stores any change to tickets/commits etc...
6  * 
7  * We now store rev ids in here..
8  * and the extra data (like file etc. in change_audit
9  * 
10  * Which will make timeline alot easier to work with..
11  * 
12  * Need to decide - this stores a commit as changes on a ticket, when it's flagged as such..
13  * -> in theory we can ignore this...
14  * -> fetch of ticket changes will have to pull in changes based on the rev id...
15  * 
16  * 
17  * history ->
18  *  ... just list scmid's since a certain time.. - then fetch them from here...
19  * 
20  *
21  * indexes : object, rev  (for looup)
22  * indexes : object, rev, changedate (for last rev)
23  * 
24  */
25 require_once 'DB/DataObject.php';
26
27 class MTrackWeb_DataObjects_Changes extends DB_DataObject 
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
32     public $__table = 'changes';                         // table name
33     public $cid;                             // int(11)  not_null primary_key auto_increment
34     public $who;                             // username
35     public $object;                          // ticket:xxxx or rep:1 etc.  
36     public $changedate;                      // datetime(19)  not_null binary
37     public $reason;                          // description (includes changeset name etc.
38     public $rev;                           // md5 has of commit id...
39     
40     /* Static get */
41     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('MTrackWeb_DataObjects_Changes',$k,$v); }
42
43     /* the code above is auto generated do not remove the tag below */
44     ###END_AUTOCODE
45     
46     function getCommit($repo, $hash, $runhistory=true) 
47     {
48         $this->object = $repo->toIdString(); // should be repo:1
49         $this->rev = $id;
50         if ($this->find(true)) {
51             $this->fetchAudit();
52             return;
53         }
54         if (!$runhistory) {
55             throw new Exception("Can not find revision");
56         }
57         $this->refreshHistory($repo);
58         if ( $this->find(true)) {
59             $this->fetchAudit();
60             return true;
61         }; /// yes I know we say you should not run this twice...
62         return false;
63     }
64     /*
65     * scans repo history and imports it... ??? does not know about branches yet...
66     */
67     function refreshHistory($repo) 
68     {
69         $c = DB_DataObject::factory('changes');
70         $c->object =  $repo->toIdString(); 
71         $c->orderBy('changedate DESC');
72         $c->whereAdd("rev != ''");
73         $last  = '';
74         if ($c->find(true)) {
75             $last = $c->scmid;
76         }
77         // history returns an array of scmEvents... - should actually return an array of this object...
78         $recs = $repo->history('', null, $last ? 'rev' : null, $last ? $last  :null);
79         
80         foreach($recs as $r) { 
81             // see if we have a copy of it already.
82             $c = DB_DataObject::factory('changes');
83             $c->object =  $repo->toIdString(); 
84             $c->changedate != $r->ctime;
85             $cr  = clone($c);
86             $c->whereAdd("rev != ''");
87             if (!$c->find(true)) {
88                 // not found, create a new one..
89                 $c->insert();
90                 
91             }
92             $cr->rev = $r->rev;
93             $cr->reason = $r->changelog;
94             $cr->who =  $r->changeby;
95             $cr->update();
96             foreach($cr->files as $f) {
97                 // we could do with adding 'X' lines changed on this
98                 $ca = DB_DataObject::factory('change_audit');
99                 $ca->cid = $cr->id;
100                 $ca->fieldname = $cr->object. ':file';
101                 $ca->action = $f->status . ($f->added ? ' +' . $f->added : '') . ($f->removed ? ' -' . $f->removed : '') ;
102                 $ca->value = $f->name;
103                 $ca->insert();
104                 
105             }
106             
107             
108         }
109     
110     }
111      
112     
113     
114 }