Change behavior of MYSQL_RED(_PAGE), handle save requested data in object for simple update handling
This commit is contained in:
72
mysql_red.py
72
mysql_red.py
@@ -8,8 +8,12 @@ class MYSQL_RED:
|
|||||||
|
|
||||||
#Save mysqldb-connection as class attribute to use only one in descendant classes
|
#Save mysqldb-connection as class attribute to use only one in descendant classes
|
||||||
connection = False
|
connection = False
|
||||||
|
db_hostname='localhost'
|
||||||
|
db_username='gwlocal'
|
||||||
|
db_password='RigTawwewmyagPepotugco'
|
||||||
|
db_name='gwlocal_wiki'
|
||||||
|
|
||||||
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
def __init__( self ):
|
||||||
"""
|
"""
|
||||||
Opens a connection to MySQL-DB
|
Opens a connection to MySQL-DB
|
||||||
|
|
||||||
@@ -19,7 +23,7 @@ class MYSQL_RED:
|
|||||||
# Connect to mysqldb only once
|
# Connect to mysqldb only once
|
||||||
if( type( self ).connection == False ):
|
if( type( self ).connection == False ):
|
||||||
|
|
||||||
type( self ).connection = mysqldb.connect( host=db_hostname, user=db_username, passwd=db_password, db=db_name )
|
type( self ).connection = mysqldb.connect( host=type( self ).db_hostname, user=type( self ).db_username, passwd=type( self ).db_password, db=type( self ).db_name )
|
||||||
|
|
||||||
def __del__( self ):
|
def __del__( self ):
|
||||||
"""
|
"""
|
||||||
@@ -30,14 +34,18 @@ class MYSQL_RED:
|
|||||||
|
|
||||||
class MYSQL_RED_PAGE( MYSQL_RED ):
|
class MYSQL_RED_PAGE( MYSQL_RED ):
|
||||||
|
|
||||||
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
def __init__( self, page_id ):
|
||||||
"""
|
"""
|
||||||
Creates a new instance, runs __init__ of parent class
|
Creates a new instance, runs __init__ of parent class
|
||||||
"""
|
"""
|
||||||
super().__init__( db_hostname, db_username, db_password, db_name )
|
super().__init__( )
|
||||||
|
|
||||||
|
self.__page_id = int( page_id );
|
||||||
|
|
||||||
|
self.data = self.get_page()
|
||||||
|
|
||||||
|
|
||||||
def get_page( self, page_id ):
|
def get_page( self ):
|
||||||
"""
|
"""
|
||||||
Retrieves a red page row from MySQL-Database for given page_id
|
Retrieves a red page row from MySQL-Database for given page_id
|
||||||
|
|
||||||
@@ -48,19 +56,67 @@ class MYSQL_RED_PAGE( MYSQL_RED ):
|
|||||||
"""
|
"""
|
||||||
cursor = type( self ).connection.cursor(mysqldb.DictCursor)
|
cursor = type( self ).connection.cursor(mysqldb.DictCursor)
|
||||||
|
|
||||||
cursor.execute( 'SELECT * FROM `red_pages` WHERE `page_id` = ?;', ( page_id, ) )
|
cursor.execute( 'SELECT * FROM `red_pages` WHERE `page_id` = ?;', ( self.__page_id, ) )
|
||||||
res = cursor.fetchone()
|
res = cursor.fetchone()
|
||||||
|
|
||||||
if res:
|
if res:
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def add_page( self, page_title, rev_id, status=0 ):
|
||||||
|
"""
|
||||||
|
Inserts a red page row in MySQL-Database for given page_id
|
||||||
|
|
||||||
|
@param int rev_id MediaWiki current rev_id for page to update
|
||||||
|
@param str page_title MediaWiki new page_title for page to update
|
||||||
|
@param int status Page parsing status (0 - not (successfully) parsed; 1 - successfully parsed)
|
||||||
|
"""
|
||||||
|
|
||||||
|
cursor = type( self ).connection.cursor()
|
||||||
|
|
||||||
|
if not page_title:
|
||||||
|
page_title = self.data[ 'page_title' ]
|
||||||
|
if not rev_id:
|
||||||
|
rev_id = self.data[ 'rev_id' ]
|
||||||
|
|
||||||
|
query = 'INSERT INTO `red_pages` ( page_id, page_title, rev_id, status ) VALUES ( ?, ?, ?, ? );'
|
||||||
|
data = ( self.__page_id, str( page_title ), int( rev_id ), int( status ) )
|
||||||
|
|
||||||
|
cursor.execute( query, data)
|
||||||
|
|
||||||
|
type( self ).connection.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def update_page( self, rev_id=None, page_title=None, status=0 ):
|
||||||
|
"""
|
||||||
|
Updates the red page row in MySQL-Database for given page_id
|
||||||
|
|
||||||
|
@param int rev_id MediaWiki current rev_id for page to update
|
||||||
|
@param str page_title MediaWiki new page_title for page to update
|
||||||
|
@param int status Page parsing status (0 - not (successfully) parsed; 1 - successfully parsed)
|
||||||
|
"""
|
||||||
|
|
||||||
|
cursor = type( self ).connection.cursor()
|
||||||
|
|
||||||
|
if not page_title:
|
||||||
|
page_title = self.data[ 'page_title' ]
|
||||||
|
if not rev_id:
|
||||||
|
rev_id = self.data[ 'rev_id' ]
|
||||||
|
|
||||||
|
query = 'UPDATE `red_pages` SET `page_title` = ?, `rev_id` = ?, `status`= ? WHERE `page_id` = ?;'
|
||||||
|
data = ( str( page_title ), int( rev_id ), int( status ), self.__page_id )
|
||||||
|
|
||||||
|
cursor.execute( query, data)
|
||||||
|
|
||||||
|
type( self ).connection.commit()
|
||||||
|
|
||||||
|
|
||||||
class MYSQL_RED_FAM( MYSQL_RED ):
|
class MYSQL_RED_FAM( MYSQL_RED ):
|
||||||
|
|
||||||
def __init__( self, db_hostname, db_username, db_password, db_name ):
|
def __init__( self ):
|
||||||
"""
|
"""
|
||||||
Creates a new instance, runs __init__ of parent class
|
Creates a new instance, runs __init__ of parent class
|
||||||
"""
|
"""
|
||||||
super().__init__( db_hostname, db_username, db_password, db_name )
|
super().__init__( )
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user