1 # OrphanedPages is now a standard macro, with a different implementation
   2 
   3 """ 
   4      MoinMoin - Orphans Macro 
   5    
   6      Copyright (c) 2001 by Richard Jones <richard@bizarsoftware.com.au> 
   7      Copyright (c) 2000 by J\374rgen Hermann <jh@web.de> 
   8      All rights reserved, see COPYING for details. 
   9    
  10      with modifications by Christian Bird <chris.bird@lineo.com>:
  11        The Orphans macro written by Richard Jones is a pretty good idea.
  12        The only problem is that it runs in O(n^2) time which slows down
  13        polynomially as the site grows. Ours consisting of 300+ pages takes
  14        over almost a minute to run the Orphans macro.  This faster macro takes
  15        advantage of hashing, compiling the regular expression only once and a
  16        couple other things to run faster in O(n) time, so it slows down
  17        linearly.  On our site, the macro execution time isn't noticeable.
  18    
  19      $Id: $ 
  20  """ 
  21    
  22 # Imports 
  23 import string, re 
  24 from MoinMoin import config, wikiutil, Page 
  25    
  26 def execute(macro, args): 
  27      # select the pages from the page list 
  28      all_pages = wikiutil.getPageList(config.text_dir) 
  29    
  30      #create a hash of all pages for a checklist 
  31      page_hash = {} 
  32      for page in all_pages: 
  33        page_hash[page] = 0 
  34    
  35      #compile the wiki name regular expression
  36      #I realize that this reg_ex won't catch all wiki names, but I'm
  37      #not sure what the full blown re for that is, so someone should
  38      #replace this with the correct reg_ex.
  39      wiki_re = re.compile("((?:[A-Z][a-z]+){2,})") 
  40    
  41      for page_name in all_pages: 
  42          page = Page.Page(page_name).get_raw_body() 
  43          #get all the wikinames on this page 
  44          wiki_names = wiki_re.findall(page) 
  45          #if that wiki name exists then set the hash value to 1 (check it off the list) 
  46          for name in wiki_names: 
  47            if page_hash.has_key(name): page_hash[name] = 1 
  48    
  49      #If there are no zeroes then all pages were found! 
  50      if 0 not in page_hash.values() : 
  51          return macro.formatter.strong(1) + 'No Orphan pages' + \ 
  52              macro.formatter.strong(0) 
  53    
  54      all_pages.sort() 
  55      l = [macro.formatter.strong(1), 'Orphan pages:', 
  56          macro.formatter.strong(0), macro.formatter.bullet_list(1)]         
  57      #for each page, if it wasn't checked off then display it 
  58      for name in all_pages: 
  59        if not page_hash[name]: 
  60          l.append(macro.formatter.listitem(1)) 
  61          l.append(macro.formatter.pagelink(name)) 
  62          l.append(macro.formatter.listitem(0)) 
  63      l.append(macro.formatter.bullet_list(0)) 
  64    
  65      return string.join(l, '\n')

MoinMoin: macro/FasterOrphans.py (last edited 2007-10-29 19:13:40 by localhost)