<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Archive.php
| Description:
| The cache class for archive posts
| Created Jul-09-2003
+------------------------------------------------------
*/
/* Class: ArchiveCache
* Description:
* Creates the achived post data cache.
*/
class ArchiveCache
{
function build( $settings, $id = -1 )
{
global $W2L, $db, $userinfo;
// If we're building based off a post, set some stuff up
$orig_post_cat = '';
if( $id != -1 )
{
$post_info = $db->query_fetch( "SELECT category_id,
time,
DAYOFYEAR( FROM_UNIXTIME( time ) ) as day,
WEEK( FROM_UNIXTIME( time ) ) as week,
MONTH( FROM_UNIXTIME( time ) ) as month,
YEAR( FROM_UNIXTIME( time ) ) as year
FROM w2l_posts
WHERE post_id = $id" );
if( array_key_exists( 'category_id', $post_info ) )
$orig_post_cat = "AND category_id = $post_info[category_id]";
}
/**
* Category Archives
**/
if( $orig_post_cat != '' )
{
// Get the category info
$cat_id = $db->query( "SELECT category_id AS id
FROM w2l_categories
WHERE log_id = $settings[log_id]
$orig_post_cat" );
// Build each category's archive file
while( $cat = $db->fetch_array( $cat_id ) )
{
$output = "<"."?php\n";
// Get posts for the category
$count = 0;
$db->query( "SELECT post_id,
title,
body,
extended,
category_id,
author_id,
name AS author_name,
time,
state,
comment_count,
allow_comments,
last_comment_name,
last_comment_time
FROM w2l_posts
LEFT JOIN w2l_users
ON author_id = user_id
WHERE state <> 'draft'
AND log_id = $settings[log_id]
AND category_id = $cat[id]
ORDER BY time DESC" );
while( $row = $db->fetch_assoc() )
{
$output .= $this->array_to_php( $row, $count );
$count++;
}
$output .= '?'.'>';
$arc_cache_fp = @fopen( $settings['path'] . $settings['cache_path'] . 'arc.cat.' . $cat['id'] . '.cache.php', "w");
if( $arc_cache_fp == FALSE )
return 'Unable to open output file!';
while( flock( $arc_cache_fp, LOCK_EX ) == FALSE )
usleep( 20 );
fwrite( $arc_cache_fp, $output );
fclose( $arc_cache_fp );
}
}
/*****
* TIME BASED ARCHIVES
**/
// If not building a specific category
if( $id == -1 )
{
$count = 0;
$posts_id = $db->query( "SELECT post_id,
title,
body,
extended,
category_id,
author_id,
name AS author_name,
time,
state,
comment_count,
allow_comments,
last_comment_name,
last_comment_time
FROM w2l_posts
LEFT JOIN w2l_users
ON author_id = user_id
WHERE state <> 'draft'
AND log_id = $settings[log_id]
ORDER BY time DESC" );
if( $settings['arc_days'] == 1 )
$this->time_based_output( $posts_id, $settings, 'day', 'z.Y' );
if( $settings['arc_weeks'] == 1 )
$this->time_based_output( $posts_id, $settings, 'week', 'W.Y' );
if( $settings['arc_months'] == 1 )
$this->time_based_output( $posts_id, $settings, 'month', 'n.Y' );
if( $settings['arc_years'] == 1 )
$this->time_based_output( $posts_id, $settings, 'year', 'Y' );
}
else
{
$temp = array();
if( $settings['arc_days'] == 1 )
$temp[] = array( "AND DAYOFYEAR( FROM_UNIXTIME( time ) ) = $post_info[day]
AND YEAR( FROM_UNIXTIME( time ) ) = $post_info[year]",
"day",
"arc_days",
"z.Y" );
if( $settings['arc_weeks'] == 1 )
$temp[] = array( "AND WEEK( FROM_UNIXTIME( time ) ) = $post_info[week]
AND YEAR( FROM_UNIXTIME( time ) ) = $post_info[year]",
"week",
"arc_days",
"z.Y" );
if( $settings['arc_months'] == 1 )
$temp[] = array( "AND MONTH( FROM_UNIXTIME( time ) ) = $post_info[month]
AND YEAR( FROM_UNIXTIME( time ) ) = $post_info[year]",
"month",
"arc_days",
"z.Y" );
if( $settings['arc_years'] == 1 )
$temp[] = array( "AND YEAR( FROM_UNIXTIME( time ) ) = $post_info[year]",
"year",
"arc_days",
"z.Y" );
foreach( $temp as $run )
{
$posts_id = $db->query( "SELECT post_id,
title,
body,
extended,
category_id,
author_id,
name AS author_name,
time,
state,
comment_count,
allow_comments,
last_comment_name,
last_comment_time
FROM w2l_posts
LEFT JOIN w2l_users
ON author_id = user_id
WHERE state <> 'draft'
AND log_id = $settings[log_id]
$run[0]
ORDER BY time DESC" );
if( $settings[$run[2]] == 1 )
$this->time_based_output( $posts_id, $settings, $run[1], $run[3] );
}
}
}
function array_to_php( $row, $count )
{
$output = "";
foreach( $row as $key => $value )
{
$value = str_replace( '"', '\\"', $value );
$value = ($value == 'NULL') ? '' : $value;
$output .= "\$arc_cache[$count]['$key'] = \"$value\";\n";
}
return $output . "\n";
}
//================
// Builds time based archives. Takes a query id, builds a 2d array
// for each time period. Then outputs each time period to it's own
// file.
//================
function time_based_output( $posts_id, $settings, $mode, $date_format )
{
global $db;
$db->data_seek( $posts_id, 0 );
$count = 0;
$time_count = 0;
$cur_time = 0;
while( $row = $db->fetch_assoc( $posts_id ) )
{
$rows[date( $date_format, $row['time'] )][] = $row;
}
foreach( $rows as $key => $week )
{
$output = '<'."?php\n";
$count = 0;
foreach( $week as $post )
{
$output .= $this->array_to_php( $post, $count );
$count++;
}
$output .= '?'.'>';
$arc_cache_fp = @fopen( $settings['path'] . $settings['cache_path'] . "arc.$mode.$key.cache.php", "w");
if( $arc_cache_fp == FALSE )
return 'Unable to open output file!';
while( flock( $arc_cache_fp, LOCK_EX ) == FALSE )
usleep( 20 );
fwrite( $arc_cache_fp, $output );
fclose( $arc_cache_fp );
}
}
}
$cache = new ArchiveCache();
?>