From e13320820ce635c1844dd9a8cdc66f5fd2db4311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Tue, 30 Aug 2016 17:45:18 +0200 Subject: [PATCH] Add API to manage status per article To be able to track changes to articles to update redfam status Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=89 FS#89] --- lib/mysqlred.py | 9 ++-- lib/redfam.py | 125 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 5 deletions(-) diff --git a/lib/mysqlred.py b/lib/mysqlred.py index 79360a8..0bb843c 100644 --- a/lib/mysqlred.py +++ b/lib/mysqlred.py @@ -336,8 +336,8 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );' cursor = type( self ).connection.cursor( mysqldb.DictCursor ) cursor.execute( - 'SELECT * FROM `{prefix}_redfams` WHERE `status` = ?;'.format( - prefix=type( self ).db_table_prefix), ( status, ) ) + 'SELECT * FROM `{prefix}_redfams` WHERE `status` = LIKE %?%;'. + format( prefix=type( self ).db_table_prefix), ( status, ) ) while True: res = cursor.fetchmany( 1000 ) @@ -358,9 +358,10 @@ article7 ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );' 'FROM `{prefix}_redfams` `F` ' + 'INNER JOIN `{prefix}_redpages` `P` ' + 'ON `F`.`status` = ? ' + - 'AND `F`.`ending` >= ? ' + 'AND `F`.`ending` >= ? ' + 'AND `F`.`redpageid` = `P`.`pageid`;').format( - prefix=type( self ).db_table_prefix), ( status, ending ) ) + prefix=type( self ).db_table_prefix), + ( status, ending ) ) while True: res = cursor.fetchmany( 1000 ) diff --git a/lib/redfam.py b/lib/redfam.py index 798d501..d5312ca 100644 --- a/lib/redfam.py +++ b/lib/redfam.py @@ -198,6 +198,116 @@ class RedFam: """ return ",".join( self._status ) + def article_add_status(self, status, index=None, title=None ): + """ + Adds a status specified by status, to article (identified by title + or index in articlesList) status set + + @param status Statusstring to add + @type status str + @param index Add to article with index in articlesList + @type index int + @param title Add to article with title in articlesList + @type title str + """ + if title and not index: + index = self._articlesList.index( title ) + + if isinstance( index, int ) and index < len(self._articlesList): + self._article_status[index].add(status) + else: + raise IndexError( "No index given or wrong format!") + + def article_remove_status(self, status, index=None, title=None, weak=True): + """ + Removes a status specified by status, from article (identified by title + or index in articlesList) status set + If weak is set to False it will throw a KeyError when trying to + remove a status not set. + + @param status Statusstring to add + @type status str + @param index Remove from article with index in articlesList + @type index int + @param title Remove from article with title in articlesList + @type title str + @param weak Change behavior on missing status + @type bool + """ + if title and not index: + index = self._articlesList.index( title ) + + if isinstance( index, int ) and index < len(self._articlesList): + if weak: + self._article_status[index].discard(status) + else: + self._article_status[index].remove(status) + else: + raise IndexError( "No index given or wrong format!") + + def article_has_status(self, status, index=None, title=None ): + """ + Adds a status specified by status, to articles (identified by title + or index in articlesList) status set + + @param status Statusstring to add + @type status str + @param index Check article with index in articlesList + @type index int + @param title Check article with title in articlesList + @type title str + """ + if title and not index: + index = self._articlesList.index( title ) + + if isinstance( index, int ) and index < len(self._articlesList): + if status in self._article_status[index]: + return True + else: + return False + else: + raise IndexError( "No index given or wrong format!") + + def _article_parse_status(self, raw_status, index=None, title=None ): + """ + Sets status based on comma separated list to articles (identified by + title or index in articlesList) status set + + @param status Statusstring to set + @type status str + @param index Add to article with index in articlesList + @type index int + @param title Add to article with title in articlesList + @type title str + """ + if title and not index: + index = self._articlesList.index( title ) + + if isinstance( index, int ) and index < len(self._articlesList): + self._article_status[index] = set( raw_status.strip().split(",")) + else: + raise IndexError( "No index given or wrong format!") + + def _article_raw_status( self, index=None, title=None ): + """ + Returns status as commaseparated string (to save in DB) of article + (identified by title or index in articlesList) status set + + @param index Get from article with index in articlesList + @type index int + @param title Get from article with title in articlesList + @type title str + @returns Raw status string + @rtype str + """ + if title and not index: + index = self._articlesList.index( title ) + + if isinstance( index, int ) and index < len(self._articlesList): + return ",".join( self._article_status[index] ) + else: + raise IndexError( "No index given or wrong format!") + class RedFamParser( RedFam ): """ @@ -491,10 +601,14 @@ class RedFamWorker( RedFam ): def __init__( self, mysql_data ): articlesList = [] + for key in sorted( mysql_data.keys() ): - if 'article' in key and mysql_data[ key ]: + if 'article' in key and 'status' not in key and mysql_data[ key ]: articlesList.append( mysql_data[ key ] ) + # Preset article status list with empty sets for existing articles + self._article_status = [set() for x in range(0, len(articlesList))] + super().__init__( articlesList, mysql_data[ 'beginning' ], mysql_data[ 'ending' ], mysql_data[ 'redpageid' ], mysql_data[ 'status' ], mysql_data[ 'famhash' ], @@ -502,6 +616,15 @@ class RedFamWorker( RedFam ): self._mysql.data = mysql_data + # Set up article status + index = 0 + for article in self._articlesList: + raw_status = mysql_data[ "article" + str(index) + "_status" ] + if not raw_status: + raw_status = str() + self._article_parse_status( raw_status, index ) + index += 1 + # Get related RedPage-Information self.redpageid = mysql_data[ 'pageid' ] self.redpagetitle = mysql_data[ 'pagetitle' ]