Attachment 'AddCategorySearchTerm.patch'

Download

   1 --- Page.py.orig	2005-07-14 16:05:31.000000000 +0100
   2 +++ Page.py	2005-07-15 09:55:13.000000000 +0100
   3 @@ -1467,7 +1467,27 @@
   4          @rtype: list
   5          @return: categories this page belongs to
   6          """
   7 -        return wikiutil.filterCategoryPages(request, self.getPageLinks(request))
   8 +	categories = []
   9 +	raw_body = self.get_raw_body()
  10 +
  11 +	bits = raw_body.split('\n----\n')
  12 +	if len(bits) == 1:
  13 +	    return categories;
  14 +
  15 +	lines = bits[-1].split()
  16 +	for line in lines:
  17 +	    line = line.strip()
  18 +	    if line == "":
  19 +	    	pass
  20 +	    elif re.match("^Category\w*$", line):
  21 +	        categories.append (line)
  22 +	    else:
  23 +	        # Any token in the last bit that doesn't match means it isn't
  24 +		# a category section
  25 +	        categories = []
  26 +		break
  27 +
  28 +        return categories
  29  
  30      def getParentPage(self):
  31          """ Return parent page or None
  32 --- search.py.orig	2005-07-14 16:26:50.000000000 +0100
  33 +++ search.py	2005-07-25 12:02:19.000000000 +0100
  34 @@ -337,7 +337,80 @@
  35      def indexed_query(self):
  36          return self
  37  
  38 -    
  39 +class CategorySearch(BaseExpression):
  40 +    """ Term searches in pattern in page category only """
  41 +
  42 +    def __init__(self, pattern, use_re=False, case=False):
  43 +        """ Init a category search
  44 +
  45 +        @param pattern: pattern to search for, ascii string or unicode
  46 +        @param use_re: treat pattern as re of plain text, bool
  47 +        @param case: do case sensitive search, bool 
  48 +        """
  49 +        self._pattern = "Category%s" % pattern
  50 +	self._textpattern = '(' + self._pattern.replace('/', '|') + ')'
  51 +        self.negated = 0
  52 +	self.textsearch = TextSearch(self._textpattern, use_re=1, case=case)
  53 +        self._build_re(unicode(pattern), use_re=use_re, case=case)
  54 +        
  55 +    def _build_re(self, pattern, use_re=False, case=False):
  56 +        """ Make a regular expression out of a text pattern """
  57 +        flags = (re.U | re.I, re.U)[case]
  58 +
  59 +        try:
  60 +            if not use_re:
  61 +                raise re.error
  62 +            self.search_re = re.compile("Category%s" % pattern, flags)
  63 +            self.static = False
  64 +        except re.error:
  65 +            self.pattern = "Category%s" % pattern
  66 +            self.static = True
  67 +        
  68 +    def costs(self):
  69 +        return 100
  70 +
  71 +    def __unicode__(self):
  72 +        if self.negated: neg = '-'
  73 +        else: neg = ''
  74 +        return u'%s!"%s"' % (neg, unicode(self._pattern))
  75 +
  76 +    def highlight_re(self):
  77 +        return u"(%s)" % self._pattern    
  78 +
  79 +    def search(self, page):
  80 +        # Get matches in page categories
  81 +        matches = []
  82 +
  83 +	Found = False
  84 +
  85 +	for category in page.getCategories(page.request):
  86 +	    if ((self.static and self.pattern == category) or
  87 +	        (not self.static and self.search_re.match(category))):
  88 +		Found = True
  89 +	        break
  90 +
  91 +	if Found:
  92 +	    # Search in page text
  93 +	    results = self.textsearch.search(page)
  94 +	    if results:
  95 +	        matches.extend(results)
  96 +	    else:
  97 +	    	matches.append(TextMatch(0,0))
  98 +
  99 +	# Decide what to do with the results.
 100 +        if ((self.negated and matches) or
 101 +            (not self.negated and not matches)):
 102 +            return None
 103 +        elif matches:
 104 +            return matches
 105 +        else:
 106 +            # XXX why not return None or empty list?
 107 +            return [Match()]
 108 +
 109 +    def indexed_query(self):
 110 +        return self
 111 +
 112 +   
 113  class LinkSearch(BaseExpression):
 114      """ Search the term in the pagelinks """
 115  
 116 @@ -663,6 +736,7 @@
 117          regex = self.regex
 118          case = self.case
 119          linkto = 0
 120 +	category = 0
 121  
 122          for m in modifiers:
 123              if "title".startswith(m):
 124 @@ -673,8 +747,12 @@
 125                  case = True
 126              elif "linkto".startswith(m):
 127                  linkto = True
 128 +	    elif "category".startswith(m):
 129 +	        category = True
 130  
 131 -        if linkto:
 132 +	if category:
 133 +	    obj = CategorySearch(text, use_re=regex, case=case)
 134 +        elif linkto:
 135              obj = LinkSearch(text, use_re=regex, case=case)
 136          elif title_search:
 137              obj = TitleSearch(text, use_re=regex, case=case)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-07-25 12:05:07, 4.0 KB) [[attachment:AddCategorySearchTerm.patch]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.