latest changes

This commit is contained in:
Tobias Merkl 2020-08-17 15:13:10 +02:00
parent 7fcf810ce4
commit 6d955441b4
No known key found for this signature in database
GPG Key ID: B971FFBCFA6D09BC
4 changed files with 52 additions and 40 deletions

View File

@ -23,7 +23,7 @@ $aLang = [
'SHOP_MODULE_psCacheWarmerKey' => 'Cronjob-Key', 'SHOP_MODULE_psCacheWarmerKey' => 'Cronjob-Key',
'SHOP_MODULE_psCacheWarmerUser' => 'Basic-Auth Benutzer', 'SHOP_MODULE_psCacheWarmerUser' => 'Basic-Auth Benutzer',
'SHOP_MODULE_psCacheWarmerPass' => 'Basic-Auth Kennwort', 'SHOP_MODULE_psCacheWarmerPass' => 'Basic-Auth Kennwort',
'SHOP_MODULE_psCacheWarmerWriteCsv' => 'Report als CSV-Datei im Log-Ordner abspeichern', 'SHOP_MODULE_psCacheWarmerWriteCsv' => 'Logfile für gesamten Crawl',
'SHOP_MODULE_psCacheWarmerWriteCsvOnlyError' => 'Alle Ergebnisse ausser HTTP-Code 200 loggen', 'SHOP_MODULE_psCacheWarmerWriteCsvOnlyError' => 'Logfile nur bei HTTP-Codes != 200',
]; ];

View File

@ -13,40 +13,28 @@
namespace ProudCommerce\CacheWarmer\Core; namespace ProudCommerce\CacheWarmer\Core;
if (PHP_SAPI != 'cli') {
throw new RuntimeException('Only cli execution allowed!');
}
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Controller\BaseController;
/** /**
* Class CacheWarmer * Class CacheWarmer
* @package ProudCommerce\CacheWarmer\Core * @package ProudCommerce\CacheWarmer\Core
*/ */
class CacheWarmer extends BaseController class CacheWarmer
{ {
/** /**
* @return string|void|null *
*/ */
public function render() public function run()
{ {
$sMessage = "<b>psCacheWarmer</b><br>" . $this->_getSitemapUrl() . "<br>---<br>";
$aUrls = $this->_getSitemapContent(); $aUrls = $this->_getSitemapContent();
if (!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && count($aUrls) > 0) { if (!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && !empty($aUrls)) {
foreach ($aUrls as $sUrl) { foreach ($aUrls as $sUrl) {
$oCurl = $this->_runCurlConnect($sUrl); $oCurl = $this->_runCurlConnect($sUrl);
$sMessage .= $this->_checkCurlResults($oCurl, $sUrl); $this->_checkCurlResults($oCurl, $sUrl);
curl_close($oCurl); curl_close($oCurl);
} }
} else {
$sMessage .= '<span style="color: red;">Keine Daten vorhanden!</span>';
} }
echo '<pre>' . $sMessage . '</pre>';
exit;
} }
/** /**
@ -70,40 +58,29 @@ class CacheWarmer extends BaseController
/** /**
* @param $oCurl * @param $oCurl
* @param $sUrl * @param $sUrl
* @return string
*/ */
protected function _checkCurlResults($oCurl, $sUrl) protected function _checkCurlResults($oCurl, $sUrl)
{ {
$sMessage = '';
$httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); $httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
if (curl_error($oCurl)) { if (curl_error($oCurl)) {
$sMessage .= '<span style="color: orange;">ERROR ' . $httpStatus . ': ' . curl_error($oCurl) . '</span><br>';
$sStatusMsg = 'ERROR'; $sStatusMsg = 'ERROR';
$sTmpText = curl_error($oCurl); $sMessage = curl_error($oCurl);
} else { } else {
$sTmpText = $sUrl; $sMessage = $sUrl;
if (in_array(trim($httpStatus), Registry::getConfig()->getShopConfVar('psCacheWarmerConfig'))) { if (in_array(trim($httpStatus), Registry::getConfig()->getShopConfVar('psCacheWarmerHttpCodes'))) {
$sMessage .= '<span style="color: green;">OK ' . $httpStatus . ': ' . $sUrl . '</span><br>';
$sStatusMsg = 'OK'; $sStatusMsg = 'OK';
} else { } else {
$sMessage .= '<span style="color: red;">ERROR <b>' . $httpStatus . '</b>: ' . $sUrl . '</span><br>';
$sStatusMsg = 'ERROR'; $sStatusMsg = 'ERROR';
} }
} }
$httpStatus = trim($httpStatus);
$aLog = [$sStatusMsg, $httpStatus, $sMessage];
print_r($aLog);
if (Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) { if (!empty($aLog) && ((Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true && $httpStatus != '200') || Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true)) {
$aTmp = [$sStatusMsg, $httpStatus, $sTmpText]; Registry::getUtils()->writeToLog(implode(' | ', $aLog) . "\r", 'pscachewarmer_' . date("d.m.Y H:i:s") . '.log');
if (trim($httpStatus) == '200' && Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true) {
$aTmp = [];
}
if (count($aTmp)) {
Registry::getUtils()->writeToLog(implode(' | ', $aTmp) . "\r", 'tabslbillomat_' . date("d.m.Y H:i:s") . '.log');
}
} }
return $sMessage;
} }
/** /**
@ -121,6 +98,8 @@ class CacheWarmer extends BaseController
$sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass'); $sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass');
$sSitemapUrl = str_replace("://", "://" . $sUsername . ":" . $sPassword . "@", $sSitemapUrl); $sSitemapUrl = str_replace("://", "://" . $sUsername . ":" . $sPassword . "@", $sSitemapUrl);
echo $sSitemapUrl . "\r\n";
$sSitemapXmlData = @file_get_contents($sSitemapUrl); $sSitemapXmlData = @file_get_contents($sSitemapUrl);
if ($oSitemap = @simplexml_load_string($sSitemapXmlData)) { if ($oSitemap = @simplexml_load_string($sSitemapXmlData)) {
if (count($oSitemap->sitemap) > 0) { if (count($oSitemap->sitemap) > 0) {
@ -136,7 +115,6 @@ class CacheWarmer extends BaseController
} }
} }
} }
#print_r($aUrls);
return $aUrls; return $aUrls;
} }

View File

@ -16,7 +16,7 @@ Installation
Usage Usage
php source/modules/pc/cachewarmer/Core/CacheWarmer.php php source/modules/pc/cachewarmer/bin/warmup.php [optional parameter s for shop-id, eg. warmup.php -s 2]
Changelog Changelog

34
bin/warmup.php Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @copyright (c) ProudCommerce | 2020
* @link www.proudcommerce.com
* @package psCacheWarmer
* @version 3.0.0
**/
if (PHP_SAPI != 'cli') {
die('Only cli execution allowed!'."\r\n");
}
$options = getopt('s:');
$shopId = $options['s'] ?? 0;
if (!$shopId) {
$shopId = 1;
}
require_once dirname(__FILE__) . '/../../../../bootstrap.php';
use OxidEsales\Eshop\Core\Registry;
use ProudCommerce\CacheWarmer\Core\CacheWarmer;
echo 'Shop-ID '.$shopId." is used!\r\n";
Registry::getConfig()->setShopId($shopId);
Registry::set(Config::class, null);
$cacheWarmer = new CacheWarmer();
$cacheWarmer->run();