1 package Newslash::Model::Tags;
2 use Newslash::Model::Base -base;
9 my $globj_id = $params->{globj_id};
10 my $uid = $params->{uid} || 1;
11 my $name = $params->{name};
12 my $private = $params->{private} ? "yes" : "no";
13 my $active = defined $params->{active} ? $params->{active} : 1;
14 my $tagname_id = $params->{tagname_id};
21 my $tagname = $self->select(tagname => $name);
23 $tagname_id = $tagname->{tagnameid};
26 $tagname_id = $self->create(tagname => $name);
34 my $inactivated = $active ? "NULL" : "NOW()";
37 (tagnameid, globjid, uid, created_at, inactivated, private, is_active)
39 (?, ?, ?, NOW(), $inactivated, ?, ?)
42 my $dbh = $self->connect_db;
43 my $rs = $dbh->do($sql, undef, $tagname_id, $globj_id, $uid, $private, $active);
48 my $id = $dbh->last_insert_id(undef, undef, undef, undef);
57 if ($params->{tagname}) {
58 my $sql = "INSERT INTO tagnames (tagname) VALUES (?)";
59 my $dbh = $self->connect_db;
60 my $rs = $dbh->do($sql, undef, $params->{tagname});
65 my $id = $dbh->last_insert_id(undef, undef, undef, undef);
76 if ($params->{tagname}) {
77 my $sql = "SELECT * FROM tagnames WHERE tagname = ?";
78 my $dbh = $self->connect_db;
79 my $sth = $dbh->prepare($sql);
80 $sth->execute($params->{tagname});
81 my $rs = $sth->fetchall_arrayref({});
91 # =================== Legacy API ====================
95 my $tags_tagname_regex = "^!?[a-z一-龠ぁ-んァ-ヴー][a-z0-9一-龠ぁ-んァ-ヴー/・]{0,63}\$";
98 my($self, $tagname) = @_;
99 return 0 unless defined($tagname) && length($tagname) > 0;
100 #my $constants = getCurrentStatic();
101 #my $regex = $constants->{tags_tagname_regex};
102 my $regex = $tags_tagname_regex;
103 return($tagname =~ /$regex/);
106 sub getTagnameidFromNameIfExists {
107 my($self, $name) = @_;
108 #my $constants = getCurrentStatic();
109 return 0 if !$self->tagnameSyntaxOK($name);
111 #my $table_cache = "_tagid_cache";
112 #my $table_cache_time = "_tagid_cache_time";
113 #$self->_genericCacheRefresh('tagid', $constants->{tags_cache_expire});
114 #if ($self->{$table_cache_time} && $self->{$table_cache}{$name}) {
115 # return $self->{$table_cache}{$name};
118 #my $mcd = $self->getMCD();
119 #my $mcdkey = "$self->{_mcd_keyprefix}:tagid:" if $mcd;
121 # my $id = $mcd->get("$mcdkey$name");
123 # if ($self->{$table_cache_time}) {
124 # $self->{$table_cache}{$name} = $id;
130 my $db = $self->new_instance_of('LegacyDB');
131 my $name_q = $db->sqlQuote($name);
132 my $id = $db->sqlSelect('tagnameid', 'tagnames',
135 #if ($self->{$table_cache_time}) {
136 # $self->{$table_cache}{$name} = $id;
138 #$mcd->set("$mcdkey$name", $id, $constants->{memcached_exptime_tags}) if $mcd;
143 my($self, $name) = @_;
144 return 0 if !$self->tagnameSyntaxOK($name);
146 my $db = $self->new_instance_of('LegacyDB');
147 my $rows = $db->sqlInsert('tagnames', {
152 # Insert failed, presumably because this tag already
153 # exists. The caller should have checked for this
154 # before attempting to create the tag, but maybe the
155 # reader that was checked didn't have this tag
156 # replicated yet. Pull the information directly
157 # from this writer DB.
158 return $self->getTagnameidFromNameIfExists($name);
160 # The insert succeeded. Return the ID that was just added.
161 return $self->getLastInsertId();
164 sub getTagnameidCreate {
165 my($self, $name) = @_;
166 return 0 if !$self->tagnameSyntaxOK($name);
167 #my $reader = getObject('Slash::Tags', { db_type => 'reader' });
168 #my $id = $reader->getTagnameidFromNameIfExists($name);
169 my $id = $self->getTagnameidFromNameIfExists($name);
171 return $self->createTagname($name);
175 my ($self, $hr, $options) = @_;
176 my $tag = { -created_at => 'NOW()' };
178 #$tag->{uid} = $hr->{uid} || getCurrentUser('uid');
179 $tag->{uid} = $hr->{uid};
181 if ($hr->{tagnameid}) {
182 $tag->{tagnameid} = $hr->{tagnameid};
184 # Need to determine tagnameid from name. We
185 # create the new tag name if necessary.
186 $tag->{tagnameid} = $self->getTagnameidCreate($hr->{name});
188 return 0 if !$options->{tagname_not_required} && !$tag->{tagnameid};
190 if ($hr->{globjid}) {
191 $tag->{globjid} = $hr->{globjid};
193 my $globjs = $self->new_instance_of('Globjids');
194 $tag->{globjid} = $globjs->getGlobjidCreate($hr->{table}, $hr->{id});
196 return 0 if !$tag->{globjid};
198 $tag->{private} = $hr->{private} ? 'yes' : 'no';
203 sub logDeactivatedTags {
204 my($self, $deactivated_tagids) = @_;
205 return 0 if !$deactivated_tagids;
207 my $db = $self->new_instance_of('LegacyDB');
208 for my $tagid (@$deactivated_tagids) {
209 $logged += $db->sqlInsert('tags_deactivated',
210 { tagid => $tagid });
216 my ($self, $hr, $options) = @_;
217 my $tag = $self->_setuptag($hr, { tagname_not_required => !$options->{tagname_required} });
220 my $prior_clause = '';
221 $prior_clause = " AND tagid < $options->{tagid_prior_to}" if $options->{tagid_prior_to};
222 my $where_clause = "uid = $tag->{uid}
223 AND globjid = $tag->{globjid}
224 AND inactivated IS NULL
226 $where_clause .= " AND tagnameid = $tag->{tagnameid}" if $tag->{tagnameid};
227 my $db = $self->new_instance_of('LegacyDB');
228 my $previously_active_tagids = $db->sqlSelectColArrayref('tagid', 'tags', $where_clause);
229 my $count = $db->sqlUpdate('tags', { -inactivated => 'NOW()', -is_active => 'NULL' }, $where_clause);
232 # Logic error, there should never be more than one
233 # tag meeting those criteria.
234 warn scalar(gmtime) . " $count deactivated tags id '$tag->{tagnameid}' for uid=$tag->{uid} on $tag->{globjid}";
237 if ($count && $previously_active_tagids && @$previously_active_tagids) {
238 #my $tagboxdb = getObject('Slash::Tagbox');
239 #$tagboxdb->logDeactivatedTags($previously_active_tagids);
240 $self->logDeactivatedTags($previously_active_tagids);