Projet

Général

Profil

Paste
Télécharger (5,27 ko) Statistiques
| Branche: | Révision:

root / other-scripts / D6-to-D7 / fix-migration-expat.py @ a500a624

1
#!/usr/bin/env python3
2

    
3
import pymysql as mysql
4
import re
5

    
6
db = mysql.connect(host="localhost", user="root", passwd="tata", db="expatd7", charset='utf8')
7

    
8
with db:
9
    cur = db.cursor()
10
    # We fix the text format for comments and nodes
11
    update_text_format = "UPDATE {table} SET {format} = 'filtered_html' WHERE {format} = -1"
12
    cur.execute(update_text_format.format(table='field_data_body', format='body_format'))
13
    cur.execute(update_text_format.format(table='field_data_comment_body', format='comment_body_format'))
14

    
15
    ### We fix the images
16
    fetch_d6_infos = """
17
    SELECT node.title, files.filepath
18
    FROM node
19
    JOIN image ON node.nid = image.nid
20
    JOIN files ON files.fid = image.fid
21
    WHERE image.image_size =  '_original'"""
22

    
23
    # We prepare the infos from D6
24
    results = []
25
    rows = ()
26

    
27
    db_d6 = mysql.connect(host="localhost", user="root", passwd="tata", db="expat", charset="utf8")
28
    with db_d6:
29
        cur_d6 = db_d6.cursor()
30
        cur_d6.execute(fetch_d6_infos)
31
        rows = cur_d6.fetchall()
32

    
33
        for row in rows:
34
            results.append(list(row))
35

    
36
        for r in results:
37
            # filepath is sites/default/files/… instead of …
38
            r[1] = re.sub(r'sites/default/files/(.*)', r'\1', r[1])
39

    
40
    # To find the nid and fid in D7
41
    fetch_nid = """
42
    SELECT nid FROM node WHERE title = %s
43
    """
44
    fetch_fid = """
45
    SELECT fid FROM file_managed WHERE uri = %s
46
    """
47

    
48
    # We do the update
49
    update_file_usage = """INSERT INTO file_usage (fid, module, type, id, count)
50
        VALUES (%s, 'node', 'file', %s, 1)"""
51

    
52
    update_field_data_image = """
53
    INSERT INTO field_data_field_image
54
    (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_image_fid, field_image_alt, field_image_title, field_image_width, field_image_height)
55
    VALUES ('node', 'image_dans_la_galerie', 0, %s, %s, 'und', 0, %s, '', '', 275, 183)
56
    """
57

    
58
    for r in results:
59
        cur.execute(fetch_nid, (r[0]))
60
        nid = cur.fetchall()[0][0] # returns something like ((133L,),)
61

    
62
        cur.execute(fetch_fid, ('public://' + r[1]))
63
        fid = cur.fetchall()[0][0]
64

    
65
        cur.execute(update_file_usage, (fid, nid))
66
        cur.execute(update_field_data_image, (nid, nid, fid))
67

    
68

    
69
    ### We fix the taxonomy
70
    vid2termName_d6 = {3: 'Image Galleries', 4: 'Pays', 5: 'Tags', 6: 'Université partenaire'}
71
    vid2termName_d7 = {1: 'Tags', 2: 'Pays', 3: 'Université partenaire', 4: 'Image Galleries'}
72
    termName2vid_d7 = {value: key for key, value in vid2termName_d7.items()}
73
    vocabulary2table = {'Tags': 'field_data_field_tags', 'Pays': 'field_data_field_pays', 'Université partenaire': 'field_data_field_universite_partenaire',\
74
    'Image Galleries': 'field_data_field_image_galleries'}
75
    vocabulary2column = {'Tags': 'field_tags_tid', 'Pays': 'field_pays_tid', 'Université partenaire': 'field_universite_partenaire_tid',\
76
    'Image Galleries': 'field_image_galleries_tid'}
77

    
78
    # NB: profile are not migrated automatically
79
    fetch_d6_nodeTitle_termName = """
80
    SELECT node.title, term_data.name FROM node
81
    JOIN term_node ON node.nid = term_node.nid
82
    JOIN term_data ON term_node.tid = term_data.tid
83
    WHERE term_data.vid = %s AND type <> 'profile' ORDER BY node.nid
84
    """
85

    
86
    fetch_d7_tids = """
87
    SELECT name, tid FROM taxonomy_term_data
88
    """
89

    
90
    fetch_d7_nid_created = """
91
    SELECT nid, created FROM node WHERE title = %s
92
    """
93

    
94
    update_taxonomy_index = """
95
    INSERT INTO taxonomy_index (nid, tid, sticky, created) VALUES
96
    (%s, %s, 0, %s)
97
    """
98

    
99
    update_field_data_field = """
100
    INSERT INTO {}
101
    (entity_type, bundle, deleted, entity_id, revision_id, language, delta, {})
102
    VALUES
103
    ('node', 'image_dans_la_galerie', 0, %s, %s, 'und', %s, %s)
104
    """
105

    
106
    # We fetch node titles and taxonomy term name for each vocabulary in drupal 6
107
    nodeTitle_termName = {}
108
    db_d6 = mysql.connect(host="localhost", user="root", passwd="tata", db="expat", charset="utf8")
109
    with db_d6:
110
        cur_d6 = db_d6.cursor()
111
        for vid in vid2termName_d6.keys():
112
            cur_d6.execute(fetch_d6_nodeTitle_termName, (vid))
113
            vocabulary = vid2termName_d6[vid]
114
            nodeTitle_termName[vocabulary] = cur_d6.fetchall()
115

    
116

    
117
    # We fetch tids for each term in drupal 7
118
    termName2tid = {}
119
    cur.execute(fetch_d7_tids)
120
    rows = cur.fetchall()
121
    for row in rows:
122
        termName2tid[row[0]] = row[1]
123

    
124
    # We perform the update
125
    delta, previous_title = 0, '' # We need this for multiple term taxonomy
126
    for vocabulary, t in nodeTitle_termName.items():
127
        for content in t:
128
            title, term_name = content
129
            cur.execute(fetch_d7_nid_created, (title))
130
            rows = cur.fetchall()
131
            nid, created = rows[0]
132
            tid = termName2tid[term_name]
133
            # We take into account the value of delta
134
            if previous_title == title:
135
                delta += 1
136
            else:
137
                delta = 0
138
                previous_title = title
139
            cur.execute(update_taxonomy_index,\
140
                        (nid, tid, created))
141
            cur.execute(update_field_data_field.\
142
                        format(vocabulary2table[vocabulary],\
143
                               vocabulary2column[vocabulary]),
144
                         (nid, nid, delta, tid))
145
    db.commit()