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