root / other-scripts / D6-to-D7 / fix-images-expat.py @ 6c38d829
1 |
#!/usr/bin/env python3
|
---|---|
2 |
|
3 |
# In the expat website, when we try to migarte it, the images not found
|
4 |
# and the only solution we found is to use this script to patch its database
|
5 |
# and so repair the links
|
6 |
|
7 |
import pymysql as mysql |
8 |
import re |
9 |
|
10 |
db = mysql.connect(host="localhost", user="root", passwd="tata", db="expat", charset='utf8') |
11 |
|
12 |
with db:
|
13 |
cur = db.cursor() |
14 |
|
15 |
|
16 |
## Update user's pictures
|
17 |
sql_select = """
|
18 |
SELECT uid, picture FROM users
|
19 |
"""
|
20 |
|
21 |
sql_update = """
|
22 |
UPDATE users SET picture = %s WHERE uid = %s
|
23 |
"""
|
24 |
|
25 |
cur.execute(sql_select) |
26 |
rows = cur.fetchall() |
27 |
for uid, picture in rows: |
28 |
picture = re.sub(r'sites/assos.centrale-marseille.fr.expat/files/(.*)', r'\1', picture) |
29 |
cur.execute(sql_update, (picture, uid)) |
30 |
|
31 |
|
32 |
## Update other files
|
33 |
sql_select = """
|
34 |
SELECT fid, filepath FROM files
|
35 |
"""
|
36 |
|
37 |
sql_update = """
|
38 |
UPDATE files SET filepath = %s WHERE fid = %s
|
39 |
"""
|
40 |
|
41 |
cur.execute(sql_select) |
42 |
rows = cur.fetchall() |
43 |
for fid, file_path in rows: |
44 |
file_path = re.sub(r'sites/assos.centrale-marseille.fr.expat/files/(.*)', r'\1', file_path) |
45 |
cur.execute(sql_update, (file_path, fid)) |
46 |
|
47 |
db.commit() |