Merge: src/doc/commands: clean commands hierarchy
[nit.git] / src / doc / commands / commands_http.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Initialize commands from HTTP requests
16 #
17 # FIXME: this module is pretty tied up to the nitwed routes.
18 # To be more generic, param names should be extracted as variables.
19 module commands_http
20
21 import commands_catalog
22 import commands_graph
23 import commands_ini
24 import commands_main
25 import commands_usage
26
27 import nitcorn::vararg_routes
28
29 redef class DocCommand
30 # Init the command from an HTTPRequest
31 fun http_init(req: HttpRequest): CmdMessage do
32 var filter = cmd_filter
33 var opt_vis = req.visibility_arg("min-visibility")
34 if opt_vis != null then filter.min_visibility = opt_vis
35 var opt_fictive = req.bool_arg("no-fictive")
36 if opt_fictive != null then filter.accept_fictive = not opt_fictive
37 var opt_test = req.bool_arg("no-test")
38 if opt_test != null then filter.accept_test = not opt_test
39 var opt_redef = req.bool_arg("no-redef")
40 if opt_redef != null then filter.accept_redef = not opt_redef
41 var opt_extern = req.bool_arg("no-extern")
42 if opt_extern != null then filter.accept_extern = not opt_extern
43 var opt_example = req.bool_arg("no-example")
44 if opt_example != null then filter.accept_example = not opt_example
45 var opt_attr = req.bool_arg("no-attribute")
46 if opt_attr != null then filter.accept_attribute = not opt_attr
47 var opt_doc = req.bool_arg("no-empty-doc")
48 if opt_doc != null then filter.accept_empty_doc = not opt_doc
49 var opt_inh = req.mentity_arg(model, "inherit")
50 if opt_inh != null then filter.accept_inherited = opt_inh
51 var opt_match = req.string_arg("match")
52 if opt_match != null then filter.accept_full_name = opt_match
53 self.filter = filter
54 return init_command
55 end
56 end
57
58 redef class CmdEntity
59 redef fun http_init(req) do
60 var name = req.param("id")
61 if name != null then name = name.from_percent_encoding
62 self.mentity_name = name
63
64 return super
65 end
66 end
67
68 redef class CmdList
69 redef fun http_init(req) do
70 var opt_limit = req.int_arg("l")
71 if opt_limit != null then limit = opt_limit
72 var opt_page = req.int_arg("p")
73 if opt_page != null then page = opt_page
74 return super
75 end
76 end
77
78 # Error Handling
79
80 # Message handling
81
82 redef class CmdMessage
83 # HTTP code to return for this message
84 var http_status_code = 200
85 end
86
87 redef class CmdError
88 redef var http_status_code = 400
89 end
90
91 redef class CmdWarning
92 redef var http_status_code = 404
93 end
94
95 redef class ErrorMEntityNoName
96 redef var http_status_code = 400
97 end
98
99 redef class ErrorMEntityNotFound
100 redef var http_status_code = 404
101 end
102
103 redef class ErrorMEntityConflict
104 redef var http_status_code = 300
105 end
106
107 # CmdModel
108
109 redef class CmdComment
110 redef fun http_init(req) do
111 var opt_full_doc = req.bool_arg("full_doc")
112 if opt_full_doc != null then full_doc = opt_full_doc
113 var opt_fallback = req.bool_arg("fallback")
114 if opt_fallback != null then fallback = opt_fallback
115 var opt_format = req.string_arg("format")
116 if opt_format != null then format = opt_format
117 return super
118 end
119 end
120
121 redef class CmdEntityLink
122 redef fun http_init(req) do
123 var opt_text = req.string_arg("text")
124 if opt_text != null then text = opt_text
125 var opt_title = req.string_arg("title")
126 if opt_title != null then title = opt_title
127 return super
128 end
129 end
130
131 redef class CmdAncestors
132 redef fun http_init(req) do
133 var opt_parents = req.bool_arg("parents")
134 if opt_parents != null then parents = opt_parents
135 return super
136 end
137 end
138
139 redef class CmdDescendants
140 redef fun http_init(req) do
141 var opt_children = req.bool_arg("children")
142 if opt_children != null then children = opt_children
143 return super
144 end
145 end
146
147 redef class CmdEntityList
148 # FIXME avoid linearization conflict
149 redef fun http_init(req) do return super
150 end
151
152 redef class CmdSearch
153 redef fun http_init(req) do
154 query = req.string_arg("q")
155 return super
156 end
157 end
158
159 redef class CmdModelEntities
160 redef fun http_init(req) do
161 var opt_kind = req.string_arg("kind")
162 if opt_kind != null then kind = opt_kind
163 return super
164 end
165 end
166
167 redef class CmdCode
168 redef fun http_init(req) do
169 var opt_format = req.string_arg("format")
170 if opt_format != null then format = opt_format
171 return super
172 end
173 end
174
175 redef class CmdEntityCode
176 # FIXME avoid linearization conflict
177 redef fun http_init(req) do
178 var name = req.param("id")
179 if name != null then name = name.from_percent_encoding
180 mentity_name = name
181
182 var opt_format = req.string_arg("format")
183 if opt_format != null then format = opt_format
184 return init_command
185 end
186 end
187
188 # CmdGraph
189
190 redef class CmdGraph
191 redef fun http_init(req) do
192 var opt_format = req.string_arg("format")
193 if opt_format != null then format = opt_format
194 return super
195 end
196 end
197
198 redef class CmdInheritanceGraph
199 redef fun http_init(req) do
200 var opt_pdepth = req.int_arg("pdepth")
201 if opt_pdepth != null then pdepth = opt_pdepth
202 var opt_cdepth = req.int_arg("cdepth")
203 if opt_cdepth != null then cdepth = opt_cdepth
204 return super
205 end
206 end
207
208 # CmdCatalog
209
210 redef class CmdCatalogTag
211 redef fun http_init(req) do
212 var tag = req.param("tid")
213 if tag != null then tag = tag.from_percent_encoding
214 self.tag = tag
215 return super
216 end
217 end
218
219 redef class CmdCatalogPerson
220 redef fun http_init(req) do
221 var name = req.param("pid")
222 if name != null then name = name.from_percent_encoding
223 self.person_name = name
224 return super
225 end
226 end
227
228 # Util
229
230 redef class HttpRequest
231
232 # Map String visiblity name to MVisibility object
233 var allowed_visibility: HashMap[String, MVisibility] is lazy do
234 var res = new HashMap[String, MVisibility]
235 res["public"] = public_visibility
236 res["protected"] = protected_visibility
237 res["private"] = private_visibility
238 return res
239 end
240
241 # Get arg as a MVisibility
242 #
243 # Return `null` if no option with that `key` or if the value is not in
244 # `allowed_visibility`.
245 fun visibility_arg(key: String): nullable MVisibility do
246 var value = string_arg(key)
247 if value == null then return null
248 if not allowed_visibility.keys.has(key) then return null
249 return allowed_visibility[value]
250 end
251
252 # Get arg as a MEntity
253 #
254 # Lookup first by `MEntity::full_name` then by `MEntity::name`.
255 # Return `null` if the mentity name does not exist or return a conflict.
256 private fun mentity_arg(model: Model, key: String): nullable MEntity do
257 var value = string_arg(key)
258 if value == null or value.is_empty then return null
259
260 var mentity = model.mentity_by_full_name(value)
261 if mentity != null then return mentity
262
263 var mentities = model.mentities_by_name(value)
264 if mentities.is_empty or mentities.length > 1 then return null
265 return mentities.first
266 end
267 end