Skip to content

Commit d91eb32

Browse files
author
maskara
committed
Reuse userId in all queries
1 parent 916b18f commit d91eb32

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

feedback.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def thanksReceived(username):
1818
data = cur.fetchall()
1919
return data[0][0]
2020

21-
def featuredImages(username):
21+
def featuredImages(userId):
2222
awards = ['Featured_pictures_on_Wikimedia_Commons', 'Quality_images']
2323
sqlins = []
2424
for award in awards:
2525
sqlins.append('"' + award + '"')
2626
sqlin = ", ".join(sqlins)
2727
with conn.cursor() as cur:
28-
sql = 'select count(cl_from), cl_to from categorylinks where cl_to in (%s) and cl_type="file" and cl_from in (select log_page from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s")) group by cl_to;' % (sqlin, username)
28+
sql = 'select count(cl_from), cl_to from categorylinks where cl_to in (%s) and cl_type="file" and cl_from in (select log_page from logging_userindex where log_type="upload" and log_user=%d) group by cl_to;' % (sqlin, userId)
2929
cur.execute(sql)
3030
data = cur.fetchall()
3131
response = {}
@@ -36,23 +36,23 @@ def featuredImages(username):
3636
response[award] = 0
3737
return response
3838

39-
def articlesUsingImages(username):
39+
def articlesUsingImages(userId):
4040
with conn.cursor() as cur:
41-
sql = 'select count(*) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s"));' % username
41+
sql = 'select count(*) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=%d);' % userId
4242
cur.execute(sql)
4343
data = cur.fetchall()
4444
return data[0][0]
4545

46-
def uniqueUsedImages(username):
46+
def uniqueUsedImages(userId):
4747
with conn.cursor() as cur:
48-
sql = 'select count(distinct gil_to) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s"));' % username
48+
sql = 'select count(distinct gil_to) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=%d);' % userId
4949
cur.execute(sql)
5050
data = cur.fetchall()
5151
return data[0][0]
5252

53-
def imagesEditedBySomeoneElse(username):
53+
def imagesEditedBySomeoneElse(userId):
5454
with conn.cursor() as cur:
55-
sql = 'select count(*) from revision where rev_page in (select log_page from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s")) and rev_user!=(select user_id from user where user_name="%s") group by rev_page having count(*)>1' % (username, username)
55+
sql = 'select count(*) from revision where rev_page in (select log_page from logging_userindex where log_type="upload" and log_user=%d) and rev_user!=%d group by rev_page having count(*)>1' % (userId, userId)
5656
cur.execute(sql)
5757
data = cur.fetchall()
5858
return len(data)
@@ -64,6 +64,13 @@ def deletedUploads(username):
6464
data = cur.fetchall()
6565
return data[0][0]
6666

67+
def getUserId(username):
68+
with conn.cursor() as cur:
69+
sql = 'select user_id from user where user_name="%s";' % username
70+
cur.execute(sql)
71+
data = cur.fetchall()
72+
return data[0][0]
73+
6774
#Print header
6875
print 'Content-type: application/json\n'
6976

@@ -103,16 +110,19 @@ def deletedUploads(username):
103110
'status': 'ok',
104111
'user': user,
105112
}
113+
114+
userid = getUserId(user)
115+
106116
if 'thanksReceived' in fetch:
107117
response['thanksReceived'] = thanksReceived(user)
108118
if 'featuredImages' in fetch:
109-
response['featuredImages'] = featuredImages(user)
119+
response['featuredImages'] = featuredImages(userid)
110120
if 'articlesUsingImages' in fetch:
111-
response['articlesUsingImages'] = articlesUsingImages(user)
121+
response['articlesUsingImages'] = articlesUsingImages(userid)
112122
if 'uniqueUsedImages' in fetch:
113-
response['uniqueUsedImages'] = uniqueUsedImages(user)
123+
response['uniqueUsedImages'] = uniqueUsedImages(userid)
114124
if 'imagesEditedBySomeoneElse' in fetch:
115-
response['imagesEditedBySomeoneElse'] = imagesEditedBySomeoneElse(user)
125+
response['imagesEditedBySomeoneElse'] = imagesEditedBySomeoneElse(userid)
116126
if 'deletedUploads' in fetch:
117127
response['deletedUploads'] = deletedUploads(user)
118128

0 commit comments

Comments
 (0)