WordPress.org

Make WordPress Core

Changeset 2478


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

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

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-blog-header.php

    r2378 r2478  
    170170
    171171// Call query posts to do the work.
    172 $posts = query_posts($query_string);
     172$posts = & query_posts($query_string);
    173173
    174174// Extract updated query vars back into global namespace.
  • trunk/wp-includes/classes.php

    r2475 r2478  
    218218    }
    219219
    220     function get_posts() {
     220    function &get_posts() {
    221221        global $wpdb, $pagenow, $request, $user_ID;
    222222
     
    596596    }
    597597   
    598     function query($query) {
     598    function &query($query) {
    599599        $this->parse_query($query);
    600600        return $this->get_posts();
     
    610610
    611611        if ($this->is_category) {
    612             global $cache_categories;
    613             if (isset($cache_categories[$this->get('cat')])) {
    614                 $this->queried_object = $cache_categories[$this->get('cat')];
    615                 $this->queried_object_id = $this->get('cat');
    616             }
     612            $category = &get_category($this->get('cat'));
     613            $this->queried_object = &$category;
     614            $this->queried_object_id = $this->get('cat');
    617615        } else if ($this->is_single) {
    618616            $this->queried_object = $this->post;
  • 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
  • trunk/wp-includes/template-functions-category.php

    r2450 r2478  
    22
    33function get_the_category($id = false) {
    4     global $post, $wpdb, $category_cache;
     4    global $post, $category_cache;
    55
    66    if ( !$id )
    77        $id = $post->ID;
    88
    9     if ( $category_cache[$id] ) {
    10         $categories = $category_cache[$id];
    11     } else {
    12         $categories = $wpdb->get_results("
    13         SELECT category_id, cat_name, category_nicename, category_description, category_parent
    14         FROM  $wpdb->categories, $wpdb->post2cat
    15         WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
    16         ");
    17     }
     9    if ( ! isset($category_cache[$id]) )
     10        update_post_category_cache($id);
     11
     12    $categories = $category_cache[$id];
    1813
    1914    if (!empty($categories))
     
    2621
    2722function get_category_link($category_id) {
    28     global $wpdb, $wp_rewrite, $querystring_start, $querystring_equal, $cache_categories;
     23    global $wp_rewrite;
    2924    $catlink = $wp_rewrite->get_category_permastruct();
    3025
     
    3328        $catlink = $file . '?cat=' . $category_id;
    3429    } else {
    35         if ($cache_categories[$category_id]->category_nicename)
    36             $category_nicename = $cache_categories[$category_id]->category_nicename;
    37         else
    38             $category_nicename = $wpdb->get_var('SELECT category_nicename FROM ' . $wpdb->categories . ' WHERE cat_ID=' . $category_id);
    39 
    40         if ($parent = $cache_categories[$category_id]->category_parent)
     30        $category = &get_category($category_id);
     31        $category_nicename = $category->category_nicename;
     32
     33        if ($parent = $category->category_parent)
    4134            $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
    4235
     
    109102
    110103function get_the_category_by_ID($cat_ID) {
    111     global $cache_categories, $wpdb;
    112     if ( !$cache_categories[$cat_ID] ) {
    113         $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    114         $cache_categories[$cat_ID]->cat_name = $cat_name;
    115     } else {
    116         $cat_name = $cache_categories[$cat_ID]->cat_name;
    117     }
    118     return($cat_name);
     104    $category = &get_category($cat_ID);
     105    return $category->cat_name;
    119106}
    120107
    121108function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
    122     global $cache_categories;
    123109    $chain = '';
    124     $parent = $cache_categories[$id];
     110        $parent = &get_category($id);
    125111    if ($nicename) {
    126112        $name = $parent->category_nicename;
     
    176162
    177163function category_description($category = 0) {
    178     global $cat, $wpdb, $cache_categories;
     164    global $cat;
    179165    if (!$category) $category = $cat;
    180     $category_description = $cache_categories[$category]->category_description;
    181     $category_description = apply_filters('category_description', $category_description, $category);
    182     return $category_description;
     166        $category = & get_category($category);
     167    return apply_filters('category_description', $category->category_description, $category->cat_ID);
    183168}
    184169
  • trunk/wp-includes/template-functions-general.php

    r2344 r2478  
    194194            $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
    195195        }
    196         $post_data = get_postdata($p);
    197         $title = $post_data['Title'];
     196        $post = & get_post($p);
     197        $title = $post->post_title;
    198198        $title = apply_filters('single_post_title', $title);
    199199        if ($display) {
     
    348348        }
    349349    } elseif ('postbypost' == $type) {
    350         $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);
     350        $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
    351351        if ($arcresults) {
    352352            foreach ($arcresults as $arcresult) {
    353353                if ($arcresult->post_date != '0000-00-00 00:00:00') {
    354                     $url  = get_permalink($arcresult->ID);
     354                    $url  = get_permalink($arcresult);
    355355                    $arc_title = $arcresult->post_title;
    356356                    if ($arc_title) {
  • trunk/wp-includes/template-functions-links.php

    r2422 r2478  
    2323}
    2424
    25 function get_permalink($id = false) {
    26     global $post, $wpdb;
    27 
     25function get_permalink($id = 0) {
    2826    $rewritecode = array(
    2927        '%year%',
     
    4038    );
    4139
    42     if ($id) {
    43         $idpost = $wpdb->get_row("SELECT ID, post_date, post_name, post_status, post_author FROM $wpdb->posts WHERE ID = $id");
    44     } else {
    45         $idpost = $post;
     40    $post = & get_post($id);
     41    if ($post->post_status == 'static') {
     42        return get_page_link($post->ID);
    4643    }
    4744
    48     if ($idpost->post_status == 'static') {
    49         return get_page_link($idpost->ID);
    50     }
    51 
    5245    $permalink = get_settings('permalink_structure');
    5346
    5447    if ('' != $permalink) {
    55         $unixtime = strtotime($idpost->post_date);
     48        $unixtime = strtotime($post->post_date);
    5649
    5750        $category = '';
    5851        if (strstr($permalink, '%category%')) {
    59             $cats = get_the_category($idpost->ID);
     52            $cats = get_the_category($post->ID);
    6053            $category = $cats[0]->category_nicename;
    6154            if ($parent=$cats[0]->category_parent) $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
    6255        }
    6356
    64         $authordata = get_userdata($idpost->post_author);
     57        $authordata = get_userdata($post->post_author);
    6558        $author = $authordata->user_nicename;
    6659        $rewritereplace =
     
    7265            date('i', $unixtime),
    7366            date('s', $unixtime),
    74             $idpost->post_name,
    75             $idpost->ID,
     67            $post->post_name,
     68            $post->ID,
    7669            $category,
    7770            $author,
    78             $idpost->post_name,
     71            $post->post_name,
    7972        );
    80         return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $idpost);
     73        return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
    8174    } else { // if they're not using the fancy permalink option
    82         $permalink = get_settings('home') . '/?p=' . $idpost->ID;
    83         return apply_filters('post_link', $permalink, $idpost);
     75        $permalink = get_settings('home') . '/?p=' . $post->ID;
     76        return apply_filters('post_link', $permalink, $post);
    8477    }
    8578}
  • trunk/wp-includes/template-functions-post.php

    r2435 r2478  
    2727
    2828function get_the_title($id = 0) {
    29     global $post, $wpdb;
    30    
    31     if ( 0 != $id ) {
    32         $id_post = $wpdb->get_row("SELECT post_title, post_password FROM $wpdb->posts WHERE ID = $id");
    33         $title = $id_post->post_title;
    34         if (!empty($id_post->post_password))
    35             $title = sprintf(__('Protected: %s'), $title);
    36     }
    37     else {
    38         $title = $post->post_title;
    39         if (!empty($post->post_password))
    40             $title = sprintf(__('Protected: %s'), $title);
    41     }
     29    $post = &get_post($id);
     30
     31    $title = $post->post_title;
     32    if (!empty($post->post_password))
     33        $title = sprintf(__('Protected: %s'), $title);
     34
    4235    return $title;
    4336}
    4437
    4538function get_the_guid( $id = 0 ) {
    46     global $post, $wpdb;
    47     $guid = $post->guid;
    48 
    49     if ( 0 != $id )
    50         $guid = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE ID = $id");
    51     $guid = apply_filters('get_the_guid', $guid);
    52     return $guid;
     39    $post = &get_post($id);
     40   
     41    return apply_filters('get_the_guid', $post->guid);
    5342}
    5443
    5544function the_guid( $id = 0 ) {
    56     echo get_the_guid();
     45    echo get_the_guid($id);
    5746}
    5847
     
    262251//
    263252
    264 function get_pages($args = '') {
     253function &get_pages($args = '') {
    265254    global $wpdb, $cache_pages;
    266255
     
    281270    }
    282271
    283     $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
    284     $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
    285    
    286272    $post_parent = '';
    287273    if ($r['child_of']) {
     
    289275    }
    290276   
    291     $pages = $wpdb->get_results("SELECT " .
    292                                                             "ID, post_title, post_name, post_parent " .
    293                                                             "$dates " .
     277    $pages = $wpdb->get_results("SELECT * " .
    294278                                                            "FROM $wpdb->posts " .
    295279                                                            "WHERE post_status = 'static' " .
     
    298282                                                            "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
    299283
    300     // Update page cache.
    301     if (count($pages)) {
    302         foreach($pages as $page) {
    303             $cache_pages[$page->ID] = $page;
    304         }
    305     }
     284    // Update cache.
     285    update_post_cache($pages);
    306286
    307287    if ( empty($pages) )
     
    320300
    321301    // Query pages.
    322     $pages = get_pages($args);
     302    $pages = & get_pages($args);
    323303    if ( $pages ) :
    324304
     
    339319        if (! empty($r['show_date'])) {
    340320            if ('modified' == $r['show_date'])
    341                 $page_tree[$page->ID]['ts'] = $page->time_modified;
     321                $page_tree[$page->ID]['ts'] = $page->post_modified;
    342322            else
    343                 $page_tree[$page->ID]['ts'] = $page->time_created;
     323                $page_tree[$page->ID]['ts'] = $page->post_date;
    344324        }
    345325
     
    384364            if(isset($args['date_format']))
    385365                $format = $args['date_format'];
    386             echo " " . gmdate($format,$cur_page['ts']);
     366            echo " " . mysql2date($format,$cur_page['ts']);
    387367        }
    388368        echo "\n";
Note: See TracChangeset for help on using the changeset viewer.