#!/bin/bash
#-
# MediaWiki Plugin full dump/restore for FusionForge
#
# Copyright © 2013
#	Thorsten “mirabilos” Glaser <t.glaser@tarent.de>
# Copyright (C) 2014  Inria (Sylvain Beucler)
# All rights reserved.
#
# This file is part of FusionForge. FusionForge is free software;
# you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software
# Foundation; either version 2 of the Licence, or (at your option)
# any later version.
#
# FusionForge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with FusionForge; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Dumps all pages of a FusionForge MediaWiki plugin project, with their
# complete history, plus all images and other files, into a tarball to
# be imported into another wiki by mw-fullrestore.

wikiprojectdir=$(forge_get_config projects_path mediawiki)/$1
if [[ $# != 1 || $1 = *[[:space:]]* ]]; then
	echo 'Syntax: mw-fulldump projectname' 1>&2
	exit 255
fi
if [[ -e $1.fulldump ]]; then
	echo "Dump already exists: $1.fulldump" 1>&2
	exit 255
fi
# The wiki may not have uploaded any image, don't check.
#if [[ ! -d $wikiprojectdir/images/. ]]; then
#	echo "Couldn't find image directory $wikiprojectdir/images/"
#	exit 255
#fi

wd=$PWD
set -x
set -e
rm -rf "$wikiprojectdir/fulldump"
mkdir "$wikiprojectdir/fulldump"
cd "$wikiprojectdir/fulldump"
$(forge_get_config source_path)/plugins/mediawiki/bin/mw-wrapper.php "$1" \
    dumpUploads.php >mf1.txt
sed \
    "smwstore://local-backend/local-public$wikiprojectdir/images" \
    <mf1.txt >mf2.txt
mkdir mfi
if [[ -s mf2.txt ]]; then
	ln $(<mf2.txt) mfi/
fi
$(forge_get_config source_path)/plugins/mediawiki/bin/mw-wrapper.php "$1" \
    dumpBackup.php --full >df.xml
rm -f mf?.txt
cd ..
tar czf "$wd/$1.fulldump" --numeric-owner --owner=0 --group=0 fulldump
rm -rf fulldump
exit 0
