Small Search Macro

Description

This simple macro executes a simple search on wiki pages directly from inside a page. I needed this cause we use the "TODO" string inside pages that still need attention, and I wanted to show a list of pages demanding attention directly on the home page of the project.

PLEASE NOTE I'm lazy, and don't know how to package this into a full featured extension, with an install.py and so on. Also, I have no idea if it works on other versions, for sure it works on 0.10.3 which I have.

As a conseguence of my lazyness, I'm placing the code here, hoping that someone more experienced will package it when needed :

class SmallSearchMacro(WikiMacroBase):
    """
    Performs a search in wiki content, and displays links to the pages.

    The first parameter is the search string, and its mandatory.

    The second parameter is an optional name prefix, only page names starting with this string will be included.

    The third parameter is an optional limit to the length of the returned list

    The fourth parameter is an optional name of a page to not include in the list
    """

    def render_macro(self, req, name, content):
        search = prefix = limit = skips = None
        if not content:
            return html.H2('Need to specify a search')

        if content:
            argv = [arg.strip() for arg in content.split(',')]
            if len(argv) < 1:
                return html.H2('Need to specify a search')
            search = argv[0]
            if len(argv) > 1:
                prefix = argv[1]
                if len(argv) > 2:
                    limit = argv[2]
                    if len(argv) > 3:
                        skips = argv[3]

        db = self.env.get_db_cnx()
        cursor = db.cursor()

        sql = 'SELECT name, max_version FROM (' \
              'SELECT name as name, text as text, ' \
              '  max(version) AS max_version, ' \
              '  max(time) AS max_time ' \
              'FROM wiki '  \
              'WHERE '
        args = []
        if prefix:
            sql += ' name LIKE %s'
            args.append(prefix + '%')
        if skips:
            if prefix:
                sql += ' AND ';
            sql += 'name != %s'
            args.append(skips)
        sql += ' GROUP BY name ORDER BY max_time ASC'
        if limit:
            sql += ' LIMIT %s'
            args.append(limit)
        sql += ') WHERE text LIKE %s'
        args.append('%' + search + '%')
        cursor.execute(sql, args)

        wiki = WikiSystem(self.env)
        return html.DIV(
            html.UL([html.LI(
                html.A(wiki.format_page_name(name), href=req.href.wiki(name)))
                      for name, version in cursor]))

Bugs/Feature Requests

Existing bugs and feature requests for SmallSearchMacro are here.

If you have any issues, create a new ticket.

Download

NOTE: Not yet, see lazyness note above

Download the zipped source from here.

Source

NOTE: Not yet, see lazyness note above

You can check out SmallSearchMacro from here using Subversion, or browse the source with Trac.

Example

In my case, i used it as simply as :

[[SmallSearch(TODO)]]

I also added a few other options. If for example you have all your documentation pages named with the Doc prefix, you can specify to look only those pages using the second parameter :

[[SmallSearch(TODO,Docs)]]

The third parameter is useful to limit the number of results. A fourth parameter is useful to exclude a page from the results (for example, in my case, the page where the usage of the TODO notation is explained).

So, complete example would be :

[[SmallSearch(TODO,Docs,10,DocsExplanations)]]

Recent Changes

[4248] by SimoneGianni on 09/10/08 01:30:56

New hack SmallSearchMacro, created by SimoneGianni

Author/Contributors

Author: SimoneGianni
Contributors: