Adds sites.root_domain (indexed string, backfilled for existing rows)
computed via a naive "last two labels" heuristic (App\Support\RootDomain)
— good enough for example.com/somesite.es, but wrong for two-part
public suffixes like .co.uk. Doing that correctly needs the Mozilla
Public Suffix List; flagged as a known simplification rather than
built now.
Deliberately not a hard FK: like Backlink.target_domain, a subdomain
can get indexed before its root domain does (or vice versa), so
Site::subdomains()/rootSite() match on the domain string rather than
requiring the relationship to exist at write time.
Site detail page: a root domain shows a paginated "Subdomains"
section (same guest-preview cap as Pages/Backlinks); a subdomain
shows "Subdomain of {root}" linking back, when the root is itself
indexed.
Verified against real data: 21k.ro correctly lists its two known
subdomains (buzau.21k.ro, timisoara.21k.ro), each of which correctly
links back to 21k.ro. Added SubdomainsTest (root lists subdomains,
subdomain links to root, empty state) — full suite (31 tests) passes.
56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Site extends Model
|
|
{
|
|
protected $fillable = [
|
|
'collection',
|
|
'domain',
|
|
'root_domain',
|
|
'url',
|
|
'title',
|
|
'description',
|
|
'keywords',
|
|
'server',
|
|
'powered_by',
|
|
'generator',
|
|
];
|
|
|
|
public function pages(): HasMany
|
|
{
|
|
return $this->hasMany(SitePage::class);
|
|
}
|
|
|
|
/** Links this site's pages make out to other domains. */
|
|
public function outboundLinks(): HasMany
|
|
{
|
|
return $this->hasMany(Backlink::class);
|
|
}
|
|
|
|
/** Backlinks pointing at this site, i.e. rows recorded against its domain as the target. */
|
|
public function inboundLinks(): HasMany
|
|
{
|
|
return $this->hasMany(Backlink::class, 'target_domain', 'domain');
|
|
}
|
|
|
|
/** Other indexed sites whose root domain is this site's domain, e.g. blog.example.com under example.com. */
|
|
public function subdomains(): HasMany
|
|
{
|
|
return $this->hasMany(Site::class, 'root_domain', 'domain')->where('id', '!=', $this->id);
|
|
}
|
|
|
|
/** The root-domain Site this one is a subdomain of, if it's indexed and if this site even is a subdomain. */
|
|
public function rootSite(): ?Site
|
|
{
|
|
if ($this->domain === $this->root_domain) {
|
|
return null;
|
|
}
|
|
|
|
return static::where('domain', $this->root_domain)->first();
|
|
}
|
|
}
|