| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | """ |
|---|
| 3 | Copyright (c) 2010 by Martin Scharrer <martin@scharrer-online.de> |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | from trac.core import Component, implements |
|---|
| 7 | from trac.config import ListOption |
|---|
| 8 | from trac.wiki.api import IWikiChangeListener |
|---|
| 9 | from trac.ticket.api import ITicketChangeListener |
|---|
| 10 | from urllib import urlopen, quote_plus |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class GoogleSitemapNotifier(Component): |
|---|
| 14 | implements(IWikiChangeListener, ITicketChangeListener) |
|---|
| 15 | |
|---|
| 16 | notifyon = ListOption( |
|---|
| 17 | 'googlesitemap', 'notify_on', |
|---|
| 18 | default='TICKET_CREATE,TICKET_MODIFY,WIKI_CREATE,WIKI_VERSION_DELETE,' |
|---|
| 19 | 'WIKI_MODIFY,WIKI_RENAME', |
|---|
| 20 | doc="""Notify Google about a new sitemap on the listed actions. |
|---|
| 21 | Valid values are: TICKET_CREATE, TICKET_DELETE, TICKET_MODIFY, |
|---|
| 22 | WIKI_CREATE, WIKI_DELETE, WIKI_VERSION_DELETE, WIKI_MODIFY, |
|---|
| 23 | WIKI_RENAME. |
|---|
| 24 | """) |
|---|
| 25 | |
|---|
| 26 | def notify(self): |
|---|
| 27 | sitemapurl = self.env.abs_href(self.config.get( |
|---|
| 28 | 'googlesitemap', 'sitemappath', 'sitemap.xml')) |
|---|
| 29 | |
|---|
| 30 | url = r'http://www.google.com/webmasters/tools/ping?sitemap=' + \ |
|---|
| 31 | quote_plus(sitemapurl) |
|---|
| 32 | |
|---|
| 33 | try: |
|---|
| 34 | response = urlopen(url).read() |
|---|
| 35 | except Exception, e: |
|---|
| 36 | self.log.warn('Google notification failed: ', e) |
|---|
| 37 | # else: |
|---|
| 38 | # self.env.log.debug('Google notification successful!') |
|---|
| 39 | |
|---|
| 40 | def ticket_created(self, ticket): |
|---|
| 41 | if 'TICKET_CREATE' in self.notifyon: |
|---|
| 42 | self.notify() |
|---|
| 43 | |
|---|
| 44 | def ticket_changed(self, ticket, comment, author, old_values): |
|---|
| 45 | if 'TICKET_MODIFY' in self.notifyon: |
|---|
| 46 | self.notify() |
|---|
| 47 | |
|---|
| 48 | def ticket_deleted(self, ticket): |
|---|
| 49 | if 'TICKET_DELETE' in self.notifyon: |
|---|
| 50 | self.notify() |
|---|
| 51 | |
|---|
| 52 | def wiki_page_added(self, page): |
|---|
| 53 | if 'WIKI_CREATE' in self.notifyon: |
|---|
| 54 | self.notify() |
|---|
| 55 | |
|---|
| 56 | def wiki_page_changed(self, page, version, t, comment, author, ipnr): |
|---|
| 57 | if 'WIKI_MODIFY' in self.notifyon: |
|---|
| 58 | self.notify() |
|---|
| 59 | |
|---|
| 60 | def wiki_page_deleted(self, page): |
|---|
| 61 | if 'WIKI_DELETE' in self.notifyon: |
|---|
| 62 | self.notify() |
|---|
| 63 | |
|---|
| 64 | def wiki_page_version_deleted(self, page): |
|---|
| 65 | if 'WIKI_VERSION_DELETE' in self.notifyon: |
|---|
| 66 | self.notify() |
|---|
| 67 | |
|---|
| 68 | def wiki_page_renamed(self, page, old_name): |
|---|
| 69 | if 'WIKI_RENAME' in self.notifyon: |
|---|
| 70 | self.notify() |
|---|