#24: Finished implementing the sitemap.xml creation logic
This commit is contained in:
parent
61a76ac8a7
commit
0371fb0d95
@ -7,6 +7,7 @@ use App\Facade\Theme;
|
|||||||
use App\Facade\UserConfig;
|
use App\Facade\UserConfig;
|
||||||
use App\Helpers\DbHelper;
|
use App\Helpers\DbHelper;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Photo;
|
||||||
use App\VisitorHit;
|
use App\VisitorHit;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
@ -42,19 +43,97 @@ class DefaultController extends Controller
|
|||||||
$xml->formatOutput = true;
|
$xml->formatOutput = true;
|
||||||
|
|
||||||
$root = $xml->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
|
$root = $xml->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
|
||||||
|
$root->appendChild($xml->createAttribute('xmlns:image'))
|
||||||
|
->appendChild($xml->createTextNode('http://www.google.com/schemas/sitemap-image/1.1'));
|
||||||
$xml->appendChild($root);
|
$xml->appendChild($root);
|
||||||
|
|
||||||
// Root URL
|
// Root URL (get last modified photo to create the lastmod date for this URL)
|
||||||
$this->createSitemapNode($xml, $root, route('home'));
|
$lastModifiedPhoto = Photo::orderBy('updated_at', 'desc')->first();
|
||||||
|
$this->createSitemapNode($xml, $root, route('home'), (is_null($lastModifiedPhoto) ? '' : $lastModifiedPhoto->updated_at), '1.0');
|
||||||
|
|
||||||
|
// Add each album URL
|
||||||
|
$albums = Album::orderBy('name');
|
||||||
|
$albums->chunk(100, function($albumsChunk) use ($xml, $root)
|
||||||
|
{
|
||||||
|
/** @var Album $album */
|
||||||
|
foreach ($albumsChunk as $album)
|
||||||
|
{
|
||||||
|
$lastModifiedPhoto = Photo::where('album_id', $album->id)->orderBy('updated_at', 'desc')->first();
|
||||||
|
$this->createSitemapNode($xml, $root, $album->url(), (is_null($lastModifiedPhoto) ? $album->updated_at : $lastModifiedPhoto->updated_at), '0.9');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add each photo URL
|
||||||
|
$photos = Photo::orderBy('name');
|
||||||
|
$photos->chunk(100, function($tempPhotos) use ($xml, $root)
|
||||||
|
{
|
||||||
|
/** @var Photo $photo */
|
||||||
|
foreach ($tempPhotos as $photo)
|
||||||
|
{
|
||||||
|
$photoMeta = [$photo->name];
|
||||||
|
if (strlen($photo->description) > 0)
|
||||||
|
{
|
||||||
|
$photoMeta[] = $photo->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->createSitemapNode(
|
||||||
|
$xml,
|
||||||
|
$root,
|
||||||
|
$photo->url(),
|
||||||
|
$photo->updated_at,
|
||||||
|
'0.8',
|
||||||
|
$photo->thumbnailUrl('fullsize', false),
|
||||||
|
join(' - ', $photoMeta)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return response($xml->saveXML(), 200, ['Content-Type' => 'text/xml']);
|
return response($xml->saveXML(), 200, ['Content-Type' => 'text/xml']);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSitemapNode(\DOMDocument $document, \DOMElement $parentElement, $url)
|
private function createSitemapNode(
|
||||||
|
\DOMDocument $document,
|
||||||
|
\DOMElement $parentElement,
|
||||||
|
$url,
|
||||||
|
$lastModifiedDate = '',
|
||||||
|
$priority = '',
|
||||||
|
$imageUrl = '',
|
||||||
|
$imageCaption = ''
|
||||||
|
)
|
||||||
{
|
{
|
||||||
$urlElement = $document->createElement('url');
|
$urlElement = $document->createElement('url');
|
||||||
$urlLocElement = $document->createElement('loc', $url);
|
|
||||||
|
$urlLocElement = $document->createElement('loc', htmlentities($url));
|
||||||
$urlElement->appendChild($urlLocElement);
|
$urlElement->appendChild($urlLocElement);
|
||||||
|
|
||||||
|
$changeFreqElement = $document->createElement('changefreq', 'weekly');
|
||||||
|
$urlElement->appendChild($changeFreqElement);
|
||||||
|
|
||||||
|
if (strlen($lastModifiedDate) > 0)
|
||||||
|
{
|
||||||
|
$lastModElement = $document->createElement('lastmod', $lastModifiedDate);
|
||||||
|
$urlElement->appendChild($lastModElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen($priority) > 0)
|
||||||
|
{
|
||||||
|
$priorityElement = $document->createElement('priority', $priority);
|
||||||
|
$urlElement->appendChild($priorityElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen($imageUrl) > 0)
|
||||||
|
{
|
||||||
|
$imageElement = $document->createElement('image:image');
|
||||||
|
|
||||||
|
$imageLocElement = $document->createElement('image:loc', htmlentities($imageUrl));
|
||||||
|
$imageElement->appendChild($imageLocElement);
|
||||||
|
|
||||||
|
$imageCaptionElement = $document->createElement('image:caption', htmlentities($imageCaption));
|
||||||
|
$imageElement->appendChild($imageCaptionElement);
|
||||||
|
|
||||||
|
$urlElement->appendChild($imageElement);
|
||||||
|
}
|
||||||
|
|
||||||
$parentElement->appendChild($urlElement);
|
$parentElement->appendChild($urlElement);
|
||||||
|
|
||||||
return $urlElement;
|
return $urlElement;
|
||||||
|
Loading…
Reference in New Issue
Block a user