വിക്കിപീഡിയ:ഉപയോക്താക്കളുടെ പട്ടിക - തിരുത്തലുകളുടെ അടിസ്ഥാനത്തിൽ/സജ്ജീകരണം

വിക്കിപീഡിയ, ഒരു സ്വതന്ത്ര വിജ്ഞാനകോശം.
Jump to navigation Jump to search

editcount.py[തിരുത്തുക]

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Public domain; MZMcBride; 2011
import datetime 
import re 
import pymysql 
import wikitools 
import settings 
def thousands(x): # From http://code.activestate.com/recipes/498181/
    return re.sub(r'(\d{3})(?=\d)', r'\1,', str(x)[::-1])[::-1] 
def get_user_list(cursor, page):
    users = []
    cursor.execute('''/* editcount.py SLOW_OK */
                   SELECT DISTINCT
                     pl_title
                   FROM page
                   JOIN pagelinks
                   ON pl_from = page_id
                   WHERE page_title = %s
                   AND page_namespace = 4
                   AND pl_namespace IN (2,3);''', page)
    for row in cursor.fetchall():
        user_name = u'%s' % unicode(row[0].replace('_', ' '), 'utf-8')
        users.append(user_name)
    return users 
def get_bot_list(cursor):
    bots = []
    cursor.execute('''/* editcount.py SLOW_OK */
                   SELECT
                     user_name
                   FROM user
                   JOIN user_groups
                   ON user_id = ug_user
                   WHERE ug_group = 'bot';
                   ''')
    for row in cursor.fetchall():
        user_name = u'%s' % unicode(row[0].replace('_', ' '), 'utf-8')
        bots.append(user_name)
    return bots 
def get_recently_active_users(cursor):
    recently_active_users = set()
    cursor.execute('''/* editcount.py SLOW_OK */
                   SELECT
                     DISTINCT actor_user
                   FROM actor
                   JOIN recentchanges
                   ON rc_actor = actor_id;''')
    results = cursor.fetchall()
    for result in results:
        if result[0]:
            recently_active_users.add(int(result[0]))
    return recently_active_users 
def get_user_groups(cursor, user):
    groups = []
    cursor.execute('''/* editcount.py SLOW_OK */
                   SELECT
                     GROUP_CONCAT(ug_group)
                   FROM user_groups
                   JOIN user
                   ON user_id = ug_user
                   WHERE user_name = %s;''', 
	user.encode('utf-8'))
    result = cursor.fetchone()
    if result[0]:
        user_groups = result[0].split(',')
        for group in user_groups:
            group = '{{subst:aug|1=%s}}' % unicode(group, 'utf-8')
            groups.append(group)
            user_groups = ', '.join(groups)
    else:
        user_groups = ''
    return user_groups 
base_page = 'വിക്കിപീഡിയ:ഉപയോക്താക്കളുടെ പട്ടിക - തിരുത്തലുകളുടെ അടിസ്ഥാനത്തിൽ/' 
ranges = [u'1\u20131000',  u'1001\u20132000'] 
age_page = 'Age' 
report_template = u'''=== %s === 
{| class="wikitable"
|- style="white-space:nowrap;"
!  
! ഉപയോക്താവ്
! തിരുത്തലുകളുടെ എണ്ണം 
! ഉപയോക്തൃസംഘങ്ങൾ
|-
%s
|}''' 
conn = pymysql.connect(host='mlwiki.labsdb',db='mlwiki_p',read_default_file='~/replica.my.cnf',charset='utf8mb4',) 
cursor = conn.cursor() 
unflagged_bots = get_user_list(cursor, 'വിക്കിപീഡിയ:ഉപയോക്താക്കളുടെ പട്ടിക - തിരുത്തലുകളുടെ അടിസ്ഥാനത്തിൽ/Unflagged_bots') 
flagged_bots = get_bot_list(cursor) 
opt_out = get_user_list(cursor, 'വിക്കിപീഡിയ:ഉപയോക്താക്കളുടെ പട്ടിക - തിരുത്തലുകളുടെ അടിസ്ഥാനത്തിൽ/Anonymous') 
recently_active = get_recently_active_users(cursor) 
cursor.execute(''' /* editcount.py SLOW_OK */ SELECT user_id,user_name,user_editcount FROM user WHERE user_editcount > 0 ORDER BY user_editcount DESC LIMIT 12000; ''') 
i = 1 
output = [] 
for row in cursor.fetchall():
    user_id = int(row[0])
    user_name = u'%s' % unicode(row[1], 'utf-8')
    just_user_name = user_name
    if user_name in unflagged_bots or user_name in flagged_bots:
        continue
    elif user_name in opt_out:
        user_name = '[[User:%s|%s]]' % (user_name,user_name)
    elif user_id in recently_active:
        user_name = u'[[User:%s|%s]]' % (user_name, user_name)
    user_editcount = thousands(str(row[2]))
    if just_user_name not in opt_out and just_user_name not in unflagged_bots:
        user_groups = get_user_groups(cursor, just_user_name)
    else:
        user_groups = ''
    table_row = u'''|%d
|%s
|%s
|%s 
|-''' % (i, user_name, user_editcount, user_groups)
    output.append(table_row)
    i += 1 
cursor.execute('''SELECT
                 UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp)
               FROM recentchanges
               ORDER BY rc_timestamp DESC
               LIMIT 1;''') 
rep_lag = int(cursor.fetchone()[0]) 
time_diff = datetime.datetime.utcnow() - datetime.timedelta(seconds=rep_lag) 
current_of = time_diff.strftime('%H:%M, %d %B %Y (UTC)') 
wiki = wikitools.Wiki(settings.apiurl) 
wiki.login(settings.username,settings.password) 
start = 0 
end = 1000 
per_page = 1000 
for range in ranges:
    report = wikitools.Page(wiki, base_page+range.encode('utf-8'))
    report_text = report_template % (range, '\n'.join(output[start:end]))
    report_text = report_text.encode('utf-8')
    report.edit(report_text,settings.editsumm, bot=1)
    start += per_page
    end += per_page 
age = wikitools.Page(wiki, base_page+age_page) 
age_text = current_of.encode('utf-8') 
age.edit(age_text,summary=settings.editsumm, bot=1) 
cursor.close()
conn.close()

settings.py[തിരുത്തുക]

username = 'Adithyakbot'
password = '[...]'
editsumm = '[[WP:BOT|Bot]]: Updated page.'
dbname = 'mlwiki_p'
host = 'mlwiki.labsdb'
rootpage = 'Wikipedia:Database reports/'
apiurl = 'https://ml.wikipedia.org/w/api.php'

crontab[തിരുത്തുക]

0 0 * * * /usr/bin/jsub -N tools.mlwikidbupdate -once python /mnt/nfs/labstore-secondary-tools-project/mlwikidbupdate/editcount.py

See also[തിരുത്തുക]

A set of instructions for generating this report for/on other Wikimedia wikis:

Old pages kept for historical interest, perhaps: