Voilà pourquoi les applications basées sur Electron m’énervent autant.
Atom a moins de fonctionnalités que l’éditeur Delphi que j’utilisais dans les années 90 mais rame plus que lui sur un ordinateur énormément plus puissant .
Je ne comprend pas comment on peut le préférer à Sublime text.
Non classé
Activer les hooks dans une page CMS PrestaShop
Normalement le contenu des pages CMS est statique dans PrestaShop mais il peut être utile d’y afficher un module pour mettre les dernières réductions, un formulaire d’inscription ou n’importe quoi.
Pour cela il faut modifier le fichier cms.tpl du thème de la boutique.
{$cms->content}
devient
{assign var="cms_content" value=$cms->content} {include file="string:$cms_content"}
ça permet au contenu d’être interprété par Smarty avant l’affichage et on peut donc mettre les appels aux hooks dans sa page CMS sous la forme {hook h=’displayCarousel’} (voir la doc).
En fait cela permet aussi d’utiliser toute la syntaxe de Smarty, les foreach , include et tout le reste
Ne pas utiliser de champs FLOAT dans MySQL
En fait je ne sais pas pourquoi les float existent, c’est que des sources de bugs.
Il y a quelques années j’ai fait un champ float dans une table et aujourd’hui 1er bug parce que MySQL transforme 27.264957 en 27.265 dans ce champ et l’arrondi devient 27.27 au lieu de 27.26. Et la compta elle aime pas.
Donc la morale c’est : Pas de float, que des decimal
Vider un dossier en PHP
J’utilise ce script pour vider un dossier rapidement. C’est pratique quand on a seulement un accès FTP et qu’on doit démarrer un nouveau projet.
Il faut le déposer dans le dossier à vider, le lancer avec le navigateur et tout est supprimé.
<?php /** * Delete all files and subfolders in the current folder * I use it to start a new project * Be careful : There is no confirmation, this script deletes everything it can, including itself */ // https://stackoverflow.com/a/17161106/2530962 php glob - scan in subfolders for a file function rglob($pattern, $flags = 0) { $files = (array) glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags)); } return $files; } // https://stackoverflow.com/a/33059445/2530962 Get all file all subfolders and all hidden file with glob // https://stackoverflow.com/a/13468943/2530962 Deleting all files from a folder using PHP? // this line returns warnings because unlink can't delete folders. There will be deleted after array_map('unlink', rglob(dirname(__FILE__)."/{,.}[!.,!..]*",GLOB_MARK|GLOB_BRACE)); $folders = array_reverse(rglob(dirname(__FILE__)."/{,.}[!.,!..]*",GLOB_MARK|GLOB_BRACE)); array_map('rmdir', $folders);
Migrer un site WordPress
Comment changer un site WordPress de serveur en 3 étapes:
Malgrès moi je fait de plus en plus de WordPress. Donc je note ça pour plus tard.
Il faut être sur le serveur source dans le dossier du site.
On suppose que le site sera dans le dossier www du serveur de destination
Copier les fichiers sur le nouveau serveur
rsync -avz ./ [login serveur destination]@[hote du serveur destination]:www/
Copier la base de données
mysqldump -u [utilisateur bdd source] -p[mot de passe bdd source] -h [hote bdd source] [nom bdd source] | sed 's/[domaine source]/[domaine destination]/g' | gzip | ssh [login serveur destination]@[hote du serveur destination] "gunzip | mysql -u [utilisateur bdd destination] -p[mot de passe bdd destination] -h [hote bdd destination] [nom bdd destination]"
Explication :
- mysqldump -u [utilisateur bdd source] -p[mot de passe bdd source] -h [hote bdd source] [nom bdd source] fait une extration de la base de données
- sed ‘s/[domaine source]/[domaine destination]/g’ remplace le nom de domaine dans l’archive
- gzip compresse le résultat
- ssh [login serveur destination]@[hote du serveur destination] « gunzip | mysql -u [utilisateur bdd destination] -p[mot de passe bdd destination] -h [hote bdd destination] [nom bdd destination] » envoit le tout sur le serveur de destination et lui fait executer gunzip | mysql -u [utilisateur bdd destination] -p[mot de passe bdd destination] -h [hote bdd destination] [nom bdd destination]
- gunzip décompresse l’archive
- mysql -u [utilisateur bdd destination] -p[mot de passe bdd destination] -h [hote bdd destination] [nom bdd destination] l’importe dans la nouvelle base de données
Paramétrer le nouveau site :
Il faut modifier le fichier wp-config.php avec les identifiants de la nouvelle base de données.
WordPress netinstall
Ce script permet d’installer WordPress sans avoir à télécharger l’archive, la décompresser et la déposer sur un serveur.
Il suffit de déposer le script dans le dossier où vous voulez installer WordPress et d’aller le visiter avec votre navigateur.
<?php // This script download and unzip the latest version of WordPress // // Put this file in the folder where you want to install WordPress // Visit it with your browser // Follow the installation process $local_zip = 'latest.zip'; $path = dirname(__FILE__); // download latest version copy('https://wordpress.org/latest.zip', $local_zip); $zip = new ZipArchive; $res = $zip->open($local_zip); if ($res === true) { // unzip $zip->extractTo($path); $zip->close(); // delete temporary files unlink($local_zip); unlink(__FILE__); // move files to current folder rename_dir($path.DIRECTORY_SEPARATOR.'wordpress', $path); // redirect to installation script header('Location: index.php'); } else { echo 'doh!'; } // http://de.php.net/manual/fr/function.copy.php#91010 function rename_dir($src, $dst) { $dir = @opendir($src); if ($dir && (is_dir($dst) || @mkdir($dst))) { while (false !== ( $file = readdir($dir))) { if (( $file != '.' ) && ( $file != '..' )) { if (is_dir($src.DIRECTORY_SEPARATOR.$file)) { if (!rename_dir($src.DIRECTORY_SEPARATOR.$file, $dst.DIRECTORY_SEPARATOR.$file)) { return false; } } else { if (!@copy($src.DIRECTORY_SEPARATOR.$file, $dst.DIRECTORY_SEPARATOR.$file)) { return false; } @unlink($src.DIRECTORY_SEPARATOR.$file); } } } closedir($dir); @rmdir($src); return true; } return false; }
Supprimer une boutique PrestaShop
Voici un script qui supprime une boutique PrestaShop. Il suffit de le placer à la racine de sa boutique en FTP ou autre et de le visiter avec son navigateur.
⚠ Le script supprime le répertoire de PrestaShop avec tous ses fichiers et sous répertoires et toutes les tables de la base de données qui ont le préfixe de PrestaShop.
⚠⚠ Le script ne demande pas de confirmation. Vous le lancez, il supprime tout.
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $root = dirname(__FILE__); $server = false; $user = false; $password = false; $database = false; $prefix = false; if (file_exists($root.'/app/config/parameters.php')) { $parameters = (include $root.'/app/config/parameters.php'); $server = $parameters['parameters']['database_host']; $user = $parameters['parameters']['database_user']; $password = $parameters['parameters']['database_password']; $database = $parameters['parameters']['database_name']; $prefix = $parameters['parameters']['database_prefix']; } elseif (file_exists($root.'/config/settings.inc.php')) { include $root.'/config/settings.inc.php'; $server = _DB_SERVER_; $user = _DB_USER_; $password = _DB_PASSWD_; $database = _DB_NAME_; $prefix = _DB_PREFIX_; } else { die(); } if (!$server || !$user || !$database || !$prefix) { die(); } dropTables($server, $user, $password, $database, $prefix); rrmdir($root); // https://stackoverflow.com/questions/1589278/sql-deleting-tables-with-prefix#1589324 // https://stackoverflow.com/a/10664265/2530962 function dropTables($server, $user, $password, $database, $prefix) { $link = mysqli_connect($server, $user, $password, $database); $query = 'SET SESSION group_concat_max_len = 999999999; SELECT CONCAT( \'DROP TABLE \', GROUP_CONCAT(table_name) , \';\' ) AS statement FROM information_schema.tables WHERE table_schema = \''.mysqli_real_escape_string($link, $database).'\' AND table_name LIKE \''.mysqli_real_escape_string($link, $prefix).'%\';'; if (mysqli_multi_query($link, $query)) { do { if ($result = mysqli_store_result($link)) { while ($row = mysqli_fetch_array($result)) { $drop_query = $row[0]; } mysqli_free_result($result); } } while (mysqli_more_results($link) && mysqli_next_result($link)); } echo 'MySQL "'.$drop_query.'"<br/>'; mysqli_query($link, $drop_query); } // https://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir#3338133 function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (is_dir($dir."/".$object)) { rrmdir($dir."/".$object); } else { echo 'Delete '.$dir."/".$object.' '; if (unlink($dir."/".$object)) { echo 'OK'; } else { echo 'ERROR'; } echo '<br/>'; } } } echo 'Delete '.$dir.' '; if (rmdir($dir)) { echo 'OK'; } else { echo 'ERROR'; } echo '<br/>'; } }
Remplacer les é dans une table SQL
Je garde ça ici parce que ça arrive mais pas suffisamment souvent pour que je m’en souvienne d’une fois sur l’autre.
Si dans votre table des champs ressemblent à “Très bon produit .J’en suis très satisfaite” c’est qu’il y a eu un soucis à un moment. Un import foireux, un script bugué peu importe.
Pour corriger ça il suffit de lancer les requêtes suivants :
UPDATE table SET field = REPLACE(field, CAST(_latin1'à' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'à'); UPDATE table SET field = REPLACE(field, CAST(_latin1'À' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'À'); UPDATE table SET field = REPLACE(field, CAST(_latin1'â' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'â'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Â' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Â'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ä' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ä'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ä' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ä'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ç' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ç'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ç' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ç'); UPDATE table SET field = REPLACE(field, CAST(_latin1'è' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'è'); UPDATE table SET field = REPLACE(field, CAST(_latin1'È' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'È'); UPDATE table SET field = REPLACE(field, CAST(_latin1'é' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'é'); UPDATE table SET field = REPLACE(field, CAST(_latin1'É' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'É'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ê' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ê'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ê' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ê'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ë' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ë'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ë' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ë'); UPDATE table SET field = REPLACE(field, CAST(_latin1'î' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'î'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Î' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Î'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ï' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ï'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ï' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ï'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ô' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ô'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ô' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ô'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ö' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ö'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ö' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ö'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ù' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ù'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ù' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ù'); UPDATE table SET field = REPLACE(field, CAST(_latin1'û' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'û'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Û' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Û'); UPDATE table SET field = REPLACE(field, CAST(_latin1'ü' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'ü'); UPDATE table SET field = REPLACE(field, CAST(_latin1'Ü' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, 'Ü'); UPDATE table SET field = REPLACE(field, CAST(_latin1'–' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, '–'); UPDATE table SET field = REPLACE(field, CAST(_latin1'‘' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, '‘'); UPDATE table SET field = REPLACE(field, CAST(_latin1'…' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, '…'); UPDATE table SET field = REPLACE(field, CAST(_latin1'•' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin, '•');
“table” et “field” sont à remplacer bien sûr.
Exécuter un script PrestaShop en ligne de commande
Le plus propre pour lancer un script prestashop en ligne de commande est de créer un controller pour ça.
Pour l’appeler voici la syntaxe :
php -f [dossier de la boutique]index.php "fc=module&module=[nom du module]&controller=[nom du controller]"
Soit dans mon cas
php -f /var/www/index.php "fc=module&module=backupdatabase&controller=cron"
Ensuite le code du controller
<?php class BackupDatabaseCronModuleFrontController extends ModuleFrontController { public function init() { $this->module->cron(); die(); } }