Projet

Général

Profil

Révision 569211e8

Ajouté par Julien Enselme il y a plus de 10 ans

D6_to_D7: tous les scripts pour migrer le blog des expats et réparer la table cas_user

Voir les différences:

scripts_divers/D6_to_D7/fix-images-expat.pl
1
# In the expat website, when we try to migarte it, the images not found
2
# and the only solution we found is to use this script to patch its database
3
# and so repair the links
4

  
5
use strict;
6
use warnings;
7
use DBI;
8

  
9
my $db = 'expat';
10
my $server = 'localhost';
11
my $user = 'root';
12
my $password = 'tata';
13
my $port = '';
14

  
15
#### Create DB connection
16
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$server;port=$port", $user, $password, { RaiseError => 1, }) or die "Cannont connect to database $db";
17

  
18
# Update user's pictures
19

  
20
my $sql_select =<<"SQL";
21
SELECT uid, picture FROM expat_users
22
SQL
23

  
24
my $sql_update =<<"SQL";
25
UPDATE expat_users SET picture = ? WHERE uid = ?
26
SQL
27

  
28
my $req_select = $dbh->prepare($sql_select);
29
my $req_update = $dbh->prepare($sql_update);
30
$req_select->execute;
31

  
32
while (my ($uid, $picture) = $req_select->fetchrow_array)
33
{
34
    $picture =~ s#sites/assos.centrale-marseille.fr.expat/(.*)#sites/default/$1#;
35
    $req_update->execute($picture, $uid);
36
}
37

  
38
$req_update->finish;
39
$req_select->finish;
40

  
41
# Update other file
42
$sql_select = "SELECT fid, filepath FROM expat_files";
43
$sql_update = "UPDATE expat_files SET filepath = ? WHERE fid = ?";
44

  
45
$req_select = $dbh->prepare($sql_select);
46
$req_update = $dbh->prepare($sql_update);
47
$req_select->execute;
48

  
49
while (my ($fid, $file_path) = $req_select->fetchrow_array)
50
{
51
    $file_path =~ s#sites/assos.centrale-marseille.fr.expat/(.*)#sites/default/$1#;
52
    $req_update->execute($file_path, $fid);
53
}
54

  
55
$req_update->finish;
56
$req_select->finish;
57

  
58
$dbh->disconnect;
scripts_divers/D6_to_D7/fix-images-expat.py
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()
scripts_divers/D6_to_D7/fix-migration-expat.py
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()
scripts_divers/D6_to_D7/prepare-expat-d6.sh
1
#!/bin/bash
2

  
3
cd /var/www/expat
4

  
5
drush cc all
6

  
7
# We uninstall the good modules in drupal 6
8
modules2uninstall=(mapbox contemplate openlayers_ui openlayers_views openlayers flowplayer flowplayer3 wijering wijering4 swftools geotaxonomy views_export views_cloud content_profile_registration content_profile fivestar_comment)
9

  
10
for i in ${!modules2uninstall[*]} ; do
11
    drush -y dis ${modules2uninstall[$i]}
12
    drush -y pm-uninstall ${modules2uninstall[$i]}
13
done
14

  
15

  
16
cd /var/www/expatd7
17

  
18
# We install the good module in drupal 7
19
modules2install=(module_filter token addthis blog cas comment_notify imce lightbox2 masquerade pathauto ctools contact ckeditor views views_ui fivestar media)
20

  
21
for i in ${!modules2install[*]} ; do
22
    drush -n dl ${modules2install[$i]}
23
    drush -y en ${modules2install[$i]}
24
done
25

  
26
#modules2install_assos=(addthis lightbox2 votingapi fivestar)
scripts_divers/rm-prefix.py
1
#!/usr/bin/env python3
2

  
3
# This was created in order to delete the prefix of drupal database
4
# These prefix were useful when all sites were in the same database
5

  
6
import pymysql as mysql
7
import sys
8
import re
9

  
10
db = sys.argv[1]
11
prefix = sys.argv[2]
12
host = 'localhost'
13
passwd = 'tata'
14
user = 'root'
15
port = ''
16

  
17
db = mysql.connect(host=host, user=user, passwd=passwd, db=db, charset='utf8')
18
with db:
19
    cur = db.cursor()
20
    cur.execute('SHOW TABLES')
21
    tables = cur.fetchall()
22
    for table in tables:
23
        table = table[0]
24
        if re.match('^{}'.format(prefix), table):
25
            new_table = re.sub('^{}(.*)'.format(prefix), r'\1', table)
26
            cur.execute('DROP TABLE IF EXISTS {}'.format(new_table))
27
            cur.execute('RENAME TABLE {} TO {}'.format(table, new_table))
28
    db.commit()

Formats disponibles : Unified diff