About how to clone a Prestashop instance

Lately, I’ve been helping my neighbor with his Prestashop website. I’ve learned a few tricks, like editing the overall appearance of the site, making and restoring backups in a limited environment, but my best invention has been cloning the Prestashop instance to have a test version where I can try things out without fear of breaking anything. Here’s the code for anyone who might find it useful, licensed under GPL 3.

You can see the code here, too: https://gist.github.com/axelei/dafd305213ed1b2655545f06edfbc905

# Programa para clonar prestashop
# Copyright Krusher 2015 - Licenciado bajo GPL3

paramfile="httpdocs/app/config/parameters.php"
extractDatabaseName="s/.*'database_name' => '\(.*\)'.*/\1/p"
extractDatabaseUser="s/.*'database_user' => '\(.*\)'.*/\1/p"
extractDatabasePassword="s/.*'database_password' => '\(.*\)'.*/\1/p"

if [ "$#" -lt 4 ]; then
  echo "Error: Se requieren 3 argumentos."
  echo "Uso: $0 dir_web_original dir_web_destino url_original url_destino"
  exit 1
fi

echo Traspasando de $1 a $2 \(urls: $3 -- $4 \)

databaseName=$(sed -n "$extractDatabaseName" $1/app/config/parameters.php)
databaseUser=$(sed -n "$extractDatabaseUser" $1/app/config/parameters.php)
databasePassword=$(sed -n "$extractDatabasePassword" $1/app/config/parameters.php)

echo Datos de la base de datos de origen: $databaseName / $databaseUser / $databasePassword

targetName=$(sed -n "$extractDatabaseName" $2/app/config/parameters.php)
targetUser=$(sed -n "$extractDatabaseUser" $2/app/config/parameters.php)
targetPassword=$(sed -n "$extractDatabasePassword" $2/app/config/parameters.php)

echo Datos de la base de datos de destino: $targetName / $targetUser / $targetPassword

if [[ -z "$databaseName" ]] || [[ -z "$databaseUser" ]] || [[ -z "$databasePassword" ]] || [[ -z "$targetName" ]] || [[ -z "$targetUser" ]] || [[ -z "$targetPassword" ]]; then
  echo "Error: Algunas de las variables no han sido extraídas, comprueba los errores y los directorios."
  exit 1
fi

echo URL de la instancia origen: $3
echo URL de la instancia destino: $4

read -p "Pulsa enter para continuar o CTRL+C para cancelar"
echo  "Procediendo con el traspaso."

echo Extrayendo base de datos
rm -f temp.sql
mysqldump -u $databaseUser $databaseName -p$databasePassword > temp.sql
echo Borrando base de datos objetivo
mysql -u $targetUser $targetName -p$targetPassword -e "drop database $targetName; create database $targetName"
echo Insertando la base de datos origen en la objetivo
mysql -u $targetUser -D $targetName -p$targetPassword < temp.sql
rm -f temp.sql

echo borra ficheros contenido antiguo
rm -Rf $2/*
echo copia ficheros
cp -r $1/* $2/

echo Configurando base de datos objetivo con los datos originales

sed -i "s/'database_name' => '\(.*\)'/'database_name' => '$targetName'/g" $2/app/config/parameters.php
sed -i "s/'database_user' => '\(.*\)'/'database_user' => '$targetUser'/g" $2/app/config/parameters.php
sed -i "s/'database_password' => '\(.*\)'/'database_password' => '$targetPassword'/g" $2/app/config/parameters.php

echo Configurando la nueva URL en la instancia destino

mysql -u $targetUser $targetName -p$targetPassword -e "update prstshp_configuration set value = '$4' where NAME IN ('PS_SHOP_DOMAIN', 'PS_SHOP_DOMAIN_SSL')"
mysql -u $targetUser $targetName -p$targetPassword -e "UPDATE prstshp_shop_url SET domain = '$4', domain_ssl = '$4' WHERE id_shop_url = 1;"
sed -i s/\\^$3\$/^$4$/g $2/.htaccess

echo Terminado.

The implementation could undoubtedly be improved—using a more suitable language or more advanced tools—but this is the solution I came up with given the constraints of an environment lacking many essential utilities and commands. If you found it helpful, feel free to share your experience in the comments.

Taking Sim City 2000 into pieces

SimCity 2000 (Maxis, 1993) is one of my superfavourite games, ever. I’ve been playing it for 20 years and it’s partially responsible of my terrible grades at high school. I have always liked modifying games, but so far I haven’t been serious about decoding the data files of this city simulator. And I have found some quite interesting things!

There were ports in a great number of platforms, from the Macintosh (the original) to GameBoy Advance, but my favourite is MS-DOS, and it’s what this article is about. There are two interesting files: the executable (SC2000.EXE) and the data file (SC2000.DAT). Unfortunately, Windows version didn’t came out in Spanish (my mother language), and Network Edition version works awfully bad (and it is available only in English). Continue reading “Taking Sim City 2000 into pieces”

JSF converter for SelectOneMenu

The main JSF feature (or, at least, the one I like the most) is the ease to link controller bean attributes to xhtml view. However there’s an inherent important limitation: in the HTTP standard keys and values always will be strings, because they are sent that way. Of course you could serialize an object to base64, but a barbaric thing like that should be avoided in almost any circumstance.

Then how can we link a HTTP control to an object? For that, JSF provides converters. Well, it will allow you to program them, of course. Neither PrimeFaces, my library of choice, includes these converters. Thus, I’ve programmed a simple one for SelectOneMenu, the dropdown menus in the like of a combobox. I’ll leave it here for my own reference, and for anyone that could use it.

Continue reading “JSF converter for SelectOneMenu”