Benutzer:Andreas Plank/Bash - Vorlagenskripte

From Offene Naturführer
Jump to: navigation, search

convert_mediawiki-export4reimport.sh

#!/bin/bash
# @description: Convert a XML Wiki export to a new XML re-impoert-file, add comment, user name and user  id accordingly
# Usage
#   ./convert_mediawiki-export4reimport.sh > Reimport_what_wikiname_` date '+%Y-%m-%d_%H-%M'`.xml
# @dependency binary sed
# @dependency saved file of the XML header until closing </siteinfo> of the Re-import-Wiki before all <page> start

#########################
# Settings section
wiki_user_name="Real User Name"
wiki_user_id="123"
reimport_comment="Erlaubte Werte (neu refaktoriert: mit Zeilenumbruch); Fix www2.ufz.de -> www.ufz.de; Datentyp=Zeichenkette -> Text"

# reimport_header_file_path: a Special:Export
export_file_path="/home/andreas/Dokumente/Programmierung/Wiki/ON/Offene+Naturführer-20190123155335-Erlaubt-Wert-Attribute-alt.xml"

reimport_header_file_path="/home/andreas/Dokumente/Programmierung/Wiki/ON/Offene+Naturführer-20190123155335-Erlaubt-Wert-Attribute-alt.xml"
#########################

# general replacements

if [[ ! -e $reimport_header_file_path ]];then
  echo -e "Error in $0"
  echo -e "$reimport_header_file_path \e[1mdoes not exist!!\e[0m (stop)"
  exit 1;
fi
if [[ ! -e $export_file_path ]];then
  echo -e "Error in $0"
  echo -e "$export_file_path \e[1mdoes not exist!!\e[0m (stop)"
  exit 1;
fi

sed --silent '/<mediawiki/,/<\/siteinfo>/{p;}' "${reimport_header_file_path}"
sed --silent '/<page>/,/<\/page>/{p;};/<minor\/>/{d}' "${export_file_path}" | sed "

/<contributor>/,/<\/contributor>/{
  s@<username>[^<]*</username>@<username>${wiki_user_name}</username>@g
  s@<id>[^<]*</id>@<id>${wiki_user_id}</id>@g
  /<\/contributor>/ {
     N;
    /<comment>/b comment_found
    /<comment>/!b comment_not_found
  }
};
:comment_not_found
    /<\/contributor>/ {
#      /<\/comment>.*<comment>/!{
a\
\ \ \ \ \ \ <comment>${reimport_comment}</comment>
#      }
    }
:comment_found
  /<comment>/,/<\/comment>/{
    :label_add_newlines
      N;
    # if line contains not (!) '</comment>' go (b)ack to label_add_newlines
    /<\/comment>/!b label_add_newlines 
    s@\(<comment>\)\(.\+\)\(</comment>\)@\1${reimport_comment}\3@g;
  }
  
# s@\(}}\)[^{}]*\(</text>\)@\1\n{{Provider Coste and Flahault 1901-1906}}\2@g;
" | sed "
/^    <id>/{d};
/^      <id>/{d};
/^      <format>/{d};
/^      <model>/{d};
/^      <parentid>/{d};
/^      <sha1>/{d};
/^      <timestamp>/{d};
s@<text xml:space=\"preserve\" bytes=\"[0-9]\+\"@<text xml:space=\"preserve\" @g;
"
echo "</mediawiki>"

# cd "/var/www/to-wiki/w/"
# sudo -u www-data php ./maintenance/importDump.php --conf ./LocalSettings.php /home/myhomeflder/temp/convertedXmlImport.xml
# sudo -u www-data php ./maintenance/rebuildrecentchanges.php --conf ./LocalSettings.php
# sudo -u www-data php ./extensions/TitleKey/rebuildTitleKeys.php --conf ./LocalSettings.php

Wikitext2Vorlagenparameter.sh

#!/bin/bash
# Benutzung
# ./Wikitext2Vorlagenparameter.sh 'Zu verarvbeitende-Textdatei.txt'
datei=$1
if ! [ -f "$datei" ]; then
  if [ "$datei" = "" ]; then
    echo -e "Zu verarbeitende Datei fehlt:\\n $0 'Vorlage:so-und-so.txt'";
  else
    echo "Datei '${datei}' nicht gefunden";
  fi
  exit;
fi
datei_neu="${datei%.*}_parameter.${datei##*.}"
cp "$datei" "$datei_neu"
sed "s@\r@@g;                        # remove return character
  s@\({{{[^{}]\+\)@\nParameter:\1@g; # all starting with {{{ mark as parameter
   " "$datei" | sort --unique | \
   sed "/^Parameter:{{{/!{d};        # delete all non marked parameter lines
   s@^\(Parameter:{{{[^|}]\+\)}}}.*@\1@g; # remove tailing '}}}…' in {{{parameter}}}
   s@^\(Parameter:{{{[^|}]\+\)|.\+@\1@g;  # remove tailing '|some stuff}}}…' in {{{parameter|…}}}
   s@^Parameter:{{{@@g; s@[ |]*\$@@g" | sort --unique --version-sort > "$datei_neu"
echo "Parameter der Vorlage in Datei '${datei_neu}' geschrieben!!"