# $Id$
# $HeadURL$
"""Contains functions that maintain/update the cache tables."""
+from xml.etree import ElementTree
from sqlalchemy import schema
from sqlalchemy.sql import select
import formencode
raise UpdateCacheError(error_msg, inn_page.page_title, e)
transaction.commit()
+
+def update_wrreportcache(connection, page_id=None):
+ """Updates the wrreportcache table.
+ :param connection: sqlalchemy connection
+ :param page_id: Updates only the reportcache table for the sledrun described on the Winterrodeln wiki page
+ with the specified page_id. Use None for this parameter to update the whole table.
+
+ >>> from sqlalchemy.engine import create_engine
+ >>> engine = create_engine('mysql://philipp@localhost:3306/winterrodeln_wiki?charset=utf8&use_unicode=1')
+ >>> update_wrreportcache(engine.connect())
+ """
+ metadata = schema.MetaData()
+ wrreport = wrmwdb.wrreport_table(metadata)
+ wrreportcache = wrmwdb.wrreportcache_table(metadata)
+ transaction = connection.begin()
+
+ # Delte the datasets we are going to update
+ sql_del = wrreportcache.delete()
+ if not page_id is None: sql_del = sql_del.where(wrreportcache.c.page_id == page_id)
+ connection.execute(sql_del)
+
+ def insert_row(connection, rowlist):
+ if len(rowlist) == 0: return
+ # Build XML
+ reports_xml = ElementTree.Element('reports')
+ for row in rowlist:
+ report_xml = ElementTree.SubElement(reports_xml, 'report')
+ report_xml.set('report_id', unicode(row.report_id))
+ report_xml.set('date_report', unicode(row.report_date_report))
+ report_xml.set('condition', unicode(row.report_condition))
+ report_xml.set('author_name', unicode(row.report_author_name))
+ report_xml.set('author_username', unicode(row.report_author_username))
+ report_xml.text = unicode(row.report_description)
+ reports_xml.set('page_id', unicode(row.page_id))
+ reports_xml.set('page_title', row.page_title)
+ reports_xml = unicode(ElementTree.tostring(reports_xml, 'utf8'), 'utf8') # there is not ElementTree.tounicode())
+ # Insert the report(s)
+ row = dict(rowlist[0])
+ row['reports_xml'] = reports_xml
+ connection.execute(wrreportcache.insert(values=row))
+
+ # Select the rows to update
+ sql = 'select page_id, page_title, wrreport.id as report_id, date_report as report_date_report, `condition` as report_condition, description as report_description, author_name as report_author_name, if(author_userid is null, null, author_username) as report_author_username from wrreport where {0}`condition` is not null and date_invalid > now() and delete_date is null order by page_id, date_report desc, date_entry desc'.format('' if page_id is None else 'page_id={0} and '.format(page_id))
+ cursor = connection.execute(sql)
+ page_id = None
+ rowlist = []
+ for row in cursor:
+ if row.page_id != page_id:
+ insert_row(connection, rowlist)
+ page_id = row.page_id
+ rowlist = []
+ rowlist.append(row)
+ insert_row(connection, rowlist)
+ transaction.commit()
+
)
+def wrreportcache_table(metadata):
+ """Returns the sqlalchemy Table representing the "wrreportcache" Winterrodeln table in MediaWiki.
+ Current table definition.
+ * version 1.5 (introduction)
+ :param metadata: metadata = sqlalchemy.MetaData()
+ """
+ return Table("wrreportcache", metadata,
+ Column("page_id", types.Integer, primary_key=True),
+ Column("page_title", types.Unicode(255), nullable=False),
+ Column("report_id", types.Integer),
+ Column("report_date_report", types.Date),
+ Column("report_condition", types.Integer),
+ Column("report_description", types.Unicode),
+ Column("report_author_name", types.Unicode(30)),
+ Column("report_author_username", types.Unicode(30)),
+ Column("reports_xml", types.Unicode),
+ )
+
+