Skip to content

Commit

Permalink
Added basic sitemap generate ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
berkanumutlu committed Mar 14, 2024
1 parent f496113 commit e613421
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
"autoload": {
"psr-4": {
"App\\": "src/",
"App\\Library\\": "src/lib/"
"App\\Library\\": "src/lib/",
"App\\Config\\": "src/config/"
}
},
"require": {
"ext-json": "*"
"ext-json": "*",
"ext-pdo": "*"
}
}
72 changes: 72 additions & 0 deletions src/ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
define('BASE_PATH', __DIR__.'/');
require_once '../vendor/autoload.php';
require_once 'config/db.php';

use App\Library\SitemapGenerator;

if (!empty($_POST['sitemap'])) {
$response = new \App\Library\Response();
$sitemap_generator = new SitemapGenerator();
try {
$sitemap_generator->getSitemap()->setHttpSecure(!empty($_POST['http_secure']));
if (!empty($_POST['domain'])) {
$sitemap_generator->getSitemap()->setDomain(trim($_POST['domain']));
}
if (!empty($_POST['last_mod'])) {
$sitemap_generator->setLastMod(trim($_POST['last_mod']));
}
if (!empty($_POST['change_freq'])) {
$sitemap_generator->setChangeFreq(trim($_POST['change_freq']));
}
if (!empty($_POST['priority'])) {
$sitemap_generator->setPriority(trim($_POST['priority']));
}
if (!empty($_POST['file_path'])) {
$sitemap_generator->getSitemap()->setFilePath(BASE_PATH.trim($_POST['file_path']));
}
if (!empty($_POST['file_name'])) {
$sitemap_generator->getSitemap()->setFileName(trim($_POST['file_name']));
}
if (!empty($_POST['file_ext'])) {
$sitemap_generator->getSitemap()->setFileExt(trim($_POST['file_ext']));
}
if (!empty($_POST['file_header'])) {
$sitemap_generator->getSitemap()->setHeader(trim($_POST['file_header']));
}
if (!empty($_POST['file_urlset_header'])) {
$sitemap_generator->getSitemap()->setUrlsetHeader(trim($_POST['file_urlset_header']));
}
if (!empty($_POST['file_urlset_footer'])) {
$sitemap_generator->getSitemap()->setUrlsetFooter(trim($_POST['file_urlset_footer']));
}
$query_pages = $db->query("SELECT * from tbl_pages", PDO::FETCH_ASSOC);
if ($query_pages && $query_pages->rowCount()) {
$pages = $query_pages->fetchAll(PDO::FETCH_ASSOC);
foreach ($pages as $page) {
$sitemap_generator->set_url_loc($page['slug']);
$sitemap_generator->set_url_last_mod(!empty($page['updated_at']) ? $page['updated_at'] : $page['created_at']);
$sitemap_generator->set_url_priority(0.8);
$sitemap_generator->add_url_to_list();
}
}
$query_products = $db->query("SELECT * from tbl_products", PDO::FETCH_ASSOC);
if ($query_products && $query_products->rowCount()) {
$products = $query_products->fetchAll(PDO::FETCH_ASSOC);
foreach ($products as $product) {
$sitemap_generator->set_url_loc('product-detail/'.$product['slug']);
$sitemap_generator->set_url_last_mod(!empty($product['updated_at']) ? $product['updated_at'] : $product['created_at']);
$sitemap_generator->set_url_priority(1);
$sitemap_generator->add_url_to_list();
}
}
$response = $sitemap_generator->generate();
} catch (\Exception $e) {
$response->setStatus(false);
$response->setStatusCode(500);
$response->setStatusText($e->getMessage());
$response->setMessage('The sitemap could not be created.');
}
echo $response->toJson();
return true;
}
16 changes: 16 additions & 0 deletions src/config/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php namespace App\Config\DB;

use PDO;
use PDOException;

$host = 'mysql';
$database = 'test';
$username = 'test_user';
$password = 'test_password';
$charset = 'UTF8';
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=$charset", $username, $password);
} catch (PDOException $e) {
echo('DB connection error: '.$e->getMessage());
exit();
}
18 changes: 9 additions & 9 deletions src/db/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Anamakine: mysql
-- Üretim Zamanı: 14 Mar 2024, 10:26:58
-- Üretim Zamanı: 14 Mar 2024, 15:56:28
-- Sunucu sürümü: 5.7.43
-- PHP Sürümü: 8.2.12

Expand All @@ -24,10 +24,10 @@ SET time_zone = "+00:00";
-- --------------------------------------------------------

--
-- Tablo için tablo yapısı `tbl_page`
-- Tablo için tablo yapısı `tbl_pages`
--

CREATE TABLE `tbl_page` (
CREATE TABLE `tbl_pages` (
`id` int(11) NOT NULL,
`language_group_id` int(11) NOT NULL,
`language_id` int(11) NOT NULL,
Expand All @@ -39,10 +39,10 @@ CREATE TABLE `tbl_page` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Tablo döküm verisi `tbl_page`
-- Tablo döküm verisi `tbl_pages`
--

INSERT INTO `tbl_page` (`id`, `language_group_id`, `language_id`, `title`, `slug`, `description`, `created_at`, `updated_at`) VALUES
INSERT INTO `tbl_pages` (`id`, `language_group_id`, `language_id`, `title`, `slug`, `description`, `created_at`, `updated_at`) VALUES
(1, 1, 1, 'About Us', 'about-us', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', '2024-03-08 13:10:58', NULL),
(2, 1, 2, 'Über uns', 'uber-uns', 'Lorem Ipsum ist ein einfacher Demo-Text für die Print- und Schriftindustrie. Lorem Ipsum ist in der Industrie bereits der Standard Demo-Text seit 1500, als ein unbekannter Schriftsteller eine Hand voll Wörter nahm und diese durcheinander warf um ein Musterbuch zu erstellen. Es hat nicht nur 5 Jahrhunderte überlebt, sondern auch in Spruch in die elektronische Schriftbearbeitung geschafft (bemerke, nahezu unverändert). Bekannt wurde es 1960, mit dem erscheinen von \"Letraset\", welches Passagen von Lorem Ipsum enhielt, so wie Desktop Software wie \"Aldus PageMaker\" - ebenfalls mit Lorem Ipsum.', '2024-03-08 13:10:58', NULL),
(3, 1, 3, 'À propos de nous', 'a-propos-de-nous', 'Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l\'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n\'a pas fait que survivre cinq siècles, mais s\'est aussi adapté à la bureautique informatique, sans que son contenu n\'en soit modifié. Il a été popularisé dans les années 1960 grâce à la vente de feuilles Letraset contenant des passages du Lorem Ipsum, et, plus récemment, par son inclusion dans des applications de mise en page de texte, comme Aldus PageMaker.', '2024-03-08 13:10:58', NULL),
Expand Down Expand Up @@ -105,9 +105,9 @@ INSERT INTO `tbl_products` (`id`, `language_group_id`, `language_id`, `name`, `s
--

--
-- Tablo için indeksler `tbl_page`
-- Tablo için indeksler `tbl_pages`
--
ALTER TABLE `tbl_page`
ALTER TABLE `tbl_pages`
ADD PRIMARY KEY (`id`);

--
Expand All @@ -121,9 +121,9 @@ ALTER TABLE `tbl_products`
--

--
-- Tablo için AUTO_INCREMENT değeri `tbl_page`
-- Tablo için AUTO_INCREMENT değeri `tbl_pages`
--
ALTER TABLE `tbl_page`
ALTER TABLE `tbl_pages`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;

--
Expand Down
12 changes: 7 additions & 5 deletions src/lib/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class SitemapGenerator
* @var Response
*/
private $response;
/**
* @var string|null
*/
private $base_url;
/**
* @var array
*/
Expand Down Expand Up @@ -82,6 +86,7 @@ public function __construct()
{
$this->sitemap = new Sitemap();
$this->response = new Response();
$this->base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http").'://'.$_SERVER['HTTP_HOST'];
}

/**
Expand Down Expand Up @@ -191,7 +196,7 @@ public function get_url_loc()
public function set_url_loc($url_loc)
{
if (strpos($url_loc, $this->getSitemap()->getDomain()) == false) {
$url_loc .= $this->getSitemap()->getDomain().$url_loc;
$url_loc = $this->getSitemap()->getDomain().'/'.$url_loc;
}
$this->url['loc'] = $url_loc;
}
Expand Down Expand Up @@ -294,10 +299,7 @@ public function write($file_name, $file_path, $file_ext, $file_data)
$full_path = $file_path.$file_name.$file_ext;
if ($create_file_path->isStatus()) {
$path_info = pathinfo($full_path);
$httpProtocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$domain = $_SERVER['HTTP_HOST'];
$base_url = $httpProtocol.'://'.$domain;
$file_url = $base_url.str_replace($_SERVER["DOCUMENT_ROOT"], '',
$file_url = $this->base_url.str_replace($_SERVER["DOCUMENT_ROOT"], '',
$path_info['dirname']).'/'.$path_info['basename'].'?v='.$this->response->getDate();
file_put_contents($full_path, $file_data);
if (file_exists($full_path)) {
Expand Down

0 comments on commit e613421

Please sign in to comment.