]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinetag.php
Merge remote branch 'gitorious/0.9.x' into 0.9.x
[quix0rs-gnu-social.git] / actions / apitimelinetag.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the latest notices for a given tag
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    Zach Copley <zach@status.net>
28  * @copyright 2009-2010 StatusNet, Inc.
29  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://status.net/
32  */
33
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 require_once INSTALLDIR . '/lib/apiprivateauth.php';
39
40 /**
41  * Returns the 20 most recent notices tagged by a given tag
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Jeffery To <jeffery.to@gmail.com>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52
53 class ApiTimelineTagAction extends ApiPrivateAuthAction
54 {
55
56     var $notices = null;
57
58     /**
59      * Take arguments for running
60      *
61      * @param array $args $_REQUEST args
62      *
63      * @return boolean success flag
64      *
65      */
66
67     function prepare($args)
68     {
69         parent::prepare($args);
70
71         common_debug("apitimelinetag prepare()");
72
73         $this->tag     = $this->arg('tag');
74         $this->notices = $this->getNotices();
75
76         return true;
77     }
78
79     /**
80      * Handle the request
81      *
82      * Just show the notices
83      *
84      * @param array $args $_REQUEST data (unused)
85      *
86      * @return void
87      */
88
89     function handle($args)
90     {
91         parent::handle($args);
92         $this->showTimeline();
93     }
94
95     /**
96      * Show the timeline of notices
97      *
98      * @return void
99      */
100
101     function showTimeline()
102     {
103         $sitename   = common_config('site', 'name');
104         $sitelogo   = (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png');
105         $title      = sprintf(_("Notices tagged with %s"), $this->tag);
106         $subtitle   = sprintf(
107             _('Updates tagged with %1$s on %2$s!'),
108             $this->tag,
109             $sitename
110         );
111         $taguribase = TagURI::base();
112         $id         = "tag:$taguribase:TagTimeline:".$tag;
113
114         $link = common_local_url(
115             'tag',
116             array('tag' => $this->tag)
117         );
118
119         $self = $this->getSelfUri();
120
121         common_debug("self link is: $self");
122
123         switch($this->format) {
124         case 'xml':
125             $this->showXmlTimeline($this->notices);
126             break;
127         case 'rss':
128             $this->showRssTimeline(
129                 $this->notices,
130                 $title,
131                 $link,
132                 $subtitle,
133                 null,
134                 $sitelogo,
135                 $self
136             );
137             break;
138         case 'atom':
139
140             header('Content-Type: application/atom+xml; charset=utf-8');
141
142             $atom = new AtomNoticeFeed($this->auth_user);
143
144             $atom->setId($id);
145             $atom->setTitle($title);
146             $atom->setSubtitle($subtitle);
147             $atom->setLogo($logo);
148             $atom->setUpdated('now');
149
150             $atom->addLink($link);
151             $atom->setSelfLink($self);
152
153             $atom->addEntryFromNotices($this->notices);
154             $this->raw($atom->getString());
155
156             break;
157         case 'json':
158             $this->showJsonTimeline($this->notices);
159             break;
160         default:
161             $this->clientError(_('API method not found.'), $code = 404);
162             break;
163         }
164     }
165
166     /**
167      * Get notices
168      *
169      * @return array notices
170      */
171
172     function getNotices()
173     {
174         $notices = array();
175
176         $notice = Notice_tag::getStream(
177             $this->tag,
178             ($this->page - 1) * $this->count,
179             $this->count + 1
180         );
181
182         while ($notice->fetch()) {
183             $notices[] = clone($notice);
184         }
185
186         return $notices;
187     }
188
189     /**
190      * Is this action read only?
191      *
192      * @param array $args other arguments
193      *
194      * @return boolean true
195      */
196
197     function isReadOnly($args)
198     {
199         return true;
200     }
201
202     /**
203      * When was this feed last modified?
204      *
205      * @return string datestamp of the latest notice in the stream
206      */
207
208     function lastModified()
209     {
210         if (!empty($this->notices) && (count($this->notices) > 0)) {
211             return strtotime($this->notices[0]->created);
212         }
213
214         return null;
215     }
216
217     /**
218      * An entity tag for this stream
219      *
220      * Returns an Etag based on the action name, language, and
221      * timestamps of the first and last notice in the timeline
222      *
223      * @return string etag
224      */
225
226     function etag()
227     {
228         if (!empty($this->notices) && (count($this->notices) > 0)) {
229
230             $last = count($this->notices) - 1;
231
232             return '"' . implode(
233                 ':',
234                 array($this->arg('action'),
235                       common_user_cache_hash($this->auth_user),
236                       common_language(),
237                       $this->tag,
238                       strtotime($this->notices[0]->created),
239                       strtotime($this->notices[$last]->created))
240             )
241             . '"';
242         }
243
244         return null;
245     }
246
247 }