1
|
DELIMITER //
|
2
|
DROP PROCEDURE IF EXISTS replicawiki2;
|
3
|
CREATE PROCEDURE replicawiki2(wikid_from INT, wikid_to INT)
|
4
|
BEGIN
|
5
|
DECLARE done INT DEFAULT 0;
|
6
|
DECLARE last_id INT;
|
7
|
DECLARE st_page VARCHAR(255);
|
8
|
DECLARE curso INT;
|
9
|
DECLARE id_or INT;
|
10
|
DECLARE id_nu INT;
|
11
|
DECLARE pages CURSOR FOR SELECT id FROM wiki_pages wp WHERE wp.wiki_id = wikid_from;
|
12
|
DECLARE copied_pages CURSOR FOR SELECT id_original, id_new FROM aux_map;
|
13
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
14
|
|
15
|
# CAUTION THIS ERASE ALL CURRENT WIKI PAGES of DESTINATION (TO) WIKI
|
16
|
DELETE FROM wiki_pages WHERE wiki_pages.wiki_id = wikid_to ;
|
17
|
|
18
|
OPEN pages;
|
19
|
|
20
|
#Fix wikis start page
|
21
|
SELECT wikis.start_page INTO st_page FROM wikis WHERE id = wikid_from;
|
22
|
UPDATE wikis SET start_page = st_page WHERE id = wikid_to;
|
23
|
|
24
|
# aux table to replicate the hierachy
|
25
|
DROP TABLE IF EXISTS aux_map;
|
26
|
CREATE TABLE aux_map(id_original INT, id_new INT);
|
27
|
|
28
|
#copy redirects
|
29
|
INSERT INTO wiki_redirects (wiki_id, `title`, redirects_to, created_on)
|
30
|
SELECT wikid_to as wiki_id, `title`, redirects_to, created_on FROM wiki_redirects WHERE wiki_id = wikid_from;
|
31
|
|
32
|
#copy all the wiki pages to origin to dest. and fill the auxiliary hierachy table
|
33
|
REPEAT
|
34
|
FETCH pages INTO curso;
|
35
|
INSERT INTO wiki_pages (wiki_id, title, created_on, protected, parent_id)
|
36
|
SELECT wikid_to as wiki_id, wp.title, wp.created_on, wp.protected, wp.parent_id FROM wiki_pages wp WHERE wp.id = curso limit 1;
|
37
|
SET last_id = last_insert_id();
|
38
|
INSERT INTO aux_map (id_original, id_new)
|
39
|
VALUES(curso, last_id);
|
40
|
UNTIL done END REPEAT;
|
41
|
|
42
|
SET done = 0;
|
43
|
OPEN copied_pages;
|
44
|
|
45
|
#update hierachy and copy all the wiki contents.
|
46
|
REPEAT
|
47
|
FETCH copied_pages INTO id_or, id_nu;
|
48
|
UPDATE wiki_pages SET parent_id = id_nu WHERE wiki_id = wikid_to AND parent_id = id_or;
|
49
|
INSERT INTO wiki_contents (page_id, author_id, `text`, `comments`, `updated_on`, `version`)
|
50
|
SELECT id_nu as page_id, author_id, `text`, `comments`, `updated_on`, `version` FROM wiki_contents wc WHERE wc.page_id = id_or;
|
51
|
UNTIL done END REPEAT;
|
52
|
|
53
|
|
54
|
CLOSE pages;
|
55
|
CLOSE copied_pages;
|
56
|
|
57
|
#Deleting auxiliary table
|
58
|
DROP TABLE IF EXISTS aux_map;
|
59
|
|
60
|
END
|
61
|
//
|