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         $watches = $w->watched('email');
47         
48         $CS = DB_DataObject::factory('mtrack_change');
49         
50         $messages = array();
51         $cache = array();
52         foreach($watches as $uid => $ar) {
53             // we can now query mtrack_change for anything that occured in that list..
54             $messages = $CS->gatherChanges($e->event_when, $uid, $ar);
55             //print_R($messages);exit;
56             // we should cache the emails. = as multiple people might be getting the same email.
57             
58             // should be like a list of tickets
59             foreach ($messages as $m=>$ar) {
60                 $lr = explode(':', $m);
61                 $o = DB_DataObject::Factory($lr[0]);
62                 $o->get($lr[1]);
63                 if (!isset($cache[implode(',', $ar)])) {
64                     $cs = clone($CS);
65                     $cs->whereAddIn('id', $ar, 'int');
66                     $cs->autoJoin();
67                     $cache[implode(',', $ar)] = $cs->fetchAll();
68                 }
69                 $u  = DB_DataObject::factory('Person');
70                 $u->get($uid);
71                 $method = $this, 'notify_'. $lr[0] ;
72                 if (method_exists($method)) {
73                     $this->$method($o,  $cache[implode(',', $ar)], $u)
74                 }
75             }
76             
77             
78             
79             
80             
81             
82             
83         }
84        // print_R($messages);
85         die("done");
86         
87         
88         
89         
90          
91         /* For each watcher, compute the changes.
92          * Group changes by ticket, sending one email per ticket.
93          * Group tickets into batch updates if the only fields that changed are
94          * bulk update style (milestone, assignment etc.)
95          *
96          * For the wiki repo, group by file so that serial edits within the batch
97          * period show up as a single email.
98          */
99          
100     }
101     
102     
103     /**
104      * sets:
105      *   - from (system or a specific user.)
106      *   - replyto (system)
107      *   - to user...
108      *   
109      */
110     function calcAddress($items, $user) {
111         
112         
113     }
114     
115     /**
116      * convert notification into an email..
117      *
118      * @param object $ticket the DataObject
119      * @param array $items the ids of the mtrack_change's
120      * @parem object $user DataObject of the user..
121      * 
122      */
123     
124     function notify_mtrack_ticket($ticket, $changes, $user)
125     {
126         //global $MAX_DIFF;
127       
128         // from is always the system.. ??
129         // unless all the changers are the same..
130         $headers = $this->calcAddress($item, $user);
131         
132           
133         //$headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
134         //$headers['From'] = make_email($from[0][0], $from[0][1]);
135         //if (count($from) > 1) {
136         //    $rep = array();
137         //    array_shift($from);
138          //       foreach ($from as $email) {
139          // $rep[] = make_email($email[0], $email[1]);
140         // }
141         //$headers['Reply-To'] = join(', ', $rep);
142         //}
143      
144         $headers += array(
145             'MIME-Version' => '1.0',
146             'Content-Type' => 'text/plain; charset="UTF-8"',
147             'Content-Transfer-Encoding' => 'quoted-printable',
148         );
149     
150         
151         $mid = $ticket->id . '@' . php_uname('n');
152         
153         
154         
155         
156         //if ($is_initial) {
157         //    $headers['Message-ID'] = "<$mid>";
158         //} else {
159         //$headers['Message-ID'] = "<$T->updated.$mid>";
160         //$headers['In-Reply-To'] = "<$mid>";
161         //$headers['References'] = "<$mid>";
162         //}
163       /* find related project(s) */
164         $p = $ticket->project();
165          
166         $subj = "[" . $p->code . ' - ' . $p->name . "] ";
167     
168         $headers['X-mtrack-project-list'] = $p->code;
169         //foreach ($projects as $pname) {
170         //  $headers["X-mtrack-project-$pname"] = $pname;
171          // $headers['X-mtrack-project'][] = $pname;
172         //}
173         //} else {
174         //$subj = '';
175         // }
176     
177         $headers['Subject'] = sprintf("%s#%s %s (%s %s)",
178           $subj, $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name);
179     
180         $owner = strlen($ticket->owner) ? $ticket->owner_id_name : 'nobody';
181     
182         $body = 
183             sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident) .
184             sprintf("#%s: %s (%s %s)\n", $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name) .
185             sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority, $ticket->severity) .
186     
187             //sprintf("Milestone: %s\n", join(', ', $T->getMilestones()));
188             //sprintf("Component: %s\n", join(', ', $T->getComponents()));
189             "\n";
190     
191       // Display changed fields grouped by the person that last changed them
192         //$who_changed = array();
193         //foreach ($field_changers as $field => $who) {
194         //  $who_changed[$who][] = $field;
195         //}
196         $body.="\n\n";
197         foreach ($changes as $change) {
198             
199             $body .= "{$change->changedate}:  Change by {$change->person_id_name}\n";
200             $body .= str_repeat('-', 80) . "\n";
201             if (!empty($change->reason)) {
202                 $body.= $change->reason;
203             }
204             $ar = $change->cachedAudit();
205             foreach($ar as $audit) {
206                 
207                 switch($audit->field()) {
208                     case 'id':
209                         continue; //??? ignore?
210                     case '@components':
211                         continue;
212                     //  $old = array();
213                     //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
214                      /*   if (!strlen($id)) continue;
215                         $c = get_component($id);
216                         $old[$id] = $c->name;
217                       }
218                       $value = $T->getComponents();
219                       $field = 'Component';
220                       break;
221                      */
222                     case '@milestones':
223                         continue;
224                     //  $old = array();
225                     //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
226                       /*  if (!strlen($id)) continue;
227                         $m = get_milestone($id);
228                         $old[$id] = $m->name;
229                       }
230                       $value = array();
231                       $value = $T->getMilestones();
232                       $field = 'Milestone';
233                       break;
234                       */
235                     case '@keywords':
236                         continue;
237                     /*
238                       $old = array();
239                       $field = 'Keywords';
240                       $value = $T->getKeywords();
241                       break;
242                       */
243                       
244                     //case 'commit?'  
245                       
246                     default:
247                         if (!strlen($audit->oldvalue)) {
248                             $body .= "Set {$audit->field()} to: {$audit->value}\n";
249                             continue;
250                         }
251                         if (!strlen($audit->value)) {
252                             $body .= "Removed {$audit->field()} - was: {$audit->oldvalue}\n";
253                             continue;
254                         }
255                         $body .= "Changed {$audit->field()} from :{$audit->oldvalue} -> {$audit->value}\n";
256                         continue;  
257                 }
258             }
259         }
260         
261         $body .= sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident);
262         
263         echo $body;   
264     
265         //  send_mail($udata['email'], $plain);
266     }
267     
268 }
269
270
271 class CanonicalLineEndingFilter extends php_user_filter {
272     function filter($in, $out, &$consumed, $closing)
273     {
274       while ($bucket = stream_bucket_make_writeable($in)) {
275         $bucket->data = preg_replace("/\r?\n/", "\r\n", $bucket->data);
276         $consumed += $bucket->datalen;
277         stream_bucket_append($out, $bucket);
278       }
279       return PSFS_PASS_ON;
280     }
281     }
282     class UnixLineEndingFilter extends php_user_filter {
283     function filter($in, $out, &$consumed, $closing)
284     {
285       while ($bucket = stream_bucket_make_writeable($in)) {
286         $bucket->data = preg_replace("/\r?\n/", "\n", $bucket->data);
287         $consumed += $bucket->datalen;
288         stream_bucket_append($out, $bucket);
289       }
290       return PSFS_PASS_ON;
291     }
292     }