Introduce new directory structure
To clarify which is a bot and which are helper scripts Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=74 FS#74]
This commit is contained in:
151
lib/redpage.py
Normal file
151
lib/redpage.py
Normal file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# redpage.py
|
||||
#
|
||||
# Copyright 2015 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301, USA.
|
||||
#
|
||||
#
|
||||
"""
|
||||
Provides a class for handling redundance discussion pages and archives
|
||||
"""
|
||||
|
||||
import pywikibot # noqa
|
||||
import mwparserfromhell as mwparser
|
||||
|
||||
import jogobot # noqa
|
||||
|
||||
from lib.mysqlred import MysqlRedPage
|
||||
from lib.redfam import RedFamParser
|
||||
|
||||
|
||||
class RedPage:
|
||||
"""
|
||||
Class for handling redundance discussion pages and archives
|
||||
"""
|
||||
|
||||
def __init__( self, page, archive=False ):
|
||||
"""
|
||||
Generate a new RedPage object based on the given pywikibot page object
|
||||
|
||||
@param page page Pywikibot/MediaWiki page object for page
|
||||
"""
|
||||
|
||||
# Safe the pywikibot page object
|
||||
self.page = page
|
||||
self._archive = archive
|
||||
|
||||
self.__handle_db( )
|
||||
|
||||
self.is_page_changed()
|
||||
|
||||
self._parsed = None
|
||||
|
||||
def __handle_db( self ):
|
||||
"""
|
||||
Handles opening of db connection
|
||||
"""
|
||||
|
||||
# We need a connection to our mysqldb
|
||||
self.__mysql = MysqlRedPage( self.page._pageid )
|
||||
|
||||
if not self.__mysql.data:
|
||||
self.__mysql.add_page( self.page.title(), self.page._revid )
|
||||
|
||||
def is_page_changed( self ):
|
||||
"""
|
||||
Check wether the page was changed since last run
|
||||
"""
|
||||
|
||||
if( self.__mysql.data != { 'page_id': self.page._pageid,
|
||||
'rev_id': self.page._revid,
|
||||
'page_title': self.page.title(),
|
||||
'status': self.__mysql.data[ 'status' ] } ):
|
||||
self._changed = True
|
||||
else:
|
||||
self._changed = False
|
||||
|
||||
def is_archive( self ):
|
||||
"""
|
||||
Detects wether current page is an archive of discussions
|
||||
"""
|
||||
|
||||
if( self._archive or ( u"/Archiv" in self.page.title() ) or
|
||||
( "{{Archiv}}" in self.page.text ) or
|
||||
( "{{Archiv|" in self.page.text ) ):
|
||||
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def is_parsing_needed( self ):
|
||||
"""
|
||||
Decides wether current RedPage needs to be parsed or not
|
||||
"""
|
||||
|
||||
if( self._changed or self.__mysql.data[ 'status' ] == 0 ):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def parse( self ):
|
||||
"""
|
||||
Handles the parsing process
|
||||
"""
|
||||
|
||||
# Generate Wikicode object
|
||||
self.wikicode = mwparser.parse( self.page.text )
|
||||
|
||||
# Select RedFam-sections
|
||||
# matches=Regexp or
|
||||
# function( gets heading content as wikicode as param 1)
|
||||
# include_lead = if true include first section (intro)
|
||||
# include_heading = if true include heading
|
||||
fams = self.wikicode.get_sections(
|
||||
matches=RedFamParser.is_section_redfam_cb,
|
||||
include_lead=False, include_headings=True )
|
||||
|
||||
# Iterate over RedFam
|
||||
for fam in fams:
|
||||
|
||||
yield fam
|
||||
|
||||
else:
|
||||
self._parsed = True
|
||||
self.__update_db()
|
||||
|
||||
def __update_db( self ):
|
||||
"""
|
||||
Updates the page meta data in mysql db
|
||||
"""
|
||||
if( self._parsed or not self._changed ):
|
||||
status = 1
|
||||
|
||||
if( self.is_archive() ):
|
||||
status = 2
|
||||
else:
|
||||
status = 0
|
||||
|
||||
self.__mysql.update_page( self.page._revid, self.page.title(), status )
|
||||
|
||||
@classmethod
|
||||
def flush_db_cache( cls ):
|
||||
"""
|
||||
Calls flush method of Mysql Interface class
|
||||
"""
|
||||
MysqlRedPage.flush()
|
||||
Reference in New Issue
Block a user