Skip to content

Commit

Permalink
Merge pull request #30 from OpenPlaceGuide/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
amenk authored Sep 14, 2024
2 parents 3b12882 + 7873acf commit 74bb9e0
Show file tree
Hide file tree
Showing 7 changed files with 1,375 additions and 983 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ EOF

EXPOSE 8000

RUN apk add --no-cache zip php-8.1 php-8.1-intl php-8.1-gd php-8.1-cgi php-8.1-phar php-8.1-iconv php-8.1-mbstring php-8.1-openssl php-8.1-dom php-8.1-curl
RUN apk add --no-cache zip php-8.1 php-8.1-intl php-8.1-gd php-8.1-cgi php-8.1-phar php-8.1-iconv php-8.1-mbstring php-8.1-openssl php-8.1-dom php-8.1-curl php-8.1-simplexml

#ENV LOG_CHANNEL=stderr

Expand Down
90 changes: 90 additions & 0 deletions app/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers;

use App\Services\Repository;
use DOMDocument;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\App;
use SimpleXMLElement;

class SitemapController extends BaseController
{
private Repository $repository;

public function __construct()
{
$repositoryName = 'ethiopia';
$this->repository = new Repository($repositoryName);
}

public function index()
{
$urls = array_merge(
$this->getAreaUrls(),
$this->getPlaceUrls(),
$this->getTypeUrls()
);

echo $this->generateSitemap($urls);
}

private function generateSitemap($urls) {
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset></urlset>');
$xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($urls as $url) {
$urlElement = $xml->addChild('url');
$urlElement->addChild('loc', htmlspecialchars($url));
}

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());

return $dom->saveXML();
}


private function getAreaUrls()
{
$areas = $this->repository->listAreas();
return $this->mapUrls($areas);
}

private function mapUrls($items)
{
$urls = array_map(function ($item): string {
return $item->getUrl();
}, $items);
sort($urls);
$urls = array_unique($urls);
return $urls;
}

private function getPlaceUrls()
{
$places = $this->repository->listPlaceIndex();
return $this->mapUrls($places);
}

private function getTypeUrls()
{
$types = $this->repository->listTypes();

$urls = [];
foreach ($types as $item) {
foreach ($this->repository->listAreas() as $area) {
$urls[] = route('typesInArea.' . App::currentLocale(), [
'typeSlug' => $item->slug,
'areaSlug' => $area->slug
]);
}
}

sort($urls);
$urls = array_unique($urls);
return $urls;
}
}
2 changes: 1 addition & 1 deletion app/Models/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getKeys()
return $keys;
}

public function getUrl(?OsmId $branch)
public function getUrl(?OsmId $branch = null)
{
$url = route('page.' . App::currentLocale(), ['slug' => $this->slug]);
if ($branch !== null) {
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"laravel/framework": "^10.0",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
"symfony/yaml": "^6.2"
"symfony/yaml": "^6.2",
"ext-simplexml": "*",
"ext-dom": "*"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
Loading

0 comments on commit 74bb9e0

Please sign in to comment.