aboutsummaryrefslogtreecommitdiff
path: root/brep/package-search.cxx
blob: b0352f331e65564bd6fde9df81d0b630fbd7661e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// file      : brep/package-search.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <brep/package-search>

#include <string>
#include <memory>  // make_shared(), shared_ptr
#include <cstddef> // size_t

#include <xml/serializer>

#include <odb/session.hxx>
#include <odb/database.hxx>
#include <odb/transaction.hxx>

#include <web/xhtml>
#include <web/module>
#include <web/mime-url-encoding>

#include <brep/page>
#include <brep/options>
#include <brep/package>
#include <brep/package-odb>
#include <brep/shared-database>

using namespace std;
using namespace odb::core;

namespace brep
{
  using namespace cli;

  void package_search::
  init (scanner& s)
  {
    MODULE_DIAG;

    options_ = make_shared<options::package_search> (
      s, unknown_mode::fail, unknown_mode::fail);

    db_ = shared_database (options_->db_host (), options_->db_port ());
  }

  template <typename T>
  static inline query<T>
  search_param (const string& q)
  {
    using query = query<T>;
    return "(" +
      (q.empty ()
       ? query ("NULL")
       : "plainto_tsquery (" + query::_val (q) + ")") +
      ")";
  }

  void package_search::
  handle (request& rq, response& rs)
  {
    using namespace xml;
    using namespace web;
    using namespace web::xhtml;

    MODULE_DIAG;

    // The module options object is not changed after being created once per
    // server process.
    //
    static const size_t rp (options_->results_on_page ());
    static const dir_path& rt (options_->root ());

    params::package_search pr;

    try
    {
      param_scanner s (rq.parameters ());
      pr = params::package_search (s, unknown_mode::fail, unknown_mode::fail);
    }
    catch (const unknown_argument& e)
    {
      throw invalid_request (400, e.what ());
    }

    const string& sq (pr.query ()); // Search query.
    string qp (sq.empty () ? "" : "q=" + mime_url_encode (sq));
    size_t pg (pr.page ());

    serializer s (rs.content (), "Packages");

    const string& title (
      sq.empty () ? s.output_name () : s.output_name () + " " + sq);

    static const path sp ("package-search.css");

    s << HTML
      <<   HEAD
      <<     TITLE << title << ~TITLE
      <<     CSS_LINKS (sp, rt)
      <<   ~HEAD
      <<   BODY
      <<     DIV_HEADER (rt)
      <<     DIV(ID="content");

    session sn;
    transaction t (db_->begin ());

    auto pc (
      db_->query_value<latest_package_count> (
        search_param<latest_package_count> (sq)));

    auto r (
      db_->query<latest_package_search_rank> (
        search_param<latest_package_search_rank> (sq) +
        "ORDER BY rank DESC, name" +
        "OFFSET" + to_string (pg * rp) +
        "LIMIT" + to_string (rp)));

    s << FORM_SEARCH (sq)
      << DIV_COUNTER (pc, "Package", "Packages");

    // Enclose the subsequent tables to be able to use nth-child CSS selector.
    //
    s << DIV;
    for (const auto& pr: r)
    {
      shared_ptr<package> p (db_->load<package> (pr.id));

      s << TABLE(CLASS="proplist package")
        <<   TBODY
        <<     TR_NAME (p->id.name, qp, rt)
        <<     TR_SUMMARY (p->summary)
        <<     TR_LICENSE (p->license_alternatives)
        <<     TR_TAGS (p->tags, rt)
        <<     TR_DEPENDS (p->dependencies, rt)
        <<     TR_REQUIRES (p->requirements)
        <<   ~TBODY
        << ~TABLE;
    }
    s << ~DIV;

    t.commit ();

    static const size_t pp (options_->pages_in_pager ());
    const string& u (qp.empty () ? rt.string () : (rt.string () + "?" + qp));

    s <<       DIV_PAGER (pg, pc, rp, pp, u)
      <<     ~DIV
      <<   ~BODY
      << ~HTML;
  }
}