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