4 use base 'PciIds::DBQAny';
6 my $adminDumpSql = 'SELECT
7 locations.id, locations.name, locations.note, locations.mainhistory, musers.email, musers.login, main.discussion, main.time,
8 history.id, history.discussion, history.nodename, history.nodenote, users.email, users.login, history.time
10 locations INNER JOIN history ON history.location = locations.id
11 LEFT OUTER JOIN users ON history.owner = users.id
12 LEFT OUTER JOIN history AS main ON locations.mainhistory = main.id
13 LEFT OUTER JOIN users AS musers ON main.owner = musers.id
14 WHERE history.seen = "0" AND locations.id LIKE ?
15 ORDER BY locations.id, history.id
20 my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
21 my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
22 return bless PciIds::DBQAny::new( $dbh, {
23 'nodes-id' => $node.'id',
24 'nodes-name' => $node.'name',
25 'nodes-rid' => $node.'id DESC',
26 'nodes-rname' => $node.'name DESC',
27 'nodes-id-r' => $noder.'id',
28 'nodes-name-r' => $noder.'name',
29 'nodes-rid-r' => $noder.'id DESC',
30 'nodes-rname-r' => $noder.'name DESC',
31 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
32 'login' => 'SELECT id FROM users WHERE login = ?',
33 'email' => 'SELECT id FROM users WHERE email = ?',
34 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
35 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
36 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
37 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
38 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
39 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
40 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
41 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
42 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
43 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
44 'history' => 'SELECT history.id, history.discussion, history.time, history.nodename, history.nodenote, history.seen, users.login, users.email FROM history LEFT OUTER JOIN users ON history.owner = users.id WHERE history.location = ? ORDER BY history.time',
45 'admindump' => "$adminDumpSql 100",#Dumps new discussion submits with their senders and corresponding main history and names
46 'delete-hist' => 'DELETE FROM history WHERE id = ?',
47 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
48 'delete-item' => 'DELETE FROM locations WHERE id = ?',
49 'set-mainhist' => 'UPDATE locations SET
51 name = ( SELECT nodename FROM history WHERE id = ? ),
52 note = ( SELECT nodenote FROM history WHERE id = ? )
55 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
56 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
57 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
58 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
59 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
60 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
61 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
62 'notify' => 'INSERT INTO pending (user, history, notification, reason) SELECT DISTINCT user, ?, ?, ? FROM notifications WHERE ( notification = 2 OR notification = ? ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
63 '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 ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
64 '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 ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
66 pending.user, users.email,
67 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
68 auth.login, history.location, locations.name, locations.note
71 INNER JOIN users ON users.id = pending.user
72 INNER JOIN history ON history.id = pending.history
73 INNER JOIN locations ON history.location = locations.id
74 INNER JOIN users AS auth ON auth.id = history.owner
76 pending.notification = 0
77 AND users.nextmail <= ?
79 pending.user, pending.reason, history.time, history.location',
81 pending.user, users.xmpp,
82 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
83 auth.login, history.location, locations.name, locations.note
86 INNER JOIN users ON users.id = pending.user
87 INNER JOIN history ON history.id = pending.history
88 INNER JOIN locations ON history.location = locations.id
89 INNER JOIN users AS auth ON auth.id = history.owner
91 pending.notification = 1
92 AND users.nextxmpp <= ?
94 pending.user, pending.reason, history.time, history.location',
95 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
96 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
97 'time' => 'SELECT NOW()',
98 'searchname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? ORDER BY l.id',
99 'searchlocalname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? AND l.id LIKE ? ORDER BY l.id',
100 'hasChildren' => 'SELECT DISTINCT 1 FROM locations WHERE parent = ?',
101 'hasMain' => 'SELECT DISTINCT 1 FROM locations WHERE id = ? AND mainhistory IS NOT NULL',
102 'notif-exists' => 'SELECT DISTINCT 1 FROM notifications WHERE user = ? AND ( location = ? OR ( recursive = 1 AND type <= 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )'
106 sub hasChildren( $$ ) {
107 my( $self, $parent ) = @_;
108 return scalar @{$self->query( 'hasChildren', [ $parent ] )};
112 my( $self, $id ) = @_;
113 return scalar @{$self->query( 'hasMain', [ $id ] )};
116 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
119 my( $self, $parent, $args, $restrict ) = @_;
121 $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
122 if( defined( $restrict ) && ( $restrict ne "" ) ) {
123 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
125 return $self->query( 'nodes-'.$q, [ $parent ] );
130 my( $self, $id ) = @_;
131 my $result = $self->query( "item", [ $id ] );
132 if( scalar @{$result} ) {
133 return $result->[ 0 ];
140 my( $self, $login ) = @_;
141 my $result = $self->query( 'login', [ $login ] );
142 return scalar @{$result};
146 my( $self, $email ) = @_;
147 my $result = $self->query( 'email', [ $email ] );
148 return scalar @{$result};
151 sub addUser( $$$$ ) {
152 my( $self, $login, $email, $passwd ) = @_;
154 if( ( defined $login ) && ( $login ne '' ) ) {
155 $self->command( 'adduser', [ $login, $email, $passwd ] );
157 $self->command( 'adduser-null', [ $email, $passwd ] );
163 return $self->last();
167 sub getLogInfo( $$ ) {
168 my( $self, $info ) = @_;
170 if( $info =~ /@/ ) {#An email address
171 $data = $self->query( 'loginfomail', [ $info ] );
173 $data = $self->query( 'loginfoname', [ $info ] );
175 if( scalar @{$data} ) {
176 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
178 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
179 $email = $info if( $info =~ /@/ );
180 return( $id, $passwd, $email, $logstring );
187 my( $self, $id ) = @_;
188 return $self->query( 'rights', [ $id ] );
191 sub setLastLog( $$$ ) {
192 my( $self, $id, $from ) = @_;
193 $self->command( 'setlastlog', [ $from, $id ] );
197 my( $self, $addr ) = @_;
198 return $self->query( 'history', [ $addr ] );
201 sub submitItem( $$$ ) {
202 my( $self, $data, $auth ) = @_;
203 my( $addr ) = ( $data->{'address'} );
204 foreach( @{$addr->addressDeps()} ) {
205 my( $dep, $error ) = @{$_};
206 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
208 return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
210 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
211 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
215 return( 'internal: '.$@, undef );
217 return( '', $self->last() );
220 sub submitHistory( $$$$ ) {
221 my( $self, $data, $auth, $address ) = @_;
222 if( $data->{'delete'} ) {
223 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
225 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
226 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
228 return $self->last();
231 sub adminDump( $$$ ) {
232 my( $self, $prefix, $limit ) = @_;
234 $limit = int( $limit * 1.2 );
235 my $q = $self->{'dbh'}->prepare( "$adminDumpSql $limit" );
236 $q->execute( "$prefix%" );
237 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
241 return $self->query( 'admindump', [ "$prefix%" ] );
245 sub deleteHistory( $$ ) {
246 my( $self, $id ) = @_;
247 $self->command( 'delete-hist', [ $id ] );
250 sub markChecked( $$ ) {
251 my( $self, $id ) = @_;
252 $self->command( 'mark-checked', [ $id ] );
255 sub deleteItem( $$ ) {
256 my( $self, $id ) = @_;
257 $self->command( 'delete-item', [ $id ] );
260 sub setMainHistory( $$$ ) {
261 my( $self, $location, $history ) = @_;
262 $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
265 sub resetInfo( $$ ) {
266 my( $self, $mail ) = @_;
267 my $result = $self->query( 'resetinfo', [ $mail ] );
268 if( scalar @{$result} ) {
269 return ( @{$result->[0]} );
275 sub changePasswd( $$$ ) {
276 my( $self, $id, $passwd ) = @_;
277 $self->command( 'changepasswd', [ $passwd, $id ] );
280 sub profileData( $$ ) {
281 my( $self, $id ) = @_;
283 ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
287 sub setEmail( $$$$ ) {
288 my( $self, $id, $email, $passwd ) = @_;
289 $self->command( 'setemail', [ $email, $passwd, $id ] );
292 sub pushProfile( $$$$$$ ) {
293 my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
294 $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
297 sub notificationsUser( $$ ) {
298 my( $self, $uid ) = @_;
299 return $self->query( 'notifuser', [ $uid ] );
302 sub getNotifData( $$$ ) {
303 my( $self, $uid, $location ) = @_;
304 my $result = $self->query( 'notifdata', [ $uid, $location ] );
306 my( $recursive, $notification, $way ) = @{$result->[0]};
308 'recursive' => $recursive,
309 'notification' => $notification,
312 return { 'recursive' => 1 };
316 sub submitNotification( $$$$ ) {
317 my( $self, $uid, $location, $data ) = @_;
318 $self->command( 'drop-notif', [ $uid, $location ] );
319 $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
322 sub pushNotifications( $$$$$ ) {
323 my( $self, $location, $history, $priority, $reason ) = @_;
324 $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
325 $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
326 $self->command( 'newtime-mail', [ $priority, $location, $location ] );
327 $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
330 sub notifExists( $$$ ) {
331 my( $self, $user, $location ) = @_;
332 return scalar @{$self->query( 'notif-exists', [ $user, $location, $location ] )};
335 sub mailNotifs( $$ ) {
336 my( $self, $time ) = @_;
337 return $self->query( 'mailout', [ $time ] );
340 sub xmppNotifs( $$ ) {
341 my( $self, $time ) = @_;
342 return $self->query( 'xmppout', [ $time ] );
347 return $self->query( 'time', [] )->[0]->[0];
350 sub dropNotifs( $$ ) {
351 my( $self, $time ) = @_;
352 $self->command( 'dropnotifsmail', [ $time ] );
353 $self->command( 'dropnotifsxmpp', [ $time ] );
356 sub searchName( $$$ ) {
357 my( $self, $search, $prefix ) = @_;
358 return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
359 return $self->query( 'searchname', [ "%$search%" ] );