Location: PHPKode > projects > Write2Left > Build/Caches/Index.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Index.php
| Description:
|   The cache class for index posts
| Created Jul-09-2003
+------------------------------------------------------
*/

/* Class: IndexCache
 * Description:
 *   Creates the main posts data cache.
 */
class IndexCache
{
	function build( $settings, $post_id = -1 )
	{
		global $W2L, $db;

		$output = "<"."?php\n";
		
		// Compute the limiting factors on posts cached
		$limit = '';
		$where = '';
		if( $settings['display_type'] == 'days' )
		{
			$first_post = $db->query_fetch( "SELECT time FROM w2l_posts 
									 		 WHERE state <> 'draft'
											   AND log_id = $settings[log_id]
											 ORDER BY time DESC" );
			$time_limit = $first_post['time'] - ( (60*60*24) * $settings['display_count'] );
			$where = "AND time > $time_limit";
		}
		else
			$limit = "LIMIT " . $settings['display_count'];
		
		// Get the categories for the log
		$count = 0;
		$db->query( "SELECT category_id AS id, name FROM w2l_categories
											  		WHERE log_id = $settings[log_id]" );
											  		
		$cats = array();									  		
		// Add them to the output
		while( $cat = $db->fetch_array() )
		{
			$cats[] = $cat;
			$output .= "\$categories[$cat[id]]='$cat[name]';\n";
			$count++;
		}
		
		$output .= "\n\n";

		// Get posts regardless of 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]
						  $where
						ORDER BY state DESC,
								 time DESC 
						$limit " );

		while( $row = $db->fetch_assoc() )
		{
			$output .= $this->array_to_php( $row, $count, 'all' );
			$count++;
		}
		
		$output .= "\n\n";

		// Get each category's posts
		foreach( $cats as $cat )
		{
			$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 category_id = $cat[id]
							  AND log_id = $settings[log_id] 
							  AND state <> 'draft'
							  $where
							ORDER BY state DESC, 
									 time DESC 
							$limit" );

			while( $row = $db->fetch_assoc() )
			{
				$output .= $this->array_to_php( $row, $count, $cat['name'] );
				$count++;
			}

			$output .= "\n\n";
		}

		$output .= "?".">";
		
		// Store the cache
		$post_cache_fp = @fopen( $settings['path'] . $settings['cache_path'] . 'post.cache.php', "w");
		
        if( $post_cache_fp == FALSE )
           return 'Unable to open output file!';
		
		while( flock( $post_cache_fp, LOCK_EX ) == FALSE )
			usleep( 20 );
		fwrite( $post_cache_fp, $output );
		fclose( $post_cache_fp );
	}

	function array_to_php( $row, $count, $category )
	{
		$output = "";

		foreach( $row as $key => $value )
		{
			$value = str_replace( '"',  '\\"', $value );
			$value = ($value == 'NULL') ? '' : $value;

			$output .= "\$post_cache['$category'][$count]['$key'] = \"$value\";\n";
		}

		return $output . "\n";
	}


}

$cache = new IndexCache();

?>
Return current item: Write2Left