// Only define DEBUG_MODE if it's not already defined
if (!defined('DEBUG_MODE')) {
define('DEBUG_MODE', false);
}
// Only show errors in development
if (DEBUG_MODE === true) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
// Check if article exists
if (empty($article)) {
// Article not found, show 404 page
include_once FRONTEND_PATH . '/templates/404.php';
exit;
}
// Set page variables
$pageTitle = $article['title'] . ' - ' . APP_NAME;
$pageDescription = !empty($article['excerpt']) ? strip_tags($article['excerpt']) : truncate_text(strip_tags($article['content']), 160);
// Clean any remaining HTML entities from the description
$pageDescription = html_entity_decode(strip_tags($pageDescription));
$ogType = 'article';
$ogImage = !empty($article['featured_image']) ? 'https://' . $_SERVER['HTTP_HOST'] . '/' . $article['featured_image'] : null;
$currentUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/article/' . $article['slug'];
$activeNav = '';
$showBreadcrumbs = true;
// Set breadcrumbs
$breadcrumbs = [];
// Add category to breadcrumbs if available
if (!empty($articleCategories) && count($articleCategories) > 0) {
$category = $articleCategories[0]; // Use first category
$breadcrumbs[] = [
'title' => $category['name'],
'url' => '/category/' . $category['slug']
];
}
// Add article to breadcrumbs
$breadcrumbs[] = [
'title' => $article['title'],
'active' => true
];
// Generate schema.org markup for article
$schemaMarkup = generate_article_schema($article);
// The update_tags function has been moved to frontend/includes/functions.php
// Debug: Before view count increment
error_log("DEBUG: Before increment_article_views - Article ID: " . $article['id']);
// Increment view count before displaying
increment_article_views($article['id']);
// Debug: After view count increment
error_log("DEBUG: After increment_article_views - Article ID: " . $article['id']);
// For debugging - create a debug log that will be displayed on the page
$debugLog = [];
$debugLog[] = "Debug: Article ID: " . $article['id'];
// Get database connection
global $db;
// Initialize debug log
$debugLog[] = "Using existing database connection via dbQuery";
// Debug: Database connection check
error_log("DEBUG: Database connection check - DB is " . ($db ? "available" : "NOT available"));
// Make sure article tags are loaded and initialized as an array
if (!isset($articleTags)) {
// Fetch existing tags for this article
try {
if ($db) {
$tagQuery = "SELECT t.id, t.name, t.slug
FROM tags t
JOIN news_tags nt ON t.id = nt.tag_id
WHERE nt.news_id = ?";
$tagStmt = $db->prepare($tagQuery);
$tagStmt->execute([$article['id']]);
$articleTags = $tagStmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$articleTags = [];
error_log('Database connection not available when fetching article tags');
}
} catch (Exception $e) {
// If there's an error, initialize as empty array
$articleTags = [];
error_log('Error fetching article tags: ' . $e->getMessage());
}
}
// Ensure $articleTags is always an array
if (!is_array($articleTags)) {
$articleTags = [];
}
// Debug: Before calling update_tags
error_log("DEBUG: About to call update_tags function - Article ID: " . $article['id']);
error_log("DEBUG: ArticleTags count before update: " . count($articleTags));
// Update article tags based on content
$tagsUpdated = false;
$tagsUpdated = update_tags();
error_log("DEBUG: update_tags function returned: " . ($tagsUpdated ? "TRUE" : "FALSE"));
// Refresh article tags after potential updates if update was successful
if ($tagsUpdated && $db) {
error_log("DEBUG: Refreshing article tags after successful update");
try {
$tagQuery = "SELECT t.id, t.name, t.slug
FROM tags t
JOIN news_tags nt ON t.id = nt.tag_id
WHERE nt.news_id = ?";
$tagStmt = $db->prepare($tagQuery);
$tagStmt->execute([$article['id']]);
$articleTags = $tagStmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
// Log error but continue with existing tags
error_log('Error refreshing article tags: ' . $e->getMessage());
}
}
// Update debug log with more information
$debugLog[] = "Debug: Database connection: " . ($db ? "Available" : "NOT available");
$debugLog[] = "Debug: ArticleTags count: " . count($articleTags);
// Add function call trace for debugging
$debugLog[] = "Debug: Execution trace:";
$debugLog[] = "- Before increment_article_views";
$debugLog[] = "- After increment_article_views";
$debugLog[] = "- Database connection check: " . ($db ? "SUCCESS" : "FAILED");
$debugLog[] = "- Article tags loaded: " . (isset($articleTags) ? "YES" : "NO");
$debugLog[] = "- About to call update_tags";
$debugLog[] = "- update_tags result: " . ($tagsUpdated ? "SUCCESS" : "FAILED");
// Add database connection details
$debugLog[] = "Debug: Database connection details:";
if ($db) {
$debugLog[] = "- Connection type: " . get_class($db);
$debugLog[] = "- Connection hash: " . spl_object_hash($db);
}
// Add more detailed database info
if ($db) {
try {
$debugLog[] = "Debug: Database info:";
$stmt = $db->query("SELECT DATABASE() as db_name");
$dbInfo = $stmt->fetch(PDO::FETCH_ASSOC);
$debugLog[] = "- Current database: " . ($dbInfo['db_name'] ?? 'unknown');
// Check if tags table exists and has data
$stmt = $db->query("SELECT COUNT(*) as tag_count FROM tags");
$tagCount = $stmt->fetch(PDO::FETCH_ASSOC);
$debugLog[] = "- Tags in database: " . ($tagCount['tag_count'] ?? 'unknown');
} catch (Exception $e) {
$debugLog[] = "- Error getting DB info: " . $e->getMessage();
}
}
// Include header
include_once FRONTEND_PATH . '/includes/header.php';
// Display debug information at the top of the page (only during development)
if (defined('DEBUG_MODE') && DEBUG_MODE === true) {
echo '
';
echo '
Debug Information
';
echo '
';
foreach ($debugLog as $log) {
echo '- ' . htmlspecialchars($log) . '
';
}
echo '
';
// Direct test of update_tags function with sample data
if ($db) {
echo '
Direct Test of update_tags Function
';
// Create sample data with proper string values
$sampleArticle = [
'id' => $article['id'],
'title' => 'Sample Title for Testing',
'content' => 'This is sample content for testing the update_tags function.'
];
// Ensure all values are strings to avoid null values
$sampleArticle['title'] = (string)$sampleArticle['title'];
$sampleArticle['content'] = (string)$sampleArticle['content'];
$sampleTags = [];
// Try to get some real tags for testing
try {
$stmt = $db->query("SELECT id, name, slug FROM tags LIMIT 5");
$testTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '
Test with ' . count($testTags) . ' sample tags:
';
echo '
';
foreach ($testTags as $tag) {
echo '- Tag ID: ' . $tag['id'] . ', Name: ' . htmlspecialchars($tag['name']) . '
';
// Add one of these tags to sample content to ensure a match
if (isset($testTags[0])) {
$sampleArticle['content'] .= ' ' . $testTags[0]['name'];
}
}
echo '
';
// Run the test
echo '
Running test update_tags function...
';
// Set global variables for the test
global $article, $articleTags;
$originalArticle = $article;
$originalTags = $articleTags;
// Temporarily set global variables for the test
$article = $sampleArticle;
$articleTags = $sampleTags;
// Run the test
$testResult = update_tags();
// Restore original values
$article = $originalArticle;
$articleTags = $originalTags;
echo '
Test result: ' . ($testResult ? 'SUCCESS' : 'FAILED') . '
';
} catch (Exception $e) {
echo '
Error during test: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
echo '
';
}
?>