| 1 | package AxKit::App::TABOO::Data::Plurals::Articles; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | use AxKit::App::TABOO::Data; | ||
| 8 | use AxKit::App::TABOO::Data::Article; | ||
| 9 | use AxKit::App::TABOO::Data::Plurals; | ||
| 10 | |||
| 11 | |||
| 12 | use vars qw/@ISA/; | ||
| 13 | @ISA = qw(AxKit::App::TABOO::Data::Plurals); | ||
| 14 | |||
| 15 | use DBI; | ||
| 16 | use Exception::Class::DBI; | ||
| 17 | |||
| 18 | |||
| 19 | our $VERSION = '0.2'; | ||
| 20 | |||
| 21 | AxKit::App::TABOO::Data::Plurals::Articles->dbtable("articles"); | ||
| 22 | AxKit::App::TABOO::Data::Plurals::Articles->dbfrom("articles JOIN languages ON (languages.ID = articles.lang_ID) JOIN mediatypes ON (mediatypes.ID = articles.format_ID)"); | ||
| 23 | AxKit::App::TABOO::Data::Plurals::Articles->dbprimkey("filename"); | ||
| 24 | AxKit::App::TABOO::Data::Plurals::Articles->elementorder("filename, lang, primcat, seccat, freesubject, editorok, authorok, title, description, AUTHORS, date, publisher, type, format, coverage, rights"); | ||
| 25 | |||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | AxKit::App::TABOO::Data::Plurals::Articles - Data objects to handle multiple Articles in TABOO | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Often, you want to retrieve many different articles from the data | ||
| 34 | store, for example all belonging to a certain category. This is a | ||
| 35 | typical situation where this class shoule be used. | ||
| 36 | |||
| 37 | =head2 Methods | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item C<new(@dbconnectargs)> | ||
| 42 | |||
| 43 | The constructor. Nothing special. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $that = shift; | ||
| 49 | my $class = ref($that) || $that; | ||
| 50 | my $self = { | ||
| 51 | ENTRIES => [], # Internally, some methods finds it useful that the entries are stored in a array of this name. | ||
| 52 | ARTICLE_IDS => [], # This field gets the id numbers of the articles, and is only here. | ||
| 53 | DBCONNECTARGS => \@_, | ||
| 54 | XMLELEMENT => undef, | ||
| 55 | XMLPREFIX => undef, | ||
| 56 | XMLNS => undef, | ||
| 57 | }; | ||
| 58 | bless($self, $class); | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | =item C<load(what =E<gt> fields, limit =E<gt> {key =E<gt> value, [...]}, orderby =E<gt> fields, entries =E<gt> number)> | ||
| 64 | |||
| 65 | This load method can be used to retrieve a number of entries from a | ||
| 66 | data store. It can use named parameters, but has a unique other | ||
| 67 | possibility too, see below. In named parameters mode, the first | ||
| 68 | C<what> is used to determine which fields to retrieve. It is a string | ||
| 69 | consisting of a commaseparated list of fields, as specified in the | ||
| 70 | data store. The C<limit> argument is to be used to determine which | ||
| 71 | records to retrieve, these will be combined by logical AND. You may | ||
| 72 | also supply a C<orderby> argument, which is an expression used to | ||
| 73 | determine the order of entries returned. Usually, it would be a simple | ||
| 74 | string with the field name to use, e.g. C<'timestamp'>, but you might | ||
| 75 | want to append the keyword "C<DESC>" to it for descending | ||
| 76 | order. Finally, you may supply a C<entries> argument, which is the | ||
| 77 | maximum number of entries to retrieve. | ||
| 78 | |||
| 79 | The other possible use to first use the C<incat> method, see below, | ||
| 80 | and then you may call this method on the object without any | ||
| 81 | parameters, and the corresponding articles will be loaded. | ||
| 82 | |||
| 83 | It will retrieve the data, and then call C<populate()> for each of the | ||
| 84 | records retrieved to ensure that the plural data objects actually | ||
| 85 | consists of an array of L<AxKit::App::TABOO::Data::Article>s. But it | ||
| 86 | calls the internal C<_load()>-method to do the hard work (and that's | ||
| 87 | in the parent class). | ||
| 88 | |||
| 89 | If there is no data that corresponds to the given arguments, this | ||
| 90 | method will return C<undef>. | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub load { | ||
| 97 | my ($self, %args) = @_; | ||
| 98 | my $what = $args{'what'} || '*'; | ||
| 99 | my $dbh = DBI->connect($self->dbconnectargs()); | ||
| 100 | my $articles; | ||
| 101 | if ($what eq '*') { | ||
| 102 | $what = 'articles.ID,articles.filename,articles.authorok,articles.editorok,articles.title,articles.description,articles.publisher,articles.date,articles.type,articles.identifieruri,articles.identifierurn,articles.coverage,articles.rights,mediatypes.mimetype,languages.code'; | ||
| 103 | } | ||
| 104 | if (scalar(@{${$self}{'ARTICLE_IDS'}})) { | ||
| 105 | # Then we allready know what to load, but can use additional constraints | ||
| 106 | my $query = "SELECT " . $what . " FROM articles JOIN languages ON (languages.ID = articles.lang_ID) JOIN mediatypes ON (mediatypes.ID = articles.format_ID) WHERE articles.id IN (" . "?, " x (scalar(@{${$self}{'ARTICLE_IDS'}})-1) . "?)"; | ||
| 107 | my @keys = keys(%{$args{'limit'}}); | ||
| 108 | if (scalar(@keys) == 1) { | ||
| 109 | $query .= " AND " . join("", @keys) . "=?"; | ||
| 110 | } | ||
| 111 | elsif(scalar(@keys) >= 1) { | ||
| 112 | $query .= " AND " . join("=? AND ", @keys) . "=?"; | ||
| 113 | } | ||
| 114 | $articles = $dbh->selectall_arrayref($query, {Slice => {}}, (@{${$self}{'ARTICLE_IDS'}}, values(%{$args{'limit'}}))); | ||
| 115 | } else { | ||
| 116 | $articles = $self->_load(%args); | ||
| 117 | foreach my $entry (@{$articles}) { | ||
| 118 | push(@{${$self}{'ARTICLE_IDS'}}, ${$entry}{'id'}); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return undef unless ((defined($articles) && @{$articles})); | ||
| 123 | my $categories = $dbh->selectall_arrayref("SELECT categories.catname, articlecats.field, articles.ID FROM categories JOIN articlecats ON (categories.ID = Cat_ID) JOIN articles ON (articlecats.Article_ID=articles.ID) WHERE articlecats.Article_ID IN (" . "?, " x (scalar(@{${$self}{'ARTICLE_IDS'}})-1) . "?)", {}, @{${$self}{'ARTICLE_IDS'}}); | ||
| 124 | my $users = $dbh->selectall_arrayref("SELECT users.username, articles.ID FROM users JOIN articleusers ON (users.ID = Users_ID) JOIN articles ON (articleusers.Article_ID=articles.ID) WHERE articleusers.Article_ID IN (" . "?, " x (scalar(@{${$self}{'ARTICLE_IDS'}})-1) . "?) ORDER BY articleusers.Users_ID", {}, @{${$self}{'ARTICLE_IDS'}}); | ||
| 125 | |||
| 126 | # Transform the users | ||
| 127 | my %userstmp; | ||
| 128 | foreach my $userentry (@{$users}) { | ||
| 129 | push(@{$userstmp{${$userentry}[1]}}, ${$userentry}[0]); | ||
| 130 | } | ||
| 131 | |||
| 132 | # Transform the categories | ||
| 133 | my %cattmp; | ||
| 134 | foreach my $catentry (@{$categories}) { | ||
| 135 | my @tmp = @{$catentry}[0..1]; | ||
| 136 | push(@{$cattmp{${$catentry}[2]}}, \@tmp); | ||
| 137 | } | ||
| 138 | |||
| 139 | foreach my $artentry (@{$articles}) { | ||
| 140 | my $article = AxKit::App::TABOO::Data::Article->new($self->dbconnectargs()); | ||
| 141 | my $id = ${$artentry}{'id'}; | ||
| 142 | $article->populate($artentry, $cattmp{$id}, $userstmp{$id}); | ||
| 143 | $article->onfile; | ||
| 144 | $self->Push($article); | ||
| 145 | } | ||
| 146 | return $self; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | =item C<addcatinfo> | ||
| 151 | |||
| 152 | =item C<adduserinfo> | ||
| 153 | |||
| 154 | =item C<addformatinfo> | ||
| 155 | |||
| 156 | These three methods are implemented in a plurals context, and can be | ||
| 157 | called on a plurals object just like a singular object. Each entry | ||
| 158 | will have their data structure extended with user, category and format | ||
| 159 | information. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | |||
| 164 | sub addcatinfo { | ||
| 165 | my $self = shift; | ||
| 166 | foreach my $article (@{${$self}{ENTRIES}}) { | ||
| 167 | $article->addcatinfo; | ||
| 168 | } | ||
| 169 | return $self; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | sub adduserinfo { | ||
| 174 | my $self = shift; | ||
| 175 | foreach my $article (@{${$self}{ENTRIES}}) { | ||
| 176 | $article->adduserinfo; | ||
| 177 | } | ||
| 178 | return $self; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | sub addformatinfo { | ||
| 184 | my $self = shift; | ||
| 185 | foreach my $article (@{${$self}{ENTRIES}}) { | ||
| 186 | $article->addformatinfo; | ||
| 187 | } | ||
| 188 | return $self; | ||
| 189 | } | ||
| 190 | |||
| 191 | =item C<incat(@catnames)> | ||
| 192 | |||
| 193 | Takes as arguments an array containing array names, and will return | ||
| 194 | the number of articles that has been classified into B<all> those | ||
| 195 | categories. It will also, internally, store which articles that was, | ||
| 196 | so that you may call the C<load> method on the object afterwards | ||
| 197 | without any further arguments to load the articles. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | # After pestering axkit-dahut a lot with the problem this routine is | ||
| 202 | # supposed to solve, Chris Prather came up with the following | ||
| 203 | # solution: | ||
| 204 | |||
| 205 | sub incat { | ||
| 206 | my $self = shift; | ||
| 207 | my @catnames = @_; | ||
| 208 | my $dbh = DBI->connect($self->dbconnectargs()); | ||
| 209 | ${$self}{'ARTICLE_IDS'} = $dbh->selectcol_arrayref("SELECT a.article_id FROM articlecats a INNER JOIN categories c ON c.id = a.cat_id WHERE c.catname = ?" . "INTERSECT SELECT a.article_id FROM articlecats a INNER JOIN categories c ON c.id = a.cat_id WHERE c.catname = ?" x (scalar(@catnames)-1), {}, @catnames); | ||
| 210 | return scalar(@{${$self}{'ARTICLE_IDS'}}) | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | =back | ||
| 215 | |||
| 216 | =head1 BUGS/TODO | ||
| 217 | |||
| 218 | Not anything particular at the moment... | ||
| 219 | |||
| 220 | |||
| 221 | =head1 FORMALITIES | ||
| 222 | |||
| 223 | See L<AxKit::App::TABOO>. | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | 1; | ||
| 228 | |||
| 229 |