<?php
/*
    Copyright (C) 2009 Fabio Alessandro Locati - fabiolocati@gmail.com

    This program 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 3 of the License, or
    (at your option) any later version.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$username="FaleBot";
$password="";

require('../sxwiki/src/SxWiki.php');

$cat = array("Abbigliamento", "Aeronautica", "Agricoltura", "Ambito sportivo", "Anatomia", "Animale", "Antico", "Antropologia", "Araldica", "Archeologia", "Architettura", "Armi", "Arma", "Armamento", "Arte", "Astronomia", "Botanica", "Biochimica", "Biologia", "Burocrazia", "Calcio", "Chimica", "Chimica generale", "Chimica inorganica", "Chimica organica", "Chimica analitica", "Chimica industriale", "Cinema", "Cinematografia", "Colore", "Commercio", "Composto organico", "Composto inorganico", "Danza", "Diritto", "Ecclesiastico", "Ecologia", "Economia", "Edilizia", "Elemento chimico", "Elettronica", "Elettrotecnica", "Entomologia", "Equitazione", "Erpetologia", "Etnologia", "Familiare", "Farmacia", "Ferrovia", "Filosofia", "Finanza", "Fisica", "Fisiologia", "Fonologia", "Formale", "Fotografia", "Gastronomia", "Geografia", /*"Geologia",*/ "Geometria", "Gergale", "Gioco", "Giornalistico", "Grammatica", "Idraulica", "Informale", "Informatica", "Ingegneria", "Internet", "Ittiologia", "Legale", "Letterario", "Letteratura", "Linguistica", "Macelleria", "Malacologia", "Mammalogia", "Marina", "Matematica", "Meccanica", "Medicina", "Metallurgia", "Meteorologia", "Metrica", "Metrica classica", "Metrica contemporanea", "Metrica latina", "Militare", "Minerale", "Mineralogia", "Mitologia", "Musica", "Neologismo", "Numismatica", "Obsoleto", "Offensivo", "Ornitologia", "Pedagogia", "Pittura", "Poesia", "Polimeri", "Politica", "Professione", "Psichiatria", "Psicologia", "Raro", "Regionale", "Religione", "Topografia", "Toponimo", "Paleontologia", "Pianta", "Scacchi", "Scuola", "Sociologia", "Specifico", "Sport", "Sport invernali", "Statistica", "Storia", "Teatro", "Tecnica", "Tecnologia", "Tessile", "Tipografia", "Veterinaria", "Volgare", "Zoologia", "Zootecnica");

$wikt=new SxWiki;
$wikt->username=$username;
$wikt->password=$password;
$wikt->url="http://it.wiktionary.org/w/";

foreach ($cat as $category)
{
  $list = $wikt->getCat($category,"0");
  echo "Scanning: " . $category . "\n";
  foreach ($list as $page)
  {
    $text = $wikt->getPage($page);

    // Do not repeat the lemma in the page
    $text = preg_replace("/\'\'\'" . $page . "\'\'\'/i", "{{pn}}", $text);

    // Languages
    $text = preg_replace("/\{\{-en-\}\}\{\{-noun-\}\}/i", "{{in|en|noun}}", $text);
    $text = preg_replace("/\{\{-en-\}\}\{\{-adjc-\}\}/i", "{{in|en|adjc}}", $text);
    $text = preg_replace("/\{\{-en-\}\}/i", "{{in|en|np}}", $text);
    $text = preg_replace("/\{\{-it-\}\}\{\{-noun-\}\}/i", "{{in|it|noun}}", $text);
    $text = preg_replace("/\{\{-it-\}\}\{\{-adjc-\}\}/i", "{{in|it|adjc}}", $text);
    $text = preg_replace("/\{\{-it-\}\}/i", "{{in|it|np}}", $text);
    $text = preg_replace("/\{\{-de-\}\}\{\{-noun-\}\}/i", "{{in|de|noun}}", $text);
    $text = preg_replace("/\{\{-de-\}\}\{\{-adjc-\}\}/i", "{{in|de|adjc}}", $text);
    $text = preg_replace("/\{\{-de-\}\}/i", "{{in|de|np}}", $text);
    $text = preg_replace("/\{\{-fr-\}\}\{\{-noun-\}\}/i", "{{in|fr|noun}}", $text);
    $text = preg_replace("/\{\{-fr-\}\}\{\{-adjc-\}\}/i", "{{in|fr|adjc}}", $text);
    $text = preg_replace("/\{\{-fr-\}\}/i", "{{in|fr|np}}", $text);

    // Chemistry
    $text = preg_replace("/\#\{\{-chim-\}\}/i", "# {{term|chimica}}", $text);
    $text = preg_replace("/\#\{\{chim\}\}/i", "# {{term|chimica}}", $text);
    $text = preg_replace("/\# \{\{-chim-\}\}/i", "# {{term|chimica}}", $text);
    $text = preg_replace("/\# \{\{chim\}\}/i", "# {{term|chimica}}", $text);
    $text = preg_replace("/\{\{-chim-\}\}/i", "\n# {{term|chimica}}", $text);
    $text = preg_replace("/\{\{chim\}\}/i", "\n# {{term|chimica}}", $text);
    
    // Phylosophy
    $text = preg_replace("/\#\{\{-filos-\}\}/i", "# {{term|filosofia}}", $text);
    $text = preg_replace("/\#\{\{filos\}\}/i", "# {{term|filosofia}}", $text);
    $text = preg_replace("/\# \{\{-filos-\}\}/i", "# {{term|filosofia}}", $text);
    $text = preg_replace("/\# \{\{filos\}\}/i", "# {{term|filosofia}}", $text);
    $text = preg_replace("/\{\{-filos-\}\}/i", "\n# {{term|filosofia}}", $text);
    $text = preg_replace("/\{\{filos\}\}/i", "\n# {{term|filosofia}}", $text);
    
    // Music
    $text = preg_replace("/\#\{\{-mus-\}\}/i", "# {{term|musica}}", $text);
    $text = preg_replace("/\#\{\{mus\}\}/i", "# {{term|musica}}", $text);
    $text = preg_replace("/\# \{\{-mus-\}\}/i", "# {{term|musica}}", $text);
    $text = preg_replace("/\# \{\{mus\}\}/i", "# {{term|musica}}", $text);
    $text = preg_replace("/\{\{-mus-\}\}/i", "\n# {{term|musica}}", $text);
    $text = preg_replace("/\{\{mus\}\}/i", "\n# {{term|musica}}", $text);

    // History
    $text = preg_replace("/\#\{\{-stor-\}\}/i", "# {{term|storia}}", $text);
    $text = preg_replace("/\#\{\{stor\}\}/i", "# {{term|storia}}", $text);
    $text = preg_replace("/\# \{\{-stor-\}\}/i", "# {{term|storia}}", $text);
    $text = preg_replace("/\# \{\{stor\}\}/i", "# {{term|storia}}", $text);
    $text = preg_replace("/\{\{-stor-\}\}/i", "\n# {{term|storia}}", $text);
    $text = preg_replace("/\{\{stor\}\}/i", "\n# {{term|storia}}", $text);

    // Sprots
    $text = preg_replace("/\#\{\{-sport-\}\}/i", "# {{term|sport}}", $text);
    $text = preg_replace("/\#\{\{sport\}\}/i", "# {{term|sport}}", $text);
    $text = preg_replace("/\# \{\{-sport-\}\}/i", "# {{term|sport}}", $text);
    $text = preg_replace("/\# \{\{sport\}\}/i", "# {{term|sport}}", $text);
    $text = preg_replace("/\{\{-sport-\}\}/i", "\n# {{term|sport}}", $text);
    $text = preg_replace("/\{\{sport\}\}/i", "\n# {{term|sport}}", $text);
    
    // Zoology
    $text = preg_replace("/\#\{\{-zool-\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\#\{\{zool\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\# \{\{-zool-\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\# \{\{zool\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\{\{-zool-\}\}/i", "\n# {{term|zoologia}}", $text);
    $text = preg_replace("/\{\{zool\}\}/i", "\n# {{term|zoologia}}", $text);
    $text = preg_replace("/\#\{\{-zoo-\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\#\{\{zoo\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\# \{\{-zoo-\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\# \{\{zoo\}\}/i", "# {{term|zoologia}}", $text);
    $text = preg_replace("/\{\{-zoo-\}\}/i", "\n# {{term|zoologia}}", $text);
    $text = preg_replace("/\{\{zoo\}\}/i", "\n# {{term|zoologia}}", $text);

    $textCopy = $text;
    echo "$page";
    while($textCopy)
    {
      if ( preg_match("/(\X*?)\{\{in\|(\w{2})\|(\w+)\}\}(\X*?){\{term\|(\w+)\}\}(\X*)/i", $textCopy, $matches) )
        $c = $c + 1;
      $textCopy = $matches[6];
    }
    echo ".";

    for ($i=1; $i<=$c; $i++)
    {
      preg_match("/(\X*?)\{\{in\|(\w{2})\|(\w+)\}\}(\X*?){\{term\|(\w+)\}\}(\X*)/i", $text, $matches);

      $out = $out . $matches[1] . "{{in|" . $matches[2] . "|" . $matches[3] . "}}" . $matches[4] . "{{term|" . $matches[5] . "|" . $matches[2] . "}}";

      $text = $matches[6];
    }
    echo ".";
    $out = $out . $matches[6];
    echo ".";
    if ($out)
    {
      $wikt->putPage($page, "FaleBot: Fix the [[template:term|term]] template", $out);
//      echo "\E[32mDone!\E[30m";
        echo "Done!";
    }
    else
      echo "Failed!";
//      echo "\E[31mFailed!\E[30m";
//    $text = "";
//    $textCopy = "";
//    $matches = "";
    $c = 0;
    $out = "";
    echo " .";
    sleep(1);
    echo ".";
    sleep(1);
    echo ".\n";
  }
}
?>