DROP PROCEDURE IF EXISTS InsertIntoDirectory;
delimiter //
CREATE PROCEDURE InsertIntoDirectory()
BEGIN
DECLARE ld_cat_id, last_insert INT;
DECLARE ld_url, ld_name CHAR(255);
DECLARE ld_description TEXT;
DECLARE ld_date DATETIME;
DECLARE done BOOLEAN DEFAULT 0;
DECLARE ld CURSOR FOR SELECT cat_id, url, name, description, date FROM ld_urls;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN ld;
REPEAT
FETCH ld INTO ld_cat_id, ld_url, ld_name, ld_description, ld_date;
IF INSTR(ld_name, '"')
THEN SET ld_name = REPLACE(ld_name, '"','"');
END IF;
IF INSTR(ld_description, '"')
THEN SET ld_description = REPLACE(ld_description, '"','"');
END IF;
IF INSTR(ld_name, ''')
THEN SET ld_name = REPLACE(ld_name, ''','\'');
END IF;
IF INSTR(ld_description, ''')
THEN SET ld_description = REPLACE(ld_description, ''','\'');
END IF;
IF INSTR(ld_name, '&')
THEN SET ld_name = REPLACE(ld_name, '&','&');
END IF;
IF INSTR(ld_description, '&')
THEN SET ld_description = REPLACE(ld_description, '&','&');
END IF;
IF INSTR(ld_name, ''')
THEN SET ld_name = REPLACE(ld_name, ''','\'');
END IF;
IF INSTR(ld_description, ''')
THEN SET ld_description = REPLACE(ld_description, ''','\'');
END IF;
INSERT INTO xf_thread (node_id, title, user_id, username, post_date) VALUES (3, ld_name, 1, 'xenforo', Unix_Timestamp(ld_date));
SET last_insert = LAST_INSERT_ID();
INSERT INTO xf_post (thread_id, user_id, username, post_date, message) VALUES (last_insert, 1, 'xenforo', Unix_Timestamp(ld_date), ld_description);
INSERT INTO sf_xenkingdir_thread_map (directory_category, thread_id, website_url, is_claimable) VALUES (ld_cat_id, last_insert, ld_url, 1);
SET last_insert = NULL;
UNTIL done END REPEAT;
CLOSE ld;
END;
//
delimiter ;
CALL InsertIntoDirectory();