From 171fcc7dade8ea675f81df84d84e8201c122e94b Mon Sep 17 00:00:00 2001 From: Tom Homer Date: Wed, 27 Feb 2019 13:54:51 -0500 Subject: [PATCH] Articles Now Can Cache Structured Data information For feature #902 --- plugins/staticpages/services.inc.php | 5 -- public_html/article.php | 42 ------------- system/classes/structureddata.class.php | 12 +++- system/lib-article.php | 81 +++++++++++++++++++++++-- system/lib-template.php | 3 + 5 files changed, 89 insertions(+), 54 deletions(-) diff --git a/plugins/staticpages/services.inc.php b/plugins/staticpages/services.inc.php index aa49c28ae..a36180a5a 100644 --- a/plugins/staticpages/services.inc.php +++ b/plugins/staticpages/services.inc.php @@ -585,7 +585,6 @@ function service_submit_staticpages($args, &$output, &$svc_msg) // Clear Cache $cacheInstance = 'staticpage__' . $sp_id . '__'; CACHE_remove_instance($cacheInstance); - $_STRUCT_DATA->clear_cachedScript('staticpages', $sp_id); } else { // If template then have to notify of all pages that use this template that a change to the page happened @@ -597,7 +596,6 @@ function service_submit_staticpages($args, &$output, &$svc_msg) // Clear Cache $cacheInstance = 'staticpage__' . $A['sp_id'] . '__'; CACHE_remove_instance($cacheInstance); - $_STRUCT_DATA->clear_cachedScript('staticpages', $A['sp_id']); } } @@ -611,7 +609,6 @@ function service_submit_staticpages($args, &$output, &$svc_msg) // Clear Cache $cacheInstance = 'staticpage__' . $sp_old_id . '__'; CACHE_remove_instance($cacheInstance); - $_STRUCT_DATA->clear_cachedScript('staticpages', $sp_old_id); } else { // If template then have to notify of all pages that use this template that a change to the page happened @@ -623,7 +620,6 @@ function service_submit_staticpages($args, &$output, &$svc_msg) // Clear Cache $cacheInstance = 'staticpage__' . $A['sp_id'] . '__'; CACHE_remove_instance($cacheInstance); - $_STRUCT_DATA->clear_cachedScript('staticpages', $A['sp_id']); } } @@ -714,7 +710,6 @@ function service_delete_staticpages($args, &$output, &$svc_msg) // Clear Cache $cacheInstance = 'staticpage__' . $sp_id . '__'; CACHE_remove_instance($cacheInstance); - $_STRUCT_DATA->clear_cachedScript('staticpages', $sp_id); return PLG_RET_OK; diff --git a/public_html/article.php b/public_html/article.php index 92a02f03f..061af435b 100644 --- a/public_html/article.php +++ b/public_html/article.php @@ -515,48 +515,6 @@ function extractExternalLinks($text) { $display .= $articleTemplate->finish($articleTemplate->parse('output', 'article')); $breadcrumbs = TOPIC_breadcrumbs('article', $article->getSid()); - - $properties['headline'] = $article->displayElements('title'); - $properties['url'] = $permalink; - $properties['datePublished'] = $article->displayElements('date'); - - // Don't include modified if empty or date is less than published - if (!empty($article->displayElements('unixmodified')) && ($article->displayElements('unixmodified') > $article->displayElements('unixdate'))) { - $properties['dateModified'] = $article->displayElements('modified'); - } - $properties['description'] = $article->DisplayElements('meta_description'); - $properties['keywords'] = $article->DisplayElements('meta_keywords'); - $properties['commentCount'] = CMT_commentCount($article->getSid(), 'article'); - $_STRUCT_DATA->add_type('article', $article->getSid(), $article->displayElements('structured_data_type'), $properties); - $_STRUCT_DATA->set_author_item('article', $article->getSid(), $article->DisplayElements('username')); - // Include any images attached to the article (taken in part from renderImageTags function in article class) - $result = DB_query("SELECT ai_filename,ai_img_num FROM {$_TABLES['article_images']} WHERE ai_sid = '{$article->getSid()}' ORDER BY ai_img_num"); - $numRows = DB_numRows($result); - - $stdImageLoc = true; - if (!strstr($_CONF['path_images'], $_CONF['path_html'])) { - $stdImageLoc = false; - } - - for ($i = 1; $i <= $numRows; $i++) { - $A = DB_fetchArray($result); - - $imgPath = ''; - - if ($stdImageLoc) { - $imgPath = substr($_CONF['path_images'], strlen($_CONF['path_html'])); - $imgSrc = $_CONF['site_url'] . '/' . $imgPath . 'articles/' . $A['ai_filename']; - } else { - $imgSrc = $_CONF['site_url'] . '/getimage.php?mode=articles&image=' . $A['ai_filename']; - } - - $sizeAttributes = COM_getImgSizeAttributes($_CONF['path_images'] . 'articles/' . $A['ai_filename'], false); - if (is_array($sizeAttributes)) { - $_STRUCT_DATA->set_image_item('article', $article->getSid(), $imgSrc, $sizeAttributes['width'], $sizeAttributes['height']); - } else { - $_STRUCT_DATA->set_image_item('article', $article->getSid(), $imgSrc); - } - } $display = COM_createHTMLDocument( $display, diff --git a/system/classes/structureddata.class.php b/system/classes/structureddata.class.php index 4711c0f2b..edba051a4 100644 --- a/system/classes/structureddata.class.php +++ b/system/classes/structureddata.class.php @@ -319,10 +319,16 @@ public function get_cachedScript($type, $id, $cache_time) * @param string $type Plugin of the content used to create the structured data * @param string $id Id of content */ - public function clear_cachedScript($type, $id) + public function clear_cachedScript($type, $id = '') { - $sd_name = $this->create_name($type, $id); - $cacheInstance = $this->get_cacheInstanceID($sd_name); + if (!empty($id)){ + $sd_name = $this->create_name($type, $id); + $cacheInstance = $this->get_cacheInstanceID($sd_name); + } else { + // clear all Structured Data cache for a specific type + $cacheInstance = 'structureddata__' . $type; + } + CACHE_remove_instance($cacheInstance); } diff --git a/system/lib-article.php b/system/lib-article.php index 04738b58a..2c1476296 100644 --- a/system/lib-article.php +++ b/system/lib-article.php @@ -66,7 +66,7 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml', $query = '', $articlecount = 1) { global $_CONF, $_TABLES, $_USER, $LANG01, $LANG05, $LANG11, $LANG_TRB, - $_IMAGE_TYPE, $mode; + $_IMAGE_TYPE, $mode, $_STRUCT_DATA; static $storyCounter = 0; @@ -105,7 +105,7 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml $current_article_tid = $story->DisplayElements('tid'); $retval = false; // If stays false will rebuild article and not used cache (checks done below) - // CHeck cache time or if search query do not use cache as need to add highlight + // Check cache time or if search query do not use cache as need to add highlight if (($cache_time > 0 || $cache_time == -1) && empty($query)) { $hash = CACHE_security_hash(); $cacheInstance = 'article__' . $story->getSid() . '_' . $index . $mode . '_' . $article_filevar . '_' . $current_article_tid . '_' . $hash . '_' . $_CONF['theme']; @@ -116,21 +116,25 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml $retval = CACHE_check_instance($cacheInstance); } + $cache_found = false; if ($retval && $cache_time == -1) { // Cache file found so use it since no time limit set to recreate - + $cache_found = true; } elseif ($retval && $cache_time > 0) { $lu = CACHE_get_instance_update($cacheInstance); $now = time(); if (($now - $lu) < $cache_time) { // Cache file found so use it since under time limit set to recreate + $cache_found = true; } else { // generate article and create cache file // Cache time is not built into template caching so need to delete it manually and reset $retval if ($_CONF['cache_templates']) { + CACHE_remove_instance($cacheInstance); + $_STRUCT_DATA->clear_cachedScript('article', $story->getSid()); + // Need to close and recreate template class since issues arise when theme templates are cached unset($article); // Close template class - CACHE_remove_instance($cacheInstance); $article = COM_newTemplate(CTL_core_templatePath($_CONF['path_layout'])); $article->set_file(array( 'article' => $storyTpl, @@ -142,6 +146,7 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml )); } else { // theme templates are not cache so can go ahead and delete story cache CACHE_remove_instance($cacheInstance); + $_STRUCT_DATA->clear_cachedScript('article', $story->getSid()); } $retval = false; } @@ -149,6 +154,17 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml // Need to reset especially if caching is disabled for a certain story but template caching has been enabled for the theme $retval = false; } + + // Now find structured data cache if required + // Structured Data is cached by itself. Need to cache in case structured data autotags exist in page. + // Since autotags are executed when the page is rendered therefore we have to cache structred data if page is cached. + // Only cache and use structured data on full article view + if ($index == 'n' && $story->DisplayElements('structured_data_type') > 0 && $cache_found) { + if (!$_STRUCT_DATA->get_cachedScript('article', $story->getSid(), $cache_time)) { + // Structured Data missing for some reason even though page cache found. Render all again + $retval = false; + } + } } // **************************************** @@ -661,6 +677,62 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml // CACHE_create_instance($cacheInstance, $article); } + + + // Figure out structured data if needed + if ($story->DisplayElements('structured_data_type') > 0 ) { + $attributes = array(); + $attributes['multi_language'] = true; + // Only cache if not search and full article view (=n) + if ($index == 'n' && empty($query) && ($cache_time > 0 || $cache_time == -1)) { + $attributes['cache'] = true; + } + + $properties['headline'] = $story->displayElements('title'); + $properties['url'] = $articleUrl; + $properties['datePublished'] = $story->displayElements('date'); + // Don't include modified if empty or date is less than published + if (!empty($story->displayElements('unixmodified')) && ($story->displayElements('unixmodified') > $story->displayElements('unixdate'))) { + $properties['dateModified'] = $story->displayElements('modified'); + } + $properties['description'] = $story->DisplayElements('meta_description'); + $properties['keywords'] = $story->DisplayElements('meta_keywords'); + $properties['commentCount'] = CMT_commentCount($story->getSid(), 'article'); + $_STRUCT_DATA->add_type('article', $story->getSid(), $story->displayElements('structured_data_type'), $properties, $attributes); + $_STRUCT_DATA->set_author_item('article', $story->getSid(), $story->DisplayElements('username')); + // Include any images attached to the article (taken in part from renderImageTags function in article class) + $result = DB_query("SELECT ai_filename,ai_img_num FROM {$_TABLES['article_images']} WHERE ai_sid = '{$story->getSid()}' ORDER BY ai_img_num"); + $numRows = DB_numRows($result); + $stdImageLoc = true; + if (!strstr($_CONF['path_images'], $_CONF['path_html'])) { + $stdImageLoc = false; + } + for ($i = 1; $i <= $numRows; $i++) { + $A = DB_fetchArray($result); + + $imgPath = ''; + if ($stdImageLoc) { + $imgPath = substr($_CONF['path_images'], strlen($_CONF['path_html'])); + $imgSrc = $_CONF['site_url'] . '/' . $imgPath . 'articles/' . $A['ai_filename']; + } else { + $imgSrc = $_CONF['site_url'] . '/getimage.php?mode=articles&image=' . $A['ai_filename']; + } + + $sizeAttributes = COM_getImgSizeAttributes($_CONF['path_images'] . 'articles/' . $A['ai_filename'], false); + if (is_array($sizeAttributes)) { + $_STRUCT_DATA->set_image_item('article', $story->getSid(), $imgSrc, $sizeAttributes['width'], $sizeAttributes['height']); + } else { + $_STRUCT_DATA->set_image_item('article', $story->getSid(), $imgSrc); + } + } + } + + + + + + + } else { PLG_templateSetVars($article_filevar, $article); // Used by Custom Block Locations @@ -1898,6 +1970,7 @@ function plugin_configchange_article($group, $changes = array()) // If any Article options changed then delete all article cache $cacheInstance = 'article__'; CACHE_remove_instance($cacheInstance); + $_STRUCT_DATA->clear_cachedScript('article'); } } diff --git a/system/lib-template.php b/system/lib-template.php index 11873dc78..d09f1550b 100644 --- a/system/lib-template.php +++ b/system/lib-template.php @@ -560,6 +560,8 @@ function plugin_itemsaved_template($id, $type, $old_id = '') */ function plugin_itemdeleted_template($id, $type) { + global $_STRUCT_DATA; + // See if uses what's new block then delete cache of whatsnew // This will not catch everything though like trackbacks, comments, and // plugins that do not use itemsaved but let's delete the cache when we can @@ -599,6 +601,7 @@ function plugin_itemdeleted_template($id, $type) if ($article) { $cacheInstance = 'article__' . $id; // remove all article instances CACHE_remove_instance($cacheInstance); + $_STRUCT_DATA->clear_cachedScript('article'); } if ($block) { $cacheInstance = 'block__' . $id; // remove all block instances