1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Commands to retrieve Catalog related data
16 module commands_catalog
20 # A DocCommand based on a Catalog
21 abstract class CmdCatalog
28 # A CmdSearch command using a Catalog
29 class CmdCatalogSearch
33 autoinit
(view
, catalog
, query
, limit
, page
, count
, max
)
35 redef fun init_results
do
36 if results
!= null then return new CmdSuccess
39 if not res
isa CmdSuccess then return res
41 var query
= self.query
42 if query
== null then return new ErrorNoQuery
45 var index
= view
.index
47 # lookup by name prefix
48 var matches
= index
.find_by_name_prefix
(query
).uniq
.
49 sort
(lname_sorter
, name_sorter
, kind_sorter
)
50 matches
= matches
.rerank
.sort
(vis_sorter
, score_sorter
)
53 var malus
= matches
.length
54 if catalog
.tag2proj
.has_key
(query
) then
55 for mpackage
in catalog
.tag2proj
[query
] do
56 matches
.add
new IndexMatch(mpackage
, malus
)
59 matches
= matches
.uniq
.rerank
.sort
(vis_sorter
, score_sorter
)
62 # lookup by full_name prefix
63 malus
= matches
.length
64 var full_matches
= new IndexMatches
65 for match
in index
.find_by_full_name_prefix
(query
).
66 sort
(lfname_sorter
, fname_sorter
) do
68 full_matches
.add match
70 matches
= matches
.uniq
72 # lookup by similarity
73 malus
= matches
.length
74 var sim_matches
= new IndexMatches
75 for match
in index
.find_by_similarity
(query
).sort
(score_sorter
, lname_sorter
, name_sorter
) do
76 if match
.score
> query
.length
then break
80 matches
.add_all sim_matches
81 matches
= matches
.uniq
82 results
= matches
.rerank
.sort
(vis_sorter
, score_sorter
).mentities
86 private var score_sorter
= new ScoreComparator
87 private var vis_sorter
= new VisibilityComparator
88 private var name_sorter
= new NameComparator
89 private var lname_sorter
= new NameLengthComparator
90 private var fname_sorter
= new FullNameComparator
91 private var lfname_sorter
= new FullNameLengthComparator
92 private var kind_sorter
= new MEntityComparator
95 # Retrieve the catalog metadata for a MPackage
99 # MPackage metadata retrieved
100 var metadata
: nullable MPackageMetadata = null is optional
, writable
102 redef fun init_command
do
103 if metadata
!= null then return new CmdSuccess
106 if not res
isa CmdSuccess then return res
107 var mentity
= self.mentity
.as(not null)
109 if mentity
isa MPackage then
110 metadata
= mentity
.metadata
112 return new WarningNoMetadata(mentity
)
118 # No metadata for `mentity`
119 class WarningNoMetadata
125 redef fun to_s
do return "No metadata for `{mentity.full_name}`"
128 # Retrieve the packages in the catalog
129 class CmdCatalogPackages
133 autoinit
(view
, catalog
, limit
, page
, count
, max
)
135 redef var sorter
= new CatalogScoreSorter(catalog
) is lazy
137 redef fun init_results
do
138 if results
!= null then return new CmdSuccess
141 if not res
isa CmdSuccess then return res
143 results
= catalog
.mpackages
.values
.to_a
148 # Retrieve the catalog stats
149 class CmdCatalogStats
152 # Retrieved catalog statistics
153 var stats
: nullable CatalogStats = null is optional
, writable
155 redef fun init_command
do
157 self.stats
= catalog
.catalog_stats
158 return new CmdSuccess
162 # Retrieve the catalog tags list
166 # Sorter to sort tags alphabetically
167 var tags_sorter
= new CatalogTagsSorter is optional
, writable
169 # Count of packages by tag
170 var packages_count_by_tags
: nullable ArrayMap[String, Int] = null is optional
, writable
172 redef fun init_command
do
174 var tags_to_projects
= new ArrayMap[String, Int]
175 var tags
= catalog
.tag2proj
.keys
.to_a
176 tags_sorter
.sort
(tags
)
178 if not catalog
.tag2proj
.has_key
(tag
) then continue
179 tags_to_projects
[tag
] = catalog
.tag2proj
[tag
].length
181 packages_count_by_tags
= tags_to_projects
182 return new CmdSuccess
186 # Retrieve the packages for a tag
188 super CmdCatalogPackages
190 autoinit
(view
, catalog
, tag
, limit
, page
, count
, max
)
192 # The tag to retrieve
193 var tag
: nullable String = null is optional
, writable
195 redef fun init_command
do
197 if tag
== null then return new ErrorNoTag
199 if not catalog
.tag2proj
.has_key
(tag
) then return new ErrorTagNotFound(tag
)
203 redef fun init_results
do
204 if results
!= null then return new CmdSuccess
207 if not res
isa CmdSuccess then return res
209 results
= catalog
.tag2proj
[tag
].to_a
214 # No tag name provided
218 redef fun to_s
do return "No tag name provided"
221 # No tag with this name in the catalog
222 class ErrorTagNotFound
225 # The tag that was not found
228 redef fun to_s
do return "No tag found for `{tag}`"
231 # Retrieve a person from the catalog
232 class CmdCatalogPerson
237 # You can also pass a `person_name`.
238 var person
: nullable Person = null is optional
, writable
240 # Name of the person to retrieve
242 # You can also pass a `person` instance.
243 var person_name
: nullable String = null is optional
, writable
245 # Initialize the `person` result
246 fun init_person
: CmdMessage do
247 var person
= self.person
248 if person
!= null then
249 person_name
= person
.name
250 return new CmdSuccess
253 var name
= self.person_name
254 if name
== null then return new ErrorNoPerson
255 if not catalog
.name2person
.has_key
(name
) then return new ErrorPersonNotFound(name
)
256 self.person
= catalog
.name2person
[name
]
257 return new CmdSuccess
260 redef fun init_command
do
266 # No person instance or name provided
270 redef fun to_s
do return "No person provided"
273 # No person found with this name
274 class ErrorPersonNotFound
277 # Name of the person that was not found
280 redef fun to_s
do return "No person found for `{name}`"
283 # Retrieve the packages maintained by a person
284 class CmdCatalogMaintaining
285 super CmdCatalogPerson
286 super CmdCatalogPackages
288 autoinit
(view
, catalog
, person
, person_name
, limit
, page
, count
, max
)
290 redef fun init_command
do return super
292 redef fun init_results
do
293 if results
!= null then return new CmdSuccess
295 if not res
isa CmdSuccess then return res
296 var person
= self.person
.as(not null)
298 if not catalog
.maint2proj
.has_key
(person
) then return res
299 results
= catalog
.maint2proj
[person
]
304 # Retrieve the packages contributed by a person
305 class CmdCatalogContributing
306 super CmdCatalogPerson
307 super CmdCatalogPackages
309 autoinit
(view
, catalog
, person
, person_name
, limit
, page
, count
, max
)
311 # Include maintained packages?
313 # Default is `false`.
314 var maintaining
= false is optional
, writable
316 # FIXME linearization
317 redef fun init_command
do return super
319 redef fun init_results
do
320 if results
!= null then return new CmdSuccess
323 if not res
isa CmdSuccess then return res
324 var person
= self.person
.as(not null)
326 if not catalog
.contrib2proj
.has_key
(person
) then return res
328 var maint2proj
= null
329 if catalog
.maint2proj
.has_key
(person
) then
330 maint2proj
= catalog
.maint2proj
[person
]
333 var results
= new Array[MPackage]
334 for mpackage
in catalog
.contrib2proj
[person
] do
335 if not maintaining
and maint2proj
!= null and maint2proj
.has
(mpackage
) then continue
338 self.results
= results