Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as PHP by ugl ( 15 years ago )
#!/usr/bin/env php
<?php
$db = "jirafe";
$mongo = new Mongo();
$siteC = $mongo->selectCollection($db, 'sites');
$appC = $mongo->selectCollection($db, 'applications');
$userC = $mongo->selectCollection($db, 'users');
$tokenC = $mongo->selectCollection($db, 'tokens');
function findSitesWithoutApplications()
{
global $siteC, $appC;
$sites = iterator_to_array($siteC->find(array(), array('application' => true)));
$appIds = iterator_to_array($appC->find(array(), array('_id' => true)));
return array_filter($sites, function($site) use ($appIds) {
return !isset($site['application']['$id'])
|| !isset($appIds[$site['application']['$id']]);
});
}
function remove(array $objs, $collection)
{
$ids = array_map(function($obj) { return $obj['_id']; }, $objs);
$collection->remove(array('_id' => array('$in' => $ids)));
}
function findSoftdeleteBug($collection)
{
$objs = iterator_to_array($collection->find(array(), array('url' => true)));
return array_filter($objs, function($obj) {
return !isset($obj['url']) || 0 !== strpos($obj['url'], 'http');
});
}
function findUsersWithNoToken()
{
global $userC, $tokenC;
$users = iterator_to_array($userC->find(array(), array('_id' => true)));
$tokens = iterator_to_array($tokenC->find(
array('user' => array('$exists' => true)),
array('user' => true)
));
$tokenUserIds = array();
foreach ($tokens as $token) {
$tokenUserIds[$token['user']['$id']] = true;
}
return array_filter($users, function($user) use ($tokenUserIds) {
return !isset($tokenUserIds[$user['_id']]);
});
}
function findAppsWithNoToken()
{
global $appC, $tokenC;
$apps = iterator_to_array($appC->find(array(), array('_id' => true)));
$tokens = iterator_to_array($tokenC->find(
array('application' => array('$exists' => true)),
array('application' => true)
));
$tokenAppIds = array();
foreach ($tokens as $token) {
$tokenAppIds[$token['application']['$id']] = true;
}
return array_filter($apps, function($app) use ($tokenAppIds) {
return !isset($tokenAppIds[$app['_id']]);
});
}
function findTokensWithNoUserAndNoApp()
{
global $appC, $userC, $tokenC;
$appIds = iterator_to_array($appC->find(array(), array('_id' => true)));
$userIds = iterator_to_array($userC->find(array(), array('_id' => true)));
$tokens = iterator_to_array($tokenC->find(array(), array('user' => true, 'application' => true)));
return array_filter($tokens, function($token) use($appIds, $userIds) {
$tokenAppId = isset($token['application']['$id']) ? $token['application']['$id'] : null;
$tokenUserId = isset($token['user']['$id']) ? $token['user']['$id'] : null;
return ($tokenAppId == null || $tokenUserId == null)
|| (!isset($appIds[$tokenAppId]) && !isset($userIds[$tokenUserId]));
});
}
function showUnfixableUrls(array $objs, $name)
{
return sprintf("%d unfixable %s urls:n%s",
count($objs),
$name,
implode(array_map(function($obj) {
return sprintf("id: %s, url: %s", $obj['_id'], $obj['url']);
}, $objs), "n")
);
}
printf("There are %d sites in %d applicationsn", $siteC->count(), $appC->count());
print "n";
$appLessSites = findSitesWithoutApplications();
printf("Removing %d app-less sites n", count($appLessSites));
print "n";
remove($appLessSites, $siteC);
$softdeleteBugSites = findSoftdeleteBug($siteC);
printf("Removing %d sites which urls start with a number (softdelete bug)n", count($softdeleteBugSites));
print "n";
remove($softdeleteBugSites, $siteC);
$usersWithNoToken = findUsersWithNoToken();
printf("Removing %d token-less users n", count($usersWithNoToken));
print "n";
remove($usersWithNoToken, $userC);
$appsWithNoToken = findAppsWithNoToken();
printf("Removing %d token-less applications n", count($appsWithNoToken));
print "n";
remove($appsWithNoToken, $appC);
$orphanTokens = findTokensWithNoUserAndNoApp();
printf("Removing %d user-less AND application-less tokens n", count($orphanTokens));
print "n";
remove($orphanTokens, $tokenC);
print "Done.";
Revise this Paste