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();
// 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($wp_query->query_vars);

View File

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

View File

@ -538,16 +538,67 @@ function get_postdata($postid) {
return $postdata;
}
function get_catname($cat_ID) {
global $cache_catnames, $wpdb;
if ( !$cache_catnames ) {
$results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.');
foreach ($results as $post) {
$cache_catnames[$post->cat_ID] = $post->cat_name;
// Retrieves post data given a post ID or post object.
// Handles post caching.
function &get_post(&$post, $output = OBJECT) {
global $post_cache, $wpdb;
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() {
@ -995,30 +1046,16 @@ function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args
}
function get_page_uri($page_id) {
global $wpdb, $cache_pages;
$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];
$page = get_post($page_id);
$uri = urldecode($page->post_name);
// A page cannot be it's own parent.
if ($page->post_parent == $page->ID) {
return $uri;
}
while ($page->post_parent != 0) {
if (isset($cache_pages[$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;
}
$page = get_post($page->post_parent);
$uri = urldecode($page->post_name) . "/" . $uri;
}
@ -1049,13 +1086,44 @@ function get_posts($args) {
return $posts;
}
function query_posts($query) {
function &query_posts($query) {
global $wp_query;
return $wp_query->query($query);
}
function update_post_caches($posts) {
global $category_cache, $comment_count_cache, $post_meta_cache;
function update_post_cache(&$posts) {
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;
// No point in doing all this work if we didn't match any posts.
@ -1063,21 +1131,15 @@ function update_post_caches($posts) {
return;
// Get the categories for all the posts
foreach ($posts as $post)
$post_id_list[] = $post->ID;
$post_id_list = implode(',', $post_id_list);
$dogs = $wpdb->get_results("SELECT DISTINCT
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;
}
for ($i = 0; $i < count($posts); $i++) {
$post_id_list[] = $posts[$i]->ID;
$post_cache[$posts[$i]->ID] = &$posts[$i];
}
$post_id_list = implode(',', $post_id_list);
update_post_category_cache($post_id_list);
// Do the same for comment numbers
$comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
FROM $wpdb->posts
@ -1114,8 +1176,10 @@ function update_post_caches($posts) {
function update_category_cache() {
global $cache_categories, $wpdb;
$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;
}
}
function update_user_cache() {

View File

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

View File

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

View File

@ -22,9 +22,7 @@ function permalink_anchor($mode = 'id') {
}
}
function get_permalink($id = false) {
global $post, $wpdb;
function get_permalink($id = 0) {
$rewritecode = array(
'%year%',
'%monthnum%',
@ -39,29 +37,24 @@ function get_permalink($id = false) {
'%pagename%'
);
if ($id) {
$idpost = $wpdb->get_row("SELECT ID, post_date, post_name, post_status, post_author FROM $wpdb->posts WHERE ID = $id");
} else {
$idpost = $post;
}
if ($idpost->post_status == 'static') {
return get_page_link($idpost->ID);
$post = & get_post($id);
if ($post->post_status == 'static') {
return get_page_link($post->ID);
}
$permalink = get_settings('permalink_structure');
if ('' != $permalink) {
$unixtime = strtotime($idpost->post_date);
$unixtime = strtotime($post->post_date);
$category = '';
if (strstr($permalink, '%category%')) {
$cats = get_the_category($idpost->ID);
$cats = get_the_category($post->ID);
$category = $cats[0]->category_nicename;
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;
$rewritereplace =
array(
@ -71,16 +64,16 @@ function get_permalink($id = false) {
date('H', $unixtime),
date('i', $unixtime),
date('s', $unixtime),
$idpost->post_name,
$idpost->ID,
$post->post_name,
$post->ID,
$category,
$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
$permalink = get_settings('home') . '/?p=' . $idpost->ID;
return apply_filters('post_link', $permalink, $idpost);
$permalink = get_settings('home') . '/?p=' . $post->ID;
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) {
global $post, $wpdb;
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);
}
$post = &get_post($id);
$title = $post->post_title;
if (!empty($post->post_password))
$title = sprintf(__('Protected: %s'), $title);
return $title;
}
function get_the_guid( $id = 0 ) {
global $post, $wpdb;
$guid = $post->guid;
if ( 0 != $id )
$guid = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE ID = $id");
$guid = apply_filters('get_the_guid', $guid);
return $guid;
$post = &get_post($id);
return apply_filters('get_the_guid', $post->guid);
}
function the_guid( $id = 0 ) {
echo get_the_guid();
echo get_the_guid($id);
}
@ -261,7 +250,7 @@ function the_meta() {
// Pages
//
function get_pages($args = '') {
function &get_pages($args = '') {
global $wpdb, $cache_pages;
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 = '';
if ($r['child_of']) {
$post_parent = ' AND post_parent=' . $r['child_of'] . ' ';
}
$pages = $wpdb->get_results("SELECT " .
"ID, post_title, post_name, post_parent " .
"$dates " .
$pages = $wpdb->get_results("SELECT * " .
"FROM $wpdb->posts " .
"WHERE post_status = 'static' " .
"$post_parent" .
"$exclusions " .
"ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
// Update page cache.
if (count($pages)) {
foreach($pages as $page) {
$cache_pages[$page->ID] = $page;
}
}
// Update cache.
update_post_cache($pages);
if ( empty($pages) )
$pages = array();
@ -319,7 +299,7 @@ function wp_list_pages($args = '') {
// Query pages.
$pages = get_pages($args);
$pages = & get_pages($args);
if ( $pages ) :
if ( $r['title_li'] )
@ -338,9 +318,9 @@ function wp_list_pages($args = '') {
// ts field.
if (! empty($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
$page_tree[$page->ID]['ts'] = $page->time_created;
$page_tree[$page->ID]['ts'] = $page->post_date;
}
// The tricky bit!!
@ -383,7 +363,7 @@ function _page_level_out($parent, $page_tree, $args, $depth = 0) {
$format = get_settings('date_format');
if(isset($args['date_format']))
$format = $args['date_format'];
echo " " . gmdate($format,$cur_page['ts']);
echo " " . mysql2date($format,$cur_page['ts']);
}
echo "\n";