Cacheing cleanup. Introduce get_post() and get_category(). http://mosquito.wordpress.org/view.php?id=1157

git-svn-id: http://svn.automattic.com/wordpress/trunk@2478 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-03-27 20:45:01 +00:00
parent 8682ab7007
commit 8f9c24ef7a
7 changed files with 162 additions and 142 deletions

View File

@ -169,7 +169,7 @@ $query_string = apply_filters('query_string', $query_string);
update_category_cache(); update_category_cache();
// Call query posts to do the work. // Call query posts to do the work.
$posts = query_posts($query_string); $posts = & query_posts($query_string);
// Extract updated query vars back into global namespace. // Extract updated query vars back into global namespace.
extract($wp_query->query_vars); extract($wp_query->query_vars);

View File

@ -217,7 +217,7 @@ class WP_Query {
$this->query_vars[$query_var] = $value; $this->query_vars[$query_var] = $value;
} }
function get_posts() { function &get_posts() {
global $wpdb, $pagenow, $request, $user_ID; global $wpdb, $pagenow, $request, $user_ID;
// Shorthand. // Shorthand.
@ -595,7 +595,7 @@ class WP_Query {
} }
} }
function query($query) { function &query($query) {
$this->parse_query($query); $this->parse_query($query);
return $this->get_posts(); return $this->get_posts();
} }
@ -609,11 +609,9 @@ class WP_Query {
$this->queried_object_id = 0; $this->queried_object_id = 0;
if ($this->is_category) { if ($this->is_category) {
global $cache_categories; $category = &get_category($this->get('cat'));
if (isset($cache_categories[$this->get('cat')])) { $this->queried_object = &$category;
$this->queried_object = $cache_categories[$this->get('cat')]; $this->queried_object_id = $this->get('cat');
$this->queried_object_id = $this->get('cat');
}
} else if ($this->is_single) { } else if ($this->is_single) {
$this->queried_object = $this->post; $this->queried_object = $this->post;
$this->queried_object_id = $this->post->ID; $this->queried_object_id = $this->post->ID;

View File

@ -538,16 +538,67 @@ function get_postdata($postid) {
return $postdata; return $postdata;
} }
function get_catname($cat_ID) { // Retrieves post data given a post ID or post object.
global $cache_catnames, $wpdb; // Handles post caching.
if ( !$cache_catnames ) { function &get_post(&$post, $output = OBJECT) {
$results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.'); global $post_cache, $wpdb;
foreach ($results as $post) {
$cache_catnames[$post->cat_ID] = $post->cat_name; if ( empty($post) ) {
if ( isset($GLOBALS['post']) )
$post = & $GLOBALS['post'];
else
$post = null;
} elseif (is_object($post) ) {
if (! isset($post_cache[$post->ID]))
$post_cache[$post->ID] = &$post;
$post = & $post_cache[$post->ID];
} else {
if ( isset($GLOBALS['post']) && ($post == $GLOBALS['post']->ID) )
$post = & $GLOBALS['post'];
elseif (isset($post_cache[$post]))
$post = & $post_cache[$post];
else {
$query = "SELECT * FROM $wpdb->posts WHERE ID=$post";
$post_cache[$post] = & $wpdb->get_row($query);
$post = & $post_cache[$post];
} }
} }
$cat_name = $cache_catnames[$cat_ID];
return $cat_name; if ( $output == OBJECT ) {
return $post;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($post);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($post));
} else {
return $post;
}
}
// Retrieves category data given a category ID or category object.
// The category cache is fully populated by the blog header, so we don't
// have to worry with managing it here.
function &get_category(&$category, $output = OBJECT) {
global $cache_categories;
if (is_object($category))
$category = & $cache_categories[$category->cat_ID];
else
$category = & $cache_categories[$category];
if ( $output == OBJECT ) {
return $category;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($category);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($category));
} else {
return $category;
}
}
function get_catname($cat_ID) {
$category = &get_category($cat_ID);
return $category->cat_name;
} }
function gzip_compression() { function gzip_compression() {
@ -995,16 +1046,7 @@ function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args
} }
function get_page_uri($page_id) { function get_page_uri($page_id) {
global $wpdb, $cache_pages; $page = get_post($page_id);
$dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
$dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
if (!isset($cache_pages[$page_id])) {
$cache_pages[$page_id] = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page_id'");
}
$page = $cache_pages[$page_id];
$uri = urldecode($page->post_name); $uri = urldecode($page->post_name);
// A page cannot be it's own parent. // A page cannot be it's own parent.
@ -1013,12 +1055,7 @@ function get_page_uri($page_id) {
} }
while ($page->post_parent != 0) { while ($page->post_parent != 0) {
if (isset($cache_pages[$page->post_parent])) { $page = get_post($page->post_parent);
$page = $cache_pages[$page->post_parent];
} else {
$page = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page->post_parent'");
$cache_pages[$page->ID] = $page;
}
$uri = urldecode($page->post_name) . "/" . $uri; $uri = urldecode($page->post_name) . "/" . $uri;
} }
@ -1049,13 +1086,44 @@ function get_posts($args) {
return $posts; return $posts;
} }
function query_posts($query) { function &query_posts($query) {
global $wp_query; global $wp_query;
return $wp_query->query($query); return $wp_query->query($query);
} }
function update_post_caches($posts) { function update_post_cache(&$posts) {
global $category_cache, $comment_count_cache, $post_meta_cache; global $post_cache;
if ( !$posts )
return;
for ($i = 0; $i < count($posts); $i++) {
$post_cache[$posts[$i]->ID] = &$posts[$i];
}
}
function update_post_category_cache($post_ids) {
global $wpdb, $category_cache, $cache_categories;
if (empty($post_ids))
return;
if (is_array($post_ids))
$post_ids = implode(',', $post_ids);
$dogs = $wpdb->get_results("SELECT DISTINCT
post_id, category_id FROM $wpdb->categories, $wpdb->post2cat
WHERE category_id = cat_ID AND post_id IN ($post_ids)");
if ( !empty($dogs) ) {
foreach ($dogs as $catt) {
$category_cache[$catt->post_id][$catt->category_id] = &$cache_categories[$catt->category_id];
}
}
}
function update_post_caches(&$posts) {
global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache;
global $wpdb; global $wpdb;
// No point in doing all this work if we didn't match any posts. // No point in doing all this work if we didn't match any posts.
@ -1063,20 +1131,14 @@ function update_post_caches($posts) {
return; return;
// Get the categories for all the posts // Get the categories for all the posts
foreach ($posts as $post) for ($i = 0; $i < count($posts); $i++) {
$post_id_list[] = $post->ID; $post_id_list[] = $posts[$i]->ID;
$post_cache[$posts[$i]->ID] = &$posts[$i];
}
$post_id_list = implode(',', $post_id_list); $post_id_list = implode(',', $post_id_list);
$dogs = $wpdb->get_results("SELECT DISTINCT update_post_category_cache($post_id_list);
post_id, category_id, cat_name, category_nicename, category_description, category_parent
FROM $wpdb->categories, $wpdb->post2cat
WHERE category_id = cat_ID AND post_id IN ($post_id_list)");
if ( !empty($dogs) ) {
foreach ($dogs as $catt) {
$category_cache[$catt->post_id][$catt->category_id] = $catt;
}
}
// Do the same for comment numbers // Do the same for comment numbers
$comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
@ -1114,8 +1176,10 @@ function update_post_caches($posts) {
function update_category_cache() { function update_category_cache() {
global $cache_categories, $wpdb; global $cache_categories, $wpdb;
$dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories"); $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
foreach ($dogs as $catt) foreach ($dogs as $catt) {
$catt->category_id = $catt->cat_ID; // Alias.
$cache_categories[$catt->cat_ID] = $catt; $cache_categories[$catt->cat_ID] = $catt;
}
} }
function update_user_cache() { function update_user_cache() {

View File

@ -1,20 +1,15 @@
<?php <?php
function get_the_category($id = false) { function get_the_category($id = false) {
global $post, $wpdb, $category_cache; global $post, $category_cache;
if ( !$id ) if ( !$id )
$id = $post->ID; $id = $post->ID;
if ( $category_cache[$id] ) { if ( ! isset($category_cache[$id]) )
$categories = $category_cache[$id]; update_post_category_cache($id);
} else {
$categories = $wpdb->get_results(" $categories = $category_cache[$id];
SELECT category_id, cat_name, category_nicename, category_description, category_parent
FROM $wpdb->categories, $wpdb->post2cat
WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
");
}
if (!empty($categories)) if (!empty($categories))
sort($categories); sort($categories);
@ -25,19 +20,17 @@ function get_the_category($id = false) {
} }
function get_category_link($category_id) { function get_category_link($category_id) {
global $wpdb, $wp_rewrite, $querystring_start, $querystring_equal, $cache_categories; global $wp_rewrite;
$catlink = $wp_rewrite->get_category_permastruct(); $catlink = $wp_rewrite->get_category_permastruct();
if ( empty($catlink) ) { if ( empty($catlink) ) {
$file = get_settings('home') . '/' . get_settings('blogfilename'); $file = get_settings('home') . '/' . get_settings('blogfilename');
$catlink = $file . '?cat=' . $category_id; $catlink = $file . '?cat=' . $category_id;
} else { } else {
if ($cache_categories[$category_id]->category_nicename) $category = &get_category($category_id);
$category_nicename = $cache_categories[$category_id]->category_nicename; $category_nicename = $category->category_nicename;
else
$category_nicename = $wpdb->get_var('SELECT category_nicename FROM ' . $wpdb->categories . ' WHERE cat_ID=' . $category_id);
if ($parent = $cache_categories[$category_id]->category_parent) if ($parent = $category->category_parent)
$category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/'; $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
$catlink = str_replace('%category%', $category_nicename, $catlink); $catlink = str_replace('%category%', $category_nicename, $catlink);
@ -108,20 +101,13 @@ function the_category($separator = '', $parents='') {
} }
function get_the_category_by_ID($cat_ID) { function get_the_category_by_ID($cat_ID) {
global $cache_categories, $wpdb; $category = &get_category($cat_ID);
if ( !$cache_categories[$cat_ID] ) { return $category->cat_name;
$cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
$cache_categories[$cat_ID]->cat_name = $cat_name;
} else {
$cat_name = $cache_categories[$cat_ID]->cat_name;
}
return($cat_name);
} }
function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
global $cache_categories;
$chain = ''; $chain = '';
$parent = $cache_categories[$id]; $parent = &get_category($id);
if ($nicename) { if ($nicename) {
$name = $parent->category_nicename; $name = $parent->category_nicename;
} else { } else {
@ -175,11 +161,10 @@ function the_category_head($before='', $after='') {
} }
function category_description($category = 0) { function category_description($category = 0) {
global $cat, $wpdb, $cache_categories; global $cat;
if (!$category) $category = $cat; if (!$category) $category = $cat;
$category_description = $cache_categories[$category]->category_description; $category = & get_category($category);
$category_description = apply_filters('category_description', $category_description, $category); return apply_filters('category_description', $category->category_description, $category->cat_ID);
return $category_description;
} }
// out of the WordPress loop // out of the WordPress loop

View File

@ -193,8 +193,8 @@ function single_post_title($prefix = '', $display = true) {
if (!$p) { if (!$p) {
$p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'"); $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
} }
$post_data = get_postdata($p); $post = & get_post($p);
$title = $post_data['Title']; $title = $post->post_title;
$title = apply_filters('single_post_title', $title); $title = apply_filters('single_post_title', $title);
if ($display) { if ($display) {
echo $prefix.strip_tags($title); echo $prefix.strip_tags($title);
@ -347,11 +347,11 @@ function get_archives($type='', $limit='', $format='html', $before = '', $after
} }
} }
} elseif ('postbypost' == $type) { } elseif ('postbypost' == $type) {
$arcresults = $wpdb->get_results("SELECT ID, post_date, post_title FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit); $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
if ($arcresults) { if ($arcresults) {
foreach ($arcresults as $arcresult) { foreach ($arcresults as $arcresult) {
if ($arcresult->post_date != '0000-00-00 00:00:00') { if ($arcresult->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($arcresult->ID); $url = get_permalink($arcresult);
$arc_title = $arcresult->post_title; $arc_title = $arcresult->post_title;
if ($arc_title) { if ($arc_title) {
$text = strip_tags($arc_title); $text = strip_tags($arc_title);

View File

@ -22,9 +22,7 @@ function permalink_anchor($mode = 'id') {
} }
} }
function get_permalink($id = false) { function get_permalink($id = 0) {
global $post, $wpdb;
$rewritecode = array( $rewritecode = array(
'%year%', '%year%',
'%monthnum%', '%monthnum%',
@ -39,29 +37,24 @@ function get_permalink($id = false) {
'%pagename%' '%pagename%'
); );
if ($id) { $post = & get_post($id);
$idpost = $wpdb->get_row("SELECT ID, post_date, post_name, post_status, post_author FROM $wpdb->posts WHERE ID = $id"); if ($post->post_status == 'static') {
} else { return get_page_link($post->ID);
$idpost = $post;
}
if ($idpost->post_status == 'static') {
return get_page_link($idpost->ID);
} }
$permalink = get_settings('permalink_structure'); $permalink = get_settings('permalink_structure');
if ('' != $permalink) { if ('' != $permalink) {
$unixtime = strtotime($idpost->post_date); $unixtime = strtotime($post->post_date);
$category = ''; $category = '';
if (strstr($permalink, '%category%')) { if (strstr($permalink, '%category%')) {
$cats = get_the_category($idpost->ID); $cats = get_the_category($post->ID);
$category = $cats[0]->category_nicename; $category = $cats[0]->category_nicename;
if ($parent=$cats[0]->category_parent) $category = get_category_parents($parent, FALSE, '/', TRUE) . $category; if ($parent=$cats[0]->category_parent) $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
} }
$authordata = get_userdata($idpost->post_author); $authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename; $author = $authordata->user_nicename;
$rewritereplace = $rewritereplace =
array( array(
@ -71,16 +64,16 @@ function get_permalink($id = false) {
date('H', $unixtime), date('H', $unixtime),
date('i', $unixtime), date('i', $unixtime),
date('s', $unixtime), date('s', $unixtime),
$idpost->post_name, $post->post_name,
$idpost->ID, $post->ID,
$category, $category,
$author, $author,
$idpost->post_name, $post->post_name,
); );
return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $idpost); return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
} else { // if they're not using the fancy permalink option } else { // if they're not using the fancy permalink option
$permalink = get_settings('home') . '/?p=' . $idpost->ID; $permalink = get_settings('home') . '/?p=' . $post->ID;
return apply_filters('post_link', $permalink, $idpost); return apply_filters('post_link', $permalink, $post);
} }
} }

View File

@ -26,34 +26,23 @@ function the_title($before = '', $after = '', $echo = true) {
} }
function get_the_title($id = 0) { function get_the_title($id = 0) {
global $post, $wpdb; $post = &get_post($id);
$title = $post->post_title;
if (!empty($post->post_password))
$title = sprintf(__('Protected: %s'), $title);
if ( 0 != $id ) {
$id_post = $wpdb->get_row("SELECT post_title, post_password FROM $wpdb->posts WHERE ID = $id");
$title = $id_post->post_title;
if (!empty($id_post->post_password))
$title = sprintf(__('Protected: %s'), $title);
}
else {
$title = $post->post_title;
if (!empty($post->post_password))
$title = sprintf(__('Protected: %s'), $title);
}
return $title; return $title;
} }
function get_the_guid( $id = 0 ) { function get_the_guid( $id = 0 ) {
global $post, $wpdb; $post = &get_post($id);
$guid = $post->guid;
if ( 0 != $id ) return apply_filters('get_the_guid', $post->guid);
$guid = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE ID = $id");
$guid = apply_filters('get_the_guid', $guid);
return $guid;
} }
function the_guid( $id = 0 ) { function the_guid( $id = 0 ) {
echo get_the_guid(); echo get_the_guid($id);
} }
@ -261,7 +250,7 @@ function the_meta() {
// Pages // Pages
// //
function get_pages($args = '') { function &get_pages($args = '') {
global $wpdb, $cache_pages; global $wpdb, $cache_pages;
parse_str($args, $r); parse_str($args, $r);
@ -280,29 +269,20 @@ function get_pages($args = '') {
} }
} }
$dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
$dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
$post_parent = ''; $post_parent = '';
if ($r['child_of']) { if ($r['child_of']) {
$post_parent = ' AND post_parent=' . $r['child_of'] . ' '; $post_parent = ' AND post_parent=' . $r['child_of'] . ' ';
} }
$pages = $wpdb->get_results("SELECT " . $pages = $wpdb->get_results("SELECT * " .
"ID, post_title, post_name, post_parent " .
"$dates " .
"FROM $wpdb->posts " . "FROM $wpdb->posts " .
"WHERE post_status = 'static' " . "WHERE post_status = 'static' " .
"$post_parent" . "$post_parent" .
"$exclusions " . "$exclusions " .
"ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
// Update page cache. // Update cache.
if (count($pages)) { update_post_cache($pages);
foreach($pages as $page) {
$cache_pages[$page->ID] = $page;
}
}
if ( empty($pages) ) if ( empty($pages) )
$pages = array(); $pages = array();
@ -319,7 +299,7 @@ function wp_list_pages($args = '') {
// Query pages. // Query pages.
$pages = get_pages($args); $pages = & get_pages($args);
if ( $pages ) : if ( $pages ) :
if ( $r['title_li'] ) if ( $r['title_li'] )
@ -338,9 +318,9 @@ function wp_list_pages($args = '') {
// ts field. // ts field.
if (! empty($r['show_date'])) { if (! empty($r['show_date'])) {
if ('modified' == $r['show_date']) if ('modified' == $r['show_date'])
$page_tree[$page->ID]['ts'] = $page->time_modified; $page_tree[$page->ID]['ts'] = $page->post_modified;
else else
$page_tree[$page->ID]['ts'] = $page->time_created; $page_tree[$page->ID]['ts'] = $page->post_date;
} }
// The tricky bit!! // The tricky bit!!
@ -383,7 +363,7 @@ function _page_level_out($parent, $page_tree, $args, $depth = 0) {
$format = get_settings('date_format'); $format = get_settings('date_format');
if(isset($args['date_format'])) if(isset($args['date_format']))
$format = $args['date_format']; $format = $args['date_format'];
echo " " . gmdate($format,$cur_page['ts']); echo " " . mysql2date($format,$cur_page['ts']);
} }
echo "\n"; echo "\n";