70 lines
1.7 KiB
Python
70 lines
1.7 KiB
Python
|
|
try:
|
|
import oursql as mysqldb
|
|
except ImportError:
|
|
import MySQLdb as mysqldb
|
|
|
|
class MYSQL_RED:
|
|
|
|
#Save mysqldb-connection as class attribute to use only one in descendant classes
|
|
connection = False
|
|
|
|
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
|
"""
|
|
Opens a connection to MySQL-DB
|
|
|
|
@returns mysql-stream MySQL Connection
|
|
"""
|
|
|
|
# Connect to mysqldb only once
|
|
if( type( self ).connection == False ):
|
|
|
|
type( self ).connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name )
|
|
|
|
def __del__( self ):
|
|
"""
|
|
Before deleting class, close connection to MySQL-DB
|
|
"""
|
|
|
|
type( self ).connection.close()
|
|
|
|
class MYSQL_RED_PAGE( MYSQL_RED ):
|
|
|
|
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
|
"""
|
|
Creates a new instance, runs __init__ of parent class
|
|
"""
|
|
super().__init__( db_hostname, db_username, db_password, db_name )
|
|
|
|
|
|
def get_page( self, page_id ):
|
|
"""
|
|
Retrieves a red page row from MySQL-Database for given page_id
|
|
|
|
@param int page_id MediaWiki page_id for page to retrieve
|
|
|
|
@returns tuple Tuple with data for given page_id otherwise if none found
|
|
bool FALSE
|
|
"""
|
|
cursor = type( self ).connection.cursor(mysqldb.DictCursor)
|
|
|
|
format_str = """SELECT * FROM `red_pages` WHERE page_id={page_id};"""
|
|
query = format_str.format( page_id=int( page_id ) )
|
|
|
|
cursor.execute( query )
|
|
res = cursor.fetchone()
|
|
|
|
if res:
|
|
return res
|
|
else:
|
|
return False
|
|
|
|
class MYSQL_RED_FAM( MYSQL_RED ):
|
|
|
|
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
|
"""
|
|
Creates a new instance, runs __init__ of parent class
|
|
"""
|
|
super().__init__( db_hostname, db_username, db_password, db_name )
|
|
|