Skip to content

Commit 9f7c7c9

Browse files
committed
Script: User: Use searched field as filter, fix syntax errors, add filter on URLs - refs BT#22702
1 parent e6f7589 commit 9f7c7c9

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

tests/scripts/unify_duplicated_user_based_on_extra_field_value.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,75 @@
77
* based on this extra field values and unify them on the most recent account
88
*/
99
exit;
10+
$now = date('Y-m-d H:i:s');
11+
echo $now . " Starting " . __FILE__ . PHP_EOL;
1012
require __DIR__.'/../../main/inc/global.inc.php';
1113

12-
// Filter to only manage those users based on their user_id :
13-
//$filteredUsers = [11,12,13]; // set the list of user_id
14+
// Filter to only manage those users based on the duplicate field's value:
15+
//$filteredUsers = ['XYZ1','XYZ2','XYZ3']; // set the list of values of the duplicated field
16+
if (!empty($filteredUsers)) {
17+
echo "Filtered users list is defined: ";
18+
foreach ($filteredUsers as $u) {
19+
echo $u . ' ';
20+
}
21+
echo PHP_EOL;
22+
}
23+
// Filter to only manage users from one sub-URL
24+
//$filteredUrls = [4];
25+
if (!empty($filteredUrls)) {
26+
echo "Filtered URLs list is defined: ";
27+
foreach ($filteredUrls as $u) {
28+
echo $u . ' ';
29+
}
30+
echo PHP_EOL;
31+
}
1432

15-
// set the extra field variable to use
33+
// set the extra field variable to use
1634
$extraFieldVariable = 'dni';
1735

1836
// define if the unified accounts should be deleted or deactivated
1937
$unifyMode = 'delete'; // 'delete' or 'deactivate'
2038

2139
$fieldInfo = MySpace::duGetUserExtraFieldByVariable($extraFieldVariable);
2240
if (empty($fieldInfo)) {
23-
echo 'ExtraField not found : '. $extraFieldVariable . PHP_EOL;
41+
echo 'ExtraField not found: ' . $extraFieldVariable . PHP_EOL;
2442
exit;
2543
}
2644

2745
$fieldId = (int) $fieldInfo['id'];
2846
$accessURLs = api_get_access_urls();
29-
foreach($accessURLs as $accessURL) {
47+
foreach ($accessURLs as $accessURL) {
3048
$urlId = $accessURL['id'];
31-
echo 'Searching duplicates on ' . $accessURL['url'] . ' with id = ' . $urlId . PHP_EOL;
49+
if (!empty($filteredUrls) && !in_array($urlId, $filteredUrls)) {
50+
echo "URL " . $accessURL['url'] . " not in filtered list, skipping..." . PHP_EOL;
51+
continue;
52+
}
53+
echo 'Searching duplicates on ' . $accessURL['url'] . ' with id = ' . $urlId . PHP_EOL;
3254
$dups = MySpace::duGetDuplicateValues($fieldId, $urlId);
3355
if (empty($dups)) {
3456
echo 'No duplicates found' . PHP_EOL;
3557
} else {
3658
foreach ($dups as $g) {
3759
$value = $g['the_value'];
38-
echo 'Analysing duplicates of ' . $value . PHP_EOL;
60+
if (!empty($filteredUsers) && !in_array($value, $filteredUsers)) {
61+
continue;
62+
} else {
63+
echo "Value $value is in the filtered list. Proceeding..." . PHP_EOL;
64+
}
65+
echo 'Analysing duplicates of ' . $value . ' ...' . PHP_EOL;
3966
$users = MySpace::duGetUsersByFieldValue($fieldId, $urlId, $value);
4067
$userIdToUnifyOn = 0;
4168
$mostRecentRegistrationDate = 0;
4269
foreach ($users as $u) {
4370
$uid = (int)$u['user_id'];
44-
if (isset($filteredUsers) && !in_array($uid, $filteredUsers) {
45-
continue;
46-
}
4771
$userInfo = api_get_user_info($uid);
48-
if ($userInfo['registration_date'] > $mostRecentRegistrationDate) {
72+
if ($userInfo['registration_date'] > $mostRecentRegistrationDate) {
4973
$mostRecentRegistrationDate = $userInfo['registration_date'];
5074
$userIdToUnifyOn = $uid;
5175
}
5276
}
5377
foreach ($users as $u) {
5478
$uid = (int)$u['user_id'];
55-
if (isset($filteredUsers) && !in_array($uid, $filteredUsers) {
56-
continue;
57-
}
5879
if ($uid === $userIdToUnifyOn) { continue; }
5980
$now = date('Y-m-d H:i:s');
6081
echo $now . ' Unifying user ' . $uid . ' on user ' . $userIdToUnifyOn . PHP_EOL;

0 commit comments

Comments
 (0)