3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Affero General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 if (!defined('GNUSOCIAL')) { exit(1); }
20 * Table Definition for mention_url_profile
23 class Mention_url_profile extends Managed_DataObject
25 public $__table = 'mention_url_profile'; // table name
26 public $profile_id; // int(4) not_null
27 public $profileurl; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
29 public static function schemaDef()
33 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'matches exactly one profile id'),
34 'profileurl' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URL of the profile'),
36 'primary key' => array('profileurl'),
37 'foreign keys' => array(
38 'mention_url_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
43 public static function fromUrl($url, $depth=0) {
44 common_debug('MentionURL: trying to find a profile for ' . $url);
46 $url = preg_replace('#https?://#', 'https://', $url);
48 $profile = Profile::fromUri($url);
49 } catch(UnknownUriException $ex) {}
51 if(!($profile instanceof Profile)) {
52 $profile = self::findProfileByProfileURL($url);
55 $url = str_replace('https://', 'http://', $url);
56 if(!($profile instanceof Profile)) {
58 $profile = Profile::fromUri($url);
59 } catch(UnknownUriException $ex) {}
62 if(!($profile instanceof Profile)) {
63 $profile = self::findProfileByProfileURL($url);
66 if(!($profile instanceof Profile)) {
67 $hcard = mention_url_representative_hcard($url);
68 if(!$hcard) return null;
70 $mention_profile = new Mention_url_profile();
71 $mention_profile->query('BEGIN');
73 $profile = new Profile();
74 $profile->profileurl = $hcard['url'][0];
75 $profile->fullname = $hcard['name'][0];
76 preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
77 if(!$hcard['nickname']) $hcard['nickname'] = array($matches[1]);
78 $profile->nickname = $hcard['nickname'][0];
79 $profile->created = common_sql_now();
81 $mention_profile->profile_id = $profile->insert();
82 if(!$mention_profile->profile_id) {
83 $mention_profile->query('ROLLBACK');
87 $mention_profile->profileurl = $profile->profileurl;
88 if(!$mention_profile->insert()) {
89 $mention_profile->query('ROLLBACK');
93 return self::fromUrl($url, $depth+1);
96 $mention_profile->query('COMMIT');
103 protected static function findProfileByProfileURL($url) {
104 $profile = Profile::getKV('profileurl', $url);
105 if($profile instanceof Profile) {
106 $mention_profile = new Mention_url_profile();
107 $mention_profile->profile_id = $profile->id;
108 $mention_profile->profileurl = $profile->profileurl;
109 $mention_profile->insert();
115 public function getProfile() {
116 return Profile::getKV('id', $this->profile_id);