Fork de wikipp, le moteur de wiki en c++, basé sur cppcms. Le fork ajoute la langue française
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "index.h"
  2. #include "index_content.h"
  3. #include "wiki.h"
  4. #include <cppcms/cache_interface.h>
  5. #include <cppcms/url_dispatcher.h>
  6. namespace apps {
  7. index::index(wiki &w):
  8. master(w)
  9. {
  10. wi.dispatcher().assign("^/index/?",&index::display_index,this);
  11. wi.dispatcher().assign("^/changes(/?|/(\\d+))$",&index::changes,this,2);
  12. }
  13. std::string index::index_url()
  14. {
  15. return wi.root()+"/index/";
  16. }
  17. std::string index::changes_url(int p)
  18. {
  19. if(p==0)
  20. return wi.root()+"/changes/";
  21. return wi.root()+(booster::locale::format("/changes/{1}") % p).str();
  22. }
  23. void index::changes(std::string page_no)
  24. {
  25. std::string key=locale_name+"_changes_"+page_no;
  26. if(cache().fetch_page(key))
  27. return;
  28. int p;
  29. const unsigned window=30;
  30. if(page_no.empty())
  31. p=0;
  32. else
  33. p=atoi(page_no.c_str());
  34. cppdb::result rs;
  35. cppdb::session sql(conn);
  36. rs = sql<<
  37. "SELECT history.title,history.version,history.created,"
  38. " history.author,pages.lang,pages.slug "
  39. "FROM history "
  40. "JOIN pages ON history.id=pages.id "
  41. "ORDER BY created DESC "
  42. "LIMIT ? "
  43. "OFFSET ?"
  44. << window << p*window;
  45. content::recent_changes c;
  46. c.content;
  47. int n;
  48. for(n=0;rs.next();n++) {
  49. c.content.push_back(content::recent_changes::element());
  50. content::recent_changes::element &d=c.content.back();
  51. std::string lang,slug;
  52. std::tm created;
  53. rs>>d.title>>d.version>>created>>d.author>>lang>>slug;
  54. d.created = mktime(&created);
  55. d.url=wi.page.page_version_url(d.version,lang,slug);
  56. if(d.version>1)
  57. d.diff_url=wi.page.diff_url(d.version-1,d.version,lang,slug);
  58. }
  59. if(c.content.size()==window)
  60. c.next=changes_url(p+1);
  61. ini(c);
  62. render("recent_changes",c);
  63. cache().store_page(key,60);
  64. // Cache changes for at most 30 sec
  65. // Generally -- prevent cache drop with frequent updates
  66. }
  67. void index::display_index()
  68. {
  69. std::string key=locale_name+"_toc_index";
  70. if(cache().fetch_page(key))
  71. return;
  72. content::toc c;
  73. ini(c);
  74. cppdb::result r;
  75. cppdb::session sql(conn);
  76. r=sql<< "SELECT slug,title FROM pages "
  77. "WHERE lang=? "
  78. "ORDER BY title ASC" << locale_name;
  79. std::string letter="";
  80. typedef std::multimap<std::string,std::string,std::locale> mapping_type;
  81. mapping_type mapping(context().locale());
  82. while(r.next()) {
  83. std::string slug,t;
  84. r >> slug >> t;
  85. mapping.insert(std::pair<std::string,std::string>(t,slug));
  86. }
  87. unsigned items=mapping.size();
  88. unsigned items_left=items/3;
  89. unsigned items_mid=items*2/3;
  90. mapping_type::iterator p=mapping.begin();
  91. int rows_no = (mapping.size() +2)/3;
  92. c.table.resize(rows_no,std::vector<content::toc::element>(3));
  93. for(unsigned i=0;p!=mapping.end();i++,++p) {
  94. int col = i / rows_no;
  95. int row = i % rows_no;
  96. content::toc::element &e = c.table.at(row).at(col);
  97. e.title = p->first;
  98. e.url=wi.page.page_url(locale_name,p->second);
  99. }
  100. render("toc",c);
  101. cache().store_page(key,30);
  102. // Cache TOC for at most 30 seconds
  103. }
  104. }