Skip to content

Commit

Permalink
Fixed Autotags in Articles executing twice when viewing an Article
Browse files Browse the repository at this point in the history
For issue #1017
  • Loading branch information
eSilverStrike committed Jan 7, 2020
1 parent c370a39 commit 19bd063
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 33 deletions.
1 change: 1 addition & 0 deletions public_html/admin/install/classes/installer.class.php
Expand Up @@ -2952,6 +2952,7 @@ private function doDatabaseUpgrades($currentGlVersion, $checkForMessage = false)
update_ConfValuesFor221();
fixDuplicateUsernames221();
addStructuredDataSecurityRight221();
calculateNumPagesArticles221();
}
$currentGlVersion = '2.2.1';
break;
Expand Down
2 changes: 2 additions & 0 deletions public_html/admin/install/devel-db-update.php
Expand Up @@ -92,6 +92,8 @@ function update_DatabaseFor221()
// Add structured data type to article table and modified date
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `structured_data_type` varchar(40) NOT NULL DEFAULT '' AFTER `commentcode`";
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `modified` DATETIME NULL DEFAULT NULL AFTER `date`";
// For number of pages in an article. Needed for when article is cached and we need to figure out what page to put the comments on
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `numpages` tinyint(1) NOT NULL DEFAULT '1' AFTER `hits`";

if (DB_count($_TABLES['features'], 'ft_name', 'structureddata.autotag') == 0) {
// Add `structureddata.autotag` feature
Expand Down
13 changes: 6 additions & 7 deletions public_html/article.php
Expand Up @@ -167,7 +167,7 @@ function extractExternalLinks($text) {
'professional' => 'layout/' . $theme . '/print.css',
'other' => 'layout/' . $theme . '/css/print.css',
);

global $_SCRIPTS;
foreach ($paths as $path) {
if (file_exists($_CONF['path_html'] . $path)) {
Expand Down Expand Up @@ -309,13 +309,13 @@ function extractExternalLinks($text) {
)
);
}

// Add hreflang link element if Multi Language Content is setup
// Only allow hreflang link element to be visible when on canonical url
// ie no second pages which can happen with comments, or if [page_break] is used or with extra trailing variables like from a search query
if (strtolower(COM_getCurrentURL()) == strtolower($permalink)) {
$headercode .= COM_createHREFLang('story', $article->getSid());
}
}

if ($article->DisplayElements('trackbackcode') == 0) {
if ($_CONF['trackback_enabled']) {
Expand Down Expand Up @@ -449,8 +449,9 @@ function extractExternalLinks($text) {
$articleTemplate->set_var('formatted_article',
STORY_renderArticle($article, 'n', $tmpl, $query));

// display comments or not?
if ($_CONF['allow_page_breaks'] == 1) {
// display comments or not on this page of the article?
$page_break_count = $article->displayElements('numpages');
if ($_CONF['allow_page_breaks'] == 1 && $page_break_count > 1) {
if (!is_numeric($mode)) {
$story_page = 1;
} else {
Expand All @@ -462,8 +463,6 @@ function extractExternalLinks($text) {
$story_page = 1;
}

$article_arr = explode('[page_break]', $article->displayElements('bodytext'));
$page_break_count = count($article_arr);
if ($page_break_count > 1) {
$conf = $_CONF['page_break_comments'];
if (
Expand Down
1 change: 1 addition & 0 deletions sql/mysql_tableanddata.php
Expand Up @@ -293,6 +293,7 @@
bodytext text,
text_version tinyint(2) NOT NULL default '1',
hits mediumint(8) unsigned NOT NULL default '0',
numpages tinyint(1) NOT NULL DEFAULT '1',
numemails mediumint(8) unsigned NOT NULL default '0',
comments mediumint(8) unsigned NOT NULL default '0',
comment_expire datetime default NULL,
Expand Down
1 change: 1 addition & 0 deletions sql/pgsql_tableanddata.php
Expand Up @@ -291,6 +291,7 @@
bodytext text,
text_version smallint NOT NULL default '1',
hits smallint NOT NULL default '0',
numpages smallint NOT NULL DEFAULT '1',
numemails smallint NOT NULL default '0',
comments smallint NOT NULL default '0',
comment_expire timestamp default NULL,
Expand Down
22 changes: 22 additions & 0 deletions sql/updates/mysql_2.2.0_to_2.2.1.php
Expand Up @@ -18,6 +18,8 @@
// Add structured data type to article table and modified date
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `structured_data_type` varchar(40) NOT NULL DEFAULT '' AFTER `commentcode`";
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `modified` DATETIME NULL DEFAULT NULL AFTER `date`";
// For number of pages in an article. Needed for when article is cached and we need to figure out what page to put the comments on
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `numpages` tinyint(1) NOT NULL DEFAULT '1' AFTER `hits`";

// Language Override value can now be longer than 255 characters
$_SQL[] = "ALTER TABLE {$_TABLES['language_items']} CHANGE `value` `value` TEXT";
Expand Down Expand Up @@ -306,3 +308,23 @@ function addStructuredDataSecurityRight221()

return true;
}

function calculateNumPagesArticles221()
{
global $_TABLES;

// Calculate number of pages for articles (that have [page_break] in body text)
$sql = "SELECT sid, bodytext FROM {$_TABLES['stories']} WHERE bodytext != ''";
$result = DB_query($sql);
$numRows = DB_numRows($result);
for ($i = 0; $i < $numRows; $i++) {
$A = DB_fetchArray($result);

$numpages = (count(explode('[page_break]', $A['bodytext'])));

// Save new name
DB_query("UPDATE {$_TABLES['stories']} SET numpages = $numpages WHERE sid = '" . DB_escapeString($A['sid']) . "'");
}

return true;
}
22 changes: 22 additions & 0 deletions sql/updates/pgsql_2.2.0_to_2.2.1.php
Expand Up @@ -18,6 +18,8 @@
// Add structured data type to article table and modified date
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `structured_data_type` varchar(40) NOT NULL DEFAULT '' AFTER `commentcode`";
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `modified` timestamp default NULL AFTER `date`";
// For number of pages in an article. Needed for when article is cached and we need to figure out what page to put the comments on
$_SQL[] = "ALTER TABLE {$_TABLES['stories']} ADD `numpages` tinyint(1) NOT NULL DEFAULT '1' AFTER `hits`";

// Language Override value can now be longer than 255 characters
$_SQL[] = "ALTER TABLE {$_TABLES['language_items']} CHANGE `value` `value` TEXT";
Expand Down Expand Up @@ -308,3 +310,23 @@ function addStructuredDataSecurityRight221()

return true;
}

function calculateNumPagesArticles221()
{
global $_TABLES;

// Calculate number of pages for articles (that have [page_break] in body text)
$sql = "SELECT sid, bodytext FROM {$_TABLES['stories']} WHERE bodytext != ''";
$result = DB_query($sql);
$numRows = DB_numRows($result);
for ($i = 0; $i < $numRows; $i++) {
$A = DB_fetchArray($result);

$numpages = (count(explode('[page_break]', $A['bodytext'])));

// Save new name
DB_query("UPDATE {$_TABLES['stories']} SET numpages = $numpages WHERE sid = '" . DB_escapeString($A['sid']) . "'");
}

return true;
}
16 changes: 14 additions & 2 deletions system/classes/article.class.php
Expand Up @@ -118,6 +118,7 @@ class Article
var $_date;
var $_modified;
var $_hits;
var $_numpages;
var $_numemails;
var $_comment_expire;
var $_comments;
Expand Down Expand Up @@ -190,6 +191,7 @@ class Article
'bodytext' => 1,
'text_version' => 1,
'hits' => 1,
'numpages' => 1,
'numemails' => 1,
'comments' => 1,
'trackbacks' => 1,
Expand Down Expand Up @@ -455,7 +457,7 @@ public function loadFromDatabase($sid, $mode = 'edit')
$sql['pgsql'] = "SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, UNIX_TIMESTAMP(s.modified) AS unixmodified, UNIX_TIMESTAMP(s.expire) as expireunix, UNIX_TIMESTAMP(s.comment_expire) as cmt_expire_unix, u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl
FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t, {$_TABLES['topic_assignments']} AS ta
WHERE ta.type = 'article' AND ta.id = sid AND ta.tdefault = 1 AND (s.uid = u.uid) AND (ta.tid = t.tid) AND (sid = '$sid')
GROUP BY s.sid, s.uid, s.title, s.page_title, s.draft_flag, s.introtext, s.bodytext, s.date, s.numemails, s.comments, s.modified, s.featured, s.comment_expire, s.commentcode, s.trackbacks, s.trackbackcode, s.related, s.hits, s.show_topic_icon, s.frontpage, s.statuscode, s.postmode, s.expire, s.advanced_editor_mode, s.owner_id, s.group_id, s.perm_owner, s.perm_group, s.perm_members, s.perm_anon, s.meta_description, s.meta_keywords, s.text_version, s.cache_time, s.structured_data_type,
GROUP BY s.sid, s.uid, s.title, s.page_title, s.draft_flag, s.introtext, s.bodytext, s.date, s.numemails, s.comments, s.modified, s.featured, s.comment_expire, s.commentcode, s.trackbacks, s.trackbackcode, s.related, s.hits, s.numpages, s.show_topic_icon, s.frontpage, s.statuscode, s.postmode, s.expire, s.advanced_editor_mode, s.owner_id, s.group_id, s.perm_owner, s.perm_group, s.perm_members, s.perm_anon, s.meta_description, s.meta_keywords, s.text_version, s.cache_time, s.structured_data_type,
u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl ";
} elseif (!empty($sid) && ($mode === 'editsubmission')) {
/* Original
Expand Down Expand Up @@ -525,6 +527,7 @@ public function loadFromDatabase($sid, $mode = 'edit')

$this->_text_version = GLTEXT_LATEST_VERSION;
$this->_hits = 0;
$this->_numpages = 1;
$this->_comments = 0;
$this->_trackbacks = 0;
$this->_numemails = 0;
Expand Down Expand Up @@ -693,6 +696,7 @@ public function loadFromDatabase($sid, $mode = 'edit')

// reset counters
$this->_hits = 0;
$this->_numpages = 1;
$this->_comments = 0;
$this->_trackbacks = 0;
$this->_numemails = 0;
Expand Down Expand Up @@ -811,6 +815,9 @@ public function saveToDatabase()
$values = '';
reset($this->_dbFields);

// Count number of pages so we can figure out which page to put the comments on
$this->_numpages = (count(explode('[page_break]', $this->displayElements('bodytext'))));

$this->_text_version = GLTEXT_LATEST_VERSION;

// Apply HTML filter to the text just before save
Expand Down Expand Up @@ -1902,6 +1909,11 @@ public function DisplayElements($item = 'title')

break;

case 'numpages':
$return = $this->_numpages;

break;

case 'topic':
$return = htmlspecialchars($this->_topic);

Expand Down Expand Up @@ -2337,7 +2349,7 @@ private function _applyTextFilter($text, $postMode)
}

/**
* Perform some basic cleanups of data, dealing with empty required, defaultable fields.
* Perform some basic cleanups of data, dealing with empty required, default table fields.
*/
public function sanitizeData()
{
Expand Down
52 changes: 28 additions & 24 deletions system/lib-article.php
Expand Up @@ -744,30 +744,34 @@ function STORY_renderArticle($story, $index = '', $storyTpl = 'articletext.thtml
}
}
} else {
// Images are required by Google for article structured data rich snippets.
// lets look in the actual content of the article for an image and add it that way as long as it is locally stored and meets the min requirements
preg_match_all('~<img.*?src=["\']+(.*?)["\']+~', ($introtext . $bodytext), $result);
$srcs = array_pop($result);
foreach ($srcs as $src) {
/* ALternate way to get image src but believe slower
$articleDoc = new DOMDocument();
libxml_use_internal_errors(true); // Incase invalid HTML is loaded
$articleDoc->loadHTML(($introtext . $bodytext));
$images = $articleDoc->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
*/
if (substr($src, 0, 1) == "/" || substr($src, 0, strlen($_CONF['site_url'])) == $_CONF['site_url']) {
// COM_getImgSizeAttributes checks if file exists
$sizeAttributes = COM_getImgSizeAttributes($_CONF['path_html'] . substr($src, 1), false);
// Make sure image meets minimum sizes as we don't want to grab something really small
// Using old Geeklog image width and height defaults
if (is_array($sizeAttributes)
&& $sizeAttributes['width'] >= 160 && $sizeAttributes['height'] >= 160) {
//&& $sizeAttributes['width'] <= $_CONF['max_image_width'] && $sizeAttributes['height'] <= $_CONF['max_image_height']) {
$_STRUCT_DATA->set_image_item('article', $story->getSid(), ($_CONF['site_url'] . $src), $sizeAttributes['width'], $sizeAttributes['height']);

break;
// Before searching content for images, check if structured data already exist for image incase autotags used to insert images and/or structured data
// Structured Data Images are stored as arrays so just check for array, if found then skip checking content for images
if (!is_array($_STRUCT_DATA->get_param_item('article', $story->getSid(), 'image'))) {
// Images are required by Google for article structured data rich snippets.
// lets look in the actual content of the article for an image and add it that way as long as it is locally stored and meets the min requirements
preg_match_all('~<img.*?src=["\']+(.*?)["\']+~', ($introtext . $bodytext), $result);
$srcs = array_pop($result);
foreach ($srcs as $src) {
/* ALternate way to get image src but believe slower
$articleDoc = new DOMDocument();
libxml_use_internal_errors(true); // Incase invalid HTML is loaded
$articleDoc->loadHTML(($introtext . $bodytext));
$images = $articleDoc->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
*/
if (substr($src, 0, 1) == "/" || substr($src, 0, strlen($_CONF['site_url'])) == $_CONF['site_url']) {
// COM_getImgSizeAttributes checks if file exists
$sizeAttributes = COM_getImgSizeAttributes($_CONF['path_html'] . substr($src, 1), false);
// Make sure image meets minimum sizes as we don't want to grab something really small
// Using old Geeklog image width and height defaults
if (is_array($sizeAttributes)
&& $sizeAttributes['width'] >= 160 && $sizeAttributes['height'] >= 160) {
//&& $sizeAttributes['width'] <= $_CONF['max_image_width'] && $sizeAttributes['height'] <= $_CONF['max_image_height']) {
$_STRUCT_DATA->set_image_item('article', $story->getSid(), ($_CONF['site_url'] . $src), $sizeAttributes['width'], $sizeAttributes['height']);

break;
}
}
}
}
Expand Down

0 comments on commit 19bd063

Please sign in to comment.