<?php
defined('WikyBlog') or die("Not an entry point...");
class forumSync{
function sync($forum_id){
global $wbTables;
if( !is_numeric($forum_id) ){
message('INVALID_REQUEST');
return;
}
$query = 'SELECT `title` FROM '.$wbTables['bb_forums'];
$query .= ' WHERE `id` = "'. wbDB::escape($forum_id) .'" ';
$result = wbDB::runQuery($query);
if( !$result ){
message('INVALID_REQUEST');
return;
}
if( mysql_num_rows($result) !== 1){
message('INVALID_REQUEST');
return;
}
$row = mysql_fetch_assoc($result);
$forumTitle = $row['title'];
forumSync::syncTopic($forum_id);
forumSync::syncForum($forum_id);
message('SYNC_FINISHED',toDisplay($forumTitle));
}
function syncTopic($forum_id,$file_id=false){
global $wbTables;
//update bb_topics
$postsSub = 'SELECT COUNT(*) ';
$postsSub .= ' FROM '.$wbTables['bb_posts'];
$postsSub .= ' WHERE ';
$postsSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$postsSub .= ' AND `file_id` = '.$wbTables['bb_topics'].'.`file_id` ';
$modSub = 'SELECT `modified` ';
$modSub .= ' FROM '.$wbTables['bb_posts'];
$modSub .= ' WHERE ';
$modSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$modSub .= ' AND `file_id` = '.$wbTables['bb_topics'].'.`file_id` ';
$modSub .= ' ORDER BY `id` DESC ';
$modSub .= ' LIMIT 1 OFFSET 0 ';
$userSub = 'SELECT `poster_name` ';
$userSub .= ' FROM '.$wbTables['bb_posts'];
$userSub .= ' WHERE ';
$userSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$userSub .= ' AND `file_id` = '.$wbTables['bb_topics'].'.`file_id` ';
$userSub .= ' ORDER BY `id` DESC ';
$userSub .= ' LIMIT 1 OFFSET 0 ';
$ipSub = 'SELECT `poster_ip` ';
$ipSub .= ' FROM '.$wbTables['bb_posts'];
$ipSub .= ' WHERE ';
$ipSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$ipSub .= ' AND `file_id` = '.$wbTables['bb_topics'].'.`file_id` ';
$ipSub .= ' ORDER BY `id` DESC ';
$ipSub .= ' LIMIT 1 OFFSET 0 ';
$query = 'UPDATE '.$wbTables['bb_topics'];
$query .= ' SET ';
$query .= ' `modified` = IFNULL( ('.$modSub.'), `modified` ) ';
$query .= ' , `posts` = IFNULL( ('.$postsSub.'), 0) ';
$query .= ' , `last_post_user` = IFNULL( ('.$userSub.'), "") ';
$query .= ' , `last_post_ip` = IFNULL( ('.$ipSub.'), "") ';
$query .= ' WHERE ';
$query .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
if( $file_id ){
$query .= ' AND `file_id` = "'. wbDB::escape($file_id) .'" ';
}
wbDB::runQuery($query);
}
function syncForum($forum_id){
global $wbTables;
//update bb_forums
$modSub = 'SELECT `modified` FROM '.$wbTables['bb_topics'];
$modSub .= ' WHERE `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$modSub .= ' ORDER BY `modified` DESC ';
$modSub .= ' LIMIT 1 OFFSET 0 ';
$postsSub = 'SELECT COUNT(*) ';
$postsSub .= ' FROM '.$wbTables['bb_posts'];
$postsSub .= ' WHERE ';
$postsSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$topicsSub = 'SELECT COUNT(*) as `topics` ';
$topicsSub .= ' FROM '.$wbTables['bb_topics'];
$topicsSub .= ' WHERE ';
$topicsSub .= ' `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$userSub = ' SELECT `last_post_user` FROM '.$wbTables['bb_topics'];
$userSub .= ' WHERE `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$userSub .= ' ORDER BY `modified` DESC ';
$userSub .= ' LIMIT 1 OFFSET 0 ';
$ipSub = ' SELECT `last_post_ip` FROM '.$wbTables['bb_topics'];
$ipSub .= ' WHERE `forum_id` = "'. wbDB::escape($forum_id) .'" ';
$ipSub .= ' ORDER BY `modified` DESC ';
$ipSub .= ' LIMIT 1 OFFSET 0 ';
$query = 'UPDATE '.$wbTables['bb_forums'];
$query .= ' SET ';
$query .= ' `modified` = IFNULL( ('.$modSub.'), `modified` ) ';
$query .= ' , `posts` = IFNULL( ('.$postsSub.'), 0) ';
$query .= ' , `topics` = IFNULL( ('.$topicsSub.'),0) ';
$query .= ' , `last_post_user` = IFNULL( ('.$userSub.'),"") ';
$query .= ' , `last_post_ip` = IFNULL( ('.$ipSub.'),"") ';
$query .= ' WHERE ';
$query .= ' `id` = "'. wbDB::escape($forum_id) .'" ';
$query .= ' LIMIT 1';
wbDB::runQuery($query);
}
}