| 1 |
from trac.core import * |
|---|
| 2 |
from tracrpc.api import IXMLRPCHandler |
|---|
| 3 |
from trac.Search import ISearchSource |
|---|
| 4 |
|
|---|
| 5 |
try: |
|---|
| 6 |
a = set() |
|---|
| 7 |
except: |
|---|
| 8 |
from sets import Set as set |
|---|
| 9 |
|
|---|
| 10 |
class SearchRPC(Component): |
|---|
| 11 |
""" Search Trac. """ |
|---|
| 12 |
implements(IXMLRPCHandler) |
|---|
| 13 |
|
|---|
| 14 |
search_sources = ExtensionPoint(ISearchSource) |
|---|
| 15 |
|
|---|
| 16 |
# IXMLRPCHandler methods |
|---|
| 17 |
def xmlrpc_namespace(self): |
|---|
| 18 |
return 'search' |
|---|
| 19 |
|
|---|
| 20 |
def xmlrpc_methods(self): |
|---|
| 21 |
yield ('SEARCH_VIEW', ((list,),), self.getSearchFilters) |
|---|
| 22 |
yield ('SEARCH_VIEW', ((list, str), (list, str, list)), self.performSearch) |
|---|
| 23 |
|
|---|
| 24 |
# Others |
|---|
| 25 |
def getSearchFilters(self, req): |
|---|
| 26 |
""" Retrieve a list of search filters with each element in the form |
|---|
| 27 |
(name, description). """ |
|---|
| 28 |
for source in self.search_sources: |
|---|
| 29 |
for filter in source.get_search_filters(req): |
|---|
| 30 |
yield filter |
|---|
| 31 |
|
|---|
| 32 |
def performSearch(self, req, query, filters = []): |
|---|
| 33 |
""" Perform a search using the given filters. Defaults to all if not |
|---|
| 34 |
provided. Results are returned as a list of tuples in the form |
|---|
| 35 |
(href, title, date, author, excerpt).""" |
|---|
| 36 |
from trac.Search import search_terms |
|---|
| 37 |
query = search_terms(query) |
|---|
| 38 |
chosen_filters = set(filters) |
|---|
| 39 |
available_filters = [] |
|---|
| 40 |
for source in self.search_sources: |
|---|
| 41 |
available_filters += source.get_search_filters(req) |
|---|
| 42 |
|
|---|
| 43 |
filters = [f[0] for f in available_filters if f[0] in chosen_filters] |
|---|
| 44 |
if not filters: |
|---|
| 45 |
filters = [f[0] for f in available_filters] |
|---|
| 46 |
self.env.log.debug("Searching with %s" % filters) |
|---|
| 47 |
|
|---|
| 48 |
results = [] |
|---|
| 49 |
for source in self.search_sources: |
|---|
| 50 |
for result in source.get_search_results(req, query, filters): |
|---|
| 51 |
result = map(unicode, result) |
|---|
| 52 |
results.append(['/'.join(req.base_url.split('/')[0:3]) |
|---|
| 53 |
+ result[0]] + list(result[1:])) |
|---|
| 54 |
return results |
|---|