Implement method to add notice to disk page
Adds the generated notice to the talkpage and starts the saving of the page Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=88 FS#88] Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=88 FS#88]
This commit is contained in:
@@ -31,6 +31,8 @@ from datetime import datetime
|
|||||||
from pywikibot import pagegenerators
|
from pywikibot import pagegenerators
|
||||||
from pywikibot.bot import CurrentPageBot
|
from pywikibot.bot import CurrentPageBot
|
||||||
|
|
||||||
|
import mwparserfromhell as mwparser
|
||||||
|
|
||||||
import jogobot
|
import jogobot
|
||||||
|
|
||||||
from lib.redfam import RedFamWorker
|
from lib.redfam import RedFamWorker
|
||||||
@@ -116,7 +118,54 @@ class MarkPagesBot( CurrentPageBot ): # sets 'current_page' on each treat()
|
|||||||
def treat_page( self ):
|
def treat_page( self ):
|
||||||
"""
|
"""
|
||||||
Handles work on current page
|
Handles work on current page
|
||||||
|
|
||||||
|
We get a reference to related redfam in current_page.redfam
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Here is the place where to do what ever you want
|
# First we need to have the current text of page
|
||||||
print( self.current_page.title() )
|
# and parse it as wikicode
|
||||||
|
self.current_wikicode = mwparser.parse( self.current_page.text )
|
||||||
|
|
||||||
|
# Add notice
|
||||||
|
self.add_disc_notice_template()
|
||||||
|
|
||||||
|
# Convert wikicode back to string to save
|
||||||
|
self.new_text = str( self.current_wikicode )
|
||||||
|
|
||||||
|
# Save
|
||||||
|
self.put_current( self.new_text )
|
||||||
|
|
||||||
|
def add_disc_notice_template( self ):
|
||||||
|
"""
|
||||||
|
Will take self.current_wikicode and adds disc notice template after the
|
||||||
|
last template in leading section or as first element if there is no
|
||||||
|
other template in leading section
|
||||||
|
"""
|
||||||
|
# The notice to add
|
||||||
|
notice = self.current_page.redfam.generate_disc_notice_template()
|
||||||
|
|
||||||
|
# Find the right place to insert notice template
|
||||||
|
# Therfore we need the first section (if there is one)
|
||||||
|
leadsec = self.current_wikicode.get_sections(
|
||||||
|
flat=False, include_lead=True )[0]
|
||||||
|
|
||||||
|
# There is none on empty pages, so we need to check
|
||||||
|
if leadsec:
|
||||||
|
# Get the last template in leadsec
|
||||||
|
ltemplate = leadsec.filter_templates()[-1]
|
||||||
|
|
||||||
|
# If there is one, add notice after this
|
||||||
|
if ltemplate:
|
||||||
|
self.current_wikicode.insert_after(ltemplate, notice )
|
||||||
|
|
||||||
|
# To have it in its own line we need to add a linbreak before
|
||||||
|
self.current_wikicode.insert_before(notice, "\n" )
|
||||||
|
|
||||||
|
# If there is no template, add before first element on page
|
||||||
|
else:
|
||||||
|
self.current_wikicode.insert( 0, notice )
|
||||||
|
|
||||||
|
# If there is no leadsec (and therefore no template in it, we will add
|
||||||
|
# before the first element
|
||||||
|
else:
|
||||||
|
self.current_wikicode.insert( 0, notice )
|
||||||
|
|||||||
Reference in New Issue
Block a user