]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SlicedFavorites/favoritedsliceaction.php
Initial SlicedFavorites plugin to allow for customized variants of 'Popular' tab...
[quix0rs-gnu-social.git] / plugins / SlicedFavorites / favoritedsliceaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List of popular notices
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  Public
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35
36 class FavoritedSliceAction extends FavoritedAction
37 {
38     private $includeUsers = array(), $excludeUsers = array();
39
40     /**
41      * Take arguments for running
42      *
43      * @param array $args $_REQUEST args
44      *
45      * @return boolean success flag
46      *
47      * @todo move queries from showContent() to here
48      */
49
50     function prepare($args)
51     {
52         parent::prepare($args);
53
54         $this->slice = $this->arg('slice', 'default');
55         $data = array();
56         if (Event::handle('SlicedFavoritesGetSettings', array($this->slice, &$data))) {
57             throw new ClientException(_m('Unknown favorites slice.'));
58         }
59         if (isset($data['include'])) {
60             $this->includeUsers = $data['include'];
61         }
62         if (isset($data['exclude'])) {
63             $this->excludeUsers = $data['exclude'];
64         }
65
66         return true;
67     }
68
69     /**
70      * Content area
71      *
72      * Shows the list of popular notices
73      *
74      * @return void
75      */
76
77     function showContent()
78     {
79         $slice = $this->sliceWhereClause();
80         if (!$slice) {
81             return parent::showContent();
82         }
83
84         $weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
85         $cutoff = sprintf("fave.modified > '%s'",
86                           common_sql_date(time() - common_config('popular', 'cutoff')));
87
88         $qry = 'SELECT notice.*, '.
89           $weightexpr . ' as weight ' .
90           'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
91           "WHERE $cutoff AND $slice " .
92           'GROUP BY id,profile_id,uri,content,rendered,url,created,notice.modified,reply_to,is_local,source,notice.conversation ' .
93           'ORDER BY weight DESC';
94
95         $offset = ($this->page - 1) * NOTICES_PER_PAGE;
96         $limit  = NOTICES_PER_PAGE + 1;
97
98         if (common_config('db', 'type') == 'pgsql') {
99             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
100         } else {
101             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
102         }
103
104         $notice = Memcached_DataObject::cachedQuery('Notice',
105                                                     $qry,
106                                                     600);
107
108         $nl = new NoticeList($notice, $this);
109
110         $cnt = $nl->show();
111
112         if ($cnt == 0) {
113             $this->showEmptyList();
114         }
115
116         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
117                           $this->page, 'favorited');
118     }
119
120     private function sliceWhereClause()
121     {
122         $include = $this->nicknamesToIds($this->includeUsers);
123         $exclude = $this->nicknamesToIds($this->excludeUsers);
124
125         if (count($include) == 1) {
126             return "profile_id = " . intval($include[0]);
127         } else if (count($include) > 1) {
128             return "profile_id IN (" . implode(',', $include) . ")";
129         } else if (count($exclude) == 1) {
130             return "profile_id != " . intval($exclude[0]);
131         } else if (count($exclude) > 1) {
132             return "profile_id NOT IN (" . implode(',', $exclude) . ")";
133         } else {
134             return false;
135         }
136     }
137
138     /**
139      *
140      * @param array $nicks array of user nicknames
141      * @return array of profile/user IDs
142      */
143     private function nicknamesToIds($nicks)
144     {
145         $ids = array();
146         foreach ($nicks as $nick) {
147             // not the most efficient way for a big list!
148             $user = User::staticGet('nickname', $nick);
149             if ($user) {
150                 $ids[] = intval($user->id);
151             }
152         }
153         return $ids;
154     }
155 }