]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/SubMirrorPlugin.php
Merge commit 'origin/0.9.x' into 0.9.x
[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 /**
21  * @package SubMirrorPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27
28 class SubMirrorPlugin extends Plugin
29 {
30     /**
31      * Hook for RouterInitialized event.
32      *
33      * @param Net_URL_Mapper $m path-to-action mapper
34      * @return boolean hook return
35      */
36     function onRouterInitialized($m)
37     {
38         $m->connect('settings/mirror',
39                     array('action' => 'mirrorsettings'));
40         $m->connect('settings/mirror/add',
41                     array('action' => 'addmirror'));
42         $m->connect('settings/mirror/edit',
43                     array('action' => 'editmirror'));
44         return true;
45     }
46
47     /**
48      * Automatically load the actions and libraries used by the plugin
49      *
50      * @param Class $cls the class
51      *
52      * @return boolean hook return
53      *
54      */
55     function onAutoload($cls)
56     {
57         $base = dirname(__FILE__);
58         $lower = strtolower($cls);
59         $files = array("$base/lib/$lower.php",
60                        "$base/classes/$cls.php");
61         if (substr($lower, -6) == 'action') {
62             $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
63         }
64         foreach ($files as $file) {
65             if (file_exists($file)) {
66                 include_once $file;
67                 return false;
68             }
69         }
70         return true;
71     }
72
73     function handle($notice)
74     {
75         // Is anybody mirroring?
76         $mirror = new SubMirror();
77         $mirror->subscribed = $notice->profile_id;
78         if ($mirror->find()) {
79             while ($mirror->fetch()) {
80                 $mirror->repeat($notice);
81             }
82         }
83     }
84
85     function onPluginVersion(&$versions)
86     {
87         $versions[] = array('name' => 'SubMirror',
88                             'version' => STATUSNET_VERSION,
89                             'author' => 'Brion Vibber',
90                             'homepage' => 'http://status.net/wiki/Plugin:SubMirror',
91                             'rawdescription' =>
92                             _m('Pull feeds into your timeline!'));
93
94         return true;
95     }
96
97     /**
98      * Menu item for settings
99      *
100      * @param Action &$action Action being executed
101      *
102      * @return boolean hook return
103      */
104
105     function onEndAccountSettingsNav(&$action)
106     {
107         $action_name = $action->trimmed('action');
108
109         $action->menuItem(common_local_url('mirrorsettings'),
110                           // TRANS: SubMirror plugin menu item on user settings page.
111                           _m('MENU', 'Mirroring'),
112                           // TRANS: SubMirror plugin tooltip for user settings menu item.
113                           _m('Configure mirroring of posts from other feeds'),
114                           $action_name === 'mirrorsettings');
115
116         return true;
117     }
118
119     function onCheckSchema()
120     {
121         $schema = Schema::get();
122         $schema->ensureTable('submirror', SubMirror::schemaDef());
123
124         // @hack until key definition support is merged
125         SubMirror::fixIndexes($schema);
126         return true;
127     }
128
129     /**
130      * Set up queue handlers for outgoing hub pushes
131      * @param QueueManager $qm
132      * @return boolean hook return
133      */
134     function onEndInitializeQueueManager(QueueManager $qm)
135     {
136         // After each notice save, check if there's any repeat mirrors.
137         $qm->connect('mirror', 'MirrorQueueHandler');
138         return true;
139     }
140
141     function onStartEnqueueNotice($notice, &$transports)
142     {
143         $transports[] = 'mirror';
144     }
145
146     /**
147      * Let the OStatus subscription garbage collection know if we're
148      * making use of a remote feed, so it doesn't get dropped out
149      * from under us.
150      *
151      * @param Ostatus_profile $oprofile
152      * @param int $count in/out
153      * @return mixed hook return value
154      */
155     function onOstatus_profileSubscriberCount($oprofile, &$count)
156     {
157         if ($oprofile->profile_id) {
158             $mirror = new SubMirror();
159             $mirror->subscribed = $oprofile->profile_id;
160             if ($mirror->find()) {
161                 while ($mirror->fetch()) {
162                     $count++;
163                 }
164             }
165         }
166         return true;
167     }
168 }