Make WordPress Core


Ignore:
Timestamp:
03/27/2005 08:45:01 PM (21 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r2477 r2478  
    539539}
    540540
     541// Retrieves post data given a post ID or post object.
     542// Handles post caching.
     543function &get_post(&$post, $output = OBJECT) {
     544        global $post_cache, $wpdb;
     545
     546        if ( empty($post) ) {
     547                if ( isset($GLOBALS['post']) )
     548                        $post = & $GLOBALS['post'];
     549                else
     550                        $post = null;
     551        } elseif (is_object($post) ) {
     552                if (! isset($post_cache[$post->ID]))
     553                        $post_cache[$post->ID] = &$post;
     554                $post = & $post_cache[$post->ID];
     555        } else {
     556                if ( isset($GLOBALS['post']) && ($post == $GLOBALS['post']->ID) )
     557                        $post = & $GLOBALS['post'];
     558                elseif (isset($post_cache[$post]))
     559                        $post = & $post_cache[$post];
     560                else {
     561                        $query = "SELECT * FROM $wpdb->posts WHERE ID=$post";
     562                        $post_cache[$post] = & $wpdb->get_row($query);
     563                        $post = & $post_cache[$post];
     564                }
     565        }
     566
     567        if ( $output == OBJECT ) {
     568                return $post;
     569        } elseif ( $output == ARRAY_A ) {
     570                return get_object_vars($post);
     571        } elseif ( $output == ARRAY_N ) {
     572                return array_values(get_object_vars($post));
     573        } else {
     574                return $post;
     575        }
     576}
     577
     578// Retrieves category data given a category ID or category object.
     579// The category cache is fully populated by the blog header, so we don't
     580// have to worry with managing it here.
     581function &get_category(&$category, $output = OBJECT) {
     582        global $cache_categories;
     583        if (is_object($category))
     584                $category = & $cache_categories[$category->cat_ID];
     585        else
     586                $category = & $cache_categories[$category];
     587
     588        if ( $output == OBJECT ) {
     589                return $category;
     590        } elseif ( $output == ARRAY_A ) {
     591                return get_object_vars($category);
     592        } elseif ( $output == ARRAY_N ) {
     593                return array_values(get_object_vars($category));
     594        } else {
     595                return $category;
     596        }
     597}
     598
    541599function get_catname($cat_ID) {
    542         global $cache_catnames, $wpdb;
    543         if ( !$cache_catnames ) {
    544         $results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.');
    545                 foreach ($results as $post) {
    546                         $cache_catnames[$post->cat_ID] = $post->cat_name;
    547                 }
    548         }
    549         $cat_name = $cache_catnames[$cat_ID];
    550         return $cat_name;
     600        $category = &get_category($cat_ID);
     601        return $category->cat_name;
    551602}
    552603
     
    9961047
    9971048function get_page_uri($page_id) {
    998         global $wpdb, $cache_pages;
    999 
    1000         $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
    1001         $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
    1002 
    1003         if (!isset($cache_pages[$page_id])) {
    1004                 $cache_pages[$page_id] = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page_id'");
    1005         }
    1006 
    1007         $page = $cache_pages[$page_id];
     1049        $page = get_post($page_id);
    10081050        $uri = urldecode($page->post_name);
    10091051
     
    10121054                return $uri;
    10131055        }
    1014 
     1056       
    10151057        while ($page->post_parent != 0) {
    1016                 if (isset($cache_pages[$page->post_parent])) {
    1017                         $page = $cache_pages[$page->post_parent];
    1018                 } else {
    1019                         $page = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page->post_parent'");
    1020                         $cache_pages[$page->ID] = $page;
    1021                 }
     1058                $page = get_post($page->post_parent);
    10221059                $uri = urldecode($page->post_name) . "/" . $uri;
    10231060        }
     
    10501087}
    10511088
    1052 function query_posts($query) {
     1089function &query_posts($query) {
    10531090        global $wp_query;
    10541091        return $wp_query->query($query);
    10551092}
    10561093
    1057 function update_post_caches($posts) {
    1058         global $category_cache, $comment_count_cache, $post_meta_cache;
     1094function update_post_cache(&$posts) {
     1095        global $post_cache;
     1096
     1097        if ( !$posts )
     1098                return;
     1099
     1100        for ($i = 0; $i < count($posts); $i++) {
     1101                $post_cache[$posts[$i]->ID] = &$posts[$i];
     1102        }
     1103}
     1104
     1105function update_post_category_cache($post_ids) {
     1106        global $wpdb, $category_cache, $cache_categories;
     1107
     1108        if (empty($post_ids))
     1109                return;
     1110
     1111        if (is_array($post_ids))
     1112                $post_ids = implode(',', $post_ids);
     1113
     1114        $dogs = $wpdb->get_results("SELECT DISTINCT
     1115        post_id, category_id FROM $wpdb->categories, $wpdb->post2cat
     1116        WHERE category_id = cat_ID AND post_id IN ($post_ids)");
     1117
     1118        if ( !empty($dogs) ) {
     1119                foreach ($dogs as $catt) {
     1120                        $category_cache[$catt->post_id][$catt->category_id] = &$cache_categories[$catt->category_id];
     1121                }
     1122        }
     1123}
     1124
     1125function update_post_caches(&$posts) {
     1126        global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache;
    10591127        global $wpdb;
    10601128       
     
    10641132
    10651133        // Get the categories for all the posts
    1066         foreach ($posts as $post)
    1067                 $post_id_list[] = $post->ID;
     1134        for ($i = 0; $i < count($posts); $i++) {
     1135                $post_id_list[] = $posts[$i]->ID;
     1136                $post_cache[$posts[$i]->ID] = &$posts[$i];
     1137        }
     1138
    10681139        $post_id_list = implode(',', $post_id_list);
    1069 
    1070         $dogs = $wpdb->get_results("SELECT DISTINCT
    1071         post_id, category_id, cat_name, category_nicename, category_description, category_parent
    1072         FROM $wpdb->categories, $wpdb->post2cat
    1073         WHERE category_id = cat_ID AND post_id IN ($post_id_list)");
    1074    
    1075         if ( !empty($dogs) ) {
    1076                 foreach ($dogs as $catt) {
    1077                         $category_cache[$catt->post_id][$catt->category_id] = $catt;
    1078                 }
    1079         }
     1140       
     1141        update_post_category_cache($post_id_list);
    10801142
    10811143        // Do the same for comment numbers
     
    11151177        global $cache_categories, $wpdb;
    11161178        $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
    1117         foreach ($dogs as $catt)
     1179        foreach ($dogs as $catt) {
     1180                $catt->category_id = $catt->cat_ID; // Alias.
    11181181                $cache_categories[$catt->cat_ID] = $catt;
     1182        }
    11191183}
    11201184
Note: See TracChangeset for help on using the changeset viewer.