]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQ.pm
Allow sending deletion requests
[pciids.git] / PciIds / DBQ.pm
1 package PciIds::DBQ;
2 use strict;
3 use warnings;
4 use base 'PciIds::DBQAny';
5
6 sub new( $ ) {
7         my( $dbh ) = @_;
8         my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
9         my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
10         return bless PciIds::DBQAny::new( $dbh, {
11                 'nodes-id' => $node.'id',
12                 'nodes-name' => $node.'name',
13                 'nodes-rid' => $node.'id DESC',
14                 'nodes-rname' => $node.'name DESC',
15                 'nodes-id-r' => $noder.'id',
16                 'nodes-name-r' => $noder.'name',
17                 'nodes-rid-r' => $noder.'id DESC',
18                 'nodes-rname-r' => $noder.'name DESC',
19                 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
20                 'login' => 'SELECT id FROM users WHERE login = ?',
21                 'email' => 'SELECT id FROM users WHERE email = ?',
22                 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
23                 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
24                 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
25                 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
26                 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
27                 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
28                 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
29                 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
30                 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
31                 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
32                 'history' => 'SELECT history.id, history.discussion, history.time, history.nodename, history.nodenote, history.seen, users.login FROM history LEFT OUTER JOIN users ON history.owner = users.id WHERE history.location = ? ORDER BY history.time',
33                 'admindump' => 'SELECT
34                         locations.id, locations.name, locations.note, locations.mainhistory, musers.login, main.discussion,
35                         history.id, history.discussion, history.nodename, history.nodenote, users.login
36                 FROM
37                         locations INNER JOIN history ON history.location = locations.id
38                         LEFT OUTER JOIN users ON history.owner = users.id
39                         LEFT OUTER JOIN history AS main ON locations.mainhistory = main.id
40                         LEFT OUTER JOIN users AS musers ON main.owner = musers.id WHERE history.seen = "0"
41                 ORDER BY locations.id, history.id
42                 LIMIT 15',#Dumps new discussion submits with their senders and corresponding main history and names
43                 'delete-hist' => 'DELETE FROM history WHERE id = ?',
44                 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
45                 'delete-item' => 'DELETE FROM locations WHERE id = ?',
46                 'set-mainhist' => 'UPDATE locations SET
47                                 mainhistory = ?,
48                                 name = ( SELECT nodename FROM history WHERE id = ? ),
49                                 note = ( SELECT nodenote FROM history WHERE id = ? )
50                         WHERE
51                                 id = ?',
52                 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
53                 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
54                 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
55                 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
56                 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
57                 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
58                 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
59                 'notify' => 'INSERT INTO pending (user, history, notification, reason) SELECT DISTINCT user, ?, ?, ? FROM notifications WHERE ( notification = 2 OR notification = ? ) AND type <= ? AND ( location = ? OR ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
60                 'newtime-mail' => 'UPDATE users SET nextmail = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * mailgather ) WHERE nextmail < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 0 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
61                 'newtime-xmpp' => 'UPDATE users SET nextxmpp = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * xmppgather ) WHERE nextxmpp < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 1 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
62                 'mailout' => 'SELECT
63                                 pending.user, users.email,
64                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
65                                 auth.login, history.location, locations.name, locations.note
66                         FROM
67                                 pending
68                                 INNER JOIN users ON users.id = pending.user
69                                 INNER JOIN history ON history.id = pending.history
70                                 INNER JOIN locations ON history.location = locations.id
71                                 INNER JOIN users AS auth ON auth.id = history.owner
72                         WHERE
73                                 pending.notification = 0
74                                 AND users.nextmail <= ?
75                         ORDER BY
76                                 pending.user, pending.reason, history.time, history.location',
77                 'xmppout' => 'SELECT
78                                 pending.user, users.xmpp,
79                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
80                                 auth.login, history.location, locations.name, locations.note
81                         FROM
82                                 pending
83                                 INNER JOIN users ON users.id = pending.user
84                                 INNER JOIN history ON history.id = pending.history
85                                 INNER JOIN locations ON history.location = locations.id
86                                 INNER JOIN users AS auth ON auth.id = history.owner
87                         WHERE
88                                 pending.notification = 1
89                                 AND users.nextxmpp <= ?
90                         ORDER BY
91                                 pending.user, pending.reason, history.time, history.location',
92                 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
93                 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
94                 'time' => 'SELECT NOW()'
95
96         } );
97 }
98
99 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
100
101 sub nodes( $$$$ ) {
102         my( $self, $parent, $args, $restrict ) = @_;
103         my $q = 'id';
104         $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
105         if( defined( $restrict ) && ( $restrict ne "" ) ) {
106                 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
107         } else {
108                 return $self->query( 'nodes-'.$q, [ $parent ] );
109         }
110 }
111
112 sub item( $$ ) {
113         my( $self, $id ) = @_;
114         my $result = $self->query( "item", [ $id ] );
115         if( scalar @{$result} ) {
116                 return $result->[ 0 ];
117         } else {
118                 return undef;
119         }
120 }
121
122 sub hasLogin( $$ ) {
123         my( $self, $login ) = @_;
124         my $result = $self->query( 'login', [ $login ] );
125         return scalar @{$result};
126 }
127
128 sub hasEmail( $$ ) {
129         my( $self, $email ) = @_;
130         my $result = $self->query( 'email', [ $email ] );
131         return scalar @{$result};
132 }
133
134 sub addUser( $$$$ ) {
135         my( $self, $login, $email, $passwd ) = @_;
136         eval {
137                 if( ( defined $login ) && ( $login ne '' ) ) {
138                         $self->command( 'adduser', [ $login, $email, $passwd ] );
139                 } else {
140                         $self->command( 'adduser-null', [ $email, $passwd ] );
141                 }
142         };
143         if( $@ ) {
144                 return 0;
145         } else {
146                 return $self->last();
147         }
148 }
149
150 sub getLogInfo( $$ ) {
151         my( $self, $info ) = @_;
152         my $data;
153         if( $info =~ /@/ ) {#An email address
154                 $data = $self->query( 'loginfomail', [ $info ] );
155         } else {
156                 $data = $self->query( 'loginfoname', [ $info ] );
157         }
158         if( scalar @{$data} ) {
159                 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
160                 my $logstring;
161                 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
162                 $email = $info if( $info =~ /@/ );
163                 return( $id, $passwd, $email, $logstring );
164         } else {
165                 return undef;
166         }
167 }
168
169 sub rights( $$ ) {
170         my( $self, $id ) = @_;
171         return $self->query( 'rights', [ $id ] );
172 }
173
174 sub setLastLog( $$$ ) {
175         my( $self, $id, $from ) = @_;
176         $self->command( 'setlastlog', [ $from, $id ] );
177 }
178
179 sub history( $$ ) {
180         my( $self, $addr ) = @_;
181         return $self->query( 'history', [ $addr ] );
182 }
183
184 sub submitItem( $$$ ) {
185         my( $self, $data, $auth ) = @_;
186         my( $addr ) = ( $data->{'address'} );
187         return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
188         eval {
189                 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
190                 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
191
192         };
193         if( $@ ) {
194                 $self->rollback();
195                 return( 'internal: '.$@, undef );
196         }
197         return( '', $self->last() );
198 }
199
200 sub submitHistory( $$$$ ) {
201         my( $self, $data, $auth, $address ) = @_;
202         if( $data->{'delete'} ) {
203                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
204         } else {
205                 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
206                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
207         }
208         return $self->last();
209 }
210
211 sub adminDump( $ ) {
212         return shift->query( 'admindump', [] );
213 }
214
215 sub deleteHistory( $$ ) {
216         my( $self, $id ) = @_;
217         $self->command( 'delete-hist', [ $id ] );
218 }
219
220 sub markChecked( $$ ) {
221         my( $self, $id ) = @_;
222         $self->command( 'mark-checked', [ $id ] );
223 }
224
225 sub deleteItem( $$ ) {
226         my( $self, $id ) = @_;
227         $self->command( 'delete-item', [ $id ] );
228 }
229
230 sub setMainHistory( $$$ ) {
231         my( $self, $location, $history ) = @_;
232         $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
233 }
234
235 sub resetInfo( $$ ) {
236         my( $self, $mail ) = @_;
237         my $result = $self->query( 'resetinfo', [ $mail ] );
238         if( scalar @{$result} ) {
239                 return ( @{$result->[0]} );
240         } else {
241                 return undef;
242         }
243 }
244
245 sub changePasswd( $$$ ) {
246         my( $self, $id, $passwd ) = @_;
247         $self->command( 'changepasswd', [ $passwd, $id ] );
248 }
249
250 sub profileData( $$ ) {
251         my( $self, $id ) = @_;
252         my %result;
253         ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
254         return \%result;
255 }
256
257 sub setEmail( $$$$ ) {
258         my( $self, $id, $email, $passwd ) = @_;
259         $self->command( 'setemail', [ $email, $passwd, $id ] );
260 }
261
262 sub pushProfile( $$$$$$ ) {
263         my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
264         $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
265 }
266
267 sub notificationsUser( $$ ) {
268         my( $self, $uid ) = @_;
269         return $self->query( 'notifuser', [ $uid ] );
270 }
271
272 sub getNotifData( $$$ ) {
273         my( $self, $uid, $location ) = @_;
274         my $result = $self->query( 'notifdata', [ $uid, $location ] );
275         if( @{$result} ) {
276                 my( $recursive, $notification, $way ) = @{$result->[0]};
277                 return {
278                         'recursive' => $recursive,
279                         'notification' => $notification,
280                         'way' => $way };
281         } else {
282                 return { 'recursive' => 1 };
283         }
284 }
285
286 sub submitNotification( $$$$ ) {
287         my( $self, $uid, $location, $data ) = @_;
288         $self->command( 'drop-notif', [ $uid, $location ] );
289         $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
290 }
291
292 sub pushNotifications( $$$$$ ) {
293         my( $self, $location, $history, $priority, $reason ) = @_;
294         $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
295         $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
296         $self->command( 'newtime-mail', [ $priority, $location, $location ] );
297         $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
298 }
299
300 sub mailNotifs( $$ ) {
301         my( $self, $time ) = @_;
302         return $self->query( 'mailout', [ $time ] );
303 }
304
305 sub xmppNotifs( $$ ) {
306         my( $self, $time ) = @_;
307         return $self->query( 'xmppout', [ $time ] );
308 }
309
310 sub time( $ ) {
311         my( $self ) = @_;
312         return $self->query( 'time', [] )->[0]->[0];
313 }
314
315 sub dropNotifs( $$ ) {
316         my( $self, $time ) = @_;
317         $self->command( 'dropnotifsmail', [ $time ] );
318         $self->command( 'dropnotifsxmpp', [ $time ] );
319 }
320
321 1;