Move common domain-to-network mapping to the plugin module

This commit is contained in:
Evan Prodromou 2011-04-27 14:48:26 -07:00
parent 20e588d212
commit b6bad0232e
3 changed files with 103 additions and 70 deletions

View File

@ -34,6 +34,11 @@ if (!defined('STATUSNET')) {
exit(1);
}
$_dir = dirname(__FILE__);
require_once $_dir . '/extlib/effectiveTLDs.inc.php';
require_once $_dir . '/extlib/regDomain.inc.php';
/**
* Tools to map one status_network to one email domain in a multi-site
* installation.
@ -48,8 +53,20 @@ if (!defined('STATUSNET')) {
class DomainStatusNetworkPlugin extends Plugin
{
static $_thetree = null;
function initialize()
{
// For various reasons this gets squished
global $tldTree;
if (empty($tldTree)) {
if (!empty(self::$_thetree)) {
$tldTree = self::$_thetree;
}
}
$nickname = StatusNet::currentSite();
if (empty($nickname)) {
@ -73,6 +90,78 @@ class DomainStatusNetworkPlugin extends Plugin
}
}
static function toDomain($raw)
{
$parts = explode('@', $raw);
if (count($parts) == 1) {
$domain = $parts[0];
} else {
$domain = $parts[1];
}
$domain = strtolower(trim($domain));
return $domain;
}
static function registeredDomain($domain)
{
return getRegisteredDomain($domain);
}
static function nicknameAvailable($nickname)
{
$sn = Status_network::staticGet('nickname', $nickname);
return empty($sn);
}
static function nicknameForDomain($domain)
{
$registered = self::registeredDomain($domain);
$parts = explode('.', $registered);
$base = $parts[0];
if (self::nicknameAvailable($base)) {
return $base;
}
$domainish = str_replace('.', '-', $registered);
if (self::nicknameAvailable($domainish)) {
return $domainish;
}
$i = 1;
// We don't need to keep doing this forever
while ($i < 1024) {
$candidate = $domainish.'-'.$i;
if (self::nicknameAvailable($candidate)) {
return $candidate;
}
$i++;
}
return null;
}
static function siteForDomain($domain)
{
$snt = Status_network_tag::withTag('domain='.$domain);
while ($snt->fetch()) {
$sn = Status_network::staticGet('site_id', $snt->site_id);
if (!empty($sn)) {
return $sn;
}
}
return null;
}
function onPluginVersion(&$versions)
{
$versions[] = array('name' => 'DomainStatusNetwork',
@ -84,3 +173,8 @@ class DomainStatusNetworkPlugin extends Plugin
return true;
}
}
// The way addPlugin() works, this global variable gets disappeared.
// So, we re-appear it.
DomainStatusNetworkPlugin::$_thetree = $tldTree;

View File

@ -29,65 +29,13 @@ Prints site information for the domain given
END_OF_SITEFORDOMAIN_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
require_once INSTALLDIR.'/plugins/EmailRegistration/extlib/effectiveTLDs.inc.php';
require_once INSTALLDIR.'/plugins/EmailRegistration/extlib/regDomain.inc.php';
function nicknameAvailable($nickname)
{
$sn = Status_network::staticGet('nickname', $nickname);
return !empty($sn);
}
$domain = DomainStatusNetworkPlugin::toDomain($args[0]);
function nicknameForDomain($domain)
{
global $tldTree;
$registered = getRegisteredDomain($domain, $tldTree);
$parts = explode('.', $registered);
$base = $parts[0];
if (nicknameAvailable($base)) {
return $base;
}
$domainish = str_replace('.', '-', $registered);
if (nicknameAvailable($domainish)) {
return $domainish;
}
$i = 1;
// We don't need to keep doing this forever
while ($i < 1024) {
$candidate = $domainish.'-'.$i;
if (nicknameAvailable($candidate)) {
return $candidate;
}
}
return null;
}
$raw = $args[0];
$parts = explode('@', $raw);
if (count($parts) == 1) {
$domain = $parts[0];
} else {
$domain = $parts[1];
}
$domain = strtolower(trim($domain));
$nickname = nicknameForDomain($domain);
$nickname = DomainStatusNetworkPlugin::nicknameForDomain($domain);
if (empty($nickname)) {
throw ClientException("No candidate found.");
throw new ClientException("No candidate found.");
} else {
print $nickname;
print "\n";

View File

@ -30,23 +30,14 @@ END_OF_SITEFORDOMAIN_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
$raw = $args[0];
$domain = DomainStatusNetworkPlugin::toDomain($args[0]);
$parts = explode('@', $raw);
$sn = DomainStatusNetworkPlugin::siteForDomain($domain);
if (count($parts) == 1) {
$domain = $parts[0];
} else {
$domain = $parts[1];
if (empty($sn)) {
exit(1);
}
$domain = strtolower(trim($domain));
$snt = Status_network_tag::withTag('domain='.$domain);
while ($snt->fetch()) {
$sn = Status_network::staticGet('site_id', $snt->site_id);
if (!empty($sn)) {
print $sn->nickname."\n";
}
}
exit(0);