Mediawiki installation/create or upgrade vhost-wiki.sh

From Biowikifarm Metawiki
Jump to: navigation, search

Run this as a script ./create_or_upgrade_vhost-wiki.sh for instance from your home directory but not via copy and paste!!! The script is interactive and prompts before running. The concept is to have two different setup types available, one for simple feature-wikis and one for rich feature-wikis.

  • it takes care of /cache, /media if not specified in the global script variables
  • it needs an existing shared wiki resource /usr/share/mediawikiXXX ready to use (see ./create_or_upgrade_shared_wiki-core-resource-structure.sh)
  • it lists the steps it will do
  • it displays disc space information before running

For the different needed features the script needs one of the following structures in /usr/share/mediawikiXXX:

/usr/share/mediawiki27wmf                   v-host-wiki
├── extensions-rich-features/            -> ├── extensions/        
├── vendor-rich-features/                -> ├── vendor/            
├── composer-rich-features.json          -> ├── composer.json      
├── composer-rich-features.local.json    -> ├── composer.local.json (manage composer extensions here!!)
└── composer-rich-features.lock          -> └── composer.lock      

/usr/share/mediawiki27wmf                   v-host-wiki
├── extensions-simple-features/          -> ├── extensions/        
├── vendor-simple-features/              -> ├── vendor/            
├── composer-simple-features.json        -> ├── composer.json      
├── composer-simple-features.local.json  -> ├── composer.local.json (manage composer extensions here!!)
└── composer-simple-features.lock        -> └── composer.lock      

To set up git extensions use ./create_or_upgrade_shared-wiki-extensions.sh. Note that composer managed extensions overwrite existing extensions.

To get a symlinked v-host-wiki functioning make sure to set up:

# LocalSettings.php
putenv('MW_INSTALL_PATH=/path/to/v-host-wiki');"

Otherwise the update.php will not work. Although usr_share_wiki_path/vendor is not used in v-host-wikis, the existence of usr_share_wiki_path/vendor/autoload.php is tested otherwise the wiki updater will complain about missing external dependencies. So once it is necessary to update the shared wiki resource of composer managed extension:

cd /usr/share/mediawikiXXX && sudo php /usr/local/bin/composer.phar update --no-dev


Bash script

Bash script: ./create_or_upgrade_vhost-wiki.sh. You can make the script executable by

# u o g means: user who owns it (u), other users in the file's group (g), other users (o)
chmod ug+x create_or_upgrade_vhost-wiki.sh # add executable mode for the owned user

Steps of the script:

  1. create template folder and set up v-host-wiki (symlink /usr/share/mediawikiXXX to v-host-wiki)
    • can be either setup_case="rich-features" or setup_case="simple-features"
    • another ./cache, ./media can be specified
  2. backup old wiki (in case of upgrade)
  3. set up ./cache, ./media (upgrade case: copy old LocalSettings.php, ./cache, ./media)

Things to take care as well:

  • check disc space df --human-readable before. If Use% is up to 98%-99% then it is likely that the script gets stuck and cannot copy large /media or /cache folders. Media or Cache folders should originate from /mnt/dump or /mnt/storage not from the Wiki folder itself.
  • clearing the cache after upgrade may be necessary (usually done by sudo -u www-data php ./maintenance/update.php --conf ./LocalSettings.php)
  • symbolic links to OpenMedia, Special Media
    • unclear (TODO): thumbnail creation of foreign media worked in testwiki2 only when a symlink was present (LocalSettings issue? or nginx?)
#!/bin/bash
# Note short conditional syntax:
# [[ test condition ]] && echo "condition true"
# [[ test condition ]] && echo "condition true" || echo "condition false"
# Note long conditional syntax:
# if [[ test condition ]]; then echo "condition true"; else echo "condition false"; fi
################################### 
# Create or upgrade a vhost-wiki from shared resources
# @dependency: binary realpath
# @dependency: upgrade_shared_wiki-core-resource-structure.sh
# @dependency: is written for wikis with a setup of composer.json, composer.local.json (MW1.23+)
# @dependency: a correctly setup set up in /usr/share/mediawikiXXX
#    it depends on simple-features or rich-features set up of the /usr/share/mediawikiXXX
#    /usr/share/mediawiki27wmf                   v-host-wiki
#    ├── extensions-rich-features/            -> ├── extensions/        
#    ├── vendor-rich-features/                -> ├── vendor/            
#    ├── composer-rich-features.json          -> ├── composer.json      
#    ├── composer-rich-features.local.json    -> ├── composer.local.json (manage composer extensions here!!)
#    └── composer-rich-features.lock          -> └── composer.lock      
#    
#    /usr/share/mediawiki27wmf                   v-host-wiki
#    ├── extensions-simple-features/          -> ├── extensions/        
#    ├── vendor-simple-features/              -> ├── vendor/            
#    ├── composer-simple-features.json        -> ├── composer.json      
#    ├── composer-simple-features.local.json  -> ├── composer.local.json (manage composer extensions here!!)
#    └── composer-simple-features.lock        -> └── composer.lock      
#    

################################### 
# Start: global variables settings
usr_share_wiki_path="/usr/share/mediawiki26"
# setup_case="rich-features" # either "rich-features" or "simple-features"
setup_case="simple-features" # either "rich-features" or "simple-features"
# setup_case="simple-features" # either "rich-features" or "simple-features"
# TODO setup_case="test-features" # a testwiki could use 3rd setting: the normal /extensions /vendor composer.json etc.

# cache, media: running as upgrade the script transfers old cache and media to upgraded wiki folder automatically
vhost_wiki_cache_other_path="" # empty (=local folder is used) or fully specified, e.g: /mnt/dump/var/www/testwiki-cache (=becomes a symlink)
vhost_wiki_media_other_path="" # empty (=local folder is used) or fully specified, e.g: /mnt/storage/var-www-testwiki-media (=becomes a symlink)

vhost_wiki_path="/var/www/v-abcd/w"
# in web base dir

# End: global variables settings
################################### 
# get actual modification time or today's date
[[ -d "$vhost_wiki_path" ]] && \
  backupdate=`stat --format=%Y "$vhost_wiki_path" | awk '{print strftime("%Y%m%d_%H%M", $1)}'` || \
  backupdate=`date '+%Y%m%d_%H%M'`
 
# backup path if any
[[ -d "$vhost_wiki_path" ]] && \
  vhost_wiki_backup_path="${vhost_wiki_path}_${backupdate}" || \
  vhost_wiki_backup_path=""
 
# wiki template to get substituted later by real wiki
vhost_wiki_template_path="${vhost_wiki_path}_upgrade_template" # 
 
# cache path
if [[ -z ${vhost_wiki_cache_create_other_path// /} ]];then # string length really zero ? no path given
  [[ -d $vhost_wiki_path/cache  ]] && \
    vhost_wiki_cache_target=`realpath "$vhost_wiki_path/cache"` || \
    vhost_wiki_cache_target="$vhost_wiki_path/cache"
else
  [[ -d $vhost_wiki_cache_create_other_path ]] && \
    vhost_wiki_cache_target=`realpath $vhost_wiki_cache_create_other_path` || \
    vhost_wiki_cache_target=$vhost_wiki_cache_create_other_path
fi
# media path
if [[ -z ${vhost_wiki_media_create_other_path// /} ]];then # string length really zero ? path not given
  [[ -d $vhost_wiki_path/media  ]] && \
    vhost_wiki_media_target=`realpath "$vhost_wiki_path/media"` || \
    vhost_wiki_media_target="$vhost_wiki_path/media"
else
  [[ -d $vhost_wiki_media_create_other_path ]] && \
    vhost_wiki_media_target=`realpath $vhost_wiki_media_create_other_path` || \
    vhost_wiki_media_target=$vhost_wiki_media_create_other_path
fi
 
# prompt before start
# │├ ─ └ 
echo     "################################################################"
[[ -d $vhost_wiki_path ]] && \
echo     "# Upgrade a vhost-wiki from shared resources" || \
echo     "# Create a vhost-wiki from shared resources"
echo     "#---------------------------------------------------------------"
echo     "# $usr_share_wiki_path"
echo     "#  │  ... gets symlinked to ..."
echo     "#  V"
echo     "# $vhost_wiki_path"
printf   "# ├─ %-50s %-s\n" "/${vhost_wiki_template_path##*/}"                            "(step 1: set up new template)"
 
[[ -d $vhost_wiki_path ]] && \
printf   "# ├─ %-50s %-s\n" "/${vhost_wiki_path##*/} -> /${vhost_wiki_backup_path##*/}"   "(step 2: backup wiki folder)" || \
printf   "# ├─ %-50s %-s\n" "/${vhost_wiki_path##*/}"                                     "(step 2: create wiki folder from template, joint with step 3)"
 
printf   "# └─ %-50s %-s\n" "/${vhost_wiki_template_path##*/} -> /${vhost_wiki_path##*/}" "(step 3: substitute template to wiki folder)"
[[ -d $vhost_wiki_path ]] && \
printf   "#    │ %-48s %-s\n" "" "(step 4: transfer old LocalSettings.php, cache, media from backup copy)" || \
printf   "#    │ %-48s %-s\n" "" "(step 4: handle LocalSettings.php, cache, media)"
 
echo     "#    ├─ /cache target: ${vhost_wiki_cache_target}"
echo     "#    ├─ /media target: ${vhost_wiki_media_target}"
echo     "#    └─ LocalSettings.php (must be checked or edited afterwards manually)"
echo     "# "
 
case "$setup_case" in
simple-features)
  echo -e  "# Is structure of $usr_share_wiki_path ready in place for setup case «\e[31m$setup_case\e[0m»?:"
  printf   "#   %-41s %s\n" "$usr_share_wiki_path" "$vhost_wiki_path"
  [[ -d $usr_share_wiki_path/extensions-simple-features ]] && \
  echo  -e "#   ├─  \e[0;32m/extensions-simple-features\e[0m         -> ├─ /extensions        " || \
  echo  -e "#   ├─  \e[1;31m/extensions-simple-features\e[0m         -> ├─ /extensions        (missing resource)"
 
  [[ -d $usr_share_wiki_path/vendor-simple-features ]] && \
  echo  -e "#   ├─  \e[0;32m/vendor-simple-features\e[0m             -> ├─ /vendor            " || \
  echo  -e "#   ├─  \e[1;31m/vendor-simple-features\e[0m             -> ├─ /vendor            (missing resource)"
 
  [[ -e $usr_share_wiki_path/composer-simple-features.json ]] && \
  echo  -e "#   ├─  \e[0;32mcomposer-simple-features.json\e[0m       -> ├─ composer.json      " || \
  echo  -e "#   ├─  \e[1;31mcomposer-simple-features.json\e[0m       -> ├─ composer.json      (missing resource)"
 
  [[ -e $usr_share_wiki_path/composer-simple-features.lock ]] && \
  echo  -e "#   ├─  \e[0;32mcomposer-simple-features.lock\e[0m       -> ├─ composer.lock      " || \
  echo  -e "#   ├─  \e[1;31mcomposer-simple-features.lock\e[0m       -> ├─ composer.lock      (missing resource)"
 
  [[ -e $usr_share_wiki_path/composer-simple-features.local.json ]] && \
  echo  -e "#   └─  \e[0;32mcomposer-simple-features.local.json\e[0m -> └─ composer.local.json" || \
  echo  -e "#   └─  \e[1;31mcomposer-simple-features.local.json\e[0m -> └─ composer.local.json (missing resource)"
;;
 
rich-features)
  echo -e  "# Is structure of $usr_share_wiki_path ready in place for setup case of «\e[31m$setup_case\e[0m»?:"
  printf   "#   %-41s %s\n" "$usr_share_wiki_path" "$vhost_wiki_path"
  [[ -d  $usr_share_wiki_path/extensions-rich-features ]] && \
  echo  -e "#   ├─  \e[0;32m/extensions-rich-features\e[0m           -> ├─ /extensions        " || \
  echo  -e "#   ├─  \e[1;31m/extensions-rich-features\e[0m           -> ├─ /extensions        (missing resource)"
 
  [[ -d  $usr_share_wiki_path/vendor-rich-features ]] && \
  echo  -e "#   ├─  \e[0;32m/vendor-rich-features\e[0m               -> ├─ /vendor            " || \
  echo  -e "#   ├─  \e[1;31m/vendor-rich-features\e[0m               -> ├─ /vendor            (missing resource)"
 
  [[ -e  $usr_share_wiki_path/composer-rich-features.json ]] && \
  echo  -e "#   ├─  \e[0;32mcomposer-rich-features.json\e[0m         -> ├─ composer.json      " || \
  echo  -e "#   ├─  \e[1;31mcomposer-rich-features.json\e[0m         -> ├─ composer.json      (missing resource)"
 
  [[ -e  $usr_share_wiki_path/composer-rich-features.lock ]] && \
  echo  -e "#   ├─  \e[0;32mcomposer-rich-features.lock\e[0m         -> ├─ composer.lock      " || \
  echo  -e "#   ├─  \e[1;31mcomposer-rich-features.lock\e[0m         -> ├─ composer.lock      (missing resource)"
 
  [[ -e  $usr_share_wiki_path/composer-rich-features.local.json ]] && \
  echo  -e "#   └─  \e[0;32mcomposer-rich-features.local.json\e[0m   -> └─ composer.local.json" || \
  echo  -e "#   └─  \e[1;31mcomposer-rich-features.local.json\e[0m   -> └─ composer.local.json (missing resource)"
;;
 
*)
  echo     "#   Unknown setup case. Please specify a setup_case in the bash script variables section:"
  echo     "#    setup_case=\"simple-features\" or \"rich-features\""
  echo     "#    (stop here)"
  exit 1
;;
esac
# end case simple rich features
 
echo "# "
[[ -z ${vhost_wiki_backup_path// /} ]] && \
  echo    "# Checking available disk space:" || \
  echo -e "# Checking available disk space (the script will leave a backup on \e[1m${vhost_wiki_backup_path}\e[0m):"
df --human-readable | sed 's@^@#   @g' | grep --color=auto '[0-9]\+%\|Filesystem.*'
 
if [[ -d $vhost_wiki_path ]]; then 
echo "#   Checking used disk space of wiki:"
du -P  --human-readable  --max-depth=1 "$vhost_wiki_path" | sed 's@^@#    @g' 
fi
 
# prompt User
CAN_I_RUN_SUDO=$(sudo -n uptime 2>&1|grep "load"|wc -l)
if [[ ! $CAN_I_RUN_SUDO -gt 0 ]]; then
echo     "# "
echo -e  "# The script was stopped because it \e[1mrequires sudo\e[0m running without promt. It will fail to work now. Please run any sudo"
echo     "# action before to have sudo action available without password prompt, e.g. list directory as sudo"
echo -e  "#   \e[1msudo ls\e[0m"
echo     "# "
exit 1
fi
 
if [[ -e ${vhost_wiki_path}/LocalSettings.php ]]; then
  if [[ `grep --count "wgDBname" ${vhost_wiki_path}/LocalSettings.php` -gt 0 ]]; then
echo     "# "
echo     "# You probably better make a database backup of «"`grep "wgDBname" ${vhost_wiki_path}/LocalSettings.php | sed 's@^.*\([\x27"]\)\([^\x27"]\+\)\1.*$@\2@g'`"» if things go too wrong ;-) who knows ... "
echo     "# "
  fi
fi
 
[[ -d $vhost_wiki_path ]] && \
echo -ne "# Do you want to upgrade existing vhost-wiki ($vhost_wiki_path)?\n[yes or no (default: no)]: " || \
echo -ne "# Do you want to create vhost-wiki ($vhost_wiki_path)?\n[yes or no (default: no)]: "
 
read yno
case $yno in
  [yY]|[yY][Ee][Ss])
    echo "Continue ..."
  ;;
  [nN]|[nN][oO])
    echo "Stop";
    exit 1
  ;;
  *)
    [[ -z ${yno// /} ]] && \
      echo "No input (stop)" || \
      echo "Invalid input: $yno -- only y, yes or n, no are valid. (stop)" 
    exit 1
  ;;
esac
 
 
##↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
# STEP 1 wiki template folder
if [[ -d $vhost_wiki_template_path ]]; then 
  echo -e "# vhost-Wiki (1: set up new template): '$vhost_wiki_template_path' exists (empty it completely)"
  sudo -u root rm --recursive "${vhost_wiki_template_path}"/*
else
  echo    "# vhost-Wiki (1: set up new template): create template folder $vhost_wiki_template_path"
  sudo -u root -g mwadmin mkdir --parent "$vhost_wiki_template_path"
fi
 
# step 1 setup template: symbolic links folders
cd "${usr_share_wiki_path}"
# \( ! -name '.*' \) avoids finding hidden directories
for this_wiki_folder in `find -L . -maxdepth 1 \( ! -name '.*' \) -type d -printf "%f\n"`; do
  # skip the following
  [[ $this_wiki_folder == cache ]] && continue;
  [[ $this_wiki_folder == images ]] && continue;
  [[ $this_wiki_folder == extensions ]] && continue;
  [[ $this_wiki_folder == vendor ]] && continue;
  case "${setup_case}" in
  simple-features)
    # skip the following
    [[ $this_wiki_folder == *-rich-features* ]] && continue;
  ;;
  rich-features)
    # skip the following
    [[ $this_wiki_folder == *-simple-features* ]] && continue;
  ;;
  esac
 
  echo -n "#   symbolic link (folder): "
  case "$setup_case" in
  simple-features)
    if [[ $this_wiki_folder == extensions-simple-features ]];then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_folder}" "$vhost_wiki_template_path/extensions"
      echo  " ${usr_share_wiki_path}/${this_wiki_folder} -> $vhost_wiki_template_path/extensions"  && continue;
    fi
    if [[ $this_wiki_folder == vendor-simple-features ]];then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_folder}" "$vhost_wiki_template_path/vendor"
      echo  " ${usr_share_wiki_path}/${this_wiki_folder} -> $vhost_wiki_template_path/vendor"  && continue;
    fi
  ;;
  rich-features)
    if [[ $this_wiki_folder == extensions-rich-features ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_folder}" "$vhost_wiki_template_path/extensions"
      echo  " ${usr_share_wiki_path}/${this_wiki_folder} -> $vhost_wiki_template_path/extensions" && continue;
    fi
    if [[ $this_wiki_folder == vendor-rich-features ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_folder}" "$vhost_wiki_template_path/vendor"
      echo  " ${usr_share_wiki_path}/${this_wiki_folder} -> $vhost_wiki_template_path/vendor" && continue;
    fi
  ;;
  esac
 
  echo  " ${usr_share_wiki_path}/${this_wiki_folder} -> $vhost_wiki_template_path/$this_wiki_folder"
  sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_folder}" "$vhost_wiki_template_path/${this_wiki_folder}"
done
 
# step 1 setup template: symbolic links files
cd "${usr_share_wiki_path}"
# \( ! -name '.*' \) avoids finding hidden directories
for this_wiki_file in `find -L . -maxdepth 1 \( ! -name '.*' \) -type f -printf "%f\n"`; do
  case "${setup_case}" in
  simple-features)
    # skip all *-rich-features*
    [[ $this_wiki_file == *-rich-features* ]] && continue;
  ;;
  rich-features)
    # skip all *-simple-features*
    [[ $this_wiki_file == *-simple-features* ]] && continue;
  ;;
  esac
 
  echo -n "#   symbolic link (file): "
  case "${setup_case}" in
  simple-features)
    if [[ $this_wiki_file == composer-simple-features.json ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.json"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.json" && continue;
    fi
    if [[ $this_wiki_file == composer-simple-features.lock ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.lock"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.lock" && continue;
    fi
    if [[ $this_wiki_file == composer-simple-features.local.json ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.local.json"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.local.json" && continue;
    fi
  ;;
  rich-features)
    if [[ $this_wiki_file == composer-rich-features.json ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.json"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.json" && continue;
    fi
    if [[ $this_wiki_file == composer-rich-features.lock ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.lock"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.lock" && continue;
    fi
    if [[ $this_wiki_file == composer-rich-features.local.json ]]; then
      sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/composer.local.json"
      echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/composer.local.json" && continue;
    fi
  ;;
  esac
 
  echo " ${vhost_wiki_template_path}/${this_wiki_file} -> ${vhost_wiki_template_path}/${this_wiki_file}"
  sudo ln --symbolic --force "${usr_share_wiki_path}/${this_wiki_file}" "${vhost_wiki_template_path}/${this_wiki_file}"
done
##↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 
##↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
# step 2: backup or create wiki folder
if [[ -z ${vhost_wiki_backup_path// /} ]]; then 
  echo    "# vhost-Wiki (2: create folder) '$vhost_wiki_path' from '$vhost_wiki_template_path'"
else
  echo    "# vhost-Wiki (2: backup wiki) '$vhost_wiki_path' -> '$vhost_wiki_backup_path'"
  sudo -u root mv "$vhost_wiki_path" "$vhost_wiki_backup_path"
fi
##↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 
##↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
# step 3: substitute template to wiki
if [[ -d $vhost_wiki_template_path ]];then
  echo    "# vhost-Wiki (3: substitute template to wiki) '$vhost_wiki_template_path' to '$vhost_wiki_path'"
  sudo -u root mv "$vhost_wiki_template_path" "$vhost_wiki_path"
else
  echo    "# vhost-Wiki (3: substitute template to wiki) '$vhost_wiki_template_path' to '$vhost_wiki_path'"
  echo -e "#   \e[1;31mWarning: template folder does not exist $vhost_wiki_template_path\e[0m"
  echo -e "#   \e[1;31mto create $vhost_wiki_path. Something went wrong (stop)\e[0m"
  exit 1;
fi
 
# step 3: substitute (upgrade-case: backup exists, create-case: no backup exist)
[[ -d $vhost_wiki_backup_path ]] && \
printf   "# vhost-Wiki %-50s\n" "(4: transfer old LocalSettings.php, cache, media)" || \
printf   "# vhost-Wiki %-50s\n" "(4: create LocalSettings.php, cache, media)"
##↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 
##↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
# step 4 LocalSettings, cache, media
# cache
echo "#   check for /cache ..."
if [[ -z ${vhost_wiki_cache_create_other_path// /} ]];then # string length really zero ? path not given
  if [[ -d $vhost_wiki_backup_path/cache  ]]; then
    if [[ -d $vhost_wiki_path/cache  ]];then
      sudo -u root mv "$vhost_wiki_path/cache" "$vhost_wiki_path/cache_BACKUP_OR_DELETE_ME"
      echo "#     move existing cache to backup: $vhost_wiki_path/cache -> $vhost_wiki_path/cache_BACKUP_OR_DELETE_ME"
    fi
 
    echo "#     copy $vhost_wiki_backup_path/cache -> $vhost_wiki_path/cache"
    echo "#     this may take a while you can check from time to time «ls -l $vhost_wiki_path/cache» in another terminal ..."
    sudo -u root -g mwadmin cp --recursive --preserve "$vhost_wiki_backup_path/cache" "$vhost_wiki_path/" # cp symlinked or folder
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_path/cache"
  else
    if [[ -d $vhost_wiki_path/cache  ]];then
      sudo -u root -g mwadmin mv "$vhost_wiki_path/cache" "$vhost_wiki_path/cache_BACKUP_OR_DELETE_ME"
      echo "#     move existing /cache to backup: $vhost_wiki_path/cache -> $vhost_wiki_path/cache_BACKUP_OR_DELETE_ME"
    fi
 
    echo "#     create $vhost_wiki_path/cache"
    sudo -u root -g mwadmin mkdir --parent "$vhost_wiki_path/cache"
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_path/cache"
  fi
else
  [[ -d $vhost_wiki_backup_path/cache  ]] && \
    echo "#     use $vhost_wiki_cache_create_other_path instead of existing /cache from old wiki (see $vhost_wiki_backup_path/cache)"
 
  if [[ ! -d $vhost_wiki_cache_create_other_path ]];then
    echo "#     create $vhost_wiki_cache_create_other_path"
    sudo -u root -g mwadmin mkdir --parent "$vhost_wiki_cache_create_other_path"
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_cache_create_other_path"
    if [[ -d $vhost_wiki_backup_path/cache  ]];then
      echo "#     copy cache from backup $vhost_wiki_backup_path/cache -> $vhost_wiki_cache_create_other_path ..."
      echo "#     this may take a while you can check from time to time «ls -l $vhost_wiki_cache_create_other_path» in another terminal ..."
      sudo -u www-data cp --preserve --recursive "$vhost_wiki_backup_path/cache"/* "$vhost_wiki_cache_create_other_path"/
    fi
  else
    echo "#     WARNING $vhost_wiki_cache_create_other_path exists (skip copying chache)"
  fi
 
  echo "#     link $vhost_wiki_cache_create_other_path -> ${vhost_wiki_path}/cache"
  sudo ln --symbolic --force "$vhost_wiki_cache_create_other_path" "${vhost_wiki_path}/cache"
  sudo -u root chown www-data:www-data  --recursive "${vhost_wiki_path}/cache"
fi
# media
echo "#   check for /media ..."
if [[ -z ${vhost_wiki_media_create_other_path// /} ]];then # string length really zero ? path not given
  if [[ -d $vhost_wiki_backup_path/media  ]]; then
    if [[ -d $vhost_wiki_path/media  ]]; then
      sudo -u root -g mwadmin mv "$vhost_wiki_path/media" "$vhost_wiki_path/media_BACKUP_OR_DELETE_ME"
      echo "#     move existing /media to backup: $vhost_wiki_path/media -> $vhost_wiki_path/media_BACKUP_OR_DELETE_ME"
    fi
    echo "#     copy $vhost_wiki_backup_path/media -> $vhost_wiki_path/media"
    echo "#     this may take a while you can check from time to time «ls -l $vhost_wiki_path/media» in another terminal ..."
    sudo -u root -g mwadmin cp --recursive --preserve "$vhost_wiki_backup_path/media" "$vhost_wiki_path/" # cp symlinked or folder
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_path/media"
  else
    if [[ -d $vhost_wiki_path/media  ]]; then
      sudo -u root -g mwadmin mv "$vhost_wiki_path/media" "$vhost_wiki_path/media_BACKUP_OR_DELETE_ME"
      echo "#     move existing media to backup: $vhost_wiki_path/media -> $vhost_wiki_path/media_BACKUP_OR_DELETE_ME"
    fi
 
    echo "#     create $vhost_wiki_path/media"
    sudo -u root -g mwadmin mkdir --parent "$vhost_wiki_path/media"
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_path/media"
  fi
else
  # case vhost_wiki_media_create_other_path
  [[ -d $vhost_wiki_backup_path/media  ]] && \
    echo "#     use $vhost_wiki_media_create_other_path instead of existing /media from old wiki (see $vhost_wiki_backup_path/media) ..."
 
  if [[ ! -d $vhost_wiki_media_create_other_path ]]; then
    echo "#     create $vhost_wiki_media_create_other_path"
    sudo -u root -g mwadmin mkdir --parent "$vhost_wiki_media_create_other_path"
    sudo -u root -g mwadmin chown --recursive www-data:www-data  "$vhost_wiki_media_create_other_path"
    if [[ -d $vhost_wiki_backup_path/media  ]];then
      echo "#     copy media from backup $vhost_wiki_backup_path/media -> $vhost_wiki_media_create_other_path ..."
      echo "#     this may take a while you can check from time to time «ls -l $vhost_wiki_media_create_other_path» in another terminal ..."
      sudo -u www-data cp --preserve --recursive "$vhost_wiki_backup_path/media"/* "$vhost_wiki_media_create_other_path"/
    fi
  else
    echo "#     WARNING $vhost_wiki_media_create_other_path exists (skip copying media, check media manually!!)"
  fi
 
  echo "#     link $vhost_wiki_media_create_other_path -> ${vhost_wiki_path}/media ..."
  sudo ln --symbolic --force "$vhost_wiki_media_create_other_path" "${vhost_wiki_path}/media"
  sudo -u root chown www-data:www-data --recursive "${vhost_wiki_path}/media"
fi
 
# LocalSettings.php
if [[ -e $vhost_wiki_backup_path/LocalSettings.php ]];then
  echo "#   transfer old LocalSettings.php to ${vhost_wiki_path} ..."
  sudo -u root cp --interactive --preserve "$vhost_wiki_backup_path/LocalSettings.php" "$vhost_wiki_path"
fi
 
if [[ ! -e $vhost_wiki_path/LocalSettings.php ]];then
  sudo -u root touch "$vhost_wiki_path/LocalSettings.php"
  echo "#   created empty ${vhost_wiki_path}/LocalSettings.php ..."
  echo "#   make settings manually in LocalSettings.php"
fi
echo    "# (Done)"
 
echo    "# "
echo    "# Read also RELEASE NOTES:"
find -L "${vhost_wiki_path}" -maxdepth 1 -type f -regex '.*RELEASE.*NOTES.*' -exec echo -en "# navigate in more forwardly by <enter> or <space> and backwardly by <b>, <q> quits\n   more " '{}' "\n" ';'
 
echo    "# "
echo    "# You can go to $vhost_wiki_path and check its structure using:"
echo    "   cd $vhost_wiki_path && ls -lp # or"
echo -e "   cd $vhost_wiki_path && tree -p -u -g -F -L 1 # \e[1mp\e[0m:permission \e[1mu\e[0m:user \e[1mg\e[0m:group \e[1mF\e[0m:append directory-slash/ \e[1mL\e[0m:levels-down"
 
echo "# Todo step 1) Backup database for $vhost_wiki_path e.g. via http://biowikifarm.net/phpmyadmin_v3-3-7/index.php"
echo "# Todo step 2) Adjust $vhost_wiki_path/LocalSettings.php to fit a proper Wiki setup"
echo "# Todo step 3) Set global setting \$bwgSetUpAsRichWiki e.g. in /var/www/MediaWikiCommonSettings_REL1_26.php"
echo "# Todo step 4) Update by maintenance script eventually:"
echo "#    cd '$vhost_wiki_path' && sudo -u www-data php ./maintenance/update.php --dbuser wikiadmin --conf ./LocalSettings.php"
 
[[ -d  $vhost_wiki_backup_path ]] && \
  echo "# Take care that you clear the cache (maintenance/update.php does this too) otherwise you might get a blank website"
##↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑