Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix yourls_api_stats() with filter==rand #2519

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,11 @@ function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {

switch( $filter ) {
case 'bottom':
$sort_by = 'clicks';
$sort_by = '`clicks`';
$sort_order = 'asc';
break;
case 'last':
$sort_by = 'timestamp';
$sort_by = '`timestamp`';
$sort_order = 'desc';
break;
case 'rand':
Expand All @@ -585,7 +585,7 @@ function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
break;
case 'top':
default:
$sort_by = 'clicks';
$sort_by = '`clicks`';
$sort_order = 'desc';
break;
}
Expand All @@ -596,7 +596,7 @@ function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
if ( $limit > 0 ) {

$table_url = YOURLS_DB_TABLE_URL;
$results = $ydb->fetchObjects( "SELECT * FROM `$table_url` WHERE 1=1 ORDER BY `$sort_by` $sort_order LIMIT $start, $limit;" );
$results = $ydb->fetchObjects( "SELECT * FROM `$table_url` WHERE 1=1 ORDER BY $sort_by $sort_order LIMIT $start, $limit;" );
Copy link
Member

@LeoColomb LeoColomb Mar 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the idea behind WHERE 1=1?

(introduced here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense if this query also had a $where_filter (or whatever) value interpolated into it that might or might not contain additional conditions. Having it anyway is harmless (essentially zero effect on query execution time), but doesn't appear to serve any purpose in this particular case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has always been there (see in 2009...).

I guess it's either because at this time I couldn't dare writing an SQL clause without a WHERE, or because at this time there were supposed to be potential troubles omitting this WHERE, either with MySQL or other DB engines.

I think it's also for readability to clearly specify this query DOESNT have a WHERE clause


$return = array();
$i = 1;
Expand Down Expand Up @@ -1063,7 +1063,7 @@ function yourls_upgrade_is_needed() {
// Check if YOURLS_VERSION exist && match value stored in YOURLS_DB_TABLE_OPTIONS, update DB if required
if( $currentver < YOURLS_VERSION )
yourls_update_option( 'version', YOURLS_VERSION );

return false;
}

Expand Down