<commit_before>#!/bin/bash

set -e   # (errexit) Exit if any subcommand or pipeline returns a non-zero status
set -u   # (nounset) Exit on any attempt to use an uninitialised variable

# Allow Dockerfile to override default document root, if necessary
: ${DOCUMENT_ROOT:=/var/www/html}
if [ "$DOCUMENT_ROOT" != "/" ]; then
	DOCUMENT_ROOT=${DOCUMENT_ROOT%/}    # Trim trailing slash
fi

# Allow Dockerfile to override default scripts directory
: ${SCRIPTS_DIR:=/scripts}
if [ "$SCRIPTS_DIR" != "/" ]; then
	SCRIPTS_DIR=${SCRIPTS_DIR%/}        # Trim trailing slash
fi

# Alias for WP-cli to include arguments that we want to use everywhere
shopt -s expand_aliases
alias wp="wp --path=$DOCUMENT_ROOT --allow-root"

# Environment variables that might have been set manually by user
: ${WORDPRESS_DB_NAME:=}
: ${WORDPRESS_DB_USER:=}
: ${WORDPRESS_DB_PASSWORD:=}
: ${WORDPRESS_DB_HOST:=}
: ${WORDPRESS_DB_PORT:=}
: ${WORDPRESS_DB_TABLE_PREFIX:=}

# Environment variables that might be set when linking to a MySQL container
: ${MYSQL_ENV_MYSQL_USER:=}
: ${MYSQL_ENV_MYSQL_PASSWORD:=}
: ${MYSQL_ENV_MYSQL_DATABASE:=}
: ${MYSQL_PORT_3306_TCP_ADDR:=}
: ${MYSQL_PORT_3306_TCP_PORT:=}

# Optional environment variables to toggle debug modes
: ${WP_DEBUG:=}
: ${WP_DEBUG_DISPLAY:=}
: ${WP_DEBUG_LOG:=}

# Optional environment variable to disable external HTTP requests
: ${WP_HTTP_BLOCK_EXTERNAL:=}

# Internal variables used to pass DB config between functions
db_name=
db_user=
db_host=
db_port=
db_pass=
db_pass_source=

# Path to WordPress configuration
config_path=${DOCUMENT_ROOT}/wp-config.php

# Paths to additional installation scripts
preinstall_scripts_dir=${SCRIPTS_DIR}/pre-install.d
postinstall_scripts_dir=${SCRIPTS_DIR}/post-install.d

#
# Extract database configuration from environment variables
#
# Precedence is given to the WORDPRESS_DB_USER, WORDPRESS_DB_PASSWORD,
# WORDPRESS_DB_HOST, WORDPRESS_DB_PORT and WORDPRESS_DB_NAME environment
# variables. When not present, MYSQL_ENV_* environment variables will be used
# where appropriate, with sane defaults used otherwise.
#
function parse_environment_variables() {
	db_name=${WORDPRESS_DB_NAME:-${MYSQL_ENV_MYSQL_DATABASE:-wordpress}}

	if [ "$WORDPRESS_DB_USER" ]; then
		db_user=$WORDPRESS_DB_USER
		if [ "WORDPRESS_DB_PASSWORD" ]; then
			db_pass=$<PASSWORD>
			db_pass_source=WORDPRESS_DB_PASSWORD
		elif [ "$WORDPRESS_DB_USER" == "$MYSQL_ENV_MYSQL_USER" ] && [ "$MYSQL_ENV_MYSQL_PASSWORD" ]; then
			db_pass=$MYSQL_ENV_MYSQL_PASSWORD
			db_pass_source=MYSQL_ENV_MYSQL_PASSWORD
		fi
	elif [ "$MYSQL_ENV_MYSQL_USER" ]; then
		db_user=$MYSQL_ENV_MYSQL_USER
		if [ "$MYSQL_ENV_MYSQL_PASSWORD" ]; then
			db_pass=$MYSQL_ENV_MYSQL_PASSWORD
			db_pass_source=MYSQL_ENV_MYSQL_PASSWORD
		fi
	else
		db_user=wordpress
	fi

	if [ "$WORDPRESS_DB_HOST"]; then
		db_host=$WORDPRESS_DB_HOST
		if [ "$MYSQL_PORT_3306_TCP_ADDR" ]; then
			echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP_ADDR found'
			echo >&2 "  Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)"
			echo >&2 '  instead of the linked mysql container'
		fi
		if [ "$WORDPRESS_DB_PORT" ]; then
			db_port=$WORDPRESS_DB_PORT
		elif [ "$MYSQL_PORT_3306_TCP_PORT" ] && [ "$WORDPRESS_DB_HOST" == "$MYSQL_PORT_3306_TCP_ADDR" ]; then
			db_port=$MYSQL_PORT_3306_TCP_PORT
		fi
	elif [ "$MYSQL_PORT_3306_TCP_ADDR" ]; then
		db_host=$MYSQL_PORT_3306_TCP_ADDR
		if [ "$MYSQL_PORT_3306_TCP_PORT" ]; then
			db_port=$MYSQL_PORT_3306_TCP_PORT
		fi
	else
		db_host=localhost
	fi

	if [ "$db_name" ]; then echo "Using DB name: $db_name"; fi
	if [ "$db_host" ]; then echo "Using DB host: $db_host"; fi
	if [ "$db_port" ]; then echo "Using DB port: $db_port"; fi
	if [ "$db_user" ]; then echo "Using DB username: $db_user"; fi
	if [ "$db_pass" ]; then echo "Using DB password from: \$$db_pass_source"; fi
}

#
# Wait up to 20 seconds for MySQL to become available
#
function wait_for_mysql() (
	echo "Waiting for MySQL to become available on $db_host..."
	local timeout=20
	while [ $timeout -gt 0 ]; do
		local response=`mysqladmin --host=$db_host --port=$db_port --user=UNKNOWN_MYSQL_USER ping 2>&1` && break
		echo "$response" | grep -q "Access denied for user" && break
		sleep 1
		let timeout=${timeout}-1
	done
	if [ $timeout -eq 0 ]; then
		echo "MySQL server could not be contacted."
		exit 1
	fi
	echo "MySQL ready."

	echo "Waiting for database '$db_name' to be ready..."
	let timeout=20
	while [ $timeout -gt 0 ]; do
		mysql --host=$db_host --port=$db_port --user=$db_user --password=$db_pass --execute="use $db_name;" && break
		sleep 1
		let timeout=${timeout}-1
	done
	if [ $timeout -eq 0 ]; then
		echo "MySQL server is up, but database '$db_name' is not accessible."
		exit 1
	fi
	echo "Database ready."
)
<commit_msg>Do not print MySQL errors while waiting for database to be created<commit_after>#!/bin/bash

set -e   # (errexit) Exit if any subcommand or pipeline returns a non-zero status
set -u   # (nounset) Exit on any attempt to use an uninitialised variable

# Allow Dockerfile to override default document root, if necessary
: ${DOCUMENT_ROOT:=/var/www/html}
if [ "$DOCUMENT_ROOT" != "/" ]; then
	DOCUMENT_ROOT=${DOCUMENT_ROOT%/}    # Trim trailing slash
fi

# Allow Dockerfile to override default scripts directory
: ${SCRIPTS_DIR:=/scripts}
if [ "$SCRIPTS_DIR" != "/" ]; then
	SCRIPTS_DIR=${SCRIPTS_DIR%/}        # Trim trailing slash
fi

# Alias for WP-cli to include arguments that we want to use everywhere
shopt -s expand_aliases
alias wp="wp --path=$DOCUMENT_ROOT --allow-root"

# Environment variables that might have been set manually by user
: ${WORDPRESS_DB_NAME:=}
: ${WORDPRESS_DB_USER:=}
: ${WORDPRESS_DB_PASSWORD:=}
: ${WORDPRESS_DB_HOST:=}
: ${WORDPRESS_DB_PORT:=}
: ${WORDPRESS_DB_TABLE_PREFIX:=}

# Environment variables that might be set when linking to a MySQL container
: ${MYSQL_ENV_MYSQL_USER:=}
: ${MYSQL_ENV_MYSQL_PASSWORD:=}
: ${MYSQL_ENV_MYSQL_DATABASE:=}
: ${MYSQL_PORT_3306_TCP_ADDR:=}
: ${MYSQL_PORT_3306_TCP_PORT:=}

# Optional environment variables to toggle debug modes
: ${WP_DEBUG:=}
: ${WP_DEBUG_DISPLAY:=}
: ${WP_DEBUG_LOG:=}

# Optional environment variable to disable external HTTP requests
: ${WP_HTTP_BLOCK_EXTERNAL:=}

# Internal variables used to pass DB config between functions
db_name=
db_user=
db_host=
db_port=
db_pass=
db_pass_source=

# Path to WordPress configuration
config_path=${DOCUMENT_ROOT}/wp-config.php

# Paths to additional installation scripts
preinstall_scripts_dir=${SCRIPTS_DIR}/pre-install.d
postinstall_scripts_dir=${SCRIPTS_DIR}/post-install.d

#
# Extract database configuration from environment variables
#
# Precedence is given to the WORDPRESS_DB_USER, WORDPRESS_DB_PASSWORD,
# WORDPRESS_DB_HOST, WORDPRESS_DB_PORT and WORDPRESS_DB_NAME environment
# variables. When not present, MYSQL_ENV_* environment variables will be used
# where appropriate, with sane defaults used otherwise.
#
function parse_environment_variables() {
	db_name=${WORDPRESS_DB_NAME:-${MYSQL_ENV_MYSQL_DATABASE:-wordpress}}

	if [ "$WORDPRESS_DB_USER" ]; then
		db_user=$WORDPRESS_DB_USER
		if [ "WORDPRESS_DB_PASSWORD" ]; then
			db_pass=$WORD<PASSWORD>
			db_pass_source=WORDPRESS_DB_PASSWORD
		elif [ "$WORDPRESS_DB_USER" == "$MYSQL_ENV_MYSQL_USER" ] && [ "$MYSQL_ENV_MYSQL_PASSWORD" ]; then
			db_pass=$MYSQL_ENV_MYSQL_PASSWORD
			db_pass_source=MYSQL_ENV_MYSQL_PASSWORD
		fi
	elif [ "$MYSQL_ENV_MYSQL_USER" ]; then
		db_user=$MYSQL_ENV_MYSQL_USER
		if [ "$MYSQL_ENV_MYSQL_PASSWORD" ]; then
			db_pass=$MYSQL_ENV_MYSQL_PASSWORD
			db_pass_source=MYSQL_ENV_MYSQL_PASSWORD
		fi
	else
		db_user=wordpress
	fi

	if [ "$WORDPRESS_DB_HOST"]; then
		db_host=$WORDPRESS_DB_HOST
		if [ "$MYSQL_PORT_3306_TCP_ADDR" ]; then
			echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP_ADDR found'
			echo >&2 "  Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)"
			echo >&2 '  instead of the linked mysql container'
		fi
		if [ "$WORDPRESS_DB_PORT" ]; then
			db_port=$WORDPRESS_DB_PORT
		elif [ "$MYSQL_PORT_3306_TCP_PORT" ] && [ "$WORDPRESS_DB_HOST" == "$MYSQL_PORT_3306_TCP_ADDR" ]; then
			db_port=$MYSQL_PORT_3306_TCP_PORT
		fi
	elif [ "$MYSQL_PORT_3306_TCP_ADDR" ]; then
		db_host=$MYSQL_PORT_3306_TCP_ADDR
		if [ "$MYSQL_PORT_3306_TCP_PORT" ]; then
			db_port=$MYSQL_PORT_3306_TCP_PORT
		fi
	else
		db_host=localhost
	fi

	if [ "$db_name" ]; then echo "Using DB name: $db_name"; fi
	if [ "$db_host" ]; then echo "Using DB host: $db_host"; fi
	if [ "$db_port" ]; then echo "Using DB port: $db_port"; fi
	if [ "$db_user" ]; then echo "Using DB username: $db_user"; fi
	if [ "$db_pass" ]; then echo "Using DB password from: \$$db_pass_source"; fi
}

#
# Wait up to 20 seconds for MySQL to become available
#
function wait_for_mysql() (
	echo "Waiting for MySQL to become available on $db_host..."
	local timeout=20
	while [ $timeout -gt 0 ]; do
		local response=`mysqladmin --host=$db_host --port=$db_port --user=UNKNOWN_MYSQL_USER ping 2>&1` && break
		echo "$response" | grep -q "Access denied for user" && break
		sleep 1
		let timeout=${timeout}-1
	done
	if [ $timeout -eq 0 ]; then
		echo "MySQL server could not be contacted."
		exit 1
	fi
	echo "MySQL ready."

	echo "Waiting for database '$db_name' to be ready..."
	let timeout=20
	while [ $timeout -gt 0 ]; do
		local response=`mysql --host=$db_host --port=$db_port --user=$db_user --password=$<PASSWORD> --execute="use $db_name;" 2>&1` && break
		echo "$response" | grep -q "Access denied for user" && break
		sleep 1
		let timeout=${timeout}-1
	done
	if [ $timeout -eq 0 ]; then
		echo "MySQL server is up, but database '$db_name' is not accessible."
		exit 1
	fi
	echo "Database ready."
)

{'fraction_non_alphanumeric': 0.11547631387996268, 'fraction_numerical': 0.013683010262257697, 'mean_word_length': 5.121827411167513, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4584708', 'n_tokens_mistral': 3666, 'n_tokens_neox': 3488, 'n_words': 908}