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