PageRenderTime 48ms CodeModel.GetById 14ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_atom_feed/resources/resource_atom_feed_search.erl

https://code.google.com/p/zotonic/
Erlang | 110 lines | 69 code | 25 blank | 16 comment | 0 complexity | fb25b31f1b906c1732304652686a4cd5 MD5 | raw file
  1%% @author Arjan Scherpenisse <arjan@scherpenisse.net>
  2%% @copyright 2009 Arjan Scherpenisse
  3%% Date: 2009-12-06
  4%% @doc Serve an atom feed for a search query. Query arguments are like /api/search.
  5
  6%% Licensed under the Apache License, Version 2.0 (the "License");
  7%% you may not use this file except in compliance with the License.
  8%% You may obtain a copy of the License at
  9%% 
 10%%     http://www.apache.org/licenses/LICENSE-2.0
 11%% 
 12%% Unless required by applicable law or agreed to in writing, software
 13%% distributed under the License is distributed on an "AS IS" BASIS,
 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15%% See the License for the specific language governing permissions and
 16%% limitations under the License.
 17
 18-module(resource_atom_feed_search).
 19-author("Arjan Scherpenisse <arjan@scherpenisse.net>").
 20
 21
 22
 23-export([
 24    init/1,
 25    service_available/2,
 26    allowed_methods/2,
 27    encodings_provided/2,
 28	expires/2,
 29	content_types_provided/2,
 30	charsets_provided/2,
 31	provide_content/2
 32]).
 33
 34-include_lib("webmachine_resource.hrl").
 35-include_lib("include/zotonic.hrl").
 36
 37%% Let cached versions expire in an hour.
 38-define(MAX_AGE, 3600).
 39
 40
 41init(DispatchArgs) -> 
 42    {ok, DispatchArgs}.
 43
 44service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
 45    Context  = z_context:new(ReqData, ?MODULE),
 46    Context1 = z_context:set(DispatchArgs, Context),
 47    Context2 = z_context:ensure_qs(Context1),
 48    ?WM_REPLY(true, Context2).
 49
 50    
 51allowed_methods(ReqData, Context) ->
 52    {['HEAD', 'GET'], ReqData, Context}.
 53
 54
 55charsets_provided(ReqData, Context) ->
 56    {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
 57
 58
 59encodings_provided(ReqData, Context) ->
 60    {[
 61        {"identity", fun(X) -> X end}, 
 62        {"gzip", fun(X) -> zlib:gzip(X) end}
 63    ], ReqData, Context}.
 64
 65
 66content_types_provided(ReqData, Context) ->
 67    {[{"application/atom+xml", provide_content}], ReqData, Context}.
 68
 69
 70expires(ReqData, State) ->
 71    RD1 = wrq:set_resp_header("Cache-Control", "public, max-age="++integer_to_list(?MAX_AGE), ReqData),
 72    NowSecs = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
 73    {calendar:gregorian_seconds_to_datetime(NowSecs + ?MAX_AGE), RD1, State}.
 74
 75
 76provide_content(ReqData, Context) ->
 77    Query0 = z_context:get_q_all(Context),
 78    try
 79        {FeedTitle, Query} = case proplists:get_value("feed_title", Query0) of
 80                                 undefined ->
 81                                     {"Latest updates", Query0};
 82                                 T ->
 83                                     {T, proplists:delete("feed_title", Query0)}
 84                             end,
 85        Q = search_query:parse_request_args(Query),
 86        Q1 = Q ++ [{sort, "-rsc.modified"}],
 87        F = fun() ->
 88                    S = z_search:search({'query', Q1}, Context),
 89                    Vars = [{ids, S#search_result.result},
 90                            {qtext, proplists:get_value(text, Q1)},
 91                            {feed_title, FeedTitle},
 92                            {updated, z_context:get(last_modified, Context)},
 93                            {site_url, z_context:abs_url("", Context)}
 94                           ],
 95                    {Content, _Context1} = z_template:render_to_iolist("atom_feed_search.tpl", Vars, Context),
 96                    Content
 97            end,
 98        Content = z_depcache:memo(F, {atom_feed_search, Q}, ?MAX_AGE, [], Context),
 99        {Content, ReqData, Context}
100
101    catch
102        _: {error, {unknown_query_term, E}} ->
103            ReqData1 = wrq:set_resp_body("Unknown query term: " ++ E, ReqData),
104            {{halt, 400}, ReqData1, Context};
105
106        _: {case_clause, {error, {error, error, _, _E, _}}} ->
107            ReqData1 = wrq:set_resp_body("Unknown error.", ReqData),
108            {{halt, 400}, ReqData1, Context}
109    end.
110