]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/SubMirrorPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / SubMirror / SubMirrorPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009-2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 /**
23  * @package SubMirrorPlugin
24  * @maintainer Brion Vibber <brion@status.net>
25  */
26 class SubMirrorPlugin extends Plugin
27 {
28     /**
29      * Hook for RouterInitialized event.
30      *
31      * @param URLMapper $m path-to-action mapper
32      * @return boolean hook return
33      */
34     public function onRouterInitialized(URLMapper $m)
35     {
36         $m->connect('settings/mirror',
37                     array('action' => 'mirrorsettings'));
38         $m->connect('settings/mirror/add/:provider',
39                     array('action' => 'mirrorsettings'),
40                     array('provider' => '[A-Za-z0-9_-]+'));
41         $m->connect('settings/mirror/add',
42                     array('action' => 'addmirror'));
43         $m->connect('settings/mirror/edit',
44                     array('action' => 'editmirror'));
45         return true;
46     }
47
48     function handle($notice)
49     {
50         // Is anybody mirroring?
51         $mirror = new SubMirror();
52         $mirror->subscribed = $notice->profile_id;
53         if ($mirror->find()) {
54             while ($mirror->fetch()) {
55                 $mirror->repeat($notice);
56             }
57         }
58     }
59
60     function onPluginVersion(array &$versions)
61     {
62         $versions[] = array('name' => 'SubMirror',
63                             'version' => GNUSOCIAL_VERSION,
64                             'author' => 'Brion Vibber',
65                             'homepage' => 'http://status.net/wiki/Plugin:SubMirror',
66                             'rawdescription' =>
67                             // TRANS: Plugin description.
68                             _m('Pull feeds into your timeline!'));
69
70         return true;
71     }
72
73     /**
74      * Menu item for personal subscriptions/groups area
75      *
76      * @param Action $action action being executed
77      *
78      * @return boolean hook return
79      */
80     function onEndAccountSettingsNav($action)
81     {
82         $action_name = $action->trimmed('action');
83
84         common_debug("ACTION NAME = " . $action_name);
85
86         $action->menuItem(common_local_url('mirrorsettings'),
87                           // TRANS: SubMirror plugin menu item on user settings page.
88                           _m('MENU', 'Mirroring'),
89                           // TRANS: SubMirror plugin tooltip for user settings menu item.
90                           _m('Configure mirroring of posts from other feeds'),
91                           $action_name === 'mirrorsettings');
92
93         return true;
94     }
95
96     function onCheckSchema()
97     {
98         $schema = Schema::get();
99         $schema->ensureTable('submirror', SubMirror::schemaDef());
100
101         // @hack until key definition support is merged
102         SubMirror::fixIndexes($schema);
103         return true;
104     }
105
106     /**
107      * Set up queue handlers for outgoing hub pushes
108      * @param QueueManager $qm
109      * @return boolean hook return
110      */
111     function onEndInitializeQueueManager(QueueManager $qm)
112     {
113         // After each notice save, check if there's any repeat mirrors.
114         $qm->connect('mirror', 'MirrorQueueHandler');
115         return true;
116     }
117
118     function onStartEnqueueNotice(Notice $notice, array &$transports)
119     {
120         $transports[] = 'mirror';
121     }
122
123     /**
124      * Let the OStatus subscription garbage collection know if we're
125      * making use of a remote feed, so it doesn't get dropped out
126      * from under us.
127      *
128      * @param Ostatus_profile $oprofile
129      * @param int $count in/out
130      * @return mixed hook return value
131      */
132     function onOstatus_profileSubscriberCount(Ostatus_profile $oprofile, &$count)
133     {
134         try {
135             $profile = $oprofile->localProfile();
136             $mirror = new SubMirror();
137             $mirror->subscribed = $profile->id;
138             if ($mirror->find()) {
139                 while ($mirror->fetch()) {
140                     $count++;
141                 }
142             }
143         } catch (NoProfileException $e) {
144             // We can't handle this kind of Ostatus_profile since it has no
145             // local profile
146         }
147
148         return true;
149     }
150
151     /**
152      * Add a count of mirrored feeds into a user's profile sidebar stats.
153      *
154      * @param Profile $profile
155      * @param array $stats
156      * @return boolean hook return value
157      */
158     function onProfileStats(Profile $profile, array &$stats)
159     {
160         $cur = common_current_user();
161         if (!empty($cur) && $cur->id == $profile->id) {
162             $mirror = new SubMirror();
163             $mirror->subscriber = $profile->id;
164             $entry = array(
165                 'id' => 'mirrors',
166                 // TRANS: Label in profile statistics section, followed by a count.
167                 'label' => _m('Mirrored feeds'),
168                 'link' => common_local_url('mirrorsettings'),
169                 'value' => $mirror->count(),
170             );
171
172             $insertAt = count($stats);
173             foreach ($stats as $i => $row) {
174                 if ($row['id'] == 'groups') {
175                     // Slip us in after them.
176                     $insertAt = $i + 1;
177                     break;
178                 }
179             }
180             array_splice($stats, $insertAt, 0, array($entry));
181         }
182         return true;
183     }
184 }