]> git.mxchange.org Git - friendica.git/commitdiff
Move ProfileFieldRepository::migrateFromLegacyProfile() & delete old repository
authorPhilipp <admin@philipp.info>
Sun, 10 Oct 2021 18:54:29 +0000 (20:54 +0200)
committerPhilipp <admin@philipp.info>
Mon, 18 Oct 2021 21:32:41 +0000 (23:32 +0200)
src/Core/UserImport.php
src/DI.php
src/Model/Profile.php
src/Module/Api/Friendica/Profile/Show.php
src/Module/Profile/Profile.php
src/Module/Settings/Profile/Index.php
src/Protocol/ActivityPub/Transmitter.php
src/Repository/ProfileField.php [deleted file]
update.php

index c0725bee8a4fd085bd24e68efafcc6aad5f68f78..309e5c2a9632426c12ce57955f5feefeeeb34ca2 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Model\Photo;
+use Friendica\Model\Profile;
 use Friendica\Object\Image;
 use Friendica\Security\PermissionSet\Depository\PermissionSet;
 use Friendica\Util\Strings;
@@ -278,7 +279,7 @@ class UserImport
                                $profile['id'] = DBA::lastInsertId();
                        }
 
-                       DI::profileField()->migrateFromLegacyProfile($profile);
+                       Profile::migrateFromLegacyProfile($profile);
                }
 
                $permissionSet = DI::permissionSet()->selectDefaultForUser($newuid);
index 562532f5c1a7f58aeeab1c3e50a105bbf37355f9..31542e3d52dd30bebd34c47c8b8e10501fac0c8f 100644 (file)
@@ -452,15 +452,7 @@ abstract class DI
                return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class);
        }
 
-       /**
-        * @return Repository\ProfileField
-        */
-       public static function profileField()
-       {
-               return self::$dice->create(Repository\ProfileField::class);
-       }
-
-       public static function profileFieldNew(): Profile\ProfileField\Depository\ProfileField
+       public static function profileField(): Profile\ProfileField\Depository\ProfileField
        {
                return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class);
        }
index 9987f86760090181156e359f3a058f1e563f3251..2baf455cfd5e7e0cc02846bdd91466e488fac1b9 100644 (file)
@@ -38,6 +38,7 @@ use Friendica\DI;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Diaspora;
+use Friendica\Security\PermissionSet\Entity\PermissionSet;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Network;
@@ -936,4 +937,86 @@ class Profile
 
                return ['total' => $total, 'entries' => $profiles];
        }
+
+       /**
+        * Migrates a legacy profile to the new slimmer profile with extra custom fields.
+        * Multi profiles are converted to ACl-protected custom fields and deleted.
+        *
+        * @param array $profile One profile array
+        * @throws \Exception
+        */
+       public static function migrateFromLegacyProfile(array $profile)
+       {
+               // Already processed, aborting
+               if ($profile['is-default'] === null) {
+                       return;
+               }
+
+               $contacts = [];
+
+               if (!$profile['is-default']) {
+                       $contacts = Contact::selectToArray(['id'], [
+                               'uid'        => $profile['uid'],
+                               'profile-id' => $profile['id']
+                       ]);
+                       if (!count($contacts)) {
+                               // No contact visibility selected defaults to user-only permission
+                               $contacts = Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'self' => true]);
+                       }
+               }
+
+               $permissionSet = DI::permissionSet()->selectOrCreate(
+                       new PermissionSet(
+                               $profile['uid'],
+                               array_column($contacts, 'id') ?? []
+                       )
+               );
+
+               $order = 1;
+
+               $custom_fields = [
+                       'hometown'  => DI::l10n()->t('Hometown:'),
+                       'marital'   => DI::l10n()->t('Marital Status:'),
+                       'with'      => DI::l10n()->t('With:'),
+                       'howlong'   => DI::l10n()->t('Since:'),
+                       'sexual'    => DI::l10n()->t('Sexual Preference:'),
+                       'politic'   => DI::l10n()->t('Political Views:'),
+                       'religion'  => DI::l10n()->t('Religious Views:'),
+                       'likes'     => DI::l10n()->t('Likes:'),
+                       'dislikes'  => DI::l10n()->t('Dislikes:'),
+                       'pdesc'     => DI::l10n()->t('Title/Description:'),
+                       'summary'   => DI::l10n()->t('Summary'),
+                       'music'     => DI::l10n()->t('Musical interests'),
+                       'book'      => DI::l10n()->t('Books, literature'),
+                       'tv'        => DI::l10n()->t('Television'),
+                       'film'      => DI::l10n()->t('Film/dance/culture/entertainment'),
+                       'interest'  => DI::l10n()->t('Hobbies/Interests'),
+                       'romance'   => DI::l10n()->t('Love/romance'),
+                       'work'      => DI::l10n()->t('Work/employment'),
+                       'education' => DI::l10n()->t('School/education'),
+                       'contact'   => DI::l10n()->t('Contact information and Social Networks'),
+               ];
+
+               foreach ($custom_fields as $field => $label) {
+                       if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) {
+                               DI::profileField()->save(DI::profileFieldFactory()->createFromString(
+                                       $profile['uid'],
+                                       $order,
+                                       trim($label, ':'),
+                                       $profile[$field],
+                                       $permissionSet
+                               ));
+                       }
+
+                       $profile[$field] = null;
+               }
+
+               if ($profile['is-default']) {
+                       $profile['profile-name'] = null;
+                       $profile['is-default']   = null;
+                       DBA::update('profile', $profile, ['id' => $profile['id']]);
+               } else if (!empty($profile['id'])) {
+                       DBA::delete('profile', ['id' => $profile['id']]);
+               }
+       }
 }
index c0f3a3f8dc544e5c4fa04da28236d668328d5b72..8102ac4bcc63f9454c691dfe7eadef21fcb33fd3 100644 (file)
@@ -45,7 +45,7 @@ class Show extends BaseApi
 
                $profile = Profile::getByUID($uid);
                
-               $profileFields = DI::profileFieldNew()->selectPublicFieldsByUserId($uid);
+               $profileFields = DI::profileField()->selectPublicFieldsByUserId($uid);
 
                $profile = self::formatProfile($profile, $profileFields);
 
index 1b8ca34357bde2057407f43e58ae78c420b72b7e..25b6eccc9abdb6c6514239985483420b19b6e43c 100644 (file)
@@ -213,9 +213,9 @@ class Profile extends BaseProfile
                $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0;
 
                if ($is_owner && $contact_id === 0) {
-                       $profile_fields = DI::profileFieldNew()->selectByUserId($profile['uid']);
+                       $profile_fields = DI::profileField()->selectByUserId($profile['uid']);
                } else {
-                       $profile_fields = DI::profileFieldNew()->selectByContactId($contact_id, $profile['uid']);
+                       $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']);
                }
 
                foreach ($profile_fields as $profile_field) {
index 28fe695ba3bcddb4290c8be79929933a64f773ad..92d6356b8d1bec86a413d7dac000cba5d11124de 100644 (file)
@@ -107,7 +107,7 @@ class Index extends BaseSettings
                        $_REQUEST['profile_field_order']
                );
 
-               DI::profileFieldNew()->saveCollectionForUser(local_user(), $profileFieldsNew);
+               DI::profileField()->saveCollectionForUser(local_user(), $profileFieldsNew);
 
                $result = Profile::update(
                        [
@@ -157,7 +157,7 @@ class Index extends BaseSettings
 
                $custom_fields = [];
 
-               $profileFields = DI::profileFieldNew()->selectByUserId(local_user());
+               $profileFields = DI::profileField()->selectByUserId(local_user());
                foreach ($profileFields as $profileField) {
                        /** @var ProfileField $profileField */
                        $defaultPermissions = $profileField->permissionSet->withAllowedContacts(
index 462460350e86b6fbdd3eb28a2cce880f8425efed..e741789dad217e22913df0bf6dbee8ad093b2f18 100644 (file)
@@ -381,7 +381,7 @@ class Transmitter
 
                $custom_fields = [];
 
-               foreach (DI::profileFieldNew()->selectByContactId(0, $uid) as $profile_field) {
+               foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) {
                        $custom_fields[] = [
                                'type' => 'PropertyValue',
                                'name' => $profile_field->label,
diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php
deleted file mode 100644 (file)
index 3bb4a0b..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * 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 <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Repository;
-
-use Friendica\BaseRepository;
-use Friendica\Core\L10n;
-use Friendica\Database\Database;
-use Friendica\Database\DBA;
-use Friendica\Model;
-use Friendica\Security\PermissionSet\Depository\PermissionSet;
-use Friendica\Util\DateTimeFormat;
-use Psr\Log\LoggerInterface;
-
-class ProfileField extends BaseRepository
-{
-       protected static $table_name = 'profile_field';
-
-       protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class;
-
-       protected static $collection_class = \Friendica\Profile\ProfileField\Collection\ProfileFields::class;
-
-       /** @var PermissionSet */
-       private $permissionSet;
-       /** @var \Friendica\Security\PermissionSet\Factory\PermissionSet */
-       private $permissionSetFactory;
-       /** @var L10n */
-       private $l10n;
-
-       public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet, \Friendica\Security\PermissionSet\Factory\PermissionSet $permissionSetFactory, L10n $l10n)
-       {
-               parent::__construct($dba, $logger);
-
-               $this->permissionSet        = $permissionSet;
-               $this->permissionSetFactory = $permissionSetFactory;
-               $this->l10n                 = $l10n;
-       }
-
-       /**
-        * @param array $fields
-        *
-        * @return \Friendica\Profile\ProfileField\Entity\ProfileField|bool
-        * @throws \Exception
-        */
-       public function insert(array $fields)
-       {
-               $fields['created'] = DateTimeFormat::utcNow();
-               $fields['edited']  = DateTimeFormat::utcNow();
-
-               return parent::insert($fields);
-       }
-
-       /**
-        * Migrates a legacy profile to the new slimmer profile with extra custom fields.
-        * Multi profiles are converted to ACl-protected custom fields and deleted.
-        *
-        * @param array $profile Profile table row
-        * @throws \Exception
-        */
-       public function migrateFromLegacyProfile(array $profile)
-       {
-               // Already processed, aborting
-               if ($profile['is-default'] === null) {
-                       return;
-               }
-
-               $contacts = [];
-
-               if (!$profile['is-default']) {
-                       $contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'profile-id' => $profile['id']]);
-                       if (!count($contacts)) {
-                               // No contact visibility selected defaults to user-only permission
-                               $contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'self' => true]);
-                       }
-               }
-
-               $psid = $this->permissionSet->selectOrCreate(
-                       new \Friendica\Security\PermissionSet\Entity\PermissionSet(
-                               $profile['uid'],
-                               array_column($contacts, 'id') ?? []
-                       )
-               )->id;
-
-               $order = 1;
-
-               $custom_fields = [
-                       'hometown'  => $this->l10n->t('Hometown:'),
-                       'marital'   => $this->l10n->t('Marital Status:'),
-                       'with'      => $this->l10n->t('With:'),
-                       'howlong'   => $this->l10n->t('Since:'),
-                       'sexual'    => $this->l10n->t('Sexual Preference:'),
-                       'politic'   => $this->l10n->t('Political Views:'),
-                       'religion'  => $this->l10n->t('Religious Views:'),
-                       'likes'     => $this->l10n->t('Likes:'),
-                       'dislikes'  => $this->l10n->t('Dislikes:'),
-                       'pdesc'     => $this->l10n->t('Title/Description:'),
-                       'summary'   => $this->l10n->t('Summary'),
-                       'music'     => $this->l10n->t('Musical interests'),
-                       'book'      => $this->l10n->t('Books, literature'),
-                       'tv'        => $this->l10n->t('Television'),
-                       'film'      => $this->l10n->t('Film/dance/culture/entertainment'),
-                       'interest'  => $this->l10n->t('Hobbies/Interests'),
-                       'romance'   => $this->l10n->t('Love/romance'),
-                       'work'      => $this->l10n->t('Work/employment'),
-                       'education' => $this->l10n->t('School/education'),
-                       'contact'   => $this->l10n->t('Contact information and Social Networks'),
-               ];
-
-               foreach ($custom_fields as $field => $label) {
-                       if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) {
-                               $this->insert([
-                                       'uid'   => $profile['uid'],
-                                       'psid'  => $psid,
-                                       'order' => $order++,
-                                       'label' => trim($label, ':'),
-                                       'value' => $profile[$field],
-                               ]);
-                       }
-
-                       $profile[$field] = null;
-               }
-
-               if ($profile['is-default']) {
-                       $profile['profile-name'] = null;
-                       $profile['is-default']   = null;
-                       $this->dba->update('profile', $profile, ['id' => $profile['id']]);
-               } elseif (!empty($profile['id'])) {
-                       $this->dba->delete('profile', ['id' => $profile['id']]);
-               }
-       }
-}
index 6a53c83f167959d2b95f5c5b18a0ab4c6e64da6b..d7598a33f5353e28a89a34c135a270f9cab96b8f 100644 (file)
@@ -206,7 +206,7 @@ function update_1332()
        $profiles = DBA::select('profile', [], $condition);
 
        while ($profile = DBA::fetch($profiles)) {
-               DI::profileField()->migrateFromLegacyProfile($profile);
+               Profile::migrateFromLegacyProfile($profile);
        }
        DBA::close($profiles);