]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQ.pm
Check a sub-system vendor exists
[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         foreach( @{$addr->addressDeps()} ) {
188                 my( $dep, $error ) = @{$_};
189                 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
190         }
191         return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
192         eval {
193                 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
194                 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
195         };
196         if( $@ ) {
197                 $self->rollback();
198                 return( 'internal: '.$@, undef );
199         }
200         return( '', $self->last() );
201 }
202
203 sub submitHistory( $$$$ ) {
204         my( $self, $data, $auth, $address ) = @_;
205         if( $data->{'delete'} ) {
206                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
207         } else {
208                 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
209                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
210         }
211         return $self->last();
212 }
213
214 sub adminDump( $ ) {
215         return shift->query( 'admindump', [] );
216 }
217
218 sub deleteHistory( $$ ) {
219         my( $self, $id ) = @_;
220         $self->command( 'delete-hist', [ $id ] );
221 }
222
223 sub markChecked( $$ ) {
224         my( $self, $id ) = @_;
225         $self->command( 'mark-checked', [ $id ] );
226 }
227
228 sub deleteItem( $$ ) {
229         my( $self, $id ) = @_;
230         $self->command( 'delete-item', [ $id ] );
231 }
232
233 sub setMainHistory( $$$ ) {
234         my( $self, $location, $history ) = @_;
235         $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
236 }
237
238 sub resetInfo( $$ ) {
239         my( $self, $mail ) = @_;
240         my $result = $self->query( 'resetinfo', [ $mail ] );
241         if( scalar @{$result} ) {
242                 return ( @{$result->[0]} );
243         } else {
244                 return undef;
245         }
246 }
247
248 sub changePasswd( $$$ ) {
249         my( $self, $id, $passwd ) = @_;
250         $self->command( 'changepasswd', [ $passwd, $id ] );
251 }
252
253 sub profileData( $$ ) {
254         my( $self, $id ) = @_;
255         my %result;
256         ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
257         return \%result;
258 }
259
260 sub setEmail( $$$$ ) {
261         my( $self, $id, $email, $passwd ) = @_;
262         $self->command( 'setemail', [ $email, $passwd, $id ] );
263 }
264
265 sub pushProfile( $$$$$$ ) {
266         my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
267         $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
268 }
269
270 sub notificationsUser( $$ ) {
271         my( $self, $uid ) = @_;
272         return $self->query( 'notifuser', [ $uid ] );
273 }
274
275 sub getNotifData( $$$ ) {
276         my( $self, $uid, $location ) = @_;
277         my $result = $self->query( 'notifdata', [ $uid, $location ] );
278         if( @{$result} ) {
279                 my( $recursive, $notification, $way ) = @{$result->[0]};
280                 return {
281                         'recursive' => $recursive,
282                         'notification' => $notification,
283                         'way' => $way };
284         } else {
285                 return { 'recursive' => 1 };
286         }
287 }
288
289 sub submitNotification( $$$$ ) {
290         my( $self, $uid, $location, $data ) = @_;
291         $self->command( 'drop-notif', [ $uid, $location ] );
292         $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
293 }
294
295 sub pushNotifications( $$$$$ ) {
296         my( $self, $location, $history, $priority, $reason ) = @_;
297         $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
298         $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
299         $self->command( 'newtime-mail', [ $priority, $location, $location ] );
300         $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
301 }
302
303 sub mailNotifs( $$ ) {
304         my( $self, $time ) = @_;
305         return $self->query( 'mailout', [ $time ] );
306 }
307
308 sub xmppNotifs( $$ ) {
309         my( $self, $time ) = @_;
310         return $self->query( 'xmppout', [ $time ] );
311 }
312
313 sub time( $ ) {
314         my( $self ) = @_;
315         return $self->query( 'time', [] )->[0]->[0];
316 }
317
318 sub dropNotifs( $$ ) {
319         my( $self, $time ) = @_;
320         $self->command( 'dropnotifsmail', [ $time ] );
321         $self->command( 'dropnotifsxmpp', [ $time ] );
322 }
323
324 1;