Fix start of week and SQL WEEK handling. props mdawaffe, fixes #10397.

git-svn-id: https://develop.svn.wordpress.org/trunk@14499 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2010-05-07 05:01:29 +00:00
parent ec9d233b8b
commit c100b80c20
3 changed files with 31 additions and 16 deletions

View File

@ -192,24 +192,15 @@ function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
$md = substr( $mysqlstring, 5, 2 ); // Mysql string day $md = substr( $mysqlstring, 5, 2 ); // Mysql string day
$day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day. $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
$weekday = date( 'w', $day ); // The day of the week from the timestamp $weekday = date( 'w', $day ); // The day of the week from the timestamp
$i = 86400; // One day
if ( !is_numeric($start_of_week) ) if ( !is_numeric($start_of_week) )
$start_of_week = get_option( 'start_of_week' ); $start_of_week = get_option( 'start_of_week' );
if ( $weekday < $start_of_week ) if ( $weekday < $start_of_week )
$weekday = 7 - $start_of_week - $weekday; $weekday += 7;
while ( $weekday > $start_of_week ) { $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
$weekday = date( 'w', $day ); $end = $start + 604799; // $start + 7 days - 1 second
if ( $weekday < $start_of_week ) return compact( 'start', 'end' );
$weekday = 7 - $start_of_week - $weekday;
$day -= 86400;
$i = 0;
}
$week['start'] = $day + 86400 - $i;
$week['end'] = $week['start'] + 604799;
return $week;
} }
/** /**
@ -4168,4 +4159,28 @@ function send_nosniff_header() {
@header( 'X-Content-Type-Options: nosniff' ); @header( 'X-Content-Type-Options: nosniff' );
} }
/**
* Returns a MySQL expression for selecting the week number based on the start_of_week option.
*
* @internal
* @since 3.0.0
* @param string $column
* @return string
*/
function _wp_mysql_week( $column ) {
switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
default :
case 0 :
return "WEEK( $column, 0 )";
case 1 :
return "WEEK( $column, 1 )";
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
}
}
?> ?>

View File

@ -965,8 +965,8 @@ function wp_get_archives($args = '') {
} }
} }
} elseif ( 'weekly' == $type ) { } elseif ( 'weekly' == $type ) {
$start_of_week = get_option('start_of_week'); $week = _wp_mysql_week( '`post_date`' );
$query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit"; $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` DESC $limit";
$key = md5($query); $key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general'); $cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) { if ( !isset( $cache[ $key ] ) ) {

View File

@ -1780,7 +1780,7 @@ class WP_Query {
} }
if ( $q['w'] ) if ( $q['w'] )
$where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'"; $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
if ( intval($q['comments_popup']) ) if ( intval($q['comments_popup']) )
$q['p'] = absint($q['comments_popup']); $q['p'] = absint($q['comments_popup']);