Fix SQL error in plugin_history.

Since merging #665 `serendipity_plugin_history`
breaks with a SQL error message on every archive
page but the first one; see #693 for reason
and context.

Unset `$serendipity['GET']['page']` before
calling `serendipity_fetchEntries()` (and
reset afterwards) to fix that. That's the
correct way, I think, as
`serendipity_fetchEntries()` is not called
in page context here.

Add a note to serendipity_fetchEntries()
about the problem with page context.

Also don't fallback to last page if
$totalPages < 1 in serendipity_fetchEntries()
That should at least avoid SQL errors.

Signed-off-by: Thomas Hochstein <thh@inter.net>
This commit is contained in:
Thomas Hochstein 2020-04-11 10:52:50 +02:00
parent c66451e203
commit ef1279fbec
3 changed files with 25 additions and 1 deletions

View File

@ -1,6 +1,10 @@
Version 2.3.5-beta1 ()
------------------------------------------------------------------------
* Fix: SQL error in serendipity_plugin_history present since we
"don't allow requesting an archive page that doesn't exist"
(2.3.3).
* Fix: Entry title in backend list of entries was double escaped.
* Fix: Don't drop upgraded_version from local plugin cache.

View File

@ -404,13 +404,26 @@ function &serendipity_fetchEntries($range = null, $full = true, $limit = '', $fe
if (!empty($limit)) {
if (isset($serendipity['GET']['page']) && ($serendipity['GET']['page'] > 1 || serendipity_db_bool($serendipity['archiveSortStable'])) && !strstr($limit, ',')) {
// serendipity_fetchEntries() is mostly called in page context to
// deliver a list of entries to display on archive, search result
// or other pages, where $limit means the number of entries to
// display on each page. This code depends on that notion,
// catches request for non-existing pages and applies
// pagination.
// If you don't work in a page context, e.g. you just want to fetch
// a list of entries to display in a sidebar, you should think
// about saving and unsetting $serendipity['GET']['page'] before
// calling serendipity_fetchEntries() and reset it to the saved
// value afterwards.
// See https://github.com/s9y/Serendipity/issues/693 for context.
$totalEntries = serendipity_getTotalEntries();
$totalPages = ceil($totalEntries / $limit);
// Do not allow requesting a page that doesn't exist
// and do a fallback to the highest page number available
if ($serendipity['GET']['page'] > $totalPages) {
if ($totalPages > 0 && $serendipity['GET']['page'] > $totalPages) {
$serendipity['GET']['page'] = $totalPages;
}

View File

@ -174,12 +174,19 @@ class serendipity_plugin_history extends serendipity_plugin
}
$oldLim = $serendipity['fetchLimit'];
if (isset($serendipity['GET']['page'])) {
$oldPage = $serendipity['GET']['page'];
unset($serendipity['GET']['page']);
}
$nowts = serendipity_serverOffsetHour();
$maxts = mktime(23, 59, 59, date('m', $nowts), date('d', $nowts), date('Y', $nowts));
$mints = mktime(0, 0, 0, date('m', $nowts), date('d', $nowts), date('Y', $nowts));
$e = serendipity_fetchEntries(array(($mints-$max_age*86400),
($maxts-$min_age*86400)), $full, $max_entries);
$serendipity['fetchLimit'] = $oldLim;
if (isset($oldPage)) {
$serendipity['GET']['page'] = $oldPage;
}
echo (empty($intro)) ? '' : '<div class="serendipity_history_intro">' . $intro . '</div>' . "\n";
if (!is_array($e)) {