commit f5ddb21e85ecdc6e6c6d87210e55a7bcd7b80655
Author: Julien Enselme <julien.enselme@centrale-marseille.fr>
Date:   Mon Oct 28 17:24:16 2013 +0100

    fix-db.rb: création.
    
    Ce script doit finir la migration vers redmine en réparant
    les liens, les contributeurs et les dates de contributions
    directement dans la base de données.
    On ne fait plus de perl.
    La version ruby corrige les liens de tâche. Reste les liens de commentaire
    et à patcher effectivement la base de données.
    
    rm_prefix.pl: ne sert plus à rien, maintenant qu’on a une version python

diff --git a/scripts_divers/migrer_taches_vers_redmine/README b/scripts_divers/migrer_taches_vers_redmine/README
new file mode 100644
index 00000000..e18ecae2
--- /dev/null
+++ b/scripts_divers/migrer_taches_vers_redmine/README
@@ -0,0 +1,22 @@
+* Migrate
+Simply use the following commands in a clean environment: the folder in which
+you launch these commands must not contains any csv or txt files from a previous
+migration otherwise fix-db.rb won't work.
+- python3 post_redmine.py
+- ruby fix-db.rb
+
+* Clean
+Use in a python3 interpreter
+
+#BEGIN_SRC python
+import post_redmine
+redmine = post_redmine.Redmine()
+redmine.sweep()
+#END_SRC
+
+* Export as html
+The export-issues-html.sh script allows you to export issues for a projet as
+html files. It requires access to the database of redmine and only work for
+mysql.
+
+export-issues-html.sh PROJECT_ID
diff --git a/scripts_divers/migrer_taches_vers_redmine/export-issues-html.sh b/scripts_divers/migrer_taches_vers_redmine/export-issues-html.sh
new file mode 100644
index 00000000..b85a3cb5
--- /dev/null
+++ b/scripts_divers/migrer_taches_vers_redmine/export-issues-html.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+mysql -h debian -e "SELECT id FROM issues WHERE project_id = $1" -u root -ptata redmine_default > issues_id_list
+
+mkdir issues_html
+cd issues_html
+while read line; do
+    wget http://debian/redmine/issues/$line > /dev/null
+done < ../issues_id_list
diff --git a/scripts_divers/migrer_taches_vers_redmine/fix-db.pl b/scripts_divers/migrer_taches_vers_redmine/fix-db.pl
deleted file mode 100644
index 134f3635..00000000
--- a/scripts_divers/migrer_taches_vers_redmine/fix-db.pl
+++ /dev/null
@@ -1,214 +0,0 @@
-# In order to complete the migration to Redmine, we need to preserve date
-# and username. To do this, we need to fix the database.
-
-use strict;
-use warnings;
-use DBI;
-
-my $db		= 'redmine_default';
-my $server	= 'localhost'; # Il est possible de mettre une adresse IP
-my $user        = 'root';      # identifiant
-my $passwd	= 'tata';
-my $port	= '';
-
-
-##### Create DB connection
-my $dbh = DBI->connect( "DBI:mysql:database=$db;host=$server;port=$port", $user, $passwd, { RaiseError => 1, } ) or die "Connection impossible à la base de données $db !\n $! \n $@\n$DBI::errstr";
-
-
-
-###### Initialisation of hashes needed to fix URL
-open(my $fix_url_issues_csv, '<', 'fix_url_issues.csv') or die "Couldn't open fix_url_issues.csv";
-my %name2iid = ();
-my %nid2iid = ();
-my %cid2iid = ();
-my %cid2post_nb = ();
-
-while (my $line = <$fix_url_issues_csv>)
-  {
-    chomp $line;
-    my ($nid, $name, $iid) = split ",", $line;
-    $name2iid{$name} = $iid;
-    $nid2iid{$nid} = $iid;
-  }
-close $fix_url_issues_csv;
-
-open(my $fix_url_comments_csv, '<', 'fix_url_comments.csv') or die "Couldn't open fix_url_comments.csv";
-while (my $line = <$fix_url_comments_csv>)
-  {
-    chomp $line;
-    my ($cid, $iid, $post_nb) = split ",", $line;
-    $cid2iid{$cid} = $iid;
-    $cid2post_nb{$cid} = $post_nb;
-  }
-close $fix_url_comments_csv;
-
-
-
-###### Definition of functions
-sub fix_issues_link {
-    my $text = $_[0];
-
-    # Find all links in text
-    my @links = ($text =~ m/:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content\/t%C3%A2che|node)\/(.*)/g);
-
-    if ( @links )
-    {
-	foreach (@links)
-	{
-	    print $_ ."\n";
-	    $text =~ s/"http:\/\/assos\.centrale-marseille\.fr(\/|\/lessive\/)(content\/t%C3%A2che|node)\/.*":http:\/\/assos\.centrale-marseille\.fr(\/|\/lessive\/)(content\/t%C3%A2che|node)\/$_/#$cid2iid{$_}#note-$cid2post_nb{$_}/g;
-	}
-    }
-    return $text;
-}
-
-sub fix_comments_link {
-    my $text = $_[0];
-
-    # Find all links in text
-    my @links = $text =~ m/"http:\/\/assos\.centrale-marseille\.fr(\/|\/lessive\/)comment\/.*"/g;
-
-     # Foreach link, get comment cid.
-    my @cids = ();
-    if (@links)
-    {
-	foreach (@links)
-	{
-	    my $cid =~ s/"http:\/\/assos\.centrale-marseille\.fr(\/|\/lessive\/)comment\/(.*)#.*"/$1/g;
-	    push @cids, $cid;
-	}
-
-	foreach (@cids)
-	{
-	    $text =~ s/"http:\/\/assos\.centrale-marseille\.fr(\/|\/lessive\/)comment\/$_#$_":http:\/\/assos\.centrale-marseille\.fr\/comment\/$_#$_/#$name2iid{$_}/g;
-	}
-    }
-
-    return $text;
-}
-
-
-
-###### Update issues
-my $sql_update_issue =<<"SQL";
-UPDATE issues
-  SET author_id = ?, created_on = ?
-  WHERE  id = ?
-SQL
-
-my $sql_fix_links_issue =<<"SQL";
-UPDATE issues
-    SET description = ?
-    WHERE id = ?
-SQL
-
-my $req_update_issue = $dbh->prepare($sql_update_issue);
-my $req_fix_links_issue = $dbh->prepare($sql_fix_links_issue);
-
-# Reading file
-open(my $issues, '<issues.csv') or die "Couldn't open issues.txt\n";
-
-while (my $line = <$issues>)
-  {
-    # Update author and date of creation
-    chomp $line;
-
-    my ($iid, $author_id, $created_on, $nid, $name) = split ",", $line;
-
-    $req_update_issue->execute($author_id, $created_on, $iid);
-
-    ## Fix links
-    # issues
-    my $select_description = "SELECT description FROM issues WHERE id = 1569";
-
-    my ($description) = $dbh->selectrow_array($select_description);
-
-    $description = fix_issues_link($description);
-
-    die "stop";
-
-    print $description . "\n";
-#    print $links[0];
-#    print $name2iid{$links[0]};
-    # Comments
-#    $description = fix_comments_link($description);
-
-#    $req_fix_links_issue->execute($description, $iid);
-  }
-
-$req_update_issue->finish;
-$req_fix_links_issue->finish;
-
-
-###### Update journals
-my $sql_get_id =<<"SQL";
-SELECT id FROM journals WHERE journalized_id = ?
-ORDER BY created_on ASC
-SQL
-
-my $sql_update_journals =<<"SQL";
-UPDATE journals
-SET created_on = ?, user_id = ?
-WHERE id = ?
-SQL
-
-my $sql_updated_on =<<"SQL";
-UPDATE issues SET updated_on = ?
-WHERE id = ?
-SQL
-
-my $sql_fix_links_comment =<<"SQL";
-UPDATE journals
-    SET notes = ?
-    WHERE id = ?
-SQL
-
-my $req_get_id = $dbh->prepare($sql_get_id);
-my $req_update_journals = $dbh->prepare($sql_update_journals);
-my $req_updated_on = $dbh->prepare($sql_updated_on);
-my $req_fix_links_comment = $dbh->prepare($sql_fix_links_comment);
-
-# Reading file.
-open(my $comments, '<comments.csv') or die "Couldn't open comments.txt\n";
-
-# We must remember current iid in order to fetch result or execute query: req_get_id return
-# more than one value
-my $current_iid = -1;
-
-while (my $line = <$comments>)
-  {
-    chomp $line;
-
-    my ($iid, $user_id, $created_on) = split ",", $line;
-
-    # We get the id of the comment.
-    if ($current_iid != $iid)
-    {
-	$current_iid = $iid;
-	$req_get_id->execute($iid);
-    }
-
-    my ($id) = $req_get_id->fetchrow_array;
-
-    # We do the update.
-    $req_update_journals->execute($created_on, $user_id, $id);
-    $req_updated_on->execute($created_on, $iid);
-
-    ## Fix links.
-    # issues
-    my $select_notes = "SELECT notes FROM journals WHERE id = $id";
-    my ($notes) = $dbh->selectrow_array($select_notes);
-#    $notes = fix_issues_link($notes);
-    # Comments
-#    $notes = fix_comments_link($notes);
-
-#    $req_fix_links_comment->execute($notes, $id);
-  }
-
-$req_get_id->finish;
-$req_update_journals->finish;
-$req_updated_on->finish;
-
-####### Close the db connection.
-$dbh->disconnect;
diff --git a/scripts_divers/migrer_taches_vers_redmine/fix-db.rb b/scripts_divers/migrer_taches_vers_redmine/fix-db.rb
new file mode 100644
index 00000000..71755811
--- /dev/null
+++ b/scripts_divers/migrer_taches_vers_redmine/fix-db.rb
@@ -0,0 +1,191 @@
+#!/usr/bin/ruby -w
+#encoding: UTF-8
+
+require "dbi"
+require "uri"
+
+###### Definition of functions
+def custom_chomp(string)
+  # Chomp line completely
+  previous_string = ''
+  while string != previous_string
+    previous_string = string.dup # = makes a reference to the original string
+    string.chomp!
+  end
+  return string
+end
+
+
+def fix_issues_link(text, name2iid)
+  # Find all links in text
+  scan_regex =  /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/([^ \n)]*)/
+
+  # Non flattened output is [['some'], ['text']]
+  # flattened output is ['some', 'text']
+  names = text.scan(scan_regex).flatten
+
+  if not names.empty?
+    names.each do |name|
+      name = custom_chomp(name)
+      unescaped_name = URI.unescape(name)
+      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/[^ ]*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/#{name}/
+      remplace_regex = "#{unescaped_name} : ##{name2iid[name]}"
+      text.gsub!(gsub_regex, remplace_regex)
+    end
+  end
+  return text
+end
+
+
+def fix_comments_link(text, cid2iid, cid2post_nb)
+  scan_regex = /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/|\/portail\/)comment\/(\d+)/
+
+  cids = text.scan(scan_regex).flatten
+
+  if not cids.empty?
+    cids.each do |cid|
+      cid = custom_chomp(cid)
+      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/.*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/#{cid}#comment-\d+/
+      post_nb = custom_chomp(cid2post_nb[cid])
+      remplace_regex = "##{cid2iid[cid]}#note-#{post_nb}"
+      text.gsub!(gsub_regex, remplace_regex)
+    end
+  end
+  return text
+end
+
+
+
+###### DATABASE
+begin
+  ##### Create DB connection
+  db = 'redmine_default'
+  dbuser = 'root'
+  dbpwd = 'tata'
+  host = 'localhost'
+  dbh = DBI.connect("DBI:Mysql:#{db}:#{host}", dbuser, dbpwd)
+  dbh.do("SET NAMES 'UTF8'")
+
+  ###### Initialisation of hashes needed to fix URL
+  name2iid = Hash.new
+  nid2iid = Hash.new
+  cid2iid = Hash.new
+  cid2post_nb = Hash.new
+
+  fix_url_issues_csv = File.open("fix_url_issues.csv", "r")
+  fix_url_issues_csv.each do |line|
+    line = line.chomp
+    nid, name, iid = line.split(',')
+    name2iid[name] = nid
+    nid2iid[nid] = iid
+  end
+  fix_url_issues_csv.close
+
+  fix_url_comments_csv = File.open("fix_url_comments.csv", "r")
+  fix_url_comments_csv.each do |line|
+    cid, iid, post_nb = line.split(',')
+    cid2iid[cid] = iid
+    cid2post_nb[cid] = post_nb
+  end
+  fix_url_comments_csv.close
+
+
+
+  ###### Update issues
+  sql_update_issue = "UPDATE issues SET author_id = ?, created_on = ?, is_private = 1 WHERE  id = ?"
+  sql_fix_links_issue = "UPDATE issues SET description = ? WHERE id = ?"
+  select_description = "SELECT description FROM issues WHERE id = ?"
+
+  req_update_issue = dbh.prepare(sql_update_issue)
+  req_fix_links_issue = dbh.prepare(sql_fix_links_issue)
+  req_select_description = dbh.prepare(select_description)
+
+  # Reading file
+  issues = File.open('issues.csv', 'r')
+  issues.each do |line|
+    # Update author and date of creation
+    line = line.chomp
+    iid, author_id, created_on, nid, name = line.split(',')
+    req_update_issue.execute(author_id, created_on, iid)
+
+    ## Fix links
+    # issues
+    req_select_description.execute(iid)
+
+    description = req_select_description.fetch[0]
+    description = fix_issues_link(description, name2iid)
+
+    # Comments
+    description = fix_comments_link(description, cid2iid, cid2post_nb)
+
+    # We update
+    req_fix_links_issue.execute(description, iid)
+  end
+
+  req_select_description.finish
+  req_update_issue.finish
+  req_fix_links_issue.finish
+
+
+
+  ###### Update journals
+  sql_get_ids = "SELECT id FROM journals WHERE journalized_id = ? ORDER BY id ASC"
+  sql_update_journals = "UPDATE journals SET created_on = ?, user_id = ? WHERE id = ?"
+  sql_updated_on = "UPDATE issues SET  updated_on = ? WHERE id = ?"
+  sql_fix_links_comment = "UPDATE journals SET notes = ? WHERE id = ?"
+
+  req_get_ids = dbh.prepare(sql_get_ids)
+  req_update_journals = dbh.prepare(sql_update_journals)
+  req_updated_on = dbh.prepare(sql_updated_on)
+  req_fix_links_comment = dbh.prepare(sql_fix_links_comment)
+
+  # Reading file
+  comments = File.open('comments.csv', 'r')
+
+  # We must remember current iid in order to fetch result or execute query: req_get_id return
+  # more than one value
+  current_iid = -1
+  select_notes = "SELECT notes FROM journals WHERE id = ?"
+  req_select_notes = dbh.prepare(select_notes)
+
+  comments.each do |line|
+    line = line.chomp
+    iid, user_id, created_on = line.split(',')
+
+    # We get the id of the comment
+    if iid != current_iid
+      current_iid = iid
+      req_get_ids.execute(iid)
+    end
+
+    id = req_get_ids.fetch[0]
+
+    # We do the update
+    req_update_journals.execute(created_on, user_id, id)
+    req_updated_on.execute(created_on, iid)
+
+    ## Fix links
+    # issues
+    req_select_notes.execute(id)
+    notes = req_select_notes.fetch[0]
+    notes = fix_issues_link(notes, name2iid)
+
+    # Comments
+    notes = fix_comments_link(notes, cid2iid, cid2post_nb)
+
+    req_fix_links_comment.execute(notes, id)
+  end
+  comments.close
+
+  req_select_notes.finish
+  req_get_ids.finish
+  req_update_journals.finish
+  req_updated_on.finish
+rescue DBI::DatabaseError => e
+     puts "An error occurred"
+     puts "Error code:    #{e.err}"
+     puts "Error message: #{e.errstr}"
+ensure
+     # disconnect from server
+     dbh.disconnect if dbh
+end
diff --git a/scripts_divers/migrer_taches_vers_redmine/goto_redmine.py b/scripts_divers/migrer_taches_vers_redmine/goto_redmine.py
index af7fdeee..1eb51126 100644
--- a/scripts_divers/migrer_taches_vers_redmine/goto_redmine.py
+++ b/scripts_divers/migrer_taches_vers_redmine/goto_redmine.py
@@ -38,12 +38,11 @@ def handle_image(txt):
     has_image = False
     # In textile, images are between !
     images = REGEXP_FIND_IMG.findall(txt)
-    print(images)
     if images:
         has_image = True
         for image in images:
             img_name = REGEXP_NAME_IMG.sub(r'!\1!', image)
-            txt.replace(image, img_name)
+            txt = txt.replace(image, img_name)
     return txt, has_image
 
 def html2textile(txt):
diff --git a/scripts_divers/rm-prefix.pl b/scripts_divers/rm-prefix.pl
deleted file mode 100644
index 00931287..00000000
--- a/scripts_divers/rm-prefix.pl
+++ /dev/null
@@ -1,35 +0,0 @@
-### Ce script a été écrit afin de supprimer les préfixes des tables des bases données.
-### Ces préfixes étaient indispensables quand tous les sites étaient dans une même base de données.
-
-use strict;
-use warnings;
-use DBI;
-
-my $bd		= $ARGV[0];
-my $serveur	= 'localhost'; # Il est possible de mettre une adresse IP
-my $identifiant = 'root';      # identifiant
-my $motdepasse	= 'tata';
-my $port	= '';
-
-my ($prefix) = $ARGV[1];
-
-my $dbh = DBI->connect( "DBI:mysql:database=$bd;host=$serveur;port=$port", $identifiant, $motdepasse, { RaiseError => 1, } ) or die "Connection impossible à la base de données $bd !\n $! \n $@\n$DBI::errstr";
-
-my @table_names = $dbh->tables;
-foreach my $table(@table_names)
-{
-    #print $table =~ m/$prefix/;
-    #print $table . "\n";
-
-    if( $table =~ m/^`$bd`\.`$prefix/ )
-    {
-	my ($new_name) = ( $table =~ m/$prefix(.*)`/ );
-	my $new_table = "`$bd`.`$new_name`";
-	print $new_name . "\n";
-	print $new_table . "\n";
-	$dbh->do('DROP TABLE IF EXISTS ' . $new_name);
-	$dbh->do('RENAME TABLE ' . $table . ' TO ' . $new_name) or die 'Ne peut exécuter la requête';
-    }
-}
-
-$dbh->disconnect();
