Skip to content

Commit

Permalink
Articles Now Can Cache Structured Data information
Browse files Browse the repository at this point in the history
For feature #902
  • Loading branch information
eSilverStrike committed Feb 27, 2019
1 parent 7e992c1 commit 171fcc7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 54 deletions.
5 changes: 0 additions & 5 deletions plugins/staticpages/services.inc.php
Expand Up @@ -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
Expand All @@ -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']);
}
}
Expand All @@ -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
Expand All @@ -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']);
}
}
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 0 additions & 42 deletions public_html/article.php
Expand Up @@ -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&amp;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,
Expand Down
12 changes: 9 additions & 3 deletions system/classes/structureddata.class.php
Expand Up @@ -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);
}

Expand Down
81 changes: 77 additions & 4 deletions system/lib-article.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
Expand All @@ -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,
Expand All @@ -142,13 +146,25 @@ 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;
}
} else {
// 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;
}
}
}

// ****************************************
Expand Down Expand Up @@ -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&amp;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
Expand Down Expand Up @@ -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');
}
}

Expand Down
3 changes: 3 additions & 0 deletions system/lib-template.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 171fcc7

Please sign in to comment.