aboutsummaryrefslogtreecommitdiff
path: root/brep/package-version-search.cxx
blob: d7fea2d7f4b517f609cf759c9e9a311ef5c7e71d (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// file      : brep/package-version-search.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <brep/package-version-search>

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

#include <xml/serializer>

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

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

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

using namespace std;
using namespace cli;
using namespace odb::core;

namespace brep
{
  void package_version_search::
  init (scanner& s)
  {
    MODULE_DIAG;

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

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

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

    MODULE_DIAG;

    const string& name (*rq.path ().rbegin ());
    params::package_version_search pr;

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

    const char* ident ("\n      ");
    const string title ("Package " + name);
    serializer s (rs.content (), title);

    s << HTML
      <<   HEAD
      <<     TITLE << title << ~TITLE
      <<     CSS_STYLE << ident
      <<       pager_style () << ident
      <<       "a {text-decoration: none;}" << ident
      <<       "a:hover {text-decoration: underline;}" << ident
      <<       ".name {font-size: xx-large; font-weight: bold;}" << ident
      <<       ".summary {font-size: x-large; margin: 0.2em 0 0;}" << ident
      <<       ".url {font-size: small;}" << ident
      <<       ".email {font-size: small;}" << ident
      <<       ".description {margin: 0.5em 0 0;}" << ident
      <<       ".tags {margin: 0.5em 0 0;}" << ident
      <<       ".tag {padding: 0 0.3em 0 0;}" << ident
      <<       ".versions {font-size: x-large; margin: 0.5em 0 0;}" << ident
      <<       ".package_version {margin: 0.5em 0 0;}" << ident
      <<       ".version {font-size: x-large;}"
      <<     ~CSS_STYLE
      <<   ~HEAD
      <<   BODY;

    transaction t (db_->begin ());

    shared_ptr<package> p;

    try
    {
      p = db_->load<package> (name);
    }
    catch (const object_not_persistent& )
    {
      throw invalid_request (404, "Package '" + name + "' not found");
    }

    s << DIV(CLASS="name")
      <<   name
      << ~DIV
      << DIV(CLASS="summary")
      <<   p->summary
      << ~DIV;

    s << DIV(CLASS="url")
      <<   A << HREF << p->url << ~HREF << p->url << ~A
      << ~DIV
      << DIV(CLASS="email")
      <<   A << HREF << "mailto:" << p->email << ~HREF << p->email << ~A
      << ~DIV;

    if (p->description)
      s << DIV(CLASS="description")
        <<   *p->description
        << ~DIV;

    if (!p->tags.empty ())
    {
      s << DIV(CLASS="tags");

      for (const auto& t: p->tags)
        s << SPAN(CLASS="tag") << t << ~SPAN << " ";

      s << ~DIV;
    }

    // @@ Query will also include search criteria if specified.
    //
    size_t pvc (
      db_->query_value<package_version_count> (
        query<package_version_count>::id.data.package == name).count);

    s << DIV(CLASS="versions")
      <<   "Versions (" << pvc << ")"
      << ~DIV;

    if (p->package_url)
      s << DIV(CLASS="url")
        <<   A << HREF << *p->package_url << ~HREF << *p->package_url << ~A
        << ~DIV;

    if (p->package_email)
      s << DIV(CLASS="email")
        <<   A
        <<   HREF << "mailto:" << *p->package_email << ~HREF
        <<     *p->package_email
        <<   ~A
        << ~DIV;

    size_t rop (options_->results_on_page ());

    // @@ Use appropriate view when clarify which package version info to be
    //    displayed and search index structure get implemented. Query will also
    //    include search criteria if specified.
    //
    using query = query<package_version>;
    auto r (
      db_->query<package_version> ((query::id.data.package == name) +
        "ORDER BY" + query::id.data.epoch + "DESC," +
        query::id.data.canonical_upstream + "DESC," +
        query::id.revision + "DESC " +
        "OFFSET" + to_string (pr.page () * rop) +
        "LIMIT" + to_string (rop)));

    for (const auto& v: r)
    {
      static const strings priority_names (
        {"low", "medium", "high", "security"});

      assert (v.priority < priority_names.size ());

      const string& vs (v.version.string ());

      s << DIV(CLASS="package_version")
        <<   DIV(CLASS="version")
        <<     A
        <<     HREF
        <<       "/go/" << mime_url_encode (name) << "/" << vs
        <<     ~HREF
        <<       vs
        <<     ~A
        <<   ~DIV
        <<   DIV(CLASS="priority")
        <<     "Priority: " << priority_names[v.priority]
        <<   ~DIV
        <<   DIV(CLASS="licenses")
        <<     "Licenses: ";

      for (const auto& la: v.license_alternatives)
      {
        if (&la != &v.license_alternatives[0])
          s << " or ";

        for (const auto& l: la)
        {
          if (&l != &la[0])
            s << ", ";

          s << l;
        }
      }

      s <<   ~DIV
        << ~DIV;
    }

    t.commit ();

    auto u (
      [&name, &pr](size_t p)
      {
        string url (name);
        if (p > 0)
          url += "?p=" + to_string (p);

        if (!pr.query ().empty ())
          url +=
            string (p > 0 ? "&" : "?") + "q=" + mime_url_encode (pr.query ());

        return url;
      });

    s <<     pager (pr.page (), pvc, rop, options_->pages_in_pager (), u)
      <<   ~BODY
      << ~HTML;
  }
}