]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/SubMirrorPlugin.php
Merge branch 'master' into testing
[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 Net_URL_Mapper $m path-to-action mapper
32      * @return boolean hook return
33      */
34     function onRouterInitialized($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     /**
49      * Automatically load the actions and libraries used by the plugin
50      *
51      * @param Class $cls the class
52      *
53      * @return boolean hook return
54      *
55      */
56     function onAutoload($cls)
57     {
58         $base = dirname(__FILE__);
59         $lower = strtolower($cls);
60         $files = array("$base/lib/$lower.php",
61                        "$base/classes/$cls.php");
62         if (substr($lower, -6) == 'action') {
63             $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
64         }
65         foreach ($files as $file) {
66             if (file_exists($file)) {
67                 include_once $file;
68                 return false;
69             }
70         }
71         return true;
72     }
73
74     function handle($notice)
75     {
76         // Is anybody mirroring?
77         $mirror = new SubMirror();
78         $mirror->subscribed = $notice->profile_id;
79         if ($mirror->find()) {
80             while ($mirror->fetch()) {
81                 $mirror->repeat($notice);
82             }
83         }
84     }
85
86     function onPluginVersion(&$versions)
87     {
88         $versions[] = array('name' => 'SubMirror',
89                             'version' => STATUSNET_VERSION,
90                             'author' => 'Brion Vibber',
91                             'homepage' => 'http://status.net/wiki/Plugin:SubMirror',
92                             'rawdescription' =>
93                             _m('Pull feeds into your timeline!'));
94
95         return true;
96     }
97
98     /**
99      * Menu item for personal subscriptions/groups area
100      *
101      * @param Widget $widget Widget being executed
102      *
103      * @return boolean hook return
104      */
105
106     function onEndSubGroupNav($widget)
107     {
108         $action = $widget->out;
109         $action_name = $action->trimmed('action');
110
111         $action->menuItem(common_local_url('mirrorsettings'),
112                           // TRANS: SubMirror plugin menu item on user settings page.
113                           _m('MENU', 'Mirroring'),
114                           // TRANS: SubMirror plugin tooltip for user settings menu item.
115                           _m('Configure mirroring of posts from other feeds'),
116                           $action_name === 'mirrorsettings');
117
118         return true;
119     }
120
121     function onCheckSchema()
122     {
123         $schema = Schema::get();
124         $schema->ensureTable('submirror', SubMirror::schemaDef());
125
126         // @hack until key definition support is merged
127         SubMirror::fixIndexes($schema);
128         return true;
129     }
130
131     /**
132      * Set up queue handlers for outgoing hub pushes
133      * @param QueueManager $qm
134      * @return boolean hook return
135      */
136     function onEndInitializeQueueManager(QueueManager $qm)
137     {
138         // After each notice save, check if there's any repeat mirrors.
139         $qm->connect('mirror', 'MirrorQueueHandler');
140         return true;
141     }
142
143     function onStartEnqueueNotice($notice, &$transports)
144     {
145         $transports[] = 'mirror';
146     }
147
148     /**
149      * Let the OStatus subscription garbage collection know if we're
150      * making use of a remote feed, so it doesn't get dropped out
151      * from under us.
152      *
153      * @param Ostatus_profile $oprofile
154      * @param int $count in/out
155      * @return mixed hook return value
156      */
157     function onOstatus_profileSubscriberCount($oprofile, &$count)
158     {
159         if ($oprofile->profile_id) {
160             $mirror = new SubMirror();
161             $mirror->subscribed = $oprofile->profile_id;
162             if ($mirror->find()) {
163                 while ($mirror->fetch()) {
164                     $count++;
165                 }
166             }
167         }
168         return true;
169     }
170
171     /**
172      * Add a count of mirrored feeds into a user's profile sidebar stats.
173      *
174      * @param Profile $profile
175      * @param array $stats
176      * @return boolean hook return value
177      */
178     function onProfileStats($profile, &$stats)
179     {
180         $cur = common_current_user();
181         if (!empty($cur) && $cur->id == $profile->id) {
182             $mirror = new SubMirror();
183             $mirror->subscriber = $profile->id;
184             $entry = array(
185                 'id' => 'mirrors',
186                 'label' => _m('Mirrored feeds'),
187                 'link' => common_local_url('mirrorsettings'),
188                 'value' => $mirror->count(),
189             );
190
191             $insertAt = count($stats);
192             foreach ($stats as $i => $row) {
193                 if ($row['id'] == 'groups') {
194                     // Slip us in after them.
195                     $insertAt = $i + 1;
196                     break;
197                 }
198             }
199             array_splice($stats, $insertAt, 0, array($entry));
200         }
201         return true;
202     }
203 }