#!/bin/bash
# Mediawiki

#
# MediaWiki is a free software wiki package written in PHP.
# It's originally for use on Wikipedia, however nowadays
# it is also used by several other projects of the non-profit
# Wikimedia Foundation and by many other wikis
#
# Being a web-application, the mediawikibuild installer will
# also install mariadb and httpd from their corresponding
# shellpacks.
#
# README about versioning. For mmtests, an older 1.8 based
# version of mediawiki is used as it is only compatible with
# the available database schemas as well as external tools
# to load wikipedia dumps. For instance, modern versions of
# mediawiki's importDump.php is extremely slow, while wmdumper
# is orders of magnitude faster, yet stale as it cannot handle
# modern (2015) dumps. Additionally, users of this web-app
# (ie: wikibench requires 1.18 as that's the data available
# of or the traces) can require certain versions. In the future
# this can be extended to pass from the shellpack user. Also
# see comments in create_wikipedia_db().
#

###SHELLPACK preamble mediawikibuild-install 1.18.6

# The older version is used as it's compatible with the sample files
#MEDIAWIKI_VERSION=1.25.3
#WEB_LOCATION=https://releases.wikimedia.org/mediawiki/1.25/
MEDIAWIKI_VERSION=1.18.6
WEB_LOCATION=https://releases.wikimedia.org/mediawiki/1.18/
MIRROR_LOCATION="$WEBROOT/mediawiki/"

MARIADB_VERSION=10.2.14
MARIADB_DBNAME=my_wiki
MARIADB_ADMIN_USER=root
MARIADB_ADMIN_PASSWORD=mmtests-default
MYSQLCMD="${SHELLPACK_SOURCES}/mariadbbuild-${MARIADB_VERSION}-installed/bin/mysql -u $MARIADB_ADMIN_USER -p$MARIADB_ADMIN_PASSWORD"
MYSQLADMIN="${SHELLPACK_SOURCES}/mariadbbuild-${MARIADB_VERSION}-installed/bin/mysqladmin -u $MARIADB_ADMIN_USER -p$MARIADB_ADMIN_PASSWORD"

APACHE_VERSION=2.4.17
PHP_VERSION=5.6.15
PHPCMD=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/bin/php

FIRST_DB_INSTALL=false

# Check requirements
if [ $TESTDISK_PRIMARY_SIZE_BYTES -le $((100*1048576*1024)) ]; then
	die Must be at least 100GB free disk space
fi

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

###SHELLPACK sources_fetch mediawiki-${MEDIAWIKI_VERSION}.tar.gz mediawikibuild-${MEDIAWIKI_VERSION}

# PART 1: Check/install dependencies and setup htdoc related dirs
if [ ! -e $SHELLPACK_SOURCES/apache-build-${APACHE_VERSION}-installed ]; then
	echo Installing apache server
	$SHELLPACK_INCLUDE/shellpack-install-apachebuild -v ${APACHE_VERSION} || die Failed to install apache httpd
	echo Apache server successfully installed.
fi

# Now install mariadb/mysql
if [ ! -e $SHELLPACK_SOURCES/mariadb-build-${MARIADB_VERSION}-installed ]; then
	echo Installing mariadb server
	FIRST_DB_INSTALL=true
	$SHELLPACK_INCLUDE/shellpack-install-mariadbbuild -v ${MARIADB_VERSION} || die Failed to install mariadb
	echo MariaDB server successfully installed.
fi

# With the apache base installed, put the mediawiki software into htdocs.
MEDIAWIKI_HTDOCS=$SHELLPACK_DATA/mediawiki-${APACHE_VERSION}/htdocs/mediawiki/
mkdir -p $MEDIAWIKI_HTDOCS

# -- see below how we set scriptpath in install.php such that urls are 'mediawiki'
eval cp -rf $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION}/* \
    $MEDIAWIKI_HTDOCS || die Failed to setup mediawiki

# -- needed for the rest of mmtests (looks for -installed)
eval mv -f $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION} $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION}-installed || die Failed to setup mediawiki
# -- ensure image file uploads directory is writable
eval chmod a+w $MEDIAWIKI_HTDOCS/images || die Faild to setup mediawiki


# PART 2: Mediawiki install and configuration (we need to have the DB started)

# this influences innodb_buffer_pool_instances, but
# let's mariadb handle that automatically, so just
# change the pool size.
BUFF_POOL_SIZE=$(($MEMTOTAL_BYTES/2))

# most of these options are based on the fact that a large
# wikipedia dump is coming next, lets try to optimize the
# database in what we can.
DBSTART_OPTIONS="--innodb_flush_method=nosync,--innodb_flush_log_at_trx_commit=0,--innodb_buffer_pool_size=${BUFF_POOL_SIZE},--innodb_log_file_size=512M,--max_allowed_packet=1G"

$SHELLPACK_INCLUDE/shellpack-bench-mariadbbuild --start \
	--start_opts $DBSTART_OPTIONS \
	--effective_cachesize $((MEMTOTAL_BYTES*6/10)) \
	--shared_buffers $((MEMTOTAL_BYTES/4)) \
	--work_mem $((16*1048576)) || die Failed to get usable database installation

# watchout of character set issues, utf8 and binary are encouraged.
$MYSQLCMD -e "CREATE DATABASE IF NOT EXISTS ${MARIADB_DBNAME} CHARACTER SET utf8 COLLATE utf8_general_ci"

# Create the mediawiki settings manually (use the same creds for DB
# and mediawiki admin) this will create the LocalSettings.php for us.
if [ ! -e $$MEDIAWIKI_HTDOCS/LocalSettings.php ] || [ $FIRST_DB_INSTALL = "true" ]; then
    $PHPCMD $MEDIAWIKI_HTDOCS/maintenance/install.php \
	--dbname $MARIADB_DBNAME \
	--dbpass $MARIADB_ADMIN_PASSWORD \
	--dbserver localhost \
	--dbtype mysql \
	--dbuser $MARIADB_ADMIN_USER \
	--email showmedamoney@lala.com \
	--installdbpass $MARIADB_ADMIN_PASSWORD \
	--installdbuser $MARIADB_ADMIN_USER \
	--pass $MARIADB_ADMIN_PASSWORD \
	--conf $MEDIAWIKI_HTDOCS/LocalSettings.php \
	--scriptpath /mediawiki mmtests-wikipedia $MARIADB_ADMIN_USER

    if [ ! -e $MEDIAWIKI_HTDOCS/LocalSettings.php ]; then
	die Failed to setup mediawiki
    fi

    # install HitCounter extension, this updates the DB with some new
    # tables required by some more up-to-date versions, depending on
    # the used dump. Uncomment the below if using mediawiki 1.25 or
    # above.
#    wget -c https://extdist.wmflabs.org/dist/extensions/HitCounters-REL1_25-7e36831.tar.gz
#    tar xf HitCounters-REL1_25-7e36831.tar.gz
#    mv -f HitCounters $MEDIAWIKI_HTDOCS/extensions/
#    echo "wfLoadExtension( 'HitCounters' );" >> $MEDIAWIKI_HTDOCS/LocalSettings.php

    # Uncomment if we ever want to upgrade from 1.18 to 1.25.
    # Also uncomment this if installing HitCounter (or any other)
    # mediawiki extension.
#    $PHPCMD $MEDIAWIKI_HTDOCS/maintenance/update.php --quick
fi

$SHELLPACK_INCLUDE/shellpack-bench-mariadbbuild --stop

echo mediawikibuild installed successfully
