Changeset 43f521291669f483d361949fa6c886c0fbd94daf

Show
Ignore:
Timestamp:
08/26/10 20:21:08 (18 months ago)
Author:
Antti-Juhani Kaijanaho <antti-juhani@…>
Children:
6c86e7ccc32f1c99d204262844a52b1b06359145
Parents:
7ccc366e52cc1e51e123b39fccaf56267361ca58
git-committer:
Antti-Juhani Kaijanaho <antti-juhani@…> (08/26/10 20:21:08)
Message:

Add support for paginating thread and article lists.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

Files:
8 modified

Legend:

Unmodified
Added
Removed
  • http/article.cc

    r751fac6 r43f5212  
    3636#include "../tlate/tlate.hh" 
    3737#include "../uri.hh" 
     38#include "../util.hh" 
    3839 
    3940#include <boost/shared_ptr.hpp> 
     
    103104                if (!is_single)  
    104105                { 
    105                         tlate::value::ptr tv(new tlate::thread_value(tn, u)); 
     106                        std::string f = req->get_form_field("s"); 
     107                        size_t start = util::is_number(f) 
     108                                ? util::parse_nonnegative_integer<size_t>(f) 
     109                                : 0; 
     110                        f = req->get_form_field("n"); 
     111                        size_t length = util::is_number(f) 
     112                                ? util::parse_nonnegative_integer<size_t>(f) 
     113                                : 10; 
     114 
     115                        tlate::thread_value::ptr tv 
     116                                (new tlate::thread_value(tn, u)); 
     117                        tv->slice_window(start, length); 
    106118                        am->insert("thread", tv); 
     119 
     120                        tlate::data_model::ptr pg(new tlate::data_model); 
     121                        pg->insert("first", start); 
     122                        pg->insert("last", start + tv->size()); 
     123                        pg->insert("size", tv->size()); 
     124                        pg->insert("max_size", length); 
     125 
     126                        if (length > tv->size()) 
     127                        { 
     128                                ::uri u = req->get_uri(); 
     129                                u.replace_query_param("s", start + length); 
     130                                u.replace_query_param("n", length); 
     131                                pg->insert("next", u.to_string()); 
     132                        } 
     133                         
     134                        if (start > 0) 
     135                        { 
     136                                ::uri u = req->get_uri(); 
     137                                u.replace_query_param("s", 
     138                                                      start > length 
     139                                                      ? start - length 
     140                                                      : 0); 
     141                                u.replace_query_param("n", length); 
     142                                pg->insert("prev", u.to_string()); 
     143                        } 
     144                        am->insert("page", pg); 
    107145                } 
    108146 
  • http/threads.cc

    r751fac6 r43f5212  
    9494                else 
    9595                { 
     96                        std::string f = req->get_form_field("s"); 
     97                        size_t start = util::is_number(f) 
     98                                ? util::parse_nonnegative_integer<size_t>(f) 
     99                                : 0; 
     100                        f = req->get_form_field("n"); 
     101                        size_t length = util::is_number(f) 
     102                                ? util::parse_nonnegative_integer<size_t>(f) 
     103                                : 10; 
     104 
    96105                        tlate::list_value::ptr tl(new tlate::list_value); 
    97106                        std::list<db::thread_node::ptr> thrs  
     
    100109                        thrs.sort(db::thread_node::cmp_latest_date_rev); 
    101110 
     111                        size_t cur = 0; 
     112                        bool more = false; 
    102113                        for (std::list<db::thread_node::ptr>::const_iterator 
    103114                                     it = thrs.begin(); 
    104115                             it != thrs.end(); it++) 
    105116                        { 
    106                                 tl->push_back(tlate::msg_value::mk(*it, u)); 
     117                                cur++; 
     118                                if (cur >= start + length) 
     119                                { 
     120                                        more = true; 
     121                                        break; 
     122                                } 
     123                                if (cur > start) 
     124                                        tl->push_back 
     125                                                (tlate::msg_value::mk(*it, u)); 
    107126                        } 
    108127                        am->insert("threads", tl); 
     128 
     129                        tlate::data_model::ptr pg(new tlate::data_model); 
     130                        pg->insert("first", start); 
     131                        pg->insert("last", cur - 1); 
     132                        pg->insert("size", cur - start); 
     133                        pg->insert("max_size", length); 
     134 
     135                        if (more) 
     136                        { 
     137                                ::uri u = req->get_uri(); 
     138                                u.replace_query_param("s", start + length); 
     139                                u.replace_query_param("n", length); 
     140                                pg->insert("next", u.to_string()); 
     141                        } 
     142                         
     143                        if (start > 0) 
     144                        { 
     145                                ::uri u = req->get_uri(); 
     146                                u.replace_query_param("s", 
     147                                                      start > length 
     148                                                      ? start - length 
     149                                                      : 0); 
     150                                u.replace_query_param("n", length); 
     151                                pg->insert("prev", u.to_string()); 
     152                        } 
     153                        am->insert("page", pg); 
    109154                } 
    110155        } 
  • tlate/data_model.hh

    r868b365 r43f5212  
    11/*  This file is part of Alue, the multiprotocol Internet discussion daemon 
    22 
    3     Copyright © 2009 Antti-Juhani Kaijanaho 
     3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho 
    44 
    55    Alue is free software: you can redistribute it and/or modify it 
     
    4444                        insert(var, new string_value(val)); 
    4545                } 
     46                void insert(std::string var, int val, unsigned md = 1) { 
     47                        insert(var, new string_value(val, md)); 
     48                } 
    4649                void insert(std::string var, value::const_ptr val) { 
    4750                        myassert(var.find('.') == std::string::npos); 
  • tlate/thread_value.cc

    r299b297 r43f5212  
    2020#include "thread_value.hh" 
    2121 
     22#include <list> 
     23 
    2224namespace tlate 
    2325{ 
     
    2628                : u(u) 
    2729        { 
     30                typedef std::list<db::thread_node::ptr> tlnc; 
     31 
    2832                tnl.push_back(tn); 
    2933                for (tl::const_iterator it = tnl.begin(); it != tnl.end(); it++) 
     
    3640                        } 
    3741                } 
    38                 tnl.sort(db::thread_node::cmp_date); 
     42                std::stable_sort(tnl.begin(), tnl.end(), 
     43                                 db::thread_node::cmp_date); 
     44        } 
     45        void thread_value::slice_window(size_t start, size_t length) 
     46        { 
     47                if (start > tnl.size()) 
     48                { 
     49                        tnl.clear(); 
     50                        return; 
     51                } 
     52                if (start > 0) tnl.erase(tnl.begin(), tnl.begin()+start); 
     53                if (length > tnl.size()) 
     54                        tnl.erase(tnl.begin()+length, tnl.end()); 
    3955        } 
    4056} 
  • tlate/thread_value.hh

    r868b365 r43f5212  
    11/*  This file is part of Alue, the multiprotocol Internet discussion daemon 
    22 
    3     Copyright © 2009 Antti-Juhani Kaijanaho 
     3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho 
    44 
    55    Alue is free software: you can redistribute it and/or modify it 
     
    2626#include "../db/threaded.hh" 
    2727 
    28 #include <list> 
     28#include <vector> 
    2929 
    3030namespace db { class user; } 
     
    3434        class thread_value : public sequential_value 
    3535        { 
    36                 typedef std::list<db::thread_node::const_ptr> tl; 
    37                 typedef std::list<db::thread_node::ptr> tlnc; 
     36                typedef std::vector<db::thread_node::const_ptr> tl; 
    3837                tl tnl; 
    3938                boost::shared_ptr<const db::user> u; 
     
    6968 
    7069        public: 
     70                typedef boost::shared_ptr<thread_value> ptr; 
     71                typedef boost::shared_ptr<const thread_value> const_ptr; 
     72 
    7173                const_iterator begin() const { return new vit(tnl.begin(), u); } 
    7274                const_iterator end() const   { return new vit(tnl.end(),   u); } 
     
    7476                thread_value(db::thread_node::const_ptr tn, 
    7577                             boost::shared_ptr<const db::user> u); 
     78                void slice_window(size_t start, size_t length); 
     79                size_t size() const { return tnl.size(); } 
    7680        }; 
    7781} 
  • uri.cc

    r868b365 r43f5212  
    11/*  This file is part of Alue, the multiprotocol Internet discussion daemon 
    22 
    3     Copyright © 2009 Antti-Juhani Kaijanaho 
     3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho 
    44 
    55    Alue is free software: you can redistribute it and/or modify it 
     
    118118} 
    119119 
     120void uri::replace_query_param(std::string name, int val) 
     121{ 
     122        std::ostringstream os; 
     123        os << val; 
     124        replace_query_param(name, os.str()); 
     125} 
     126 
    120127void uri::remove_query_param(std::string name) 
    121128{ 
     
    123130        { 
    124131                size_t nix = query.find("&", ix+1); 
    125                 if (query.substr(ix, name.length()) == name) 
     132                if (query.substr(ix, name.length()+1) == name + "=") 
    126133                        query.erase(ix, nix - ix); 
    127134                else 
  • uri.hh

    r868b365 r43f5212  
    11/*  This file is part of Alue, the multiprotocol Internet discussion daemon 
    22 
    3     Copyright © 2009 Antti-Juhani Kaijanaho 
     3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho 
    44 
    55    Alue is free software: you can redistribute it and/or modify it 
     
    5858        std::string get_query_param(std::string name) const; 
    5959        void replace_query_param(std::string name, std::string new_value); 
     60        void replace_query_param(std::string name, int new_value); 
    6061        void remove_query_param(std::string name); 
    6162 
  • util.cc

    r12f5162 r43f5212  
    3838        bool is_number(std::string s) 
    3939        { 
     40                if (s.empty()) return false; 
    4041                for (size_t i = 0; i < s.length(); i++) { 
    4142                        if (!is_digit(s[i])) return false;