root/branches/multiuser/tags.php

Revision 1604, 6.0 kB (checked in by mdodoo, 3 years ago)

Sync with trunk up to (but not including changeset [1585]). I should *definitely* do this more often, because this was really annoying.

  • Property svn:eol-style set to native
  • Property svn:eolstyle set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.      See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:          mbonetti at gmail dot com
24# Web page:        http://gregarius.net/
25#
26###############################################################################
27
28require_once ('init.php');
29
30function relatedTags($tags) {
31        /* related tags */
32        $twhere = "";
33        foreach ($tags as $tag) {
34                $twhere .= "t.tag='$tag' or ";
35        }
36        $twhere .= "1=0";
37
38        $sql = "select fid,tid,m.tdate from ".getTable('metatag') . " m "
39                . "inner join " . getTable('tag')." t  on m.tid=t.id "
40                . "where m.ttype = 'item' and ($twhere)";
41
42        //echo $sql;
43        $res = rss_query($sql);
44        $fids = array ();
45        $ctid = -1;
46        while (list ($fid, $tid) = rss_fetch_row($res)) {
47                $fids[] = $fid;
48                $tids[] = $tid;
49        }
50        $fids = array_unique($fids);
51        $tids = array_unique($tids);
52
53        $rtags = array ();
54        if (count($fids)) {
55                $sql = "select t.tag, count(*) as cnt from ".getTable('metatag')
56                ." m left join ".getTable('item')." i on (m.fid=i.id) "
57                ." inner join " . getTable('tag')." t on (m.tid=t.id)"
58                ." where m.fid in (".implode(",", $fids).")"
59                ." and t.id not in (".implode(",", $tids).")";
60
61                if (hidePrivate()) {
62                        $sql .= " and not(i.unread & ".RSS_MODE_PRIVATE_STATE.") ";
63                }
64
65                $sql .= " group by t.tag order by cnt desc";
66
67                //echo $sql;
68                $res = rss_query($sql);
69                while ((list ($rtag, $cnt) = rss_fetch_row($res))) {
70                        $rtags[$rtag] = $cnt;
71                }
72        }
73        return $rtags;
74}
75
76if (array_key_exists('tag', $_GET)) {
77        // while this one displays a list of items for the requested tag(s)
78        $tag = strip_tags($_GET['tag']);
79        $twhere = "";
80        $tarr = explode(" ", $tag);
81        $hrTag = implode(" ".__('and')." ", $tarr);
82        $urlTag = implode("+", $tarr);
83
84        foreach ($tarr as $ttkn) {
85                $twhere .= " t.tag='".trim(rss_real_escape_string($ttkn))."' or";
86        }
87        $twhere .= " 1=0";
88
89        $sql = "select fid, count(*) as cnt from ".getTable('metatag')." m "
90        ."inner join " . getTable('tag')." t on m.tid=t.id "
91        ." where ($twhere) and m.ttype = 'item'"
92        ." group by fid order by 2 desc";
93
94        $res = rss_query($sql);
95        $ids = array ();
96        while ((list ($id, $cnt) = rss_fetch_row($res)) && $cnt >= count($tarr)) {
97                $ids[] = $id;
98        }
99
100        $gotsome = count($ids) > 0;
101        if ($gotsome) {
102
103                $taggedItems = new ItemList();
104                $sqlWhere = " i.id in (".implode(",", $ids).") ";
105                // include deprecated feeds while showing tags.
106                $taggedItems->populate($sqlWhere, "", 0, -1, ITEM_SORT_HINT_MIXED, true);
107
108                $rtags = relatedTags($tarr);
109                $related = array ();
110                foreach ($rtags as $rtag => $cnt) {
111                        $relLbl = "<a href=\"".getPath().""
112                        . (getConfig('rss.output.usemodrewrite') ? "tag/$rtag" : "tags.php?tag=$rtag").""."\">$rtag</a>";
113
114                        $relPlus = array_key_exists($rtag, $taggedItems->allTags);
115                        if ($relPlus) {
116                                $relLbl .= "&nbsp;[<a "."title=\"$cnt ". ($cnt > 1 ? __('items') : __('item'))." ". ($cnt > 1 || $cnt == 0 ? __('tagged') : __('tagged'))." '".$hrTag." ".__('and')." $rtag'\" "."href=\"".getPath()."". (getConfig('rss.output.usemodrewrite') ? "tag/$rtag" : "tags.php?tag=$rtag").""."+".$urlTag."\">+</a>]";
117                        }
118                        $idx = ($relPlus ? $taggedItems->allTags[$rtag] : 0);
119                        $related["$idx"."_"."$rtag"] = $relLbl."";
120                }
121                krsort($related);
122        }
123
124        // done! Render some stuff
125        if (array_key_exists('rss', $_REQUEST)) {
126                rss_require('cls/rdf.php');
127                // RSS view
128                $title = _TITLE_." - ".__('Tags')." - ".$hrTag;
129                $baselink = guessTransportProto().$_SERVER['HTTP_HOST'].getPath()
130                . (getConfig('rss.output.usemodrewrite') ? "tag/" : "tags.php?tag=");
131
132                if ($gotsome) {
133                        $rdf = new RDFItemList($taggedItems);
134                } else {
135                        $rdf = new RDFItemList(null);
136                }
137                $rdf->baselink = $baselink;
138                $rdf->resource = $urlTag;
139                $rdf->render($title);
140                exit ();
141        } else {
142                // HTML view
143                //rss_header("Tags " . TITLE_SEP . " " . $hrTag);
144                $GLOBALS['rss']->header = new Header("Tags ".TITLE_SEP." ".$hrTag);
145                $GLOBALS['rss']->feedList = new FeedList(false);
146
147                //echo "\n\n<div id=\"items\" class=\"frame\">\n";
148
149                if ($gotsome) {
150
151                        $title = $taggedItems->itemCount." ". ($taggedItems->itemCount > 1 ? __('items') : __('item'))." "
152                        . ($taggedItems->itemCount > 1 || $taggedItems->itemCount == 0 ? __('tagged') : __('tagged'))
153                        .""." \"".$hrTag."\"";
154
155                        if (count($related)) {
156                                $taggedItems->beforeList = "\n<p>".__('Related tags: ')."\n".implode(", \n", $related)."\n</p>\n";
157                        }
158
159                        $taggedItems->setTitle($title);
160                        $taggedItems->setRenderOptions(IL_NO_COLLAPSE|IL_TITLE_NO_ESCAPE);
161                        $GLOBALS['rss']->appendContentObject($taggedItems);
162
163                        $GLOBALS['rss']->renderWithTemplate('index.php', 'items');
164
165                } else {
166                        echo "<p style=\"height: 10em; text-align:center\">";
167                        printf(__('Oops! No items tagged &laquo;%s&raquo; were found.'), $hrTag);
168                        echo "</p>";
169                }
170                //echo "</div>\n";
171                //rss_footer();
172        }
173
174} elseif (array_key_exists('alltags', $_GET)) {
175        rss_require('cls/alltags.php');
176
177        $GLOBALS['rss']->header = new Header("Tags ".TITLE_SEP." ".__('All Tags'));
178        $GLOBALS['rss']->feedList = new FeedList(false);
179        $allTags = new Tags();
180        $allTags->setRenderOptions(IL_TITLE_NO_ESCAPE);
181        $GLOBALS['rss']->appendContentObject($allTags);
182        $GLOBALS['rss']->renderWithTemplate('index.php', 'items');
183}
184?>
Note: See TracBrowser for help on using the browser.