Skip to content

Instantly share code, notes, and snippets.

@harej

harej/app.py Secret

Created September 5, 2023 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harej/875d3e407f155be82fd1664ab34da36f to your computer and use it in GitHub Desktop.
Save harej/875d3e407f155be82fd1664ab34da36f to your computer and use it in GitHub Desktop.
Wikibase Registry to Wikibase World redirect Flask app
from flask import Flask, jsonify, redirect, request
app = Flask(__name__)
app.debug = True
# "X" is used for entities that were dropped during transition
# None is used for entity IDs that were never assigned to begin with
mappings = {
"Q1": None,
"Q2": "Q1",
"Q3": "Q56",
"Q4": "Q58",
"Q5": "Q29",
"Q6": "Q26",
"Q7": "Q63",
"Q8": "Q65",
"Q9": "Q64",
"Q10": "Q65",
"Q11": "Q66",
"Q12": "Q67",
"Q13": "Q71",
"Q14": "Q73",
"Q15": "Q41",
"Q16": "Q45",
"Q17": "Q30",
"Q18": "Q50",
"Q19": "Q34",
"Q20": "Q10",
"Q21": "Q9",
"Q22": "Q116",
"Q23": None,
"Q24": "Q74",
"Q25": "Q75",
"Q26": "Q39",
"Q27": "Q51",
"Q28": "Q52",
"Q29": "Q53",
"Q30": "Q120",
"Q31": "Q55",
"Q32": "Q77",
"Q33": "Q39",
"Q34": "Q76",
"Q35": "Q78",
"Q36": "Q79",
"Q37": "Q40",
"Q38": "Q80",
"Q39": "Q2",
"Q40": "Q81",
"Q41": "Q82",
"Q42": None,
"Q43": None,
"Q44": "Q83",
"Q45": "Q97",
"Q46": "Q95",
"Q47": "Q95",
"Q48": "Q95",
"Q49": "Q98",
"Q50": "Q96",
"Q51": "Q51",
"Q52": "Q133",
"Q53": "Q132",
"Q54": "X",
"Q55": "Q159",
"Q56": "Q164",
"Q57": "Q138",
"Q58": "Q160",
"Q59": "Q169",
"Q60": "Q163",
"Q61": "Q68",
"Q62": "Q69",
"Q63": "Q92",
"Q64": "Q139",
"Q65": "Q143",
"Q66": "Q165",
"Q67": "Q167",
"Q68": "Q194",
"Q69": "Q84",
"Q70": "Q144",
"Q71": "Q166",
"Q72": "X",
"Q73": "Q151",
"Q74": "Q170",
"Q75": "Q131",
"Q76": "Q145",
"Q77": "Q152",
"Q78": "Q44",
"Q79": "Q130",
"Q80": "Q168",
"Q81": "Q201",
"Q82": "Q196",
"Q83": "Q129",
"Q84": "Q154",
"Q85": "Q146",
"Q86": "Q197",
"Q87": "Q156",
"Q88": "Q199",
"Q89": "Q161",
"Q90": "Q162",
"Q91": "Q198",
"Q92": "X",
"Q93": "X",
"Q94": "Q155",
"Q95": "Q158",
"Q96": "Q157",
"Q97": "X",
"Q98": "X",
"Q99": "Q121",
"Q100": "Q153",
"Q101": "X",
"Q102": "Q93",
"Q103": "Q94",
"Q104": "Q61",
"Q105": "Q59",
"Q106": "Q60",
"Q107": "Q122",
"Q108": "Q100",
"Q109": "Q123",
"Q110": "Q124",
"Q111": "Q125",
"Q112": "Q126",
"Q113": "Q127",
"Q114": "Q128",
"Q115": "Q102",
"Q116": "Q101",
"Q117": "Q99",
"Q118": "Q22",
"Q119": "Q147",
"Q120": "Q148",
"Q121": "Q95",
"Q122": "Q86",
"Q123": "Q28",
"Q124": "Q89",
"Q125": "Q135",
"Q126": "Q43",
"Q127": "Q104",
"Q128": "Q24",
"Q129": "Q140",
"Q130": "Q105",
"Q131": "Q91",
"Q132": "Q62",
"Q133": "Q134",
"Q134": "Q106",
"Q135": "Q107",
"Q136": "Q33",
"Q137": "Q108",
"Q138": "Q109",
"Q139": "Q110",
"Q140": "Q37",
"Q141": "Q111",
"Q142": "Q85",
"Q143": "Q112",
"Q144": "Q113",
"Q145": "Q141",
"Q146": "Q142",
"Q147": "Q103",
"Q148": "Q90",
"Q149": "Q114",
"Q150": "Q115",
"Q151": "Q19",
"Q152": "Q20",
"P1": None,
"P2": "P1",
"P3": "P7",
"P4": "P8",
"P5": "P5",
"P6": "P9",
"P7": "P21",
"P8": "P6",
"P9": "P10",
"P10": "P11",
"P11": "P3",
"P12": "P12",
"P13": None,
"P14": "P27",
"P15": "P34",
"P16": "P30",
"P17": "X",
"P18": "P33",
"P19": "P26",
"P20": "P29",
"P21": "P17",
"P22": "P25",
"P23": None,
"P24": "X",
"P25": "P24",
"P26": "P20",
"P27": "P14",
"P28": "P29",
"P29": "X",
"P30": "X",
"P31": "P23",
"P32": "P4",
"P33": "P15",
"P34": "P1",
"P35": "P5",
"P36": "P22",
"P37": "P13",
"P38": "P4"
}
NEW_DOMAIN = "https://wikibase.world"
def go_home():
return redirect(NEW_DOMAIN, code=301)
@app.route('/wiki/Special:EntityData/<path:path>')
def entity_data_redirect(path):
id_from_path = path.split('.')[0]
for old_id, new_id in mappings.items():
if old_id == id_from_path:
if new_id in ['X', None]:
return go_home()
else:
path = path.replace(old_id, new_id)
break
return redirect(f"{NEW_DOMAIN}/wiki/Special:EntityData/{path}", code=301)
@app.route('/entity/<path:path>')
def entity_redirect(path):
for old_id, new_id in mappings.items():
if old_id == path:
if new_id in ['X', None]:
return go_home()
else:
path = path.replace(old_id, new_id)
break
return redirect(f"{NEW_DOMAIN}/entity/{path}", code=301)
@app.route('/w/index.php')
def index_php_redirect():
args = request.args.copy()
title = args.get('title')
if title is None:
return go_home()
title = title.replace('Item:', '')
title = title.replace('Property:', '')
if title in mappings:
t = mappings[title]
if t in ['X', None]:
return go_home()
if t.startswith('P'):
t = "Property:" + t
elif t.startswith('Q'):
t = "Item:" + t
args['title'] = t
query_string = '&'.join([f"{key}={value}" for key, value in args.items()])
return redirect(f"{NEW_DOMAIN}/w/index.php?{query_string}", code=301)
@app.route('/wiki/<path:path>')
def generic_redirect(path):
if path == '' or path is None:
return go_home()
if path.startswith('Item:') or path.startswith('Property:'):
path = path.replace('Item:', '').replace('Property:', '')
for old_id, new_id in mappings.items():
if old_id == path:
if new_id in ['X', None]:
return go_home()
path = path.replace(old_id, new_id)
break
if path.startswith('Q'):
path = 'Item:' + path
elif path.startswith('P'):
path = 'Property:' + path
return redirect(f"{NEW_DOMAIN}/wiki/{path}", code=301)
@app.route('/mapping')
def print_mappings():
filtered_mappings = {key: value for key, value in mappings.items()
if value != "X" and value is not None}
return jsonify(filtered_mappings)
@app.route('/dropped')
def print_dropped():
filtered_mappings = [key for key, value in mappings.items() if value == "X"]
return jsonify(filtered_mappings)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
return go_home()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8989)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment