posts p, $bbdb->topics t WHERE p.topic_id = t.topic_id AND post_status = 0 group by t.topic_id ORDER BY p.post_time";
$matches = $bbdb->get_results($topic_query);
if ($matches) {
foreach($matches as $match) {
$url=stripafter(get_topic_link($match->tid),"?replies");
$freq=get_frequency_for_topic($match->tid);
$priority=get_priority_for_topic($match->tid);
$time=strtotime($match->tim);
$nice_time = date("Y-m-d",$time);
add_row($url, $nice_time, $freq, $priority);
}
}
}
/**
* Find all tags in the database and add them to the data that
* will be added to the sitemap;
*/
function discover_tags() {
global $bbdb;
$total_tag_count = $bbdb->get_var("SELECT sum(tag_count) FROM $bbdb->tags");
$total_tag_count = max($total_tag_count, 1);
$tag_query="SELECT t.tag_id as tid, t.tag as tag, t.tag_count, min(x.tagged_on) as tim FROM $bbdb->tags t, $bbdb->tagged x where t.tag_id = x.tag_id group by t.tag_id ORDER BY t.tag_id;";
$matches = $bbdb->get_results($tag_query);
$earliest_tag = time();
if ($matches) {
foreach($matches as $tag) {
// tag url
$url=get_tag_link($tag->tag);
// tag modification time
$time=strtotime($tag->tim);
$nice_time = date("Y-m-d",$time);
// tag priority
$priority=get_priority_for_tag($tag->tid, $tag->tag_count, $total_tag_count);
// tag update frequency
$freq=get_frequency_for_tag($tag->tid, $time, $total_tag_count);
add_row($url, $nice_time, $freq[freq], $priority, array("FRQT=".$freq[time], "time=".$time, "nt=".$nice_time));
$earliest_tag = min($time, $earliest_tag);
}
}
$tag_page_url = get_tag_page_link();
// fix for ticket 482 - http://trac.bbpress.org/ticket/482
if (strpos($tag_page_url, "://") <= 0) {
$tag_page_url = bb_get_option('domain') . $tag_page_url;
}
add_row($tag_page_url, date("Y-m-d",$earliest_tag), "daily", "0.5");
}
/**
* Manage the file handling bit of writing to the sitemap file
*/
function dump_sitemap_file() {
global $sitemap_file;
$f = fopen($sitemap_file, "w");
dump_sitemap_header($f);
dump_rows($f);
dump_sitemap_footer($f);
fflush($f);
fclose($f);
}
/**
* Neatly package up and write the XML header for the file
*/
function dump_sitemap_header($handle) {
global $version, $generator;
fwrite($handle, '');
fwrite($handle, '');
fwrite($handle, '');
fwrite($handle, '');
fwrite($handle, '');
}
/**
* Neatly package up and write the XML footer for the file
*/
function dump_sitemap_footer($handle) {
fwrite($handle, '');
}
/**
* Add a row of data to the result set that will be written to the file.
*/
function add_row($url, $mod, $freq, $priority, $debug = "") {
global $rows;
$rows[] = array( url=>$url, mod_time=>$mod, freq=>$freq, priority=>$priority, debug=>$debug );
}
/**
* Dump every row that's been discovered to the available file handle
*/
function dump_rows($handle) {
global $rows;
foreach($rows as $row) {
dump_row( $handle, $row );
}
$rows = array();
unset($rows);
}
/**
* Write out the individual row data
*/
function dump_row($handle, $row) {
global $show_debug;
$u = parse_url( $row[url] );
$u2 = $u[scheme] ."://".$u[host].$u[path].urlencode($u[query]).$u[fragment];
fwrite($handle, "\n");
fwrite($handle, " $u2\n");
fwrite($handle, " $row[mod_time]\n");
fwrite($handle, " $row[freq]\n");
fwrite($handle, " $row[priority]\n");
fwrite($handle, "\n");
}
function sometimes_create_sitemap() {
// this hook gets fired an awful lot, so to reduce
// the server workload, there's only a 1 in 10 chance
// that it will actually do anything. Most of the time
// it just returns.
$n = mt_rand(1, 10);
if ( ($n % 10) ) {
create_sitemap();
}
}
/**
* This is the main function that gets linked to various bb_press
* hooks (aka actions).
*/
function create_sitemap() {
// discover what there is to know
discover_topics();
discover_tags();
// write it out
dump_sitemap_file();
// finished already!
}
/**
* BBPress Hooks which the plugin uses to ensure it knows
* when there has been an update.
*/
function add_hooks() {
add_filter( 'bb_new_topic', 'sometimes_create_sitemap', 0);
add_filter( 'bb_update_topic', 'sometimes_create_sitemap', 0);
add_filter( 'bb_delete_topic', 'sometimes_create_sitemap', 0);
add_filter( 'bb_new_post', 'sometimes_create_sitemap', 0);
add_filter( 'bb_update_post', 'sometimes_create_sitemap', 0);
add_filter( 'bb_delete_post', 'sometimes_create_sitemap', 0);
}
/**
* this is useful when testing - just add a favourite to fire
* the generation - because there's no plugin UI in whcih to
* stick a "regenerate" button.
*/
function add_debug_hooks() {
add_filter( 'bb_add_user_favorite', 'create_sitemap', 0);
}
?>