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

/* Class: ItemCache
 * Description:
 *   Creates the individual post data cache.
 */
class ItemCache
{
	function build( $settings, $post_id )
	{
		global $W2L, $db;

		if( $post_id != -1 )
			$id = $db->query( "SELECT post_id, 
									  title, 
									  p.category_id, 
									  c.name AS category_name, 
									  body, 
									  extended, 
									  author_id, 
									  u.name AS author_name, 
									  time, 
									  p.comment_count, 
									  allow_comments, 
									  last_comment_name, 
									  last_comment_time 
							     FROM w2l_posts AS p 
							          LEFT JOIN w2l_users AS u 
							        	ON author_id = user_id 
							          LEFT JOIN w2l_categories AS c 
							        	ON c.category_id = p.category_id 
							    WHERE post_id = $post_id
						    	  AND p.log_id = $settings[log_id]" );
		else
			$id = $db->query( "SELECT post_id, 
									  title, 
									  p.category_id, 
									  c.name AS category_name, 
									  body, 
									  extended, 
									  author_id, 
									  u.name AS author_name, 
									  time, 
									  p.comment_count, 
									  allow_comments, 
									  last_comment_name, 
									  last_comment_time 
							     FROM w2l_posts AS p 
							          LEFT JOIN w2l_users AS u 
							        	ON author_id = user_id 
							          LEFT JOIN w2l_categories AS c 
							        	ON c.category_id = p.category_id 
							    WHERE p.log_id = $settings[log_id]" );
					  
		while( $row = $db->fetch_assoc( $id ) )
		{
			$output = "<"."?php\n";
			
			foreach( $row as $key => $value )
			{
				switch( $key )
				{
					case 'title':
					case 'body':
					case 'extended':
						$value = ($value == 'NULL') ? '' : $value;
				}
				
				$output .= "\$item_data['$key'] = \"" . str_replace( '"', '\\"', $value ) ."\";\n";
			}
				
			$output .= "\n\n";
			
			$output .= "\$comments = array();\n";
			$db->query( "SELECT comment_id, 
								body, 
								author_name, 
								author_email, 
								author_url, 
								time 
						   FROM w2l_comments 
						  WHERE post_id = $row[post_id]" );
	
			$num = 0;	
			while ( $comment = $db->fetch_assoc() )
			{
				foreach( $comment as $key => $value )
					$output .= "\$comments[$num]['$key'] = \"" . nl2br( str_replace( '"', '\"', $value ) ) ."\";\n";
				$num++;
			}
			
			$output .= '?'.'>';
	
			$post_cache_fp = @fopen( $settings['path'] . $settings['cache_path'] . 'item.' . $row['post_id'] . '.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 );
		}
	}
}

$cache = new ItemCache();
?>
Return current item: Write2Left