/*
MIGRAR de una web hecha a medida a Wordpress.
Copyright 2007 Galder Segurola (email: Find it at my blog www.galder.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
//Formato de la tabla wp_posts de Wordpress.
CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`post_author` bigint(20) NOT NULL default '0',
`post_date` datetime NOT NULL default '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
`post_content` longtext NOT NULL,
`post_title` text NOT NULL,
`post_category` int(4) NOT NULL default '0',
`post_excerpt` text NOT NULL,
`post_status` enum('publish','draft','private','static','object','attachment','inherit','future') NOT NULL default 'publish',
`comment_status` enum('open','closed','registered_only') NOT NULL default 'open',
`ping_status` enum('open','closed') NOT NULL default 'open',
`post_password` varchar(20) NOT NULL default '',
`post_name` varchar(200) NOT NULL default '',
`to_ping` text NOT NULL,
`pinged` text NOT NULL,
`post_modified` datetime NOT NULL default '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
`post_content_filtered` text NOT NULL,
`post_parent` bigint(20) NOT NULL default '0',
`guid` varchar(255) NOT NULL default '',
`menu_order` int(11) NOT NULL default '0',
`post_type` varchar(20) NOT NULL default 'post',
`post_mime_type` varchar(100) NOT NULL default '',
`comment_count` bigint(20) NOT NULL default '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
*/
// Incluimos el archivo de Wordpress wp-config.php que es donde Wordpress tiene las variables de conexión, etc...
require_once("wp-config.php");
if ($link = mysql_connect("localhost",DB_USER,DB_PASSWORD)) {
echo "Conectado
";
} else {
echo "No conectado
";
}
//Este es el formato de la antigua web, Artikuluak son los artículos.
$query = "select * from Artikuluak where izenerdartikulua != ''";
// Komunikabideak. Es una tabla para los medios de comunicación.
$array_komunikabideak = array ();
$query_komunikabideak = "select * from Komunikabideak";
$result_komunikabideak = mysql_query($query_komunikabideak);
while ($rs = mysql_fetch_array($result_komunikabideak)) {
$kodkom = $rs["kodkom"];
$array_komunikabideak[$kodkom] = trim($rs["izenakom"]);
}
mysql_free_result($result_komunikabideak);
// Gaiak. Una tabla para las categorías de la antigua web.
$array_gaia = array ();
$query_gaia = "select * from Gaiak";
$result_gaia = mysql_query($query_gaia);
while ($rs = mysql_fetch_array($result_gaia)) {
$kodgaiak = $rs["kodgaiak"];
$array_gaia[$kodgaiak]["es"] = trim($rs["izenerdgaiak"]);
$array_gaia[$kodgaiak]["eu"] = trim($rs["izengaiak"]);
}
mysql_free_result($result_gaia);
// Egileak. Una tabla para los autores de la antigua web.
$array_egileak = array ();
$query_egileak = "select * from Egileak";
$result_egileak = mysql_query($query_egileak);
while ($rs = mysql_fetch_array($result_egileak)) {
$kodegilea = $rs["kodegilea"];
$array_egileak[$kodegilea] = trim($rs["izenaegilea"]);
}
mysql_free_result($result_egileak);
$result = mysql_query ($query);
//Para cada artículo vamos sacando la información.
while ($rs = mysql_fetch_array($result)) {
//El autor 1 es el admin de Wordpress normalmente.
$post_author = 1;
//Sacamos el título del post.
$post_title = utf8_encode($rs["izenerdartikulua"]);
$post_title = str_replace("'","\'",$post_title);
//Sacamos el contenido del post.
$post_content = utf8_encode($rs["testuaerd"]);
//A partir del título vamos a hacer el post_name o URL del artículo.
$post_name = $post_title;
//Si el $post_title está vacío significa que el artículo no estaba en castellano.
if ($post_title != "") {
echo "Título : " . $post_title ."
";
//Quitar todas las comas, puntos, guiones y traducir por '' en el post_name o URL del post
$caracteres = array("¡","!","¿","?",",",".",":","“","\"","’","(",")");
$post_name = str_replace($caracteres, "", $post_name);
$caracteres = array("_"," ");
$post_name = str_replace($caracteres, "-", $post_name);
$post_name = strtolower ($post_name);
$post_name = str_replace("ñ","n",$post_name);
$post_name = str_replace("á","a",$post_name);
$post_name = str_replace("é","e",$post_name);
$post_name = str_replace("í","i",$post_name);
$post_name = str_replace("ó","o",$post_name);
$post_name = str_replace("ú","u",$post_name);
echo "URL del post: " . $post_name ."
";
$post_content= str_replace("\r\n","
",$post_content);
//Unos cambios que hago en el contenido traído de la vieja web.
$post_content = str_replace ("','Materiala','alwaysRaised=yes,dependent=yes,titlebar=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes')","",$post_content);
$post_content = str_replace ("Javascript:Ventana ('","/",$post_content);
$post_content = str_replace ("
";
if ($tags == "") {
$tags = $array_gaia[$kodgaiak]["es"];
} else {
$tags .= "," . $array_gaia[$kodgaiak]["es"];
}
$tag = str_replace(" ","-",trim($array_gaia[$kodgaiak]["es"]));
/*
Miramos si existe la etiqueta en la base de datos. wp_tags
Si existe, sacamos su ID y lo añadimos a wp_post2tag
Si no existe, creamos el wp_tags y lo añadimos luego al wp_post2tag
*/
$query = "select * from es_wp_tags where tag = '$tag'";
$result_tag = mysql_query ($query);
if ($rs_tag = mysql_fetch_array($result_tag)) {
$tag_ID = $rs_tag["tag_ID"];
} else {
//Añadir aquí y sacar la $tag_ID
$query = "INSERT INTO es_wp_tags (tag) VALUES ('$tag')";
mysql_query ($query);
$tag_ID = mysql_insert_id();
}
array_push ($array_tags,$tag_ID);
}
if ($egilea != 0) {
$post_content .= "Autor/a: " . $array_egileak[$egilea] . "
";
if ($tags == "") {
$tags = $array_egileak[$egilea];
} else {
$tags .= "," . $array_egileak[$egilea];
}
$tag = str_replace(" ","-",trim($array_egileak[$egilea]));
/*
Miramos si existe la etiqueta en la base de datos. wp_tags
Si existe, sacamos su ID y lo añadimos a wp_post2tag
Si no existe, creamos el wp_tags y lo añadimos luego al wp_post2tag
*/
$query = "select * from es_wp_tags where tag = '$tag'";
$result_tag = mysql_query ($query);
if ($rs_tag = mysql_fetch_array($result_tag)) {
$tag_ID = $rs_tag["tag_ID"];
} else {
//Añadir aquí y sacar la $tag_ID
$query = "INSERT INTO es_wp_tags (tag) VALUES ('$tag')";
mysql_query ($query);
$tag_ID = mysql_insert_id();
}
array_push ($array_tags,$tag_ID);
}
if ($komunikabidea != 0) {
$post_content .= "Medio de comunicación: " . $array_komunikabideak[$komunikabidea] . "
";
if ($tags == "") {
$tags = $array_komunikabideak[$komunikabidea];
} else {
$tags .= "," . $array_komunikabideak[$komunikabidea];
}
$tag = str_replace(" ","-",trim($array_komunikabideak[$komunikabidea]));
/*
Miramos si existe la etiqueta en la base de datos. wp_tags
Si existe, sacamos su ID y lo añadimos a wp_post2tag
Si no existe, creamos el wp_tags y lo añadimos luego al wp_post2tag
*/
$query = "select * from es_wp_tags where tag = '$tag'";
$result_tag = mysql_query ($query);
if ($rs_tag = mysql_fetch_array($result_tag)) {
$tag_ID = $rs_tag["tag_ID"];
} else {
//Añadir aquí y sacar la $tag_ID
$query = "INSERT INTO es_wp_tags (tag) VALUES ('$tag')";
mysql_query ($query);
$tag_ID = mysql_insert_id();
}
array_push ($array_tags,$tag_ID);
}
echo "Cuerpo: " . $post_content ."
";
$dataintranet = $rs["dataintranet"];
echo "Fecha GMT $dataintranet: " . $dataintranet . "
";
//Fecha del post, y fecha GMT
$post_date_gmt = $dataintranet;
$post_modified_gmt = $dataintranet;
$post_date = date ("Y-m-d H:i:s",strtotime($dataintranet)+3600);
$post_modified = $post_date;
//Categoría 0, tipo de noticia: post, estado publicado con comentarios y pings abiertos.
$post_category = 0;
$post_status = "publish";
$comment_status ="open";
$ping_status = "open";
$post_type = "post";
//El nombre del post, la URL debe ser único, por lo que si no lo está añadimos un número por detrás.
$post_name_no_libre = true;
$post_name_alternativo = $post_name;
$i = 1;
while ($post_name_no_libre) {
$query = "SELECT * FROM es_wp_posts WHERE post_name='$post_name_alternativo'";
//¿Estará ese post-name en uso?
$result_post_name = mysql_query ($query);
if ($array_post_name = mysql_fetch_array($result_post_name)) {
$i = $i + 1;
$post_name_alternativo = $post_name . "-$i";
} else {
//Está libre ese post_name, perfecto
$post_name_no_libre = false;
}
}
$post_name = $post_name_alternativo;
//Hacemos el insert del wp_posts
$query = "INSERT INTO es_wp_posts (post_author,post_date,post_date_gmt,post_content,post_title,post_category,post_status,comment_status,ping_status,post_name,post_modified,post_modified_gmt,post_type) VALUES (1,'$post_date','$post_date_gmt','$post_content','$post_title',$post_category,'$post_status','$comment_status','$ping_status','$post_name','$post_modified','$post_modified_gmt','$post_type')";
if (mysql_query ($query)) {
//Último ID de post
$post_id = mysql_insert_id($link);
echo "Añadido el post número $post_id
";
//Importante, tenemos que vincular este post a una categoría. Previamente hemos creado la 15 en nuestro caso para "Web vieja"
$query = "insert into es_wp_post2cat (post_id,category_id) VALUES ($post_id,15)";
if (mysql_query ($query)) {
echo "La categoria correspondiente ha sido introducida
";
} else {
echo "No se ha podido meter el post en la categoria 'Web vieja'
";
}
foreach ($array_tags as $tag_id) {
//Hacemos los inserts del array de tags a wp_post2tag. Esto es para las etiquetas del Ultimate Tag Warrior.
$query = "INSERT INTO es_wp_post2tag (tag_id,post_id,ip_address) VALUES ($tag_id,$post_id,'')";
if (mysql_query ($query)) {
echo "Añadida la etiqueta al post $post_id
";
}
}
} else {
//echo mysql_error();
echo "No se ha podido añadir el post con el titulo $post_title
";
}
}
}
mysql_close($link);
?>