From 321c92e83050ce1b4300105d05cc44b5d1375921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20G=C3=A4rtner?= Date: Wed, 17 Jul 2019 17:17:28 +0200 Subject: [PATCH] Write Report in a file - Write a report in acsv-file in log-Folder - set the HTTP-Code 500 and 404 as an error --- Application/Controller/CacheWarmer.php | 121 ++++++++++++++++++++----- 1 file changed, 100 insertions(+), 21 deletions(-) diff --git a/Application/Controller/CacheWarmer.php b/Application/Controller/CacheWarmer.php index a505a53..c94ebad 100644 --- a/Application/Controller/CacheWarmer.php +++ b/Application/Controller/CacheWarmer.php @@ -18,6 +18,13 @@ use OxidEsales\Eshop\Core\Controller\BaseController; class CacheWarmer extends BaseController { + protected $_sFileAndPathToExportFile = ''; + protected $_handle = null; + protected $_sSeparator = ";"; + protected $_sEnclosure = "'"; + + // todo: add to config + protected $_aHttpCodesIsOkay = array('200','302'); /** * Executes cache warmer */ @@ -25,31 +32,18 @@ class CacheWarmer extends BaseController { $sMessage = "psCacheWarmer
".$this->_getSitemapUrl()."
---
"; + $this->_sFileAndPathToExportFile = $this->_getPathWithFileName(); + if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) + { + $this->_handle = fopen( $this->_sFileAndPathToExportFile, "w+");; + } + if($this->_checkAuthentification()) { $aUrls = $this->_getSitemapContent(); if(!empty(Registry::getConfig()->getShopConfVar('psCacheWarmerSitemapUrl')) && count($aUrls) > 0) { foreach($aUrls as $sUrl) { - $oCurl = curl_init(); - curl_setopt($oCurl, CURLOPT_URL, $sUrl); - curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 25); - curl_setopt($oCurl, CURLOPT_HEADER, true); - $sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser'); - $sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass'); - curl_setopt($oCurl, CURLOPT_USERPWD, $sUsername . ":" . $sPassword); - curl_exec($oCurl); - $httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); - if(curl_error($oCurl)) { - $sMessage .= 'ERROR '.$httpStatus.': ' . curl_error($oCurl) . '
'; - } else { - if(trim($httpStatus) == '500') - { - $sMessage .= 'ERROR '.$httpStatus.': ' . $sUrl. '
'; - } - else{ - $sMessage .= 'OK '.$httpStatus.': ' . $sUrl . '
'; - } - } + $oCurl = $this->_runCurlConnect($sUrl); + $sMessage .= $this->_checkCurlResults($oCurl,$sUrl); curl_close($oCurl); } } else { @@ -59,10 +53,73 @@ class CacheWarmer extends BaseController $sMessage .= 'Authentifizierung fehlgeschlagen!'; } + if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) + { + fclose($this->_handle); + } echo '
'.$sMessage.'
'; exit; } + /** + * @param $sUrl + * @return false|resource + */ + protected function _runCurlConnect($sUrl) + { + $oCurl = curl_init(); + curl_setopt($oCurl, CURLOPT_URL, $sUrl); + curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 25); + curl_setopt($oCurl, CURLOPT_HEADER, true); + $sUsername = Registry::getConfig()->getShopConfVar('psCacheWarmerUser'); + $sPassword = Registry::getConfig()->getShopConfVar('psCacheWarmerPass'); + curl_setopt($oCurl, CURLOPT_USERPWD, $sUsername . ":" . $sPassword); + curl_exec($oCurl); + return $oCurl; + } + + protected function _checkCurlResults($oCurl,$sUrl) + { + $sMessage = ''; + $httpStatus = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); + if(curl_error($oCurl)) { + $sMessage .= 'ERROR '.$httpStatus.': ' . curl_error($oCurl) . '
'; + $sStatusMsg = 'ERROR'; + $sTmpText = curl_error($oCurl); + } else { + $sTmpText = $sUrl; + if(in_array(trim($httpStatus),$this->_aHttpCodesIsOkay)) + { + $sMessage .= 'OK '.$httpStatus.': ' . $sUrl . '
'; + $sStatusMsg = 'OK'; + } + else{ + $sMessage .= 'ERROR '.$httpStatus.': ' . $sUrl. '
'; + $sStatusMsg = 'ERROR'; + } + } + + if(Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsv') == true) + { + $aTmp = array($sStatusMsg, + $httpStatus, + $sTmpText + ); + + if(trim($httpStatus) == '200' && Registry::getConfig()->getShopConfVar('psCacheWarmerWriteCsvOnlyError') == true) + { + $aTmp = array(); + } + + if(count($aTmp)) { + fputcsv($this->_handle, $aTmp, $this->_sSeparator, $this->_sEnclosure); + } + } + + return $sMessage; + } + /** * Returens urls from sitemap * @@ -125,4 +182,26 @@ class CacheWarmer extends BaseController } return false; } + + + /** + * Return Path with Filename, from /log + * + * @return string + */ + protected function _getPathWithFileName() + { + return Registry::getConfig()->getConfigParam('sShopDir').'/log/'.$this->_getFileName(); + } + + /** + * Return Filename, Formae psCacheWarmerReport_20190717-122345.csv + * + * @return string + */ + protected function _getFileName() + { + return 'psCacheWarmerReport_'.date("Ymd-His").".csv"; + } + }