#!/usr/bin/python3 import sys, re import bz2, html, urllib.parse import sqlite3 usageInfo = f"usage: {sys.argv[0]}\n" usageInfo += "For a set of page-ids, looks up their content in an enwiki dump,\n" usageInfo += "trying to get infobox image filenames, adding info to an sqlite db.\n" if len(sys.argv) > 1: print(usageInfo, file=sys.stderr) sys.exit(1) def getInputPageIds(): pageIds = set() dbCon = sqlite3.connect("../data.db") dbCur = dbCon.cursor() for (pageId,) in dbCur.execute("SELECT id from wiki_ids"): pageIds.add(pageId) dbCon.close() return pageIds dumpFile = "enwiki-20220501-pages-articles-multistream.xml.bz2" indexDb = "dumpIndex.db" imgDb = "enwikiImgs.db" # Output db idLineRegex = re.compile(r"(.*)") imageLineRegex = re.compile(r".*\| *image *= *([^|]*)") bracketImageRegex = re.compile(r"\[\[(File:[^|]*).*]]") imageNameRegex = re.compile(r".*\.(jpg|jpeg|png|gif|tiff|tif)", flags=re.IGNORECASE) cssImgCropRegex = re.compile(r"{{css image crop\|image *= *(.*)", flags=re.IGNORECASE) # Open dbs indexDbCon = sqlite3.connect(indexDb) indexDbCur = indexDbCon.cursor() imgDbCon = sqlite3.connect(imgDb) imgDbCur = imgDbCon.cursor() # Create image-db table imgDbCur.execute("CREATE TABLE page_imgs (page_id INT PRIMARY KEY, img_name TEXT)") imgDbCur.execute("CREATE INDEX page_imgs_idx ON page_imgs(img_name)") # Get input pageIds print("Getting input page-ids", file=sys.stderr) pageIds = getInputPageIds() # Get page-id dump-file offsets print("Getting dump-file offsets", file=sys.stderr) offsetToPageids = {} offsetToEnd = {} iterNum = 0 for pageId in pageIds: iterNum += 1 if iterNum % 1e4 == 0: print(f"At iteration {iterNum}", file=sys.stderr) # query = "SELECT offset, next_offset FROM offsets WHERE id = ?" row = indexDbCur.execute(query, (pageId,)).fetchone() if row == None: print(f"WARNING: Page id {pageId} not found", file=sys.stderr) continue (chunkOffset, endOffset) = row offsetToEnd[chunkOffset] = endOffset if chunkOffset not in offsetToPageids: offsetToPageids[chunkOffset] = [] offsetToPageids[chunkOffset].append(pageId) print(f"Found {len(offsetToEnd)} chunks to check", file=sys.stderr) # Look through dump file, jumping to chunks containing relevant pages print("Reading through dump file", file=sys.stderr) def getImageName(content): """ Given an array of text-content lines, returns an image-filename, or None """ for line in content: match = imageLineRegex.match(line) if match != None: imageName = match.group(1).strip() if imageName == "": return None imageName = html.unescape(imageName) # Account for {{... if imageName.startswith("{"): match = cssImgCropRegex.match(imageName) if match == None: return None imageName = match.group(1) # Account for [[File:...|...]] if imageName.startswith("["): match = bracketImageRegex.match(imageName) if match == None: return None imageName = match.group(1) # Account for