1 package Newslash::Model::Stories;
2 use Newslash::Model::Base -base;
4 use Newslash::Model::SlashDB;
6 use Newslash::Model::Stories::Text;
7 use Newslash::Model::Stories::RelatedStories;
10 use DateTime::Format::MySQL;
11 use DateTime::Format::ISO8601;
12 use List::Util qw(any all);
23 datetime => [qw(time last_update day_published
24 stuckendtime archive_last_update
26 other => [qw(uid dept hits discussion primaryskid
27 tid submitter commentcount hitparade
29 qid body_length word_count sponsor
30 stuck stuckpos fakeemail homepage
32 aliases => { user_id => "uid",
39 use constant FACULTIES => { 1000 => [qw(hits hitparade)] };
44 SELECT uid, COUNT(stoid) AS count
46 WHERE stories.time <= NOW()
50 my $dbh = $self->connect_db;
51 my $sth = $dbh->prepare($sql);
53 my $rs = $sth->fetchall_hashref("uid");
55 $self->set_error("select failed", $dbh->errstr, $dbh->err);
67 my $join = 'LEFT JOIN firehose ON (stories.stoid = firehose.srcid AND firehose.type = "story")';
70 if ($params->{hide_future}) {
71 push @clauses, 'stories.time < NOW()';
73 if ($params->{public_only}) {
74 push @clauses, 'firehose.public != "no"';
76 my $where = join " AND ", @clauses;
78 return $self->generic_count(table => "stories",
87 sub text { return shift->_get_model('::Stories::Text'); }
88 sub related_stories { return shift->_get_model('::Stories::RelatedStories'); }
91 my ($self, $class) = @_;
92 $self->{sub_models} ||= {};
93 if (!$self->{sub_models}->{$class}) {
94 $self->{sub_models}->{$class} = $self->new_instance_of($class);
97 if ($self->transaction_mode && !$self->{sub_models}->{$class}->transaction_mode) {
98 $self->{sub_models}->{$class}->use_transaction($self->{_tr_dbh});
101 return $self->{sub_models}->{$class};
104 #========================================================================
106 =head2 select($query_type, $value)
118 query key, "sid" or "stoid"
128 HASH of story contents
139 my $unique_keys = { id => "stories.stoid",
140 story_id => "stories.stoid",
141 stoid => "stories.stoid",
142 sid => "stories.sid",
144 my $keys = { user_id => "stories.uid",
145 uid => "stories.uid",
146 topic_id => "stories.tid",
147 tid => "stories.tid",
148 discussion_id => "stories.discussion",
149 commentcount => "stories.commentcount",
150 hits => "stories.hits",
151 submitter => "stories.submitter",
152 create_time => "stories.time",
153 update_time => "stories.last_update",
154 public => "firehose.public",
156 my $datetime_keys = { create_time => "stories.time",
157 update_time => "stories.last_update",
159 my $timestamp = "stories.time";
161 my ($where_clause, $where_values, $unique) = $self->build_where_clause(unique_keys => $unique_keys,
163 datetime_keys => $datetime_keys,
164 timestamp => $timestamp,
166 my ($limit_clause, $limit_values) = $self->build_limit_clause(params => $params);
167 my ($orderby_clause, $orderby_values) = $self->build_order_by_clause(keys => $keys,
170 # TODO: give reasonable LIMIT Value...
171 $limit_clause = "LIMIT 50" if !$limit_clause;
175 if ($params->{hide_future}) {
176 push @where_clauses, "stories.time <= NOW()";
179 # hide non-public story?
180 if ($params->{public_only}) {
181 push @where_clauses, "firehose.public != 'no'";
184 # exclude "preview" item
185 push @where_clauses, "globj_types.maintable != 'preview'";
187 if (@where_clauses) {
189 $where_clause = $where_clause . " AND ";
192 $where_clause = "WHERE ";
194 $where_clause = $where_clause . join(" AND ", @where_clauses);
198 push @attrs, @$where_values, @$limit_values, @$orderby_values;
200 my $dbh = $self->connect_db;
204 users.nickname as author,
212 discussions.type AS discussion_type,
213 discussions.commentcount AS comment_count
216 ON stories.stoid = story_text.stoid
218 ON stories.uid = users.uid
220 ON (stories.stoid = firehose.srcid AND firehose.type = "story")
221 LEFT JOIN discussions
222 ON firehose.discussion = discussions.id
224 ON firehose.globjid = globjs.globjid
225 LEFT JOIN globj_types
226 ON globjs.gtid = globj_types.gtid
232 $self->_last_query($sql, \@attrs);
234 my $sth = $dbh->prepare($sql);
235 $sth->execute(@attrs);
236 my $stories = $sth->fetchall_arrayref({});
239 $self->disconnect_db();
242 if (@$stories == 0) {
243 $self->disconnect_db();
244 return $unique ? undef : [];
249 SELECT tags.*, tagnames.tagname, target.stoid
250 FROM (SELECT stories.stoid FROM stories
252 ON (stories.stoid = firehose.srcid AND firehose.type = "story")
254 ON firehose.globjid = globjs.globjid
255 LEFT JOIN globj_types
256 ON globjs.gtid = globj_types.gtid
257 $where_clause $orderby_clause $limit_clause) AS target
259 ON target.stoid = globjs.target_id
260 LEFT JOIN globj_types
261 ON globjs.gtid = globj_types.gtid
263 ON globjs.globjid = tags.globjid
265 ON tags.tagnameid = tagnames.tagnameid
266 WHERE globjs.gtid = 1
269 $sth = $dbh->prepare($sql);
270 $sth->execute(@attrs);
271 my $tags_table = $sth->fetchall_arrayref({});
275 SELECT story_topics_rendered.*, story_topics_chosen.weight, topics.*
276 FROM (SELECT stories.stoid FROM stories
278 ON (stories.stoid = firehose.srcid AND firehose.type = "story")
280 ON firehose.globjid = globjs.globjid
281 LEFT JOIN globj_types
282 ON globjs.gtid = globj_types.gtid
283 $where_clause $orderby_clause $limit_clause) AS target
284 LEFT JOIN story_topics_rendered
285 ON target.stoid = story_topics_rendered.stoid
286 LEFT JOIN story_topics_chosen
287 ON story_topics_rendered.stoid = story_topics_chosen.stoid
288 AND story_topics_rendered.tid = story_topics_chosen.tid
290 ON story_topics_rendered.tid = topics.tid
293 $sth = $dbh->prepare($sql);
294 $sth->execute(@attrs);
295 my $topics_table = $sth->fetchall_arrayref({});
300 FROM (SELECT stories.stoid FROM stories
302 ON (stories.stoid = firehose.srcid AND firehose.type = "story")
304 ON firehose.globjid = globjs.globjid
305 LEFT JOIN globj_types
306 ON globjs.gtid = globj_types.gtid
307 $where_clause $orderby_clause $limit_clause) AS target
308 LEFT JOIN story_param
309 ON target.stoid = story_param.stoid
312 $sth = $dbh->prepare($sql);
313 $sth->execute(@attrs);
314 my $params_table = $sth->fetchall_arrayref({});
317 $self->disconnect_db();
321 for my $tag (@$tags_table) {
322 my $stoid = $tag->{stoid};
323 if (!$tags->{$stoid}) {
324 $tags->{$stoid} = [];
326 push @{$tags->{$stoid}}, $tag;
330 for my $topic (@$topics_table) {
331 my $stoid = $topic->{stoid};
332 if (!$topics->{$stoid}) {
333 $topics->{$stoid} = [];
335 push @{$topics->{$stoid}}, $topic;
339 for my $param (@$params_table) {
340 my $stoid = $param->{stoid};
341 if (!$params->{$stoid}) {
342 $params->{$stoid} = [];
344 push @{$params->{$stoid}}, $param;
347 for my $story (@$stories) {
348 my $stoid = $story->{stoid};
349 $story->{tags} = $tags->{$stoid} || [];
350 $story->{topics} = $topics->{$stoid} || [];
351 if ($params->{$stoid}) {
352 for my $param (@{$params->{$stoid}}) {
353 $story->{$param->{name}} = $param->{value};
356 $self->_generalize($story, $params);
359 return $stories->[0] if $unique;
363 sub _check_and_regularize_params {
364 my ($self, $params) = @_;
367 if (defined $params->{title}) {
368 if (length($params->{title}) > $self->{options}->{Story}->{title_max_byte}) {
369 $msg = "title too long. max: $self->{options}->{Story}->{title_max_byte} bytes";
370 $self->set_error($msg, -1);
375 $params->{commentstatus} = $params->{commentstatus} || $params->{comment_status} || "enabled";
376 if (defined $params->{commentstatus}) {
377 if (!grep /\A$params->{commentstatus}\z/, qw(disabled
384 $msg = "invalid comment_status";
385 $self->set_error($msg, -1);
390 # check timestamp. use ISO8601 style timestamp like: 2006-08-14T02:34:56-0600
391 if ($params->{time}) {
392 my $rex_timestamp = qr/
393 ^(\d+)-(\d+)-(\d+)\D+(\d+):(\d+):(\d+(?:\.\d+)?) # datetime
394 (?:Z|([+-])(\d+):(\d+))?$ # tz
396 if ($params->{time} =~ $rex_timestamp) {
397 $params->{time} = "$1-$2-$3 $4:$5:$6";
404 sub _set_tags_from_topics {
405 my ($self, $user, $stoid, $topics) = @_;
410 my $globjs = $self->new_instance_of("Newslash::Model::Globjs");
411 my $globj_id = $globjs->getGlobjidFromTargetIfExists("stories", $stoid);
414 my $tags = $self->new_instance_of("Tags");
415 for my $tid (keys %$topics) {
416 my $ret = $tags->set_tag(uid => $user->{uid} || $user->{user_id},
418 globj_id => $globj_id,
421 #warn "set_tag fault..." if !$ret
429 this implementation uses old slash's updateStory($sid, $data),
430 $sid is takable sid or stoid.
435 #my ($self, $params, $user, $extra_params, $opts) = @_;
438 my $user = $params->{user};
442 my $stoid = $params->{stoid} || $params->{story_id} || $params->{id};
443 my $sid = $params->{sid};
446 $story = $self->select(stoid => $stoid);
449 $story = $self->select(sid => $sid);
453 $sid = $story->{sid};
454 $stoid = $story->{stoid};
458 $self->last_error("no story given");
463 return if !$self->_check_and_regularize_params($params);
465 my $slash_db = Newslash::Model::SlashDB->new($self->{options});
467 my $add_related = $params->{add_related};
468 delete $params->{add_related} if $add_related;
469 my $add_tags = $params->{add_tags};
470 delete $params->{add_tags} if $add_tags;
473 if (!$params->{primaryskid}) {
474 $params->{primaryskid} = 1; # mainpage
478 my $topic_nexus = $self->new_instance_of('Topics')->get_topic_nexus();
479 if (!$params->{tid} || all { $params->{tid} != $_->{tid} } @$topic_nexus) {
480 $params->{tid} = 49; # mainpage
483 $stoid = $slash_db->updateStory($stoid, $params);
486 $self->_set_tags_from_topics($params->{user}, $stoid, $params->{topics_chosen});
490 my $tags = $self->new_instance_of("Tags");
491 my $globjs = $self->new_instance_of("Newslash::Model::Globjs");
492 my $globj_id = $globjs->getGlobjidFromTargetIfExists("stories", $params->{stoid});
493 for my $tag (@$add_tags) {
494 my $rs = $tags->add(uid => $user->{uid} || $user->{user_id},
495 globj_id => $globj_id,
498 $self->logger->warn("Stories::update: tag $tag set failed: " . $tags->last_error);
503 # insert related story
505 for my $related_sid (@$add_related) {
506 my $rs = $self->add_related_story(sid => $sid,
507 related_sid => $related_sid);
509 $self->logger->warn("Stories::update: insert related story $related_sid to $sid failed: " . $self->last_error);
512 $rs = $self->add_related_story(sid => $related_sid,
513 related_sid => $sid);
515 $self->logger->warn("Stories::update: insert related story $sid to $related_sid failed: " . $self->last_error);
520 return wantarray ? ($sid, $stoid) : $stoid;
526 return $self->generic_update(params => $params);
530 =head2 create(\%params, $uid)
562 #my ($self, $params, $user, $extra_params, $opts) = @_;
564 return if $self->check_readonly;
567 my $user = $params->{user};
571 $msg = "no_title" if !$params->{title};
572 $msg = "no_introtext" if !$params->{introtext} && !$params->{intro_text};
573 $msg = "no_topics" if !defined $params->{topics_chosen};
574 $msg = "invalid_user" if !defined $user->{uid};
576 if (length($params->{title}) > $self->{options}->{Story}->{title_max_byte}) {
577 $msg = "title too long. max: $self->{options}->{Story}->{title_max_byte} bytes";
580 $params->{commentstatus} = $params->{commentstatus} || $params->{comment_status} || "enabled";
581 if (!grep /\A$params->{commentstatus}\z/, qw(disabled enabled friends_only friends_fof_only no_foe no_foe_eof logged_in)) {
582 $msg = "invalid comment_status";
585 # check timestamp. use ISO8601 style timestamp like: 2006-08-14T02:34:56-0600
586 if ($params->{time}) {
587 my $rex_timestamp = qr/
588 ^(\d+)-(\d+)-(\d+)[^ 0-9]+(\d+):(\d+):(\d+(?:\.\d+)?) # datetime
589 (?:Z|([+-])(\d+):(\d+))?$ # tz
591 if ($params->{time} =~ $rex_timestamp) {
592 my $dt = DateTime::Format::ISO8601->parse_datetime($params->{time});
593 $params->{time} = DateTime::Format::MySQL->format_datetime($dt);
597 # check parameters finish
598 if (length($msg) > 0) {
599 $self->set_error($msg, -1);
602 $params->{neverdisplay} ||= 0;
603 $params->{uid} = $user->{uid};
606 if ($params->{submission_id}) {
607 my $submission = $self->new_instance_of("Submissions")->select(id => $params->{submission_id});
609 $params->{submitter} = $submission->{uid};
610 $params->{fhid} = $submission->{fhid};
613 $self->logger->error("Stories::create - invalid submission_id: $params->{submission_id}");
616 if ($params->{journal_id}) {
617 my $journal = $self->new_instance_if("Journals")->select(id => $params->{journal_id});
619 $params->{submitter} = $journal->{uid};
620 $params->{fhid} = $journal->{fhid};
623 $self->logger->error("Stories::create - invalid journal_id: $params->{journal_id}");
626 $params->{submitter} ||= $user->{uid};
628 # createStory() deletes topics_chosen, so need to save here.
629 my $topics_chosen = $params->{topics_chosen};
632 my $topic_nexus = $self->new_instance_of('Topics')->get_topic_nexus();
633 if (!$params->{tid} || all { $params->{tid} != $_->{tid} } @$topic_nexus) {
634 $params->{tid} = 49; # mainpage
638 if (!$params->{primaryskid}) {
639 $params->{primaryskid} = 1; # mainpage
642 my $add_related = $params->{add_related};
643 delete $params->{add_related} if $add_related;
644 my $add_tags = $params->{add_tags};
645 delete $params->{add_tags} if $add_tags;
647 my $slash_db = Newslash::Model::SlashDB->new($self->{options});
649 if ($params->{update}) {
650 $stoid = $params->{stoid} || $params->{story_id} || $params->{id};
651 $sid = $slash_db->updateStory($stoid, $params);
652 $self->set_error("updateStory failed");
656 ($sid, $stoid) = $slash_db->createStory($params);
659 my $globjs = $self->new_instance_of("Newslash::Model::Globjs");
660 my $globj_id = $globjs->getGlobjidFromTargetIfExists("stories", $params->{stoid});
663 my $tags = $self->new_instance_of("Tags");
665 for my $tid (keys %$topics_chosen) {
666 my $rs = $tags->add(uid => $user->{uid} || $user->{user_id},
668 globj_id => $globj_id,
672 $self->logger->warn("Stories::create: tag $tid set failed: " . $tags->last_error);
679 for my $tag (@$add_tags) {
680 my $rs = $tags->add(uid => $user->{uid} || $user->{user_id},
681 globj_id => $globj_id,
684 $self->logger->warn("Stories::create: tag $tag set failed: " . $tags->last_error);
689 # insert related story
691 for my $related_sid (@$add_related) {
692 my $rs = $self->add_related_story(sid => $sid,
693 related_sid => $related_sid);
695 $self->logger->warn("insert related story failed: " . $self->last_error);
698 $rs = $self->add_related_story(sid => $related_sid,
699 related_sid => $sid);
701 $self->logger->warn("Stories::create: insert related story failed: " . $self->last_error);
706 # add firehose item to related_story
707 if ($params->{submission_id}) {
708 my $submissions = $self->new_instance_of("Submissions");
709 my $submission = $submissions->select(id => $params->{submission_id});
711 my $rs = $self->add_related_firehose(sid => $sid,
712 fhid => $submission->{fhid});
714 $self->logger->error("Stories::create - insert related firehose failed: " . $self->last_error);
717 # change submission status
718 if (!$submissions->update(id => $submission->{subid}, del => 2)) {
719 $self->logger->error("Stories::create - cannot change submission status");
723 $self->logger->error("Stories::create - invalid submission_id: $params->{submission_id}");
729 return wantarray ? ($sid, $stoid) : $stoid;
735 return $self->generic_insert(params => $params);
739 my ($self, @params) = @_;
740 my $params = {@params};
741 my $dt = $params->{base_datetime} || DateTime->now;
743 # create sid from timestamp
744 # my $sid_format = '%02d/%02d/%02d/%02d%0d2%02d';
745 my $sid_format = '%y/%m/%d/%H%M%S';
746 my $sid = $dt->strftime($sid_format);
748 # insert blank story with given sid
749 my $dbh = $self->connect_db;
750 my $sql = "INSERT INTO stories (sid) VALUES (?)";
752 my $n = 100; # retry 100 times
754 my $rs = $dbh->do($sql, undef, $sid);
756 $self->set_error("sid_insert_error", -1);
760 my $stoid = $dbh->last_insert_id(undef, undef, undef, undef);
761 $self->disconnect_db;
762 return ($sid, $stoid);
765 # allocate failed, so recreate sid
766 $dt->subtract( seconds => 1 );
767 $sid = $dt->strftime($sid_format);
769 $self->set_error("sid_allocate_failed", -1);
770 $self->disconnect_db;
777 my ($self, $bogus_sid) = @_;
778 # yes, this format is correct, don't change it :-)
779 my $sidformat = '%02d/%02d/%02d/%02d%0d2%02d';
780 # Create a sid based on the current time.
782 my $start_time = time;
784 # If we were called being told that there's at
785 # least one sid that is invalid (already taken),
786 # then look backwards in time until we find it,
787 # then go one second further.
791 @lt = localtime($start_time);
792 $lt[5] %= 100; $lt[4]++; # year and month
793 last if $bogus_sid eq sprintf($sidformat, @lt[reverse 0..5]);
796 # Found the bogus sid by looking
797 # backwards. Go one second further.
800 # Something's wrong. Skip ahead in
801 # time instead of back (not sure what
803 $start_time = time + 1;
806 @lt = localtime($start_time);
807 $lt[5] %= 100; $lt[4]++; # year and month
808 return sprintf($sidformat, @lt[reverse 0..5]);
819 =head2 get_related_items($stoid)
837 ARRAY of related links
843 sub get_related_items {
846 my $stoid = $params->{stoid} || $params->{story_id} || $params->{id};
849 my $dbh = $self->connect_db;
853 story_text.title as title2,
858 SELECT * FROM related_stories
860 ORDER BY ordernum ASC
862 LEFT JOIN story_text ON story_text.stoid = related.rel_stoid
863 LEFT JOIN firehose ON firehose.id = related.fhid
864 LEFT JOIN stories ON stories.sid = related.rel_sid
865 LEFT JOIN topics ON topics.tid = stories.tid
868 my $sth = $dbh->prepare($sql);
869 $sth->execute($stoid);
870 my $related = $sth->fetchall_arrayref({});
871 $self->disconnect_db();
873 for my $r (@$related) {
874 $r->{create_time} = $r->{time};
875 $r->{title} = $r->{title2} unless $r->{title};
877 $r->{type} = "story";
878 $r->{key_id} = $r->{rel_sid};
880 $r->{type} = "submission";
881 $r->{key_id} = $r->{srcid};
883 $r->{primary_topic} = {};
884 $r->{primary_topic}->{tid} = $r->{tid};
885 for my $k (qw{keyword textname series image width height
886 submittable searchable storypickable usesprite}) {
888 $r->{primary_topic}->{$k} = $r->{$k};
896 sub add_related_story {
900 my $story = $args->{story};
902 if ($args->{stoid}) {
903 $story = $self->select(stoid => $args->{stoid});
904 } elsif ($args->{sid}) {
905 $story = $self->select(sid => $args->{sid});
909 if (!$story || !$story->{stoid} || !$story->{sid}) {
910 $self->last_error("cannot get target story");
914 my $related = $args->{related};
916 if ($args->{related_stoid}) {
917 $related = $self->select(stoid => $args->{related_stoid});
918 } elsif ($args->{related_sid}) {
919 $related = $self->select(sid => $args->{related_sid});
923 if (!$related || !$related->{stoid} || !$related->{sid} ) {
924 $self->last_error("cannot get related story");
928 # get related item count
929 my $dbh = $self->start_transaction;
930 my $sql = "SELECT rel_stoid FROM related_stories WHERE stoid = ?";
931 my $stoids = $dbh->do($sql, undef, $story->{stoid});
932 $stoids = [] if ref($stoids) ne "ARRAY";
934 # check if related story is already set
935 if (any { $_ eq $related->{stoid} } @$stoids) {
941 my $count = @$stoids;
942 my $rs = $self->related_stories->insert(stoid => $story->{stoid},
943 rel_stoid => $related->{stoid},
944 rel_sid => $related->{sid},
945 ordernum => $count + 1);
948 $self->last_error("insert failed: " . $self->related_stories->last_error);
956 sub add_related_firehose {
960 my $story = $args->{story};
962 if ($args->{stoid}) {
963 $story = $self->select(stoid => $args->{stoid});
964 } elsif ($args->{sid}) {
965 $story = $self->select(sid => $args->{sid});
969 if (!$story || !$story->{stoid} || !$story->{sid}) {
970 $self->last_error("cannot get target story");
974 my $fh_item = $args->{firehose_item};
977 $fh_item = $self->new_instance_of("Firehose")->select(id => $args->{fhid});
982 $self->last_error("cannot get related firehose item");
986 # get related item count
987 my $dbh = $self->start_transaction;
988 my $sql = "SELECT rel_stoid FROM related_stories WHERE stoid = ?";
989 my $stoids = $dbh->do($sql, undef, $story->{stoid});
990 $stoids = [] if ref($stoids) ne "ARRAY";
993 my $count = @$stoids;
994 my $rs = $self->related_stories->insert(stoid => $story->{stoid},
997 title => "Firehose: $fh_item->{title}",
998 fhid => $fh_item->{id},
999 ordernum => $count + 1);
1002 $self->last_error("insert failed: " . $self->related_stories->last_error);
1011 =head2 parameters($stoid)
1013 get story parameters.
1036 my ($self, $stoid) = @_;
1038 my $dbh = $self->connect_db;
1040 my $sql = <<"EOSQL";
1041 SELECT * FROM story_param WHERE stoid = ?
1044 my $sth = $dbh->prepare($sql);
1045 $sth->execute($stoid);
1046 my $params = $sth->fetchall_hashref('name');
1049 # get next/prev story info
1050 if ($params->{next_stoid} || $params->{prev_stoid}) {
1052 SELECT stories.*, story_text.* FROM stories JOIN story_text ON stories.stoid = story_text.stoid WHERE stories.stoid = ? OR stories.stoid = ?
1054 $sth = $dbh->prepare($sql);
1055 my $next = $params->{next_stoid}->{value} || 0;
1056 my $prev = $params->{prev_stoid}->{value} || 0;
1057 $sth->execute($next, $prev);
1058 my $sids = $sth->fetchall_hashref('stoid');
1059 $params->{next_stoid}->{story} = $sids->{$next} if $sids->{$next};
1060 $params->{prev_stoid}->{story} = $sids->{$prev} if $sids->{$prev};
1064 $self->disconnect_db();
1068 #========================================================================
1070 =head2 set_dirty($key, $id)
1072 set writestatus dirty for the story.
1099 my ($self, $key, $id) = @_;
1100 return if $self->check_readonly;
1103 if ($key eq 'stoid') {
1110 my $dbh = $self->connect_db;
1111 my $sql = <<"EOSQL";
1112 INSERT INTO story_dirty (stoid) VALUES (?)
1115 my $rs = $dbh->do($sql, undef, $stoid);
1123 my ($self, $story, $params) = @_;
1127 $story->{id} = $story->{stoid};
1128 $story->{story_id} = $story->{stoid};
1129 $story->{create_time} = $story->{time};
1130 $story->{update_time} = $story->{last_update};
1132 for my $t (@{$story->{topics}}) {
1133 if ($t->{tid} && $t->{tid} == $story->{tid}) {
1134 $story->{primary_topic} = $t;
1138 $story->{content_type} = "story";
1139 $story->{intro_text} = $story->{introtext};
1140 $story->{bodytext} ||= "";
1141 $story->{body_text} = $story->{bodytext};
1142 if ($story->{body_text}) {
1143 $story->{full_text} = join("\n", $story->{intro_text}, $story->{body_text});
1146 $story->{full_text} = $story->{intro_text};
1148 $story->{fulltext} = $story->{full_text};
1150 $story->{discussion_id} = $story->{discussion};
1152 # no public flag given, public is 'yes'
1153 $story->{public} = 'yes' if !$story->{public};
1157 # delete story from database
1158 # this method is for test purpose only.
1161 return if $self->check_readonly;
1164 my $stoid = $params->{story_id};
1170 #my $dbh = $self->connect_db({AutoCommit => 0,});
1171 my $dbh = $self->start_transaction;
1172 for my $table (qw(stories story_param story_text story_topics_chosen story_topics_rendered)) {
1173 my $sql = "DELETE FROM $table WHERE stoid = ?";
1174 my $rs = $dbh->do($sql, undef, $stoid);
1175 if (!defined $rs || $rs == 0) {
1176 Mojo::Log->new->warn("DELETE FROM $table failed. stoid is $stoid.");
1181 # delete from firehose
1182 my $firehose = $self->new_instance_of("Firehose");
1183 $firehose->hard_delete("story", $stoid);