Make WordPress Core


Ignore:
Timestamp:
05/24/2004 02:55:39 AM (21 years ago)
Author:
rboren
Message:

Introducing query_posts(), update_post_caches(), update_user_cache(), and update_category_cache().

File:
1 edited

Legend:

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

    r1338 r1354  
    120120if (!isset($what_to_show))
    121121    $what_to_show = get_settings('what_to_show');
    122 $archive_mode = get_settings('archive_mode');
    123 $use_gzipcompression = get_settings('gzipcompression');
    124 
    125 // First let's clear some variables
    126 $whichcat = '';
    127 $whichauthor = '';
    128 $result = '';
    129 $where = '';
    130 $limits = '';
    131 $distinct = '';
    132 $join = '';
    133 
    134 if ($pagenow != 'post.php') { timer_start(); }
    135 
    136122if (isset($showposts) && $showposts) {
    137123    $showposts = (int)$showposts;
    138124    $posts_per_page = $showposts;
    139125}
     126$archive_mode = get_settings('archive_mode');
     127$use_gzipcompression = get_settings('gzipcompression');
    140128
    141 $add_hours = intval(get_settings('gmt_offset'));
    142 $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
    143 $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
     129$more_wpvars = array('posts_per_page', 'what_to_show', 'showposts');
    144130
    145 // If a month is specified in the querystring, load that month
    146 if ('' != $m) {
    147     $m = '' . preg_replace('|[^0-9]|', '', $m);
    148     $where .= ' AND YEAR(post_date)=' . substr($m, 0, 4);
    149     if (strlen($m)>5)
    150         $where .= ' AND MONTH(post_date)=' . substr($m, 4, 2);
    151     if (strlen($m)>7)
    152         $where .= ' AND DAYOFMONTH(post_date)=' . substr($m, 6, 2);
    153     if (strlen($m)>9)
    154         $where .= ' AND HOUR(post_date)=' . substr($m, 8, 2);
    155     if (strlen($m)>11)
    156         $where .= ' AND MINUTE(post_date)=' . substr($m, 10, 2);
    157     if (strlen($m)>13)
    158         $where .= ' AND SECOND(post_date)=' . substr($m, 12, 2);
    159 }
    160 
    161 if ('' != $hour) {
    162     $hour = '' . intval($hour);
    163     $where .= " AND HOUR(post_date)='$hour'";
    164 }
    165 
    166 if ('' != $minute) {
    167     $minute = '' . intval($minute);
    168     $where .= " AND MINUTE(post_date)='$minute'";
    169 }
    170 
    171 if ('' != $second) {
    172     $second = '' . intval($second);
    173     $where .= " AND SECOND(post_date)='$second'";
    174 }
    175 
    176 if ('' != $year) {
    177     $year = '' . intval($year);
    178     $where .= " AND YEAR(post_date)='$year'";
    179 }
    180 
    181 if ('' != $monthnum) {
    182     $monthnum = '' . intval($monthnum);
    183     $where .= " AND MONTH(post_date)='$monthnum'";
    184 }
    185 
    186 if ('' != $day) {
    187     $day = '' . intval($day);
    188     $where .= " AND DAYOFMONTH(post_date)='$day'";
    189 }
    190 
    191 if ('' != $name) {
    192     $name = preg_replace('/[^a-z0-9-_]/', '', $name);
    193     $where .= " AND post_name = '$name'";
    194 }
    195 
    196 if ('' != $w) {
    197     $w = ''.intval($w);
    198     $where .= " AND WEEK(post_date, 1)='$w'";
    199 }
    200 
    201 // If a post number is specified, load that post
    202 if (($p != '') && ($p != 'all')) {
    203     $p = intval($p);
    204     $where = ' AND ID = '.$p;
    205 }
    206 
    207 // If a search pattern is specified, load the posts that match
    208 if (!empty($s)) {
    209     $s = addslashes_gpc($s);
    210     $search = ' AND (';
    211     $s = preg_replace('/, +/', ' ', $s);
    212     $s = str_replace(',', ' ', $s);
    213     $s = str_replace('"', ' ', $s);
    214     $s = trim($s);
    215     if ($exact) {
    216         $n = '';
    217     } else {
    218         $n = '%';
    219     }
    220     if (!$sentence) {
    221         $s_array = explode(' ',$s);
    222         $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
    223         for ( $i = 1; $i < count($s_array); $i = $i + 1) {
    224             $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
    225         }
    226         $search .= ' OR (post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')';
    227         $search .= ')';
    228     } else {
    229         $search = ' AND ((post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\'))';
     131// Construct the query string.
     132$query_string = '';
     133foreach (array_merge($wpvarstoreset, $more_wpvars) as $wpvar) {
     134    if ($$wpvar != '') {
     135        $query_string .= (strlen($query_string) < 1) ? '' : '&';
     136        $query_string .= $wpvar . '=' . rawurlencode($$wpvar);
    230137    }
    231138}
    232139
    233 // Category stuff
    234 $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
    235 foreach ($dogs as $catt) {
    236     $cache_categories[$catt->cat_ID] = $catt;
    237 }
     140if ($pagenow != 'post.php') { timer_start(); }
    238141
    239 if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||
    240     // Bypass cat checks if fetching specific posts
    241     (
    242         intval($year) || intval($monthnum) || intval($day) || intval($w) ||
    243         intval($p) || !empty($name) || !empty($s)
    244     )
    245         ) {
    246     $whichcat='';
    247 } else {
    248     $cat = ''.urldecode($cat).'';
    249     $cat = addslashes_gpc($cat);
    250     if (stristr($cat,'-')) {
    251         // Note: if we have a negative, we ignore all the positives. It must
    252         // always mean 'everything /except/ this one'. We should be able to do
    253         // multiple negatives but we don't :-(
    254         $eq = '!=';
    255         $andor = 'AND';
    256         $cat = explode('-',$cat);
    257         $cat = intval($cat[1]);
    258     } else {
    259         $eq = '=';
    260         $andor = 'OR';
     142// Update some caches.
     143update_user_cache();
     144update_category_cache();
     145
     146// Call query posts to do the work.
     147$posts = query_posts($query_string);
     148
     149// Update per post caches.
     150update_post_caches($posts);
     151
     152if (1 == count($posts)) {
     153    if ($p || $name) {
     154        $more = 1;
     155        $single = 1;
    261156    }
    262     $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) ";
    263     $cat_array = explode(' ',$cat);
    264     $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
    265     $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
    266     for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
    267             $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
    268         $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
    269     }
    270     $whichcat .= ')';
    271     if ($eq == '!=') {
    272         $cat = '-'.$cat; // Put back the knowledge that we are excluding a category.
     157    if ($s && empty($paged)) { // If they were doing a search and got one result
     158        if (!strstr($_SERVER['PHP_SELF'], 'wp-admin')) // And not in admin section
     159            header('Location: ' . get_permalink($posts[0]->ID));
    273160    }
    274161}
    275162
    276 // Category stuff for nice URIs
    277 
    278 if ('' != $category_name) {
    279     if (stristr($category_name,'/')) {
    280         $category_name = explode('/',$category_name);
    281         if ($category_name[count($category_name)-1]) {
    282         $category_name = $category_name[count($category_name)-1]; // no trailing slash
    283         } else {
    284         $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
    285         }
    286     }
    287     $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name);
    288     $tables = ", $tablepost2cat, $tablecategories";
    289     $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) ";
    290     $whichcat = " AND (category_nicename = '$category_name'";
    291     $cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'");
    292     $whichcat .= get_category_children($cat, " OR category_id = ");
    293     $whichcat .= ")";
    294 }
    295 
    296 // Author/user stuff
    297 $users = $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0");
    298 foreach ($users as $user) {
    299     $cache_userdata[$user->ID] = $user;
    300 }
    301 
    302 if ((empty($author)) || ($author == 'all') || ($author == '0')) {
    303     $whichauthor='';
    304 } else {
    305     $author = ''.urldecode($author).'';
    306     $author = addslashes_gpc($author);
    307     if (stristr($author, '-')) {
    308         $eq = '!=';
    309         $andor = 'AND';
    310         $author = explode('-', $author);
    311         $author = ''.intval($author[1]);
    312     } else {
    313         $eq = '=';
    314         $andor = 'OR';
    315     }
    316     $author_array = explode(' ', $author);
    317     $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
    318     for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
    319         $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
    320     }
    321     $whichauthor .= ')';
    322 }
    323 
    324 // Author stuff for nice URIs
    325 
    326 if ('' != $author_name) {
    327     if (stristr($author_name,'/')) {
    328         $author_name = explode('/',$author_name);
    329         if ($author_name[count($author_name)-1]) {
    330         $author_name = $author_name[count($author_name)-1];#no trailing slash
    331         } else {
    332         $author_name = $author_name[count($author_name)-2];#there was a trailling slash
    333         }
    334     }
    335     $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name));
    336     $author = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
    337     $whichauthor .= ' AND (post_author = '.intval($author).')';
    338 }
    339 
    340 $where .= $search.$whichcat.$whichauthor;
    341 
    342 if ((empty($order)) || ((strtoupper($order) != 'ASC') && (strtoupper($order) != 'DESC'))) {
    343     $order='DESC';
    344 }
    345 
    346 // Order by
    347 if (empty($orderby)) {
    348     $orderby='date '.$order;
    349 } else {
    350     // Used to filter values
    351     $allowed_keys = array('author','date','category','title');
    352     $orderby = urldecode($orderby);
    353     $orderby = addslashes_gpc($orderby);
    354     $orderby_array = explode(' ',$orderby);
    355     if (!in_array($orderby_array[0],$allowed_keys)) {
    356         $orderby_array[0] = 'date';
    357     }
    358     $orderby = $orderby_array[0].' '.$order;
    359     if (count($orderby_array)>1) {
    360         for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) {
    361             // Only allow certain values for safety
    362             if (in_array($orderby_array[$i],$allowed_keys)) {
    363                 $orderby .= ',post_'.$orderby_array[$i].' '.$order;
    364             }
    365         }
    366     }
    367 }
    368 
    369 if ((!$whichcat) && (!$m) && (!$p) && (!$w) && (!$s) && empty($poststart) && empty($postend)) {
    370     if ($what_to_show == 'posts') {
    371         $limits = ' LIMIT '.$posts_per_page;
    372     } elseif ($what_to_show == 'days' && empty($monthnum) && empty($year) && empty($day)) {
    373         $lastpostdate = get_lastpostdate();
    374         $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    375         $lastpostdate = mysql2date('U',$lastpostdate);
    376         $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($posts_per_page-1) * 86400)));
    377         $where .= " AND post_date > '$otherdate'";
    378     }
    379 }
    380 
    381 if ( !empty($postend) && ($postend > $poststart) && (!$m) && empty($monthnum) && empty($year) && empty($day) &&(!$w) && (!$whichcat) && (!$s) && (!$p)) {
    382     if ($what_to_show == 'posts' || ($what_to_show == 'paged' && (!$paged))) {
    383         $poststart = intval($poststart);
    384         $postend = intval($postend);
    385         $limposts = $postend - $poststart;
    386         $limits = ' LIMIT '.$poststart.','.$limposts;
    387     } elseif ($what_to_show == 'days') {
    388         $poststart = intval($poststart);
    389         $postend = intval($postend);
    390         $limposts = $postend - $poststart;
    391         $lastpostdate = get_lastpostdate();
    392         $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    393         $lastpostdate = mysql2date('U',$lastpostdate);
    394         $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400)));
    395         $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400)));
    396         $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'";
    397     }
    398 } else {
    399     if (($what_to_show == 'paged') && (!$p) && (!$more)) {
    400         if ($pagenow != 'post.php') {
    401             $pgstrt = '';
    402             if ($paged) {
    403                 $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    404             }
    405             $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    406         } else {
    407             if (($m) || ($p) || ($w) || ($s) || ($whichcat)) {
    408                 $limits = '';
    409             } else {
    410                 $pgstrt = '';
    411                 if ($paged) {
    412                     $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    413                 }
    414                 $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    415             }
    416         }
    417     }
    418     elseif (($m) || ($p) || ($w) || ($s) || ($whichcat) || ($author) || $monthnum || $year || $day) {
    419         $limits = '';
    420     }
    421 }
    422 
    423 if ($p == 'all') {
    424     $where = '';
    425 }
    426 
    427 $now = gmdate('Y-m-d H:i:59');
    428 
    429163if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
    430     if ((empty($poststart)) || (empty($postend)) || !($postend > $poststart)) {
    431         $where .= " AND post_date_gmt <= '$now'";
    432     }
    433 
    434     $distinct = 'DISTINCT';
    435 
    436164    if ($use_gzipcompression) {
    437165        // gzipping the output of the script
     
    439167    }
    440168}
    441 $where .= ' AND (post_status = "publish"';
    442 
    443 // Get private posts
    444 if (isset($user_ID) && ('' != intval($user_ID)))
    445     $where .= " OR post_author = $user_ID AND post_status != 'draft')";
    446 else
    447     $where .= ')';
    448 $where .= " GROUP BY $tableposts.ID";
    449 $request = " SELECT $distinct * FROM $tableposts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
    450 
    451 
    452 if ($preview) {
    453     $request = 'SELECT 1-1'; // dummy mysql query for the preview
    454     // little funky fix for IEwin, rawk on that code
    455     $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT)));
    456     if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) {
    457         $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content);
    458     }
    459 }
    460 
    461 // error_log("$request");
    462 // echo $request;
    463 $posts = $wpdb->get_results($request);
    464 
    465 // No point in doing all this work if we didn't match any posts.
    466 if ($posts) {
    467     // Get the categories for all the posts
    468     foreach ($posts as $post) {
    469         $post_id_list[] = $post->ID;
    470     }
    471     $post_id_list = implode(',', $post_id_list);
    472 
    473     $dogs = $wpdb->get_results("SELECT DISTINCT
    474         ID, category_id, cat_name, category_nicename, category_description, category_parent
    475         FROM $tablecategories, $tablepost2cat, $tableposts
    476         WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");
    477        
    478     foreach ($dogs as $catt) {
    479         $category_cache[$catt->ID][] = $catt;
    480     }
    481 
    482     // Do the same for comment numbers
    483     $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
    484         FROM $tableposts
    485         LEFT JOIN $tablecomments ON ( comment_post_ID = ID  AND comment_approved =  '1')
    486         WHERE post_status =  'publish' AND ID IN ($post_id_list)
    487         GROUP BY ID");
    488    
    489     if ($comment_counts) {
    490         foreach ($comment_counts as $comment_count) {
    491             $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
    492         }
    493     }
    494 
    495     // Get post-meta info
    496     if ( $meta_list = $wpdb->get_results("
    497             SELECT post_id,meta_key,meta_value
    498             FROM $tablepostmeta
    499             WHERE post_id IN($post_id_list)
    500             ORDER BY post_id,meta_key
    501         ", ARRAY_A) ) {
    502        
    503         // Change from flat structure to hierarchical:
    504         $post_meta_cache = array();
    505         foreach ($meta_list as $metarow) {
    506             $mpid = $metarow['post_id'];
    507             $mkey = $metarow['meta_key'];
    508             $mval = $metarow['meta_value'];
    509            
    510             // Force subkeys to be array type:
    511             if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
    512                 $post_meta_cache[$mpid] = array();
    513             if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
    514                 $post_meta_cache[$mpid]["$mkey"] = array();
    515            
    516             // Add a value to the current pid/key:
    517             $post_meta_cache[$mpid][$mkey][] = $mval;
    518         }
    519     }
    520 
    521 
    522     if (1 == count($posts)) {
    523         if ($p || $name) {
    524             $more = 1;
    525             $single = 1;
    526         }
    527         if ($s && empty($paged)) { // If they were doing a search and got one result
    528             if (!strstr($_SERVER['PHP_SELF'], 'wp-admin')) // And not in admin section
    529                 header('Location: ' . get_permalink($posts[0]->ID));
    530         }
    531     }
    532 } // End if posts.
    533169?>
Note: See TracChangeset for help on using the changeset viewer.