-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki_db.py
296 lines (218 loc) · 11.1 KB
/
wiki_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import pymysql
import os
from config import IS_DEV
import yaml
from time import sleep
class WikiDB:
conn = None
reachedMaxStatementTime = False
langFrom = 'en'
langTo = 'lv'
oneChunkSize = 50
def __init__(self, languageFrom: str, languageTo: str):
self.langFrom = languageFrom
self.langTo = languageTo
self.connect()
def formatCategory(self, inputStr):
return inputStr.replace(' ','_')
def encode_if_necessary(self, b):
if type(b) is bytes:
return b.decode('utf8')
return b
def whereIn(self, arguments):
try:
return ','.join(['%s']*len(arguments))
except:
print('whereIn ex ',arguments)
exit()
def connect(self):
try:
if IS_DEV:
config = yaml.safe_load(open('db_local.yaml'))
self.conn = pymysql.connect(host=config['host'], user=config['user'], password=config['password'], port=config['port'], database=self.langFrom+'wiki_p', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
else:
self.conn = pymysql.connect( database=self.langFrom+'wiki_p', host=self.langFrom+'wiki.web.db.svc.eqiad.wmflabs', read_default_file=os.path.expanduser("~/replica.my.cnf"), charset='utf8mb4' , cursorclass=pymysql.cursors.DictCursor)
with self.conn.cursor() as cursor:
cursor.execute('SET SESSION MAX_STATEMENT_TIME=300;')#5 minutes = 300
except pymysql.Error as e:
print('e:', e)
exit()
def runQuery(self, sql: str, params: tuple = (), maxTries: int = 2) -> dict:
if maxTries == 0:
return []
try:
with self.conn.cursor() as cursor:
cursor.execute(sql, params)
result = cursor.fetchall()
return result
except pymysql.Error as e:
errorCode = str(e)
if 'Query execution was interrupted' in errorCode:#Query execution was interrupted (max_statement_time exceeded)
self.reachedMaxStatementTime = True
return []
if 'max_user_connections' in errorCode:#User %s already has more than 'max_user_connections' active connections
import random
sleep(random.uniform(10, 60))
return self.runQuery(sql, params, maxTries-1)
return []
def getSubcategories(self, rootcategories: list, currentCategoryList: list, depth: int):
if depth == 0:
return currentCategoryList
sqlTemplate = "SELECT page_title FROM categorylinks cl join page p on p.page_id=cl.cl_from WHERE cl_to IN ({}) AND cl_type='subcat'"
categoriesPreparedForSQL = [self.formatCategory(f) for f in rootcategories]
if len(categoriesPreparedForSQL) == 0:
return []
sqlTemplateFormatted = sqlTemplate.format(self.whereIn(categoriesPreparedForSQL))
result = self.runQuery(sqlTemplateFormatted, tuple(categoriesPreparedForSQL))
if len(result) == 0:
return currentCategoryList
currLevel = [self.encode_if_necessary(f['page_title']) for f in result]
currentCategoryList.extend(currLevel)
depth -= 1
return self.getSubcategories(currLevel, currentCategoryList, depth)
def handleCategory(self, category, depth):
formattedCategory = self.formatCategory(category)
subcategories = self.getSubcategories([formattedCategory], [formattedCategory], int(depth))
sqlSubquery = 'select l.ll_from from categorylinks cla where cla.cl_type="page" and l.ll_from=cla.cl_from and cla.cl_to in ({})'
sqlSubqueriesParams = subcategories
return {
'query':sqlSubquery.format(self.whereIn(subcategories)),
'params': sqlSubqueriesParams
}
def handleTemplate(self, title):
formattedCategory = self.formatCategory(title)
sqlSubquery = 'select l.ll_from from templatelinks tl where l.ll_from=tl.tl_from and tl_namespace=10 and tl.tl_from_namespace=0 and tl.tl_title=%s'
return {
'query':sqlSubquery,
'params': [formattedCategory]
}
def makeOneLanglinksSubquery(self, subQuery, subParams):
tpl = """select ll_from, count(l.ll_lang) as langs
from langlinks l
where not exists (select * from langlinks m where m.ll_from=l.ll_from and m.ll_lang=%s)
and exists ({})
group by l.ll_from"""
finalQuery = tpl.format(subQuery)
finalParams = [self.langTo]
finalParams.extend(subParams)
return {
'query':finalQuery,
'params': finalParams
}
def getLanglinksForPageIds(self, pages):
tpl = """select ll_from, count(l.ll_lang) as langs
from langlinks l
where not exists (select * from langlinks m where m.ll_from=l.ll_from and m.ll_lang=%s)
and ll_from in ({})
group by l.ll_from"""
finalQuery = tpl.format(self.whereIn(pages))
finalParams = [self.langTo]
finalParams.extend(pages)
result = self.runQuery(finalQuery, tuple(finalParams))
return {f['ll_from']:f['langs'] for f in result}
def handlePetscan(self, id):
import requests, json
response = requests.get('http://petscan.wmflabs.org/?psid={}&format=json'.format(id))
if not response.status_code == 200:
return {}
try:
data = response.json()['*'][0]['a']['*']
pageIds = [f['id'] for f in data]
chunks = self.chunker(pageIds, self.oneChunkSize)
idMappingToTitles = {}
for chunk in chunks:
currRes = self.getLanglinksForPageIds(chunk)
idMappingToTitles.update(currRes)
except:
return {}
return idMappingToTitles
def mainLanglinksQuery(self, params):
tpl = """select ll_from, langs from ({}) tablea order by langs desc limit 500"""
allInfo = []
if 'category' in params:
allInfo.extend([self.makeOneLanglinksSubquery(categoryInfo['query'], categoryInfo['params']) for categoryInfo in params['category']])
if 'template' in params:
allInfo.extend([self.makeOneLanglinksSubquery(templateInfo['query'], templateInfo['params']) for templateInfo in params['template']])
allSubqueriesData = allInfo
res = {}
if len(allSubqueriesData)>0:
allSubqueries = [f['query'] for f in allSubqueriesData]
allParams = [f['params'] for f in allSubqueriesData]
finalQuery = tpl.format(" union ".join(allSubqueries))
finalParams = [item for sublist in allParams for item in sublist]
result = self.runQuery(finalQuery, tuple(finalParams))
res = {f['ll_from']:f['langs'] for f in result}
if 'petscan' in params:
for petscanInfo in params['petscan']:
petscanResult = self.handlePetscan(petscanInfo)
res.update(petscanResult)
return res
''' def mainLanglinksQuery(self, categoryInfo, templateInfo):
tpl = """select ll_from, count(l.ll_lang) as langs
from langlinks l
where not exists (select * from langlinks m where m.ll_from=l.ll_from and m.ll_lang=%s)
and ({})
group by l.ll_from
order by count(l.ll_lang) desc
limit 500"""
subQuery = "exists ({}) or exists ({})".format(categoryInfo['query'], templateInfo['query'])
finalQuery = tpl.format(subQuery)
finalParams = [self.langTo]
finalParams.extend(categoryInfo['params'])
finalParams.extend(templateInfo['params'])
print(finalQuery)
print('params',finalParams)
#exit()
result = self.runQuery(finalQuery, tuple(finalParams))
print('query finished')
exit()
return {f['ll_from']:f['langs'] for f in result} '''
def chunker(self, seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def getPageInfoForPages(self, pages):
sqlTpl = """select page_id as pageId, page_title as title from page p where p.page_namespace=0 and p.page_is_redirect=0 and p.page_id in ({})"""
sql = sqlTpl.format(self.whereIn(pages))
result = self.runQuery(sql, tuple(pages))
return {f['pageId']:self.encode_if_necessary(f['title']) for f in result}
def getPageInfoInBatches(self, pages):
chunks = self.chunker(pages, self.oneChunkSize)
idMappingToTitles = {}
for chunk in chunks:
currRes = self.getPageInfoForPages(chunk)
idMappingToTitles.update(currRes)
return idMappingToTitles
def main(self, inputData):# = { 'title': '1957 births', 'depth': 5, 'tplTitle': 'Infobox park' }
#return [['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48],['Shavkat_Mirziyoyev', 62], ['Jackie_Shroff', 61], ['Bernie_Mac', 56], ['Melanie_Griffith', 56], ['Wolfgang_Ketterle', 56], ['Goodluck_Jonathan', 54], ['Dani_Rodrik', 52], ['Mukesh_Ambani', 48]]
subQueryParams = {'category':[], 'template':[], 'petscan': []}
for reqParam in inputData:
reqType = reqParam['type']
reqSpecifics = reqParam['specific']
if reqType == 'category':
subQueryParams['category'].append(self.handleCategory(reqSpecifics['title'], reqSpecifics['depth']))
elif reqType == 'template':
subQueryParams['template'].append(self.handleTemplate(reqSpecifics['title']))
elif reqType == 'petscan':
subQueryParams['petscan'].append(reqSpecifics['id'])
#categoryInfo = self.handleCategory(inputData['title'], inputData['depth'])
#templateInfo = self.handleTemplate(inputData['tplTitle'])
pageIdMappingToLanglinks = self.mainLanglinksQuery(subQueryParams)
pageIDs = list(pageIdMappingToLanglinks)
pageIdMappingToTitles = self.getPageInfoInBatches(pageIDs)
finalResult = []
for pageID in pageIdMappingToLanglinks:
if pageID not in pageIdMappingToTitles:#it may be non-article page ...or a bug
#print("not found: ",pageID)
continue
finalResult.append([pageIdMappingToTitles[pageID], pageIdMappingToLanglinks[pageID]])
#
finalResult.sort(key=lambda x: (-x[1], x[0]))
#print(finalResult)
return {'data':finalResult,'wasMaxStatementTime':self.reachedMaxStatementTime}
#return self.fromCategory
#
#inst = WikiDB('en','lv')
""" inst.main([
{ 'type': 'category', 'specific': { 'title': '1957 births', 'depth': 0, 'talk': False } },
{ 'type': 'template', 'specific': { 'title': 'Infobox park', 'talk': False } },
{ 'type': 'petscan', 'specific': { 'id': '' } }
]) """#'from':'en','to':'lv', 'ignoreCache': True,