File mediawiki-admin of Package mediawiki

#!/bin/sh

# ### 
# Administer mediawiki-installations
#
# (c) Johannes Weberhofer, Weberhofer GmbH, 2011, 2020
#
# This script is licensed under the GPL
#


MW_SOURCE=/usr/share/php/mediawiki
MW_MAIN_INSTANCE=/srv/www/mediawiki/webroot
MW_MAIN_LEGACY_INSTANCE=/var/lib/mediawiki/webroot
ADMIN_LOG_FILE=/var/log/mediawiki-update.log
ASYNC_LOG_FILE=/var/log/mediawiki-async.log

HTTP_USER=wwwrun
HTTP_GROUP=www

INSTANCES_FILE=/etc/mediawiki-installations
LOCK_FILE=.mediawiki-admin-lock

SCRIPTDIR=`dirname $0`

#########################################
# If set, do for all specified instances
ALL=0
# show help
COMMAND=HELP
# parameter used for packaging purposes
BUILD=0
# build-prefix is empty if not set
PREFIX_PLAIN=''
PREFIX=''

while [ $# -ge 1 ] ; do
	case "$1" in
	"async-tasks")
		COMMAND=ASYNC_TASKS
		# get base-paths
		shift
		if ( [ -z "$1" ] || [ "$1" != "--all" ] ) ; then
			echo "Specify MediaWiki base directory or set parameter --all run async tasks for all instances!" 1>&2
			exit 1
		fi
		if [ "$1" == '--all' ] ; then
			ALL=1
		else
			BASE_DIRECTORY="$1"
		fi
		shift
		;;
	"upgrade")
		COMMAND=UPGRADE
		# get base-paths
		shift
		if ( [ -z "$1" ] || [ "$1" != "--all" ] ) ; then
			echo "Specify MediaWiki base directory or set parameter --all to upgrde all instances!" 1>&2
			exit 1
		fi
		if [ "$1" == '--all' ] ; then
			ALL=1
		else
			BASE_DIRECTORY="$1"
		fi
		;;
	"install")
		COMMAND=INSTALL
		# get base-path
		shift
		if ( [ -n "$1" ] && [ -d `dirname "$1"` ] ) ; then
			if [ -d "$1" ] ; then
				echo "MediaWiki installation exists in $1" 1>&2
				exit 1
			else
				BASE_DIRECTORY=`readlink -f "$1"`
			fi
		else
			echo "Specify a valid MediaWiki base directory $1" 1>&2
			exit 1
		fi
		
		# if prexix is set: use it for packaging purposes
		shift
		if [ -n "$1" ] ; then 
			if [ -d "$1" ] ; then
				PREFIX_PLAIN=`readlink -f "$1"`
				PREFIX="$PREFIX_PLAIN/"
				BUILD=1
			else
				echo "Invalid packaging prefix $1" 1>&2
				exit 1
			fi
		fi
		;;
	*)
		echo "Unknown option: $1" 1>&2
		exit 1
		;;
	esac
	shift
done
if [ -z "$PREFIX_PLAIN" ] ; then
	MW_FILELIST_SOURCE="$MW_SOURCE"
else
	MW_FILELIST_SOURCE="$PREFIX_PLAIN/$MW_SOURCE"
fi

if [ -e "$MW_FILELIST_SOURCE/mediawiki-files" ] ; then
	. "$MW_FILELIST_SOURCE/mediawiki-files"
	# Read MW_DOCFILES MW_MAKEDIRS MW_LINKFILES MW_REMOVEFILES
else
	echo "Filelist not found in $MW_FILELIST_SOURCE/mediawiki-files" 1>&2
	exit 4
fi

#
# usage section
#
if [ $COMMAND == 'HELP' ] ; then
	echo \
"Usage: "`basename $0`" COMMAND [ARGS...]

Mediawiki administration tool

commands:
    async-tasks [ base-directory | --all ]: Run async tasks
    upgrade     [ base-directory | --all ]: Upgrade instances
    install     base-directory [prefix]   : Create a new instance. 
                                            The prefix must only be set for packaging purposes 

Specifying the --all parameter invokes the specified command for all installations
defined in $INSTANCES_FILE. This file must contain a single line for each instance,
containing the instance's base-path.
"
	exit 0
fi

#
# upgrade an installation
#
function doInstall() {
	TARGETDIR=`echo "$BASE_DIRECTORY" | sed -e 's|//|/|g ; s|/$||'`
	#create hosting-relevant directories
	for DIR in $MW_SYSDIRS ; do
		mkdir -p "$PREFIX$TARGETDIR/$DIR"
		if [ $BUILD -eq 0 ] ; then
			chown $APACHEUSER. "$PREFIX$TARGETDIR/$DIR"
		fi
	done
	# Create mediawiki symlinks
	for CFILE in $MW_LINKFILES ; do
		ln -s "$MW_SOURCE/$CFILE" "$PREFIX$TARGETDIR/webroot/"
	done
	# Create mediawiki directories
	for CFILE in $MW_MAKEDIRS ; do
		mkdir "$PREFIX$TARGETDIR/webroot/$CFILE"
		chown $APACHEUSER. "$PREFIX$TARGETDIR/webroot/$CFILE"
	done
	
	if [ $BUILD -eq 0 ] ; then
		echo $TARGETDIR/webroot >> /etc/mediawiki-installations
	fi

	echo "### Suggested file for Alias-based wiki-configuration
### Have a look to the documentation in:
### /usr/share/doc/packages/mediawiki/README.DISTRIBUTION
### Enable next line to restrict downloads via img_auth.php
#	Alias /wiki/images         /var/lib/mediawiki/webroot/img_auth.php
### Enable next line for configuration
#       Alias /wiki/mw-config/     $TARGETDIR/webroot/mw-config/
	Alias /w/                  $TARGETDIR/webroot/
	Alias /wiki                $TARGETDIR/webroot/index.php

	<Directory $TARGETDIR/webroot>
	        Require all granted
		Options +FollowSymLinks
		php_admin_flag register_globals off
		php_admin_flag magic_quotes_gpc off
		php_admin_flag allow_url_include off
		php_admin_flag allow_url_fopen off
		php_admin_flag safe_mode Off
		php_admin_value session.save_path \"$TARGETDIR/session\"
		php_admin_value upload_tmp_dir \"$TARGETDIR/tmp\"
		php_admin_value sys_temp_dir \"$TARGETDIR/tmp\"
		php_admin_value open_basedir \"$TARGETDIR/:$MW_SOURCE/:/usr/share/php7/PEAR:/usr/bin/texvc\"
	</Directory>
	<Directory $TARGETDIR/webroot/images>
        # Protect against bug T30235
        <IfModule rewrite_module>
	      RewriteEngine On
	      RewriteOptions inherit
	      RewriteCond %{QUERY_STRING} \.[^\\/:*?\x22<>|%]+(#|\?|$) [nocase]
	      RewriteRule . - [forbidden]
	      # Fix for bug T64289
	      Options +FollowSymLinks
        </IfModule>
	</Directory>
	<Directory $TARGETDIR/webroot/includes>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/languages>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/maintenance>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/maintenance/archives>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/serialized>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/vendor>
	        Require all denied
	</Directory>
	<Directory $TARGETDIR/webroot/cache>
	        Require all denied
	</Directory>
"

	if [ $BUILD -eq 0 ] ; then
		echo "For installation the setup via command line is recommented"
		echo "A full explanation is available running this command:"
		echo "  php \"$MW_SOURCE/maintenance/install.php\""
		echo ""
		echo "The suggested command to install Mediawiki is:"
		echo "  php \"$MW_SOURCE/maintenance/install.php\" \\"
		echo "    --confpath \"$TARGETDIR/webroot\" \\"
		echo "    --server http://www.MYSERVER_NAME.COM --dbname MY_DATABASE --dbuser MYDB_USER --dbpass MYDB_PASSWORD \\"
		echo "    --dbtype mysql WIKINAME ADMIN_NAME --pass MY_ADMIN_PASSWORD"
	fi
}

#
# upgrade an installation
#
function doUpgrade() {
	WEBROOT=''
	if [ -e "$1/LocalSettings.php" ] ; then
		# try updating BASE directory
		WEBROOT=$1
	else
		# try updating BASE/webroot directory
		if [ -e "$1/webroot/LocalSettings.php" ] ; then
			WEBROOT=$1/webroot
		fi
	fi
	if [ -z "$WEBROOT" ] ; then
		echo "No Mediawiki-installation found in $1!" 1>&2
	else
		echo "Updating $WEBROOT"
		echo "**************************************************" >> "$ADMIN_LOG_FILE"
		echo `date` " Updating $WEBROOT" >> "$ADMIN_LOG_FILE"

		#delete old directories/mediawiki-files
		for CFILE in $MW_REMOVEFILES ; do
			if [ -d "$WEBROOT/$CFILE" ] ; then
				rmdir "$WEBROOT/$CFILE"
			else
				rm -f "$WEBROOT/$CFILE"
			fi
		done
		
		#check/recreate hosting-relevant directories
		for DIR in $MW_SYSDIRS ; do
			if [ ! -d "$WEBROOT/../$DIR" ] ; then
				mkdir -p "$WEBROOT/../$DIR"
				chown $HTTP_USER. "$WEBROOT/../$DIR"
			fi
		done
		# Create mediawiki symlinks
		for CFILE in $MW_LINKFILES ; do
			if [ -L "$WEBROOT/$CFILE" ] ; then
				if [ "$MW_SOURCE/$CFILE" != `readlink "$WEBROOT/$CFILE"` ] ; then
					rm "$WEBROOT/$CFILE"
					ln -s "$MW_SOURCE/$CFILE" "$WEBROOT/"
				fi
			else
				if [ -e "$WEBROOT/$CFILE" ] ; then
					rm -rf "$WEBROOT/$CFILE"
				fi
				ln -s "$MW_SOURCE/$CFILE" "$WEBROOT/"
			fi
		done
		# Create mediawiki directories
		for CFILE in $MW_MAKEDIRS ; do
			if [ ! -d "$WEBROOT/$CFILE" ] ; then
				mkdir "$WEBROOT/$CFILE"
			fi
			chown $HTTP_USER. "$WEBROOT/$CFILE"
		done
		
		# remove extensions directory: must be symlinked
		if [ -d "$WEBROOT/extensions" ] ; then
			if [ ! -L "$WEBROOT/extensions" ] ; then
				rm -f "$WEBROOT/extensions"
			fi
		fi

		# Remove broken symlinks
		find "$WEBROOT" -maxdepth 1 -xtype l -exec rm {} \;

		# run mediawiki update script
		php "$MW_SOURCE/maintenance/update.php" --quick --skip-external-dependencies --conf "$WEBROOT/LocalSettings.php" >> "$ADMIN_LOG_FILE" 2>&1
		STATUS=$?
		if [ $STATUS -ne 0 ] ; then
			echo "ERROR: Check $ADMIN_LOG_FILE"
		fi
	fi
}

#
# invoke the archive operation
#
function doAsyncTasks {
	if [ -e"$1/LocalSettings.php" ] ; then
		sudo -u $HTTP_USER /usr/bin/php \
			"$MW_SOURCE/maintenance/runJobs.php" --maxtime=3600 --conf "$1/LocalSettings.php" \
			>> "$ASYNC_LOG_FILE" 2>&1
	fi
}

#
# process the invoked command for an instance
#
function processInstance {
	if [ -e "$LOCK_FILE" ] ; then
		echo "Lock exists in $PWD" 1>&2
	else
		touch "$LOCK_FILE"
		case "$COMMAND" in
		"ASYNC_TASKS")
			doAsyncTasks "$1"
			;;
		"INSTALL")
			doInstall
			;;
		"UPGRADE")
			doUpgrade "$1"
			;;
		*)
			echo "No command given!" 1>&2
			;;
		esac
		rm "$LOCK_FILE"
	fi
}

if [ $ALL -eq 0 ] ; then
	# process a single instance
	processInstance "$BASE_DIRECTORY"
else
	# update legacy instance
	if [ -d "$MW_MAIN_LEGACY_INSTANCE" ] ; then
		processInstance "$MW_MAIN_LEGACY_INSTANCE"
	fi
	# update main instance
	if [ -d "$MW_MAIN_INSTANCE" ] ; then
		processInstance "$MW_MAIN_INSTANCE"
	fi

	# iterate over all instances
	if [ -e "$INSTANCES_FILE" ] ; then
		while read DIR
		do
			if [ -d "$DIR" ] ; then
				processInstance "$DIR"
			else
				echo "ERROR: No instance in $DIR" 1>&2
			fi
		done < "$INSTANCES_FILE"
	fi
fi

openSUSE Build Service is sponsored by