]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MentionURL/MentionURLPlugin.php
[TRANSLATION] Update POTs and normalize files
[quix0rs-gnu-social.git] / plugins / MentionURL / MentionURLPlugin.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5 require_once __DIR__ . '/lib/util.php';
6
7 /*
8  * This plugin lets you type @twitter.com/singpolyma
9  * so that you can be specific instead of relying on heuristics.
10  */
11 class MentionURLPlugin extends Plugin
12 {
13     const PLUGIN_VERSION = '2.0.0';
14
15     function onEndFindMentions(Profile $sender, $text, &$mentions)
16     {
17         $matches = array();
18
19         preg_match_all('/(?:^|\s+)@([A-Za-z0-9_:\-\.\/%]+)\b/',
20                        $text,
21                        $atmatches,
22                        PREG_OFFSET_CAPTURE);
23
24         foreach ($atmatches[1] as $match) {
25             $url = $match[0];
26             if(!common_valid_http_url($url)) { $url = 'http://' . $url; }
27             if(common_valid_http_url($url)) {
28                 $mentioned = Mention_url_profile::fromUrl($url);
29                 $text = mb_strlen($mentioned->nickname) <= mb_strlen($match[0]) ? $mentioned->nickname : $match[0];
30             }
31
32             if($mentioned instanceof Profile) {
33                 $matches[$match[1]] = array('mentioned' => array($mentioned),
34                                             'type' => 'mention',
35                                             'text' => $text,
36                                             'position' => $match[1],
37                                             'length' => mb_strlen($match[0]),
38                                             'url' => $mentioned->profileurl);
39             }
40         }
41
42         foreach ($mentions as $i => $other) {
43             // If we share a common prefix with a local user, override it!
44             $pos = $other['position'];
45             if (isset($matches[$pos])) {
46                 $mentions[$i] = $matches[$pos];
47                 unset($matches[$pos]);
48             }
49         }
50         foreach ($matches as $mention) {
51             $mentions[] = $mention;
52         }
53
54         return true;
55     }
56
57     public function onStartGetProfileFromURI($uri, &$profile)
58     {
59         $mention_profile = Mention_url_profile::getKV('profileurl', $uri);
60         if($mention_profile instanceof Mention_url_profile) {
61             $profile = $mention_profile->getProfile();
62             return !($profile instanceof Profile);
63         }
64
65         return true;
66     }
67
68     public function onCheckSchema()
69     {
70         $schema = Schema::get();
71         $schema->ensureTable('mention_url_profile', Mention_url_profile::schemaDef());
72         return true;
73     }
74
75     public function onPluginVersion(array &$versions)
76     {
77         $versions[] = array('name' => 'MentionURL',
78                             'version' => self::PLUGIN_VERSION,
79                             'author' => 'Stephen Paul Weber',
80                             'homepage' => 'http://gnu.io/',
81                             'description' =>
82                             // TRANS: Plugin description.
83                             _m('Plugin to allow mentioning arbitrary URLs.'));
84         return true;
85     }
86 }