]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/SubMirrorPlugin.php
Merge remote branch 'gitorious/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 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 settings
97      *
98      * @param Action &$action Action being executed
99      *
100      * @return boolean hook return
101      */
102
103     function onEndAccountSettingsNav(&$action)
104     {
105         $action_name = $action->trimmed('action');
106
107         $action->menuItem(common_local_url('mirrorsettings'),
108                           // TRANS: SubMirror plugin menu item on user settings page.
109                           _m('MENU', 'Mirroring'),
110                           // TRANS: SubMirror plugin tooltip for user settings menu item.
111                           _m('Configure mirroring of posts from other feeds'),
112                           $action_name === 'mirrorsettings');
113
114         return true;
115     }
116
117     function onCheckSchema()
118     {
119         $schema = Schema::get();
120         $schema->ensureTable('submirror', SubMirror::schemaDef());
121
122         // @hack until key definition support is merged
123         SubMirror::fixIndexes($schema);
124         return true;
125     }
126
127     /**
128      * Set up queue handlers for outgoing hub pushes
129      * @param QueueManager $qm
130      * @return boolean hook return
131      */
132     function onEndInitializeQueueManager(QueueManager $qm)
133     {
134         // After each notice save, check if there's any repeat mirrors.
135         $qm->connect('mirror', 'MirrorQueueHandler');
136         return true;
137     }
138
139     function onStartEnqueueNotice($notice, &$transports)
140     {
141         $transports[] = 'mirror';
142     }
143
144     /**
145      * Let the OStatus subscription garbage collection know if we're
146      * making use of a remote feed, so it doesn't get dropped out
147      * from under us.
148      *
149      * @param Ostatus_profile $oprofile
150      * @param int $count in/out
151      * @return mixed hook return value
152      */
153     function onOstatus_profileSubscriberCount($oprofile, &$count)
154     {
155         if ($oprofile->profile_id) {
156             $mirror = new SubMirror();
157             $mirror->subscribed = $oprofile->profile_id;
158             if ($mirror->find()) {
159                 while ($mirror->fetch()) {
160                     $count++;
161                 }
162             }
163         }
164         return true;
165     }
166 }