OSDN Git Service

move unused (deprecated) files
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Model / Stories.pm
index 4615e7d..99ea3cd 100644 (file)
@@ -3,11 +3,34 @@ use Newslash::Model::Base -base;
 
 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)] };
 
@@ -23,6 +46,10 @@ sub count {
                                 @_);
 }
 
+##### sub models
+sub text { return shift->new_instance_of('::Stories::Text'); }
+
+
 #========================================================================
 
 =head2 select($query_type, $value)
@@ -92,14 +119,14 @@ sub select {
     # 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'";
     }
 
@@ -340,7 +367,12 @@ sub update {
 
     $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);
 }
 
 
@@ -385,11 +417,10 @@ sub create {
 
     # 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";
@@ -403,11 +434,12 @@ sub create {
     # 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);
         }
     }
 
@@ -416,8 +448,9 @@ sub create {
         $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};
@@ -448,9 +481,53 @@ sub create {
             #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 :-)
@@ -529,7 +606,8 @@ sub get_related_items {
     my $sql = <<"EOSQL";
 SELECT related.*, 
        story_text.title as title2,
-       firehose.srcid,
+       firehose.*,
+       stories.*,
        topics.*
   FROM (
     SELECT * FROM related_stories
@@ -538,7 +616,7 @@ SELECT related.*,
     ) 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
 
@@ -548,6 +626,7 @@ 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";
@@ -728,7 +807,8 @@ sub hard_delete {
     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);
@@ -737,14 +817,14 @@ sub hard_delete {
             $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