use Newslash::Model::SlashDB;
+use DateTime;
use DateTime::Format::MySQL;
+use DateTime::Format::ISO8601;
use Data::Dumper;
use DateTime;
-use Newslash::Util::Formatters qw(datetime_to_string);
+
+
+sub key_definition {
+ return {
+ table => "stories",
+ primary => "stoid",
+ unique => [qw(sid)],
+ datetime => [qw(time last_update day_published
+ stuckendtime archive_last_update
+ )],
+ other => [qw(uid dept hits discussion primaryskid
+ tid submitter commentcount hitparade
+ is_archived in_trash
+ qid body_length word_count sponsor
+ stuck stuckpos fakeemail homepage
+ )],
+ aliases => { user_id => "uid",
+ id => "stoid",
+ create_time => time,
+ }
+ };
+}
use constant FACULTIES => { 1000 => [qw(hits hitparade)] };
@_);
}
+##### sub models
+sub text { return shift->new_instance_of('::Stories::Text'); }
+
+
#========================================================================
=head2 select($query_type, $value)
# TODO: give reasonable LIMIT Value...
$limit_clause = "LIMIT 50" if !$limit_clause;
- # show future story?
+ # hide future story?
my @where_clauses;
- if (!$params->{show_future}) {
+ if ($params->{hide_future}) {
push @where_clauses, "stories.time <= NOW()";
}
- # show non-public story?
- if (!$params->{show_nonpublic} && !$params->{public}) {
+ # hide non-public story?
+ if ($params->{public_only}) {
push @where_clauses, "firehose.public != 'no'";
}
$self->_set_tags_from_topics($params->{user}, $stoid, $params->{topics_chosen});
return $stoid;
+}
+sub update2 {
+ my $self = shift;
+ my $params = {@_};
+ return $self->generic_update(params => $params);
}
# check parameters
my $msg = "";
- $msg = "no title" if !$params->{title};
- $msg = "no introtext" if !$params->{introtext} || $params->{intro_text};
- $msg = "no uid" if !$params->{uid} || $params->{user_id};
- $msg = "no topics" if !defined $params->{topics_chosen};
- $msg = "invalid user" if ref($user) ne 'HASH';
+ $msg = "no_title" if !$params->{title};
+ $msg = "no_introtext" if !$params->{introtext} || $params->{intro_text};
+ $msg = "no_topics" if !defined $params->{topics_chosen};
+ $msg = "invalid_user" if !defined $user->{uid};
if (length($params->{title}) > $self->{options}->{Story}->{title_max_byte}) {
$msg = "title too long. max: $self->{options}->{Story}->{title_max_byte} bytes";
# check timestamp. use ISO8601 style timestamp like: 2006-08-14T02:34:56-0600
if ($params->{time}) {
my $rex_timestamp = qr/
- ^(\d+)-(\d+)-(\d+)\D+(\d+):(\d+):(\d+(?:\.\d+)?) # datetime
+ ^(\d+)-(\d+)-(\d+)[^ 0-9]+(\d+):(\d+):(\d+(?:\.\d+)?) # datetime
(?:Z|([+-])(\d+):(\d+))?$ # tz
/xi;
if ($params->{time} =~ $rex_timestamp) {
- $params->{time} = "$1-$2-$3 $4:$5:$6";
+ my $dt = DateTime::Format::ISO8601->parse_datetime($params->{time});
+ $params->{time} = DateTime::Format::MySQL->format_datetime($dt);
}
}
$self->set_error($msg, -1);
return;
}
-
$params->{neverdisplay} ||= 0;
+ $params->{submitter} ||= $user->{uid};
+ $params->{uid} = $user->{uid};
# createStory() deletes topics_chosen, so need to save here.
my $topics_chosen = $params->{topics_chosen};
#warn "set_tag fault..." if !$ret
}
}
- return $stoid;
+ return wantarray ? ($sid, $stoid) : $stoid;
+}
+
+sub create2 {
+ my $self = shift;
+ my $params = {@_};
+ return $self->generic_insert(params => $params);
}
+sub allocate_sid {
+ my ($self, @params) = @_;
+ my $params = {@params};
+ my $dt = $params->{base_datetime} || DateTime->now;
+
+ # create sid from timestamp
+ # my $sid_format = '%02d/%02d/%02d/%02d%0d2%02d';
+ my $sid_format = '%y/%m/%d/%H%M%S';
+ my $sid = $dt->strftime($sid_format);
+
+ # insert blank story with given sid
+ my $dbh = $self->connect_db;
+ my $sql = "INSERT INTO stories (sid) VALUES (?)";
+
+ my $n = 100; # retry 100 times
+ while (--$n) {
+ my $rs = $dbh->do($sql, undef, $sid);
+ if (!defined $rs) {
+ $self->set_error("sid_insert_error", -1);
+ return;
+ }
+ if ($rs) {
+ my $stoid = $dbh->last_insert_id(undef, undef, undef, undef);
+ $self->disconnect_db;
+ return ($sid, $stoid);
+ }
+
+ # allocate failed, so recreate sid
+ $dt->subtract( seconds => 1 );
+ $sid = $dt->strftime($sid_format);
+ }
+ $self->set_error("sid_allocate_failed", -1);
+ $self->disconnect_db;
+ return;
+}
+
+
+# Legacy API
sub createSid {
my ($self, $bogus_sid) = @_;
# yes, this format is correct, don't change it :-)
my $sql = <<"EOSQL";
SELECT related.*,
story_text.title as title2,
- firehose.srcid,
+ firehose.*,
+ stories.*,
topics.*
FROM (
SELECT * FROM related_stories
) AS related
LEFT JOIN story_text ON story_text.stoid = related.rel_stoid
LEFT JOIN firehose ON firehose.id = related.fhid
- LEFT JOIN stories ON stories.stoid = related.rel_stoid
+ LEFT JOIN stories ON stories.sid = related.rel_sid
LEFT JOIN topics ON topics.tid = stories.tid
EOSQL
$self->disconnect_db();
for my $r (@$related) {
+ $r->{create_time} = $r->{time};
$r->{title} = $r->{title2} unless $r->{title};
if ($r->{rel_sid}) {
$r->{type} = "story";
my $error = 0;
my $sql;
- my $dbh = $self->connect_db({AutoCommit => 0,});
+ #my $dbh = $self->connect_db({AutoCommit => 0,});
+ my $dbh = $self->start_transaction;
for my $table (qw(stories story_param story_text story_topics_chosen story_topics_rendered)) {
my $sql = "DELETE FROM $table WHERE stoid = ?";
my $rs = $dbh->do($sql, undef, $stoid);
$error = 1;
}
}
- $dbh->commit;
- $self->disconnect_db;
+ # delete from firehose
+ my $firehose = $self->new_instance_of("Firehose");
+ $firehose->hard_delete("story", $stoid);
+
+ $self->commit;
return !$error;
- # delete firehose item
- # delete firehose_text item
- # delete firehose_topics_rendererd item
# delete globjs
# delete tags