Skip to content

Instantly share code, notes, and snippets.

View joostvanveen's full-sized avatar

Joost van Veen joostvanveen

View GitHub Profile
@joostvanveen
joostvanveen / check_prefixes_and_autoincrements_for_magento_2_orders.sql
Created October 13, 2020 08:00
Find incorrect prefixes for order and invoice settings in Magebto 2. Sometimes, after using the data migration tool, Magento uses the wrong prefixes. This usually becomes unnoticed at first and can quickly become a disaster, leading to duplicate order numbers. Magento uses tables to work out which prefixes to use on which store, and also to work…
-- Get all stores where orders, invoices, etc use the wrong prefix
-- E.g. the '2' in order number '200002345'
SELECT store_id, prefix, entity_type, sequence_table FROM sales_sequence_meta
JOIN sales_sequence_profile ON sales_sequence_profile.meta_id = sales_sequence_meta.meta_id
WHERE prefix <> store_id
ORDER BY store_id;
-- Get all stores that use the wrong prefix autoincrement table for orders, invoices, etc.
-- E.g. the '2345' in order number '200002345'
@joostvanveen
joostvanveen / delete_files_continaing_a_string.sh
Last active July 27, 2020 17:53
Delete log files containing a certain string. Handy to clean up Magento reports after committing a fix.
# Delete log files containing a certain string.
# Handy to clean up Magento reports after committing a fix.
find var/report/* -exec grep -q 'A non-numeric value encountered' '{}' \; -delete
# If you ant to chcek first:
find var/report/* -exec grep -q 'A non-numeric value encountered' '{}' \; -find
@joostvanveen
joostvanveen / top-20-user-agents-and-ips-from-directadmin-logs.sh
Created February 17, 2019 21:25
Loop though all rotated access logs and get the top 20 list of User AgentsThis can be used to identify bots
# Loop though all rotated Directadmin access logs and get the top 20 list of User Agents
# This can be used to identify bots
zcat /home/USER/domains/DOMAIN/logs/LOGNAME.tar.gz* | awk -F\" '{print $6}' | sort | uniq -c | sort -nr | head -20
# Get top 20 IP addresses
zcat /home/USER/domains/DOMAIN/logs/Feb-2019.tar.gz | awk -F\" '{print $1}' | sort | uniq -c | sort -nr | head -20
# Get top 20 most visited URLs
zcat /home/USER/domains/DOMAIN/logs/Feb-2019.tar.gz | awk -F\" '{print $4}' | sort | uniq -c | sort -nr | head -20
@joostvanveen
joostvanveen / .htaccess
Last active April 1, 2020 05:26
Force trailing slash for SEO purposes using htaccess 301 redirects (mod_rewrite) - but on GET requests only, to avoid losing POST data on a POST request to a URI without a trailing slash.
# Force trailing slash for SEO purposes
RewriteEngine On
# For GET and HEAD requests only. We do not want to redirect posted forms and such, or we'll lose all POST data!
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
# Not for actual files. We do not want to redirect urls like test.jpg to test.jpg/
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect to trailing slash if no slash is present in URI
@joostvanveen
joostvanveen / convert_to_utf8.php
Created December 31, 2012 10:52
Check to see if a string is UTF-8 and convert it if it's not. Accidentally posted this as anonymous before :(
<?php
function getUtf8String($string) {
if ( !isUtf8($string) )
return utf8_encode($string);
return $string;
}
function isUtf8($string) {
if ( function_exists("mb_check_encoding") ) {
return mb_check_encoding($string, 'UTF8');
@joostvanveen
joostvanveen / convert_windows_cp1252_to_latin1.php
Created December 31, 2012 10:59
Convert a Windows CP1252 string to Latin1. Converts the typical Windows (Word) crappy display characters like curly quotation marks, triple dots, etc. Windows-1252 or CP-1252 is a character encoding of the Latin alphabet, used by default in the legacy components of Microsoft Windows in English and some other Western languages. It is one version …
<?php
function transcribe_cp1252_to_latin1($cp1252) {
return strtr(
$cp1252,
array(
"\x80" => "e", "\x81" => " ", "\x82" => "'", "\x83" => 'f',
"\x84" => '"', "\x85" => "…", "\x86" => "+", "\x87" => "#",
"\x88" => "^", "\x89" => "0/00", "\x8A" => "S", "\x8B" => "<",
"\x8C" => "OE", "\x8D" => " ", "\x8E" => "Z", "\x8F" => " ",
"\x90" => " ", "\x91" => "`", "\x92" => "'", "\x93" => '"',
@joostvanveen
joostvanveen / clean_magento_product_images.sh
Created April 25, 2018 11:20
Clean old product images cache and orphaned product images for Magento 1
# Delete all cached images older than 365 days
find ~/public/media/catalog/product/cache/* -type f -atime +365 -exec rm {} \;
# Remove product images that are not present in the database
magerun media:images:removeorphans
@joostvanveen
joostvanveen / textile_filter.php
Created March 3, 2013 17:47
A pre_replace filter that strip out all characters that are NOT letters or numbers or Textile Markup special characters. You can use this to filter user input. If you have any improvements, let me know!
<?php
/**
* Filter input based on a whitelist. This filter strips out all characters that
* are NOT:
* - letters
* - numbers
* - Textile Markup special characters.
*
* Textile markup special characters are:
@joostvanveen
joostvanveen / mysql_alter_charset_and_collation_to_utf8.sql
Created May 29, 2019 05:20
Alter the charset and collation to UTF8 for a database and a table. Also inspects the default charset and collation for the entire database.
-- Inspect the default charset and collation for your database
SELECT default_character_set_name FROM information_schema.schemata WHERE schema_name = 'MYDATABASE_NAME';
-- Alter the default charset and collation for your database
-- CAREFUL: when you change the charset for your database, make sure the data in your tables matches that character set.
-- If it is not, follow these steps: http://www.alphadevx.com/a/420-Converting-a-MySQL-database-from-latin1-to-utf8
ALTER DATABASE MYDATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci;
-- Alter the default charset and collation for your database
ALTER TABLE MYTABLE_NAME CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
@joostvanveen
joostvanveen / .gitignore
Last active April 10, 2019 08:54
Default gitignore entrances for IDEs, OS specific hidden files, tarballs and compressed files. Use this for new projects.
############ Project specific rules ###############
# Project specific rules go here...
# To ignore a folder, add asterix -> folder/*
# This way, unignored files like .keep and .gitkeep will still be added to git
################# Global rules ####################
## Never ignore .gitignore
!.gitignore
## Dependencies