Skip to content

Commit

Permalink
optimize module suggestion listing process
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreesh Arora committed Jan 25, 2023
1 parent f6ec75c commit ffd9b41
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 63 deletions.
28 changes: 28 additions & 0 deletions classes/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,20 @@ public static function cleanNonUnicodeSupport($pattern)
return preg_replace('/\\\[px]\{[a-z]{1,2}\}|(\/[a-z]*)u([a-z]*)$/i', '$1$2', $pattern);
}

/**
* @param string $file
* @param int $timeout
* @return bool
*/
public static function isFresh($file, $timeout = _TIME_1_WEEK_)
{
if (($time = @filemtime(_PS_ROOT_DIR_.$file)) && filesize(_PS_ROOT_DIR_.$file) > 0) {
return ((time() - $time) < $timeout);
}

return false;
}

protected static $is_addons_up = true;
public static function addonsRequest($request, $params = array())
{
Expand Down Expand Up @@ -3421,6 +3435,20 @@ public static function addonsRequest($request, $params = array())
return false;
}

/**
* @param string $file_to_refresh
* @param string $external_file
* @return bool
*/
public static function refresh($file_to_refresh, $external_file)
{
if (self::$is_addons_up && $content = Tools::file_get_contents($external_file)) {
return (bool)file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content);
}
self::$is_addons_up = false;
return false;
}

/**
* Returns an array containing information about
* HTTP file upload variable ($_FILES)
Expand Down
49 changes: 8 additions & 41 deletions classes/controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2214,36 +2214,7 @@ protected function filterTabModuleList($tab_modules_list)

public function ajaxProcessRefreshModuleList($force_reload_cache = false)
{
if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) {
$this->status = 'refresh';
} else {
$this->status = 'error';
}
} else {
$this->status = 'cache';
}

if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) {
$this->status = 'refresh';
} else {
$this->status = 'error';
}
} else {
$this->status = 'cache';
}

if (!$this->isFresh(Module::CACHE_FILE_TAB_MODULES_LIST, _TIME_1_WEEK_)) {
if ($this->refresh(Module::CACHE_FILE_TAB_MODULES_LIST, _QLO_TAB_MODULE_LIST_URL_)) {
$this->status = 'refresh';
} else {
$this->status = 'error';
}
} else {
$this->status = 'cache';
}
Module::generateTrustedXml();
$this->status = Module::refreshModuleList($force_reload_cache);
}

/**
Expand Down Expand Up @@ -2835,7 +2806,7 @@ public function init()
));

// get upgrade available info
if (!$this->isFresh(Upgrader::CACHE_FILE_UPGRADE_AVAILABE, _TIME_1_DAY_)) {
if (!Tools::isFresh(Upgrader::CACHE_FILE_UPGRADE_AVAILABE, _TIME_1_DAY_)) {
file_put_contents(_PS_ROOT_DIR_.Upgrader::CACHE_FILE_UPGRADE_AVAILABE, Tools::addonsRequest('check-version'));
}
if (file_exists(_PS_ROOT_DIR_.Upgrader::CACHE_FILE_UPGRADE_AVAILABE)) {
Expand Down Expand Up @@ -4075,28 +4046,24 @@ public function jsonError($message)
* @param string $file
* @param int $timeout
* @return bool
* @deprecated Deprecated 1.6.0.0 Use Tools::isFresh instead
*/
public function isFresh($file, $timeout = _TIME_1_WEEK_)
{
if (($time = @filemtime(_PS_ROOT_DIR_.$file)) && filesize(_PS_ROOT_DIR_.$file) > 0) {
return ((time() - $time) < $timeout);
}

return false;
Tools::displayAsDeprecated();
return Tools::isFresh($file, $timeout);
}

/**
* @param string $file_to_refresh
* @param string $external_file
* @return bool
* @deprecated Deprecated 1.6.0.0 Use Tools::refresh instead
*/
public function refresh($file_to_refresh, $external_file)
{
if (self::$is_qloapps_up && $content = Tools::file_get_contents($external_file)) {
return (bool)file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content);
}
self::$is_qloapps_up = false;
return false;
Tools::displayAsDeprecated();
return Tools::refresh($file_to_refresh, $external_file);
}

/**
Expand Down
57 changes: 50 additions & 7 deletions classes/module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ public static function getSuggestedModules()
array('type' => 'addonsMustHave', 'file' => _PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, 'loggedOnAddons' => 0),
array('type' => 'addonsNative', 'file' => _PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, 'loggedOnAddons' => 0),
);
$modulesOnDisk = Module::getModulesDirOnDisk();
$module_list = array();
foreach ($files_list as $f) {
$file = $f['file'];
Expand Down Expand Up @@ -1642,18 +1643,21 @@ public static function getSuggestedModules()
$item->url = isset($modaddons->url) ? $modaddons->url : null;
$item->is_native = false;

if (isset($modaddons->img)) {
if (!file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) {
if (!file_put_contents(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg', Tools::file_get_contents($modaddons->img))) {
copy(_PS_IMG_DIR_.'404.gif', _PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg');
if (!in_array($modaddons->name, $modulesOnDisk)) {
if (isset($modaddons->img)) {
if (!file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) {
if (!file_put_contents(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg', Tools::file_get_contents($modaddons->img))) {
copy(_PS_IMG_DIR_.'404.gif', _PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg');
}
}
}

if (file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) {
$item->image = '../img/tmp/'.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg';
if (file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) {
$item->image = '../img/tmp/'.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg';
}
}
}


if ($item->type == 'addonsMustHave') {
$item->addons_buy_url = strip_tags((string)$modaddons->url);
$prices = (array)$modaddons->price;
Expand Down Expand Up @@ -1759,6 +1763,42 @@ public static function getModulesInstalled($position = 0)
return Db::getInstance()->executeS($sql);
}

public static function refreshModuleList($force_reload_cache = false)
{
$status = false;
if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) {
$status = 'refresh';
} else {
$status = 'error';
}
} else {
$status = 'cache';
}

if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) {
$status = 'refresh';
} else {
$status = 'error';
}
} else {
$status = 'cache';
}

if (!Tools::isFresh(Module::CACHE_FILE_TAB_MODULES_LIST, _TIME_1_WEEK_)) {
if (Tools::refresh(Module::CACHE_FILE_TAB_MODULES_LIST, _QLO_TAB_MODULE_LIST_URL_)) {
$status = 'refresh';
} else {
$status = 'error';
}
} else {
$status = 'cache';
}

return $status;
}

/**
* Return if the module is provided by addons.prestashop.com or not
*
Expand Down Expand Up @@ -1841,6 +1881,9 @@ final public static function generateTrustedXml()
$trusted = array();
$untrusted = array();

// before generating list first check if file exists and are updated
Module::refreshModuleList();

$trusted_modules_xml = array(
// _PS_ROOT_DIR_.self::CACHE_FILE_ALL_COUNTRY_MODULES_LIST,
_PS_ROOT_DIR_.self::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST,
Expand Down
6 changes: 3 additions & 3 deletions controllers/admin/AdminDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ public function ajaxProcessSetSimulationMode()
public function ajaxProcessGetBlogRss()
{
$return = array('has_errors' => false, 'rss' => array());
if (!$this->isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _TIME_1_DAY_)) {
if (!$this->refresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _PS_API_URL_.'/rss/blog/blog-'.$this->context->language->iso_code.'.xml')) {
if (!Tools::isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _TIME_1_DAY_)) {
if (!Tools::refresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _PS_API_URL_.'/rss/blog/blog-'.$this->context->language->iso_code.'.xml')) {
$return['has_errors'] = true;
}
}
Expand Down Expand Up @@ -506,7 +506,7 @@ public function ajaxProcessGetRecommendationContent()

public function getRecommendationContent()
{
if (!$this->isFresh(self::DASHBOARD_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) {
if (!Tools::isFresh(self::DASHBOARD_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) {
@file_put_contents(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT, Tools::addonsRequest('dashboard-recommendation'));
}
if (file_exists(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT)) {
Expand Down
6 changes: 3 additions & 3 deletions controllers/admin/AdminModulesCatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function initToolbar()

public function getRecommendationContent()
{
if (!$this->isFresh(self::CATALOG_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) {
if (!Tools::isFresh(self::CATALOG_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) {
@file_put_contents(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT, Tools::addonsRequest('catalog-recommendation'));
}
if (file_exists(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT)) {
Expand Down Expand Up @@ -311,11 +311,11 @@ public function ajaxProcessSetSorting()

protected function initSuggestedModulesList()
{
if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_)) {
if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_)) {
file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'));
}

if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_)) {
if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_)) {
@file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'));
}
}
Expand Down
15 changes: 7 additions & 8 deletions controllers/admin/AdminModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ public function setMedia()
public function ajaxProcessRefreshModuleList($force_reload_cache = false)
{
// Refresh modules_list.xml every week
if (!$this->isFresh(Module::CACHE_FILE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (!Tools::isFresh(Module::CACHE_FILE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
$xml_modules_list = _QLO_API_DOMAIN_.'/xml/'.str_replace('.', '', _QLOAPPS_VERSION_).'.xml';
if ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$xml_modules_list)) {
if (Tools::refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$xml_modules_list)) {
$this->status = 'refresh';
} elseif ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$xml_modules_list)) {
} elseif (Tools::refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$xml_modules_list)) {
$this->status = 'refresh';
} else {
$this->status = 'error';
Expand All @@ -187,7 +187,7 @@ public function ajaxProcessRefreshModuleList($force_reload_cache = false)

// If logged to Addons Webservices, refresh default country native modules list every day
if ($this->status != 'error') {
if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) {
$this->status = 'refresh';
} else {
Expand All @@ -197,7 +197,7 @@ public function ajaxProcessRefreshModuleList($force_reload_cache = false)
$this->status = 'cache';
}

if (!$this->isFresh(Module::CACHE_FILE_ADDONS_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (!Tools::isFresh(Module::CACHE_FILE_ADDONS_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_ADDONS_MODULES_LIST, Tools::addonsRequest('addons-modules'))) {
$this->status = 'refresh';
} else {
Expand All @@ -207,7 +207,7 @@ public function ajaxProcessRefreshModuleList($force_reload_cache = false)
$this->status = 'cache';
}

if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) {
$this->status = 'refresh';
} else {
Expand All @@ -220,7 +220,7 @@ public function ajaxProcessRefreshModuleList($force_reload_cache = false)

// If logged to Addons Webservices, refresh customer modules list every 5 minutes
if ($this->logged_on_addons && $this->status != 'error') {
if (!$this->isFresh(Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 300) || $force_reload_cache) {
if (!Tools::isFresh(Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 300) || $force_reload_cache) {
if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_CUSTOMER_MODULES_LIST, Tools::addonsRequest('customer'))) {
$this->status = 'refresh';
} else {
Expand All @@ -230,7 +230,6 @@ public function ajaxProcessRefreshModuleList($force_reload_cache = false)
$this->status = 'cache';
}
}
Module::generateTrustedXml();
}

public function displayAjaxRefreshModuleList()
Expand Down
2 changes: 1 addition & 1 deletion controllers/admin/AdminThemesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public function downloadAddonsThemes()
) {
return false;
}
if (!$this->isFresh(Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, _TIME_1_DAY_)) {
if (!Tools::isFresh(Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, _TIME_1_DAY_)) {
file_put_contents(_PS_ROOT_DIR_.Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, Tools::addonsRequest('customer_themes'));
}

Expand Down

0 comments on commit ffd9b41

Please sign in to comment.