Advertisement
Guest User

discussion_indexer.patch

a guest
Feb 26th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.54 KB | None | 0 0
  1. --- discussion_indexer.py   2010-08-26 09:37:19.000000000 -0300
  2. +++ discussion_indexer2.py  2010-08-26 13:04:50.000000000 -0300
  3. @@ -1,28 +1,59 @@
  4.  import re, time
  5. -MONTHS = ('January', 'February', 'March', 'April', 'May',
  6. +MONTHS = {
  7. +'en' : ('January', 'February', 'March', 'April', 'May',
  8.         'June', 'July', 'August', 'September', 'October',
  9.         'November', 'December')
  10. +'pt' : ('Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio',
  11. +       'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro',
  12. +       'Novembro', 'Dezembro')
  13. +}
  14. +
  15. +#TODO: How to use $dateFormats from /languages/messages/MessagesXX.php ?
  16. +# en: 'dmy both' => 'H:i, j F Y'
  17. +# pt: 'dmy both' => 'H\hi\m\i\n \d\e j \d\e F \d\e Y'
  18. +
  19. +TIMESTAMPS = {
  20. +'en' : ur'^(.*?)([0-9]{2}\:[0-9]{2}\,' + \
  21. +       ' [0-9]{1,2} (?:%s) [0-9]{4})' % \
  22. +       '|'.join(MONTHS['en']) + ' \(UTC\)\s*$'
  23. +'pt' : ur'^(.*?)([0-9]{2}h[0-9]{2}min' + \
  24. +       ' de [0-9]{1,2} de (?:%s) de [0-9]{4})' % \
  25. +       '|'.join(MONTHS['pt']) + ' \(UTC\)\s*$'
  26. +}
  27. +
  28. +TIMESTAMPS2 = {
  29. +'en' : '%H:%M, %d %B %Y'
  30. +'pt' : '%Hh%Mmin de %d de %B de %Y'
  31. +}
  32. +
  33. +USERS = {
  34. +'en' : ur'.*\[\[[Uu]ser(?:[ _]talk)?\:([^]|]*)'
  35. +'pt' : ur'.*\[\[[Uu]suário(?:[ _]Discussão)?\:([^]|]*)'
  36. +}
  37.  
  38.  import mwclient
  39.  import cgitb; cgitb.enable(format = 'text')
  40.  
  41.  class DiscussionIndexer(object):
  42. +   LANG = 'en' #TODO: How to get $wgLanguageCode for the current project?
  43.     def __init__(self, site):
  44.         self.site = site
  45.            
  46.         self.cache = {}
  47.            
  48. -   r_timestamp = re.compile(ur'^(.*?)([0-9]{2}\:[0-9]{2}\,' + \
  49. -       ' [0-9]{1,2} (?:%s) [0-9]{4})' % \
  50. -       '|'.join(MONTHS) + ' \(UTC\)\s*$', re.MULTILINE)
  51. -   r_user = re.compile(ur'.*\[\[[Uu]ser(?:[ _]talk)?\:([^]|]*)')
  52. +   r_timestamp = re.compile(TIMESTAMPS[LANG], re.MULTILINE)
  53. +   r_user = re.compile(USERS[LANG])
  54.     def get_last_comment(self, section):
  55.         """ Returns a tuple (user, timestamp) of the
  56.         last comment made to the section."""
  57.        
  58.         comments = self.r_timestamp.findall(section)
  59. +       for c in comments:
  60. +           for m in range(12):
  61. +               c[1] = re.sub(MONTHS['pt'][m], MONTHS['en'][m], c[1])
  62. +
  63.         comments = [(self.r_user.search(line), time.strptime(timestamp,
  64. -           '%H:%M, %d %B %Y')) for line, timestamp in comments]
  65. +           TIMESTAMPS2[LANG])) for line, timestamp in comments]
  66.        
  67.         comments.sort(cmp = lambda x, y: -cmp(x[1], y[1]))
  68.        
  69. @@ -73,7 +104,7 @@
  70.         items = []
  71.         for page, title, user, timestamp in sections:
  72.             if timestamp:
  73. -               timestamp = time.strftime('%H:%M, %d %B %Y', timestamp)
  74. +               timestamp = time.strftime(TIMESTAMPS2[LANG], timestamp)
  75.             else:
  76.                 timestamp = u''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement