Szerkesztő:BinBot/documentwritewarning.py

A Wikipédiából, a szabad enciklopédiából

Ez a script 2015. augusztus 6-án este futott.

# -*- coding: utf-8 -*-
"""
Warns users who have document.write in their .js files.
This is just a quick disposable solution, don't expect nice code!
"""

#
# (C) Bináris, 2015
#
# Distributed under the terms of the MIT license.

import pywikibot
from pagegenerators import TextfilePageGenerator
import replace

# Write here the section title of the warning:
sectionTitle = u'Elavult "document.write" van a javascriptedben!'
# Edit summary with the "new section" in your language:
editSummary = u'/* %s */ (új szakasz)' % sectionTitle
# This text is what you want to say to users in your wiki.
# Title of the .js page will be substituted.
warntext = u"""\n
== %s ==
A [[%s]] oldalad a ''document.write'' függvényt tartalmazza, aminek a támogatása
a MediaWiki szoftverben megszűnt, és működési problémákat okozhat. Erősen javasolt az eltávolítása vagy lecserélése.

*További információk [%s].
*[https://hu.wikipedia.org/w/index.php?title=MediaWiki:Gadget-revisionjumper.js&curid=803742&diff=16350274&oldid=15232001 Egy lehetséges javítási minta] (Nem biztos, hogy neked is működik.)

Tgr megbízásából és Bináris közreműködésével: ~~~~
"""
# I need this one because of % characters, for you it might be ''
innerString = u'https://hu.wikipedia.org/w/index.php?title=Wikip%C3%A9dia:Botgazd%C3%A1k_%C3%BCzen%C5%91fala&oldid=16352211#document.write-talan.C3.ADt.C3.A1s a WP:BÜ-n'
myFile = 'jsfail.txt'
replace.main(
    "-ns:2", "-search:document.write", "-regex", "-requiretitle:.+\\/.+\\.js$", 
    "document.write", "dummy", "-savenew:%s" % myFile, "-always")


site = pywikibot.getSite()
gen = TextfilePageGenerator(myFile, site)
for page in gen:
    pywikibot.output(page.title())
    target = pywikibot.Page(
                site,
                page.title().split('/')[0]
                ).toggleTalkPage()
    while target.isRedirectPage():
        target = target.getRedirectTarget()
        # Might be wrong if the talk page is redirected to the user page for a blocked user.
    pywikibot.output(target.title())
    try:
        text = target.get() + (warntext % (sectionTitle, page.title(), innerString))
        try:
            target.put(text, editSummary, minorEdit=False)
        except:
            pass
    except:
        pass