From 5749f1ce740e4cfd6d886e8d2fda6b8782389d33 Mon Sep 17 00:00:00 2001
From: matthiasgrimm <matthiasgrimm@users.sourceforge.net>
Date: Wed, 25 May 2005 18:16:58 +0200
Subject: [PATCH] fix for history navigation buttons

This patch adds some sanity checks for the history start parameter
'first'. Only the needed history entries will be loaded at once
now. This will reduce server load a bit

darcs-hash:20050525161658-7ef76-1ab681b4c784bbe834fae91301ee7b9cd2a2cd8b.gz
---
 feed.php         |  2 +-
 inc/common.php   | 17 +++++++++++------
 inc/html.php     | 27 +++++++++++++++++----------
 inc/template.php |  3 +--
 lang/de/lang.php |  4 ++--
 lang/en/lang.php |  4 ++--
 6 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/feed.php b/feed.php
index 3b58670d5..8b24d2d28 100644
--- a/feed.php
+++ b/feed.php
@@ -77,7 +77,7 @@
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function rssRecentChanges(&$rss,$num,$ltype){
-  $recents = getRecents($num);
+  $recents = getRecents(0,$num);
   foreach(array_keys($recents) as $id){
     $desc = cleanDesc(p_wiki_xhtml($id,'',false));
     $item = new FeedItem();
diff --git a/inc/common.php b/inc/common.php
index 775071b7f..162a1a8e4 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -476,16 +476,18 @@ function addLogEntry($date,$id,$summary=""){
 /**
  * returns an array of recently changed files using the
  * changelog
+ * first   : first entry in array returned
  * num     : return 'num' entries
- * num =  0: return count of entries set by $conf['recent']
- * num = -1: return all available entries
  *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
-function getRecents($num=0,$incdel=false){
+function getRecents($first,$num,$incdel=false){
   global $conf;
   $recent = array();
-  if(!$num) $num = $conf['recent'];
+  $names  = array();
+
+  if(!$num)
+    return $recent;
 
   if(!@is_readable($conf['changelog'])){
     msg($conf['changelog'].' is not readable',-1);
@@ -500,17 +502,20 @@ function getRecents($num=0,$incdel=false){
     if(empty($line)) continue;   //skip empty lines
     $info = split("\t",$line);   //split into parts
     //add id if not in yet and file still exists and is allowed to read
-    if(!$recent[$info[2]] && 
+    if(!$names[$info[2]] && 
        (@file_exists(wikiFN($info[2])) || $incdel) &&
        (auth_quickaclcheck($info[2]) >= AUTH_READ)
       ){
+      $names[$info[2]] = 1;
+      if(--$first >= 0) continue;  /* skip "first" entries */
+      
       $recent[$info[2]]['date'] = $info[0];
       $recent[$info[2]]['ip']   = $info[1];
       $recent[$info[2]]['user'] = $info[3];
       $recent[$info[2]]['sum']  = $info[4];
       $recent[$info[2]]['del']  = !@file_exists(wikiFN($info[2]));
     }
-    if($num != -1 && count($recent) >= $num){
+    if(count($recent) >= $num){
       break; //finish if enough items found
     }
   }
diff --git a/inc/html.php b/inc/html.php
index c9b1539f1..b08f313e1 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -446,22 +446,28 @@ function html_revisions(){
  * display recent changes
  *
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
  */
 function html_recent($first=0){
   global $conf;
   global $lang;
-  $recents = getRecents(-1,true);
-  
-  if($first >= count($recents)) $first = 0;
-  $last = $first + $conf['recent'];
-  if ($last > count($recents))
-    $last = count($recents);
+
+  /* we need to get one additionally log entry to be able to
+   * decide if this is the last page or is there another one.
+   * This is the cheapest solution to get this information.
+   */
+  $recents = getRecents($first,$conf['recent'] + 1,true);
+  if(count($recents) == 0 && $first != 0){
+    $first=0;
+    $recents = getRecents(0,$conf['recent'] + 1,true);
+  }
+  $cnt = count($recents) <= $conf['recent'] ? count($recents) : $conf['recent']; 
 
   print p_locale_xhtml('recent');
   print '<ul>';
   
   $keys = array_keys($recents);
-  for ($n=$first; $n < $last; $n++){
+  for ($n=0; $n < $cnt; $n++){
     $id = $keys[$n];
     $date = date($conf['dformat'],$recents[$id]['date']);
     print '<li>';
@@ -492,16 +498,17 @@ function html_recent($first=0){
   print '</ul>';
 
   print '<div class="pagenav">';
+  $last = $first + $conf['recent'];
   if ($first > 0) {
     $first -= $conf['recent']; 
     if ($first < 0) $first = 0;
     print '<div class="pagenav-prev">';
-    print html_btn('prevpage','',"p",array('do' => 'recent', 'first' => $first));
+    print html_btn('newer','',"p",array('do' => 'recent', 'first' => $first));
     print '</div>';
   }
-  if ($last < count($recents)) {
+  if ($conf['recent'] < count($recents)) {
     print '<div class="pagenav-next">';
-    print html_btn('nextpage','',"n",array('do' => 'recent', 'first' => $last));
+    print html_btn('older','',"n",array('do' => 'recent', 'first' => $last));
     print '</div>';
   }
   print '</div>';
diff --git a/inc/template.php b/inc/template.php
index a5e656443..ea8b99b6c 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -75,8 +75,7 @@ function tpl_content(){
       html_diff();
       break;
     case 'recent':
-      $first = $_REQUEST['first'];
-      if(empty($first)) $first=0;
+      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
       html_recent($first);
       break;
     case 'index':
diff --git a/lang/de/lang.php b/lang/de/lang.php
index eaf4286e8..145be6bcc 100644
--- a/lang/de/lang.php
+++ b/lang/de/lang.php
@@ -18,8 +18,8 @@ $lang['btn_search'] = 'Suche';
 $lang['btn_save']   = 'Speichern';
 $lang['btn_preview']= 'Vorschau';
 $lang['btn_top']    = 'Nach oben';
-$lang['btn_prevpage']= '<< zurück';
-$lang['btn_nextpage']= 'weiter >>';
+$lang['btn_newer']  = '<< jüngere Änderungen';
+$lang['btn_older']  = 'ältere Änderungen >>';
 $lang['btn_revs']   = 'Ältere Versionen';
 $lang['btn_recent'] = 'Letzte Änderungen';
 $lang['btn_upload'] = 'Hochladen';
diff --git a/lang/en/lang.php b/lang/en/lang.php
index d98256736..d639276d9 100644
--- a/lang/en/lang.php
+++ b/lang/en/lang.php
@@ -17,8 +17,8 @@ $lang['btn_search'] = 'Search';
 $lang['btn_save']   = 'Save';
 $lang['btn_preview']= 'Preview';
 $lang['btn_top']    = 'Back to top';
-$lang['btn_prevpage']= '<< previous page';
-$lang['btn_nextpage']= 'next page >>';
+$lang['btn_newer']  = '<< more recent';
+$lang['btn_older']  = 'less recent >>';
 $lang['btn_revs']   = 'Old revisions';
 $lang['btn_recent'] = 'Recent changes';
 $lang['btn_upload'] = 'Upload';
-- 
GitLab