83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
# -*- coding: iso-8859-1 -*-
|
|
"""
|
|
MoinMoin - "opensearch" action
|
|
|
|
Generate an opensearch xml description.
|
|
|
|
@copyright: 2006 by Grégoire Détrez <gdetrez@crans.org>
|
|
@license: GNU GPL, see COPYING for details.
|
|
"""
|
|
|
|
from MoinMoin import config, wikiutil
|
|
from MoinMoin.util import MoinMoinNoFooter
|
|
from MoinMoin.wikixml.util import XMLGenerator
|
|
import StringIO
|
|
from xml.sax import saxutils
|
|
|
|
def execute(pagename, request):
|
|
_ = request.getText
|
|
form = request.form
|
|
|
|
if form.has_key('searchtype'):
|
|
searchtype = form['searchtype'][0]
|
|
else:
|
|
searchtype = "Titres"
|
|
|
|
request.http_headers(["Content-Type: text/xml; charset=%s" % config.charset ])
|
|
|
|
name = request.cfg.sitename + " (%s)" % searchtype
|
|
if searchtype == "Titres":
|
|
Desc = "%s, recherche dans les titres" % request.cfg.sitename
|
|
else:
|
|
Desc = "%s, recherche dans le texte" % request.cfg.sitename
|
|
|
|
if searchtype == "Titres":
|
|
searchtype_arg = "titlesearch=Titres"
|
|
else:
|
|
searchtype_arg = "fullsearch=Texte"
|
|
server_search_addr = "http://%s%s?action=fullsearch&context=180&value={searchTerms}&%s" % (request.http_host, request.script_name, searchtype_arg)
|
|
|
|
# prepare output
|
|
out = StringIO.StringIO()
|
|
handler = OpenSearchGenerator(out)
|
|
|
|
|
|
handler.startDocument()
|
|
|
|
handler.node('ShortName', name)
|
|
handler.node('Description', Desc)
|
|
|
|
handler.node('Image', 'http://%s/favicon.ico' % request.http_host, {
|
|
'height': "16",
|
|
'width': "16",
|
|
'type': "image/x-icon",
|
|
})
|
|
handler.node('Url', None, {
|
|
'template': server_search_addr,
|
|
'method': "get",
|
|
'type': "text/html",
|
|
})
|
|
handler.endDocument()
|
|
request.write(out.getvalue())
|
|
|
|
raise MoinMoinNoFooter
|
|
|
|
class OpenSearchGenerator(saxutils.XMLGenerator):
|
|
default_xmlns = {
|
|
'opensearch':"http://a9.com/-/spec/opensearch/1.1/",
|
|
}
|
|
def __init__(self, out):
|
|
saxutils.XMLGenerator.__init__(self, out=out, encoding=config.charset)
|
|
#self.xmlns = self.default_xmlns
|
|
def startDocument(self):
|
|
saxutils.XMLGenerator.startDocument(self)
|
|
self.startElement('OpenSearchDescription', {'xmlns': "http://a9.com/-/spec/opensearch/1.1/"})
|
|
def endDocument(self):
|
|
self.endElement('OpenSearchDescription')
|
|
saxutils.XMLGenerator.endDocument(self)
|
|
def node(self, tag, value, attr={}):
|
|
self.startElement(tag, attr)
|
|
if value: self.characters(value)
|
|
self.endElement(tag)
|
|
|
|
|