MTrackWeb/Cron/Notify.php
[web.mtrack] / MTrackWeb / Cron / Notify.php
1 <?php
2 require_once 'MTrackWeb.php';
3
4 class MTrackWeb_Cron_Notify extends MTrackWeb
5 {
6     
7     function getAuth()
8     {
9         $ff = HTML_FlexyFramework::get();
10         if (!$ff->cli) {
11             die("access denied");
12         }
13         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
14         return true;
15         
16     }
17     
18     function get()    
19     {
20         DB_DataObject::debugLevel(1);
21         date_default_timezone_set('UTC');
22         
23         // what's the baserul..
24     
25         
26         $MAX_DIFF = 200 * 1024;
27         $USE_BATCHING = false;
28         
29          
30         $e= DB_DataObject::factory('Events');
31         $e->action = 'MTRACK.NOTIFY';
32         $e->selectAdd();
33         $e->selectAdd('MAX(event_when) as event_when');
34         
35         if (!$e->find(true)) {
36             $e->event_when = date('Y-m-d H:i:s', 0); // should be a long time ago..
37         }
38          
39         
40         
41         stream_filter_register("mtrackcanonical", 'CanonicalLineEndingFilter');
42         stream_filter_register("mtrackunix", 'UnixLineEndingFilter');
43         
44         $w = DB_DataObject::factory('core_watch');
45         // returns a list of user with the objects they are watching..
46         $watched = $w->watched('email');
47         
48         $CS = DB_DataObject::factory('mtrack_change');
49         
50         $messages = array();
51         foreach($watches as $uid => $ar) {
52             // we can now query mtrack_change for anything that occured in that list..
53             $messages[$uid]  = $CS->gatherChanges($e->event_when, $uid, $ar);
54             
55         }
56         print_R($messages);
57         die("done");
58         
59         
60         
61         
62         $watched = MTrackWatch::getWatchedItemsAndWatchers($last, 'email');
63         printf("Got %d watchers\n", count($watched));
64         
65         /* For each watcher, compute the changes.
66          * Group changes by ticket, sending one email per ticket.
67          * Group tickets into batch updates if the only fields that changed are
68          * bulk update style (milestone, assignment etc.)
69          *
70          * For the wiki repo, group by file so that serial edits within the batch
71          * period show up as a single email.
72          */
73         
74         foreach ($watched as $user => $objects) {
75             $udata = MTrackAuth::getUserData($user);
76         
77           foreach ($objects as $object => $items) {
78             list($otype, $oid) = explode(':', $object, 2);
79         
80             $fname = "notify_$otype";
81             if (function_exists($fname)) {
82               call_user_func($fname, $object, $oid, $items, $user, $udata);
83             } else {
84               echo "WARN: no notifier for $otype $oid\n";
85             }
86             foreach ($items as $o) {
87               if ($o instanceof MTrackSCMEvent) {
88                 $t = strtotime($o->ctime);
89               } else {
90                 $t = strtotime($o->changedate);
91               }
92               if ($t > $LATEST) {
93                 $LATEST = $t;
94               }
95             }
96           }
97         }
98     }
99 }
100
101
102 class CanonicalLineEndingFilter extends php_user_filter {
103     function filter($in, $out, &$consumed, $closing)
104     {
105       while ($bucket = stream_bucket_make_writeable($in)) {
106         $bucket->data = preg_replace("/\r?\n/", "\r\n", $bucket->data);
107         $consumed += $bucket->datalen;
108         stream_bucket_append($out, $bucket);
109       }
110       return PSFS_PASS_ON;
111     }
112     }
113     class UnixLineEndingFilter extends php_user_filter {
114     function filter($in, $out, &$consumed, $closing)
115     {
116       while ($bucket = stream_bucket_make_writeable($in)) {
117         $bucket->data = preg_replace("/\r?\n/", "\n", $bucket->data);
118         $consumed += $bucket->datalen;
119         stream_bucket_append($out, $bucket);
120       }
121       return PSFS_PASS_ON;
122     }
123     }