Browse Source

Add a tablename prefix depending on Site

To be able to run the bot on different wikis the db tables should be
named pywikibot.Site dependend and changed automatically

Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=79 FS#79]
develop
Jonathan Golder 8 years ago
parent
commit
77d1de4473
  1. 39
      lib/mysqlred.py

39
lib/mysqlred.py

@ -33,6 +33,7 @@ except ImportError:
import atexit import atexit
import pywikibot
from pywikibot import config from pywikibot import config
import jogobot import jogobot
@ -53,6 +54,7 @@ class MysqlRed:
db_username = config.db_username db_username = config.db_username
db_password = config.db_password db_password = config.db_password
db_name = config.db_username + jogobot.config['db_suffix'] db_name = config.db_username + jogobot.config['db_suffix']
db_table_prefix = pywikibot.Site().family.dbName(pywikibot.Site().code)
# Class variables for storing cached querys # Class variables for storing cached querys
_cached_update_data = [] _cached_update_data = []
@ -136,12 +138,14 @@ class MysqlRedPage( MysqlRed ):
# Class variables for storing cached querys # Class variables for storing cached querys
_cached_update_data = [] _cached_update_data = []
_update_query = 'UPDATE `red_pages` \ _update_query = 'UPDATE `{pre}_red_pages` \
SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;' SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'.format(
pre=MysqlRed.db_table_prefix)
_cached_insert_data = {} _cached_insert_data = {}
_insert_query = 'INSERT INTO `red_pages` \ _insert_query = 'INSERT INTO `{pre}_red_pages` \
( page_id, page_title, rev_id, status ) VALUES ( ?, ?, ?, ? );' ( page_id, page_title, rev_id, status ) VALUES ( ?, ?, ?, ? );'.format(
pre=MysqlRed.db_table_prefix)
def __init__( self, page_id ): def __init__( self, page_id ):
""" """
@ -169,8 +173,10 @@ SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'
cursor = type( self ).connection.cursor(mysqldb.DictCursor) cursor = type( self ).connection.cursor(mysqldb.DictCursor)
cursor.execute( 'SELECT * FROM `red_pages` WHERE `page_id` = ?;', cursor.execute(
( self.__page_id, ) ) 'SELECT * FROM `{pre}_red_pages` WHERE `page_id` = ?;'.format(
pre=MysqlRed.db_table_prefix), ( self.__page_id, ) )
res = cursor.fetchone() res = cursor.fetchone()
if res: if res:
@ -221,15 +227,17 @@ class MysqlRedFam( MysqlRed ):
# Class variables for storing cached querys # Class variables for storing cached querys
_cached_update_data = [] _cached_update_data = []
_update_query = 'UPDATE `red_families` \ _update_query = 'UPDATE `{pre}_red_families` \
SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, `ending` = ?, \ SET `red_page_id` = ?, `heading` = ?, `beginning` = ?, `ending` = ?, \
`status`= ? WHERE `fam_hash` = ?;' `status`= ? WHERE `fam_hash` = ?;'.format(
pre=MysqlRed.db_table_prefix)
_cached_insert_data = {} _cached_insert_data = {}
_insert_query = 'INSERT INTO `red_families` \ _insert_query = 'INSERT INTO `{pre}_red_families` \
( fam_hash, red_page_id, beginning, ending, status, heading, \ ( fam_hash, red_page_id, beginning, ending, status, heading, \
article0, article1, article2, article3, article4, article5, article6, \ article0, article1, article2, article3, article4, article5, article6, \
article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );' article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'.format(
pre=MysqlRed.db_table_prefix)
def __init__( self ): def __init__( self ):
""" """
@ -252,8 +260,10 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
cursor = type( self ).connection.cursor( mysqldb.DictCursor ) cursor = type( self ).connection.cursor( mysqldb.DictCursor )
cursor.execute( 'SELECT * FROM `red_families` WHERE `fam_hash` = ?;', cursor.execute(
( fam_hash, ) ) 'SELECT * FROM `{pre}_red_families` WHERE `fam_hash` = ?;'.format(
pre=MysqlRed.db_table_prefix), ( fam_hash, ) )
self.data = cursor.fetchone() self.data = cursor.fetchone()
def add_fam( self, articlesList, heading, red_page_id, def add_fam( self, articlesList, heading, red_page_id,
@ -301,8 +311,9 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
cursor = type( self ).connection.cursor( mysqldb.DictCursor ) cursor = type( self ).connection.cursor( mysqldb.DictCursor )
cursor.execute( 'SELECT * FROM `red_families` WHERE `status` = ?;', cursor.execute(
( status, ) ) 'SELECT * FROM `{pre}_red_families` WHERE `status` = ?;'.format(
pre=type( self ).db_table_prefix), ( status, ) )
while True: while True:
res = cursor.fetchmany( 1000 ) res = cursor.fetchmany( 1000 )

Loading…
Cancel
Save