diff options
-rw-r--r-- | lib/VNDB/DB/Misc.pm | 60 | ||||
-rw-r--r-- | lib/VNDB/DB/Producers.pm | 39 | ||||
-rw-r--r-- | lib/VNDB/Handler/Producers.pm | 39 | ||||
-rw-r--r-- | lib/VNDB/Util/FormHTML.pm | 1 |
4 files changed, 107 insertions, 32 deletions
diff --git a/lib/VNDB/DB/Misc.pm b/lib/VNDB/DB/Misc.pm index 61b1223c..ced47084 100644 --- a/lib/VNDB/DB/Misc.pm +++ b/lib/VNDB/DB/Misc.pm @@ -6,7 +6,7 @@ use warnings; use Exporter 'import'; our @EXPORT = qw| - dbStats + dbStats dbRevisionInsert dbItemInsert |; @@ -24,5 +24,63 @@ sub dbStats { } @_ }; } + +# Inserts a new revision and updates the item to point to this revision +# This function leaves the DB in an inconsistent state, the actual revision +# will have to be inserted directly after calling this function, otherwise +# the commit will fail. +# Arguments: type [0..2], item ID, edit summary +# Returns: local revision, global revision +sub dbRevisionInsert { + my($self, $type, $iid, $editsum) = @_; + + my $table = [qw|vn releases producers|]->[$type]; + + my $c = $self->dbRow(q| + INSERT INTO changes (type, requester, ip, comments, rev) + VALUES (?, ?, ?, ?, ( + SELECT c.rev+1 + FROM changes c + JOIN !s_rev ir ON ir.id = c.id + WHERE ir.!sid = ? + ORDER BY c.id DESC + LIMIT 1 + )) + RETURNING id, rev|, + $type, $self->authInfo->{id}, $self->reqIP, $editsum, + $table, [qw|v r p|]->[$type], $iid + ); + + $self->dbExec(q|UPDATE !s SET latest = ? WHERE id = ?|, $table, $c->{id}, $iid); + + return ($c->{rev}, $c->{id}); +} + + +# Comparable to RevisionInsert, but creates a new item with a corresponding +# change. Same things about inconsistent state, etc. +# Argumments: type [0..2], edit summary +# Returns: item id, global revision +sub dbItemInsert { + my($self, $type, $editsum) = @_; + + my $cid = $self->dbRow(q| + INSERT INTO changes (type, requester, ip, comments) + VALUES (?, ?, ?, ?) + RETURNING id|, + $type, $self->authInfo->{id}, $self->reqIP, $editsum + )->{id}; + + my $iid = $self->dbRow(q| + INSERT INTO !s (latest) + VALUES (?) + RETURNING id|, + [qw|vn releases producers|]->[$type], $cid + )->{id}; + + return ($iid, $cid); +} + + 1; diff --git a/lib/VNDB/DB/Producers.pm b/lib/VNDB/DB/Producers.pm index eee42f48..8b8b29aa 100644 --- a/lib/VNDB/DB/Producers.pm +++ b/lib/VNDB/DB/Producers.pm @@ -5,7 +5,7 @@ use strict; use warnings; use Exporter 'import'; -our @EXPORT = qw|dbProducerGet dbProducerMod dbProducerEdit|; +our @EXPORT = qw|dbProducerGet dbProducerMod dbProducerEdit dbProducerAdd|; # options: results, page, id, search, char, rev @@ -88,32 +88,35 @@ sub dbProducerMod { } -# arguments: id, %options ->( editsum + columns in producers_rev ) +# arguments: id, %options ->( editsum + insert_rev ) # returns: ( local revision, global revision ) sub dbProducerEdit { my($self, $pid, %o) = @_; + my($rev, $cid) = $self->dbRevisionInsert(2, $pid, $o{editsum}); + insert_rev($self, $cid, $pid, \%o); + return ($rev, $cid); +} + - my $c = $self->dbRow(q| - INSERT INTO changes (type, requester, ip, comments, rev) - VALUES (2, ?, ?, ?, ( - SELECT c.rev+1 - FROM changes c - JOIN producers_rev pr ON pr.id = c.id - WHERE pr.pid = ? - ORDER BY c.id DESC - LIMIT 1 - )) - RETURNING id, rev|, - $self->authInfo->{id}, $self->reqIP, $o{editsum}, $pid); +# arguments: %options ->( editsum + insert_rev ) +# returns: ( item id, global revision ) +sub dbProducerAdd { + my($self, %o) = @_; + my($pid, $cid) = $self->dbItemInsert(2, $o{editsum}); + insert_rev($self, $cid, $pid, \%o); + return ($pid, $cid); +} + +# helper function, inserts a producer revision +# Arguments: global revision, item id, { columns in producers_rev } +sub insert_rev { + my($self, $cid, $pid, $o) = @_; $self->dbExec(q| INSERT INTO producers_rev (id, pid, name, original, website, type, lang, "desc") VALUES (!l)|, - [ $c->{id}, $pid, @o{qw| name original website type lang desc|} ] + [ $cid, $pid, @$o{qw| name original website type lang desc|} ] ); - - $self->dbExec(q|UPDATE producers SET latest = ? WHERE id = ?|, $c->{id}, $pid); - return ($c->{rev}, $c->{id}); } diff --git a/lib/VNDB/Handler/Producers.pm b/lib/VNDB/Handler/Producers.pm index e0097b40..717bef74 100644 --- a/lib/VNDB/Handler/Producers.pm +++ b/lib/VNDB/Handler/Producers.pm @@ -9,7 +9,7 @@ use VNDB::Func; YAWF::register( qr{p([1-9]\d*)(?:\.([1-9]\d*))?} => \&page, - qr{p([1-9]\d*)/edit} => \&edit, + qr{p(?:([1-9]\d*)/edit|/new)} => \&edit, qr{p([1-9]\d*)/(lock|hide)} => \&mod, ); @@ -62,15 +62,18 @@ sub page { } +# pid as argument = edit producer +# no arguments = add new producer sub edit { my($self, $pid) = @_; - my $p = $self->dbProducerGet(id => $pid)->[0]; - return 404 if !$p->{id}; + my $p = $pid && $self->dbProducerGet(id => $pid)->[0]; + return 404 if $pid && !$p->{id}; - return $self->htmlDenied if !$self->authCan('edit') || $p->{locked} && !$self->authCan('lock') || $p->{hidden} && !$self->authCan('del'); + return $self->htmlDenied if !$self->authCan('edit') + || $pid && ($p->{locked} && !$self->authCan('lock') || $p->{hidden} && !$self->authCan('del')); - my %b4 = map { $_ => $p->{$_} } qw|type name original lang website desc|; + my %b4 = !$pid ? () : map { $_ => $p->{$_} } qw|type name original lang website desc|; my $frm; if($self->reqMethod eq 'POST') { @@ -85,9 +88,14 @@ sub edit { ); if(!$frm->{_err}) { return $self->resRedirect("/p$pid", 'post') - if !grep $frm->{$_} ne $b4{$_}, keys %b4; + if $pid && !grep $frm->{$_} ne $b4{$_}, keys %b4; - my($rev) = $self->dbProducerEdit($pid, %$frm); + my $rev = 1; + if($pid) { + ($rev) = $self->dbProducerEdit($pid, %$frm); + } else { + ($pid) = $self->dbProducerAdd(%$frm); + } $self->multiCmd("ircnotify p$pid.$rev"); @@ -96,21 +104,26 @@ sub edit { } !defined $frm->{$_} && ($frm->{$_} = $b4{$_}) for keys %b4; + $frm->{lang} = 'ja' if !$pid && !defined $frm->{lang}; - $self->htmlHeader(title => 'Edit '.$p->{name}); - $self->htmlMainTabs('p', $p, 'edit'); + $self->htmlHeader(title => $pid ? 'Edit '.$p->{name} : 'Add new producer'); + $self->htmlMainTabs('p', $p, 'edit') if $pid; div class => 'mainbox'; - h1 'Edit '.$p->{name}; + h1 $pid ? 'Edit '.$p->{name} : 'Add new producer'; div class => 'notice'; h2 'Before editing:'; ul; li; lit 'Read the <a href="/d4">guidelines</a>!'; end; - li; lit qq|Check for any existing discussions on the <a href="/t/p$pid">discussion board</a>|; end; - li; lit qq|Browse the <a href="/p$pid/hist">edit history</a> for any recent changes related to what you want to change.|; end; + if($pid) { + li; lit qq|Check for any existing discussions on the <a href="/t/p$pid">discussion board</a>|; end; + li; lit qq|Browse the <a href="/p$pid/hist">edit history</a> for any recent changes related to what you want to change.|; end; + } else { + li; lit qq|<a href="/p">Search the database</a> to see if we already have information about this producer|; end; + } end; end; end; - $self->htmlForm({ frm => $frm, action => "/p$pid/edit", editsum => 1 }, "General info" => [ + $self->htmlForm({ frm => $frm, action => $pid ? "/p$pid/edit" : '/p/new', editsum => 1 }, "General info" => [ [ select => name => 'Type', short => 'type', options => [ map [ $_, $self->{producer_types}{$_} ], sort keys %{$self->{producer_types}} ] ], [ input => name => 'Name (romaji)', short => 'name' ], diff --git a/lib/VNDB/Util/FormHTML.pm b/lib/VNDB/Util/FormHTML.pm index 2b2a488c..558abda1 100644 --- a/lib/VNDB/Util/FormHTML.pm +++ b/lib/VNDB/Util/FormHTML.pm @@ -19,6 +19,7 @@ my %formerr_names = ( name => 'Name', original => 'Original', lang => 'Language', + website => 'Website', desc => 'Description', editsum => 'Edit summary', ); |