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