Skip to content

Instantly share code, notes, and snippets.

View dotancohen's full-sized avatar

Dotan Cohen dotancohen

View GitHub Profile
@dotancohen
dotancohen / db-get-column-names-common
Created February 14, 2022 09:43
Get most common column names from SQL dump file.
grep -P "^\s+\`" foo.sql | \
sed -E 's/\([0-9]*/(/' | \
awk '{print $1 "\t" $2}' | \
sort | \
uniq -c | \
sort -n -r > db-common-column-names
# 300 MiB SQL file: 0.4 seconds
@dotancohen
dotancohen / svn-diff-ignore
Last active November 9, 2020 21:38
Show diff for files not in .svnignore file. BE CAREFUL TO SPECIFY FILENAMES WHEN COMMITTING!
#!/bin/bash
# BE CAREFUL TO SPECIFY FILENAMES WHEN COMMITTING!
IGNORE_FILENAME=.svnignore
svn status | awk '{print $2}' | while read FILE ; do
grep -x $FILE $IGNORE_FILENAME > /dev/null
STATUS=$?
if ! $(exit $STATUS) ; then
@dotancohen
dotancohen / use-remote-media.php
Created May 10, 2020 09:08 — forked from kingkool68/use-remote-media.php
Check if a local file exists in the WordPress media library and if it doesn't exist, replace the URL with a different URL. Helpful when working with a local site that doesn't have all of the media downloaded as the production site. See https://localwp.com/community/t/proxying-requests-to-wp-content-uploads-to-a-production-site/15701
<?php
// Put this in wp-config.php and replace https://example.com/ with the URL of the production site.
define( 'RH_USE_REMOTE_MEDIA_URL', 'https://example.com/' );
// Put the rest of this in functions.php or a custom plugin or somewhere else.
if ( defined( 'RH_USE_REMOTE_MEDIA_URL' ) && ! empty( RH_USE_REMOTE_MEDIA_URL ) ) {
add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_wp_get_attachment_image_src' ) );
add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_wp_calculate_image_srcset' ) );
add_filter( 'wp_get_attachment_url', array( $this, 'filter_wp_get_attachment_url' ) );
}
@dotancohen
dotancohen / Contract Killer 3.md
Created January 24, 2019 07:53 — forked from malarkey/Contract Killer 3.md
The latest version of my ‘killer contract’ for web designers and developers

Contract Killer

The popular open-source contract for web professionals by Stuff & Nonsense

  • Originally published: 23rd December 2008
  • Revised date: March 15th 2016
  • Original post

<?php
/**
* Properly format a user-entered URL for use in an href or src attribute.
*
* @param string $url User-entered URL
* @return string Formatted URL
*/
function url_esc(string $url) : string
{
@dotancohen
dotancohen / localurl.sh
Created May 7, 2018 11:41
Output URLs of the website whose DOCUMENT_ROOT is the current directory.
# Output URLs of the website whose DOCUMENT_ROOT is the current directory.
# Reads from hosts file and Apache config
alias localurl='grep "$(grep -r -C3 $(pwd) /etc/httpd/vhost.d/ | grep -ioP "servername(.*)|serveralias(.*)" | awk '\''{print $2}'\'')" /etc/hosts | awk '\''{print $2}'\'' '
@dotancohen
dotancohen / sshhosts
Created January 1, 2018 10:06 — forked from yaronuliel/sshhosts.sh
Connect to preconfigure ssh hosts (set in ~/.ssh/config) via simple list with numbers
#!/usr/bin/env bash
get_hosts="cat ~/.ssh/config | grep Host\\\\s | awk '{ print \$2 }' | sort -u"
if [[ $# -eq 0 ]] ; then
eval "$get_hosts | nl -ba"
exit;
fi
host_num=$1
grep -roE "do_action\s*\(\s*['\"](\w+)" . | awk -F"'|\"" '{print $2}' | sort -u
@dotancohen
dotancohen / mysql-alias.md
Last active May 16, 2017 08:33
Log into MySQL databases easily and securely

Log into MySQL databases via preconfigured aliases. Note that the alias name 'foo' is to be identified in the config file as 'clientfoo'.

$ cat ~/.bashrc

my() {
    /usr/bin/mysql --defaults-group-suffix="$@"
}
@dotancohen
dotancohen / what-country.sh
Last active May 16, 2017 07:14
What country is that website hosted in?
whois $(dig stackoverflow.com | grep -P "IN\s*A(AAA)?\s*\d" | awk '{print $5}' | head -n1) | grep -i country