| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * StatusNet - the distributed open-source microblogging tool | 
					
						
							|  |  |  |  * Copyright (C) 2009-2010, StatusNet, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 22:31:41 +02:00
										 |  |  | if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @package SubMirrorPlugin | 
					
						
							|  |  |  |  * @maintainer Brion Vibber <brion@status.net> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class SubMirrorPlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |     const PLUGIN_VERSION = '2.0.0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Hook for RouterInitialized event. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-11-07 15:24:05 +01:00
										 |  |  |      * @param URLMapper $m path-to-action mapper | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |      * @return boolean hook return | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-07 15:24:05 +01:00
										 |  |  |     public function onRouterInitialized(URLMapper $m) | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         $m->connect('settings/mirror', | 
					
						
							| 
									
										
										
										
											2019-07-11 19:14:03 +01:00
										 |  |  |                     ['action' => 'mirrorsettings']); | 
					
						
							| 
									
										
										
										
											2011-01-18 18:01:57 -08:00
										 |  |  |         $m->connect('settings/mirror/add/:provider', | 
					
						
							| 
									
										
										
										
											2019-07-11 19:14:03 +01:00
										 |  |  |                     ['action' => 'mirrorsettings'], | 
					
						
							|  |  |  |                     ['provider' => '[A-Za-z0-9_-]+']); | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |         $m->connect('settings/mirror/add', | 
					
						
							| 
									
										
										
										
											2019-07-11 19:14:03 +01:00
										 |  |  |                     ['action' => 'addmirror']); | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |         $m->connect('settings/mirror/edit', | 
					
						
							| 
									
										
										
										
											2019-07-11 19:14:03 +01:00
										 |  |  |                     ['action' => 'editmirror']); | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 00:15:38 +01:00
										 |  |  |     function handle($notice) : bool | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Is anybody mirroring?
 | 
					
						
							|  |  |  |         $mirror = new SubMirror(); | 
					
						
							|  |  |  |         $mirror->subscribed = $notice->profile_id; | 
					
						
							|  |  |  |         if ($mirror->find()) { | 
					
						
							|  |  |  |             while ($mirror->fetch()) { | 
					
						
							|  |  |  |                 $mirror->repeat($notice); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 15:03:30 +01:00
										 |  |  |     public function onPluginVersion(array &$versions): bool | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         $versions[] = array('name' => 'SubMirror', | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |                             'version' => self::PLUGIN_VERSION, | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |                             'author' => 'Brion Vibber', | 
					
						
							| 
									
										
										
										
											2019-11-21 00:21:22 +00:00
										 |  |  |                             'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SubMirror', | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |                             'rawdescription' => | 
					
						
							| 
									
										
										
										
											2011-06-06 00:38:38 +02:00
										 |  |  |                             // TRANS: Plugin description.
 | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |                             _m('Pull feeds into your timeline!')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-01-18 12:34:04 -08:00
										 |  |  |      * Menu item for personal subscriptions/groups area | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-29 07:58:52 +00:00
										 |  |  |      * @param Action $action action being executed | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return boolean hook return | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-29 07:58:52 +00:00
										 |  |  |     function onEndAccountSettingsNav($action) | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         $action_name = $action->trimmed('action'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-29 07:58:52 +00:00
										 |  |  |         common_debug("ACTION NAME = " . $action_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |         $action->menuItem(common_local_url('mirrorsettings'), | 
					
						
							|  |  |  |                           // TRANS: SubMirror plugin menu item on user settings page.
 | 
					
						
							|  |  |  |                           _m('MENU', 'Mirroring'), | 
					
						
							|  |  |  |                           // TRANS: SubMirror plugin tooltip for user settings menu item.
 | 
					
						
							|  |  |  |                           _m('Configure mirroring of posts from other feeds'), | 
					
						
							|  |  |  |                           $action_name === 'mirrorsettings'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function onCheckSchema() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $schema = Schema::get(); | 
					
						
							|  |  |  |         $schema->ensureTable('submirror', SubMirror::schemaDef()); | 
					
						
							| 
									
										
										
										
											2010-08-10 11:45:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // @hack until key definition support is merged
 | 
					
						
							|  |  |  |         SubMirror::fixIndexes($schema); | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Set up queue handlers for outgoing hub pushes | 
					
						
							|  |  |  |      * @param QueueManager $qm | 
					
						
							|  |  |  |      * @return boolean hook return | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function onEndInitializeQueueManager(QueueManager $qm) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // After each notice save, check if there's any repeat mirrors.
 | 
					
						
							|  |  |  |         $qm->connect('mirror', 'MirrorQueueHandler'); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function onStartEnqueueNotice($notice, &$transports) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $transports[] = 'mirror'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Let the OStatus subscription garbage collection know if we're | 
					
						
							|  |  |  |      * making use of a remote feed, so it doesn't get dropped out | 
					
						
							|  |  |  |      * from under us. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Ostatus_profile $oprofile | 
					
						
							|  |  |  |      * @param int $count in/out | 
					
						
							|  |  |  |      * @return mixed hook return value | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-05-19 17:58:05 +02:00
										 |  |  |     function onOstatus_profileSubscriberCount(Ostatus_profile $oprofile, &$count) | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-05-19 17:58:05 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $profile = $oprofile->localProfile(); | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |             $mirror = new SubMirror(); | 
					
						
							| 
									
										
										
										
											2014-05-19 17:58:05 +02:00
										 |  |  |             $mirror->subscribed = $profile->id; | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |             if ($mirror->find()) { | 
					
						
							|  |  |  |                 while ($mirror->fetch()) { | 
					
						
							|  |  |  |                     $count++; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-05-19 17:58:05 +02:00
										 |  |  |         } catch (NoProfileException $e) { | 
					
						
							|  |  |  |             // We can't handle this kind of Ostatus_profile since it has no
 | 
					
						
							|  |  |  |             // local profile
 | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-01-27 15:27:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-06 10:56:18 -07:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-01-18 13:32:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Add a count of mirrored feeds into a user's profile sidebar stats. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Profile $profile | 
					
						
							|  |  |  |      * @param array $stats | 
					
						
							|  |  |  |      * @return boolean hook return value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function onProfileStats($profile, &$stats) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cur = common_current_user(); | 
					
						
							|  |  |  |         if (!empty($cur) && $cur->id == $profile->id) { | 
					
						
							|  |  |  |             $mirror = new SubMirror(); | 
					
						
							|  |  |  |             $mirror->subscriber = $profile->id; | 
					
						
							|  |  |  |             $entry = array( | 
					
						
							|  |  |  |                 'id' => 'mirrors', | 
					
						
							| 
									
										
										
										
											2011-06-06 00:38:38 +02:00
										 |  |  |                 // TRANS: Label in profile statistics section, followed by a count.
 | 
					
						
							| 
									
										
										
										
											2011-01-18 13:32:12 -08:00
										 |  |  |                 'label' => _m('Mirrored feeds'), | 
					
						
							|  |  |  |                 'link' => common_local_url('mirrorsettings'), | 
					
						
							|  |  |  |                 'value' => $mirror->count(), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $insertAt = count($stats); | 
					
						
							|  |  |  |             foreach ($stats as $i => $row) { | 
					
						
							|  |  |  |                 if ($row['id'] == 'groups') { | 
					
						
							|  |  |  |                     // Slip us in after them.
 | 
					
						
							|  |  |  |                     $insertAt = $i + 1; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             array_splice($stats, $insertAt, 0, array($entry)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-05 13:37:47 -07:00
										 |  |  | } |