<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);
require ('commonFunctions.php');

$category = $_GET['category'];
$action = $_GET['action'];
$id = $_GET['id'];
$ind = $_GET['ind'];
$print = $_GET['print'];

function fileWrite($x){
     $x .= "\r\n";
     global $file ;
     if (!$file) {
         $file = fopen('KeyItems.xml', 'w+');
     }
     if (!fwrite($file, $x)){
         print "&errMsg=Unable to print line $x/r<br>";
     }
}

$array = @file('keyItemParse.txt');
$data = array();
$newData = array();
$counter = 0;
$symbolsIn = array('0x2642', '0x2640');
$symbolsOut = array('Male', 'Female');
$checkNames = array();
$numdup = 0;
$renameIn = array(':', '#', '"');
$renameOut = array('', '', '');

if (!$array) {
    print ('Unable to open file');
} elseif ($action == null || $action == "link" || $action == "XML") {
    foreach($array as $count => $row) {
        $currow = explode('|', $row);
        $desc = explode('[br]', $currow[7], 2);
        if (count($desc) == 2) {
            $currow[7] = implode('|', $desc);
            $row = implode('|', $currow);
            $currow = explode('|', $row);
        }
        if ($checkNames[$currow[6]] > 0) {
            $checkNames[$currow[6]]++;
        } else {
            $checkNames[$currow[6]] = 1;
        }
        if ($checkType[$currow[9]] > 0) {
            $checkType[$currow[9]]++;
        } else {
            $checkType[$currow[9]] = 1;
        }
        $link = ($print == true) ? "Image-O-Matic2.php?&fileSystem=keyItem&id=$count" : "Key-O-Matic.php?&action=seek&id=$count";
        if ($action == "XML") {
            print "*[[".ucfirst($currow[6])."]]<br />\r\n";
        } else {
            print "*[[<a href=\"$link\">".ucfirst($currow[6])."</a>]]<br />\r\n";
        }
    }
    $checkText = "";
    foreach($checkNames as $key => $thisrow) {
        if ($thisrow > 1) {
            $checkText .= $key."- $thisrow<br />\r\n";
        }
    }
    if ($checkText != "") {
        print "<font size=\"+3\">Duplicated items</font><br />\r\n".$checkText;
    }
    foreach ($checkType as $key => $thisrow) {
        print "<b><a href=\"Key-O-Matic.php?&action=".urlencode(trim($key))."\">$key</a></b>: $thisrow<br />\r\n";
    }
} else if ($action=="delete") {
    foreach($array as $count => $row) {
        $currow = explode('|', $row);
        $desc = explode('[br]', $currow[7], 2);
        if (count($desc) == 2) {
            $currow[7] = implode('|', $desc);
            $row = implode('|', $currow);
            $currow = explode('|', $row);
        }
        if ($checkNames[$currow[6]] > 0) {
            $checkNames[$currow[6]]++;
        } else {
            $checkNames[$currow[6]] = 1;
        }
        print "*<a href=\"http://wiki.bluegartrls.com/index.php?title=".ucfirst($currow[6])."&action=delete\">".ucfirst($currow[6])."</a>  ".(count($array)-$count)."<br />\r\n";
    }
} elseif ($action=="seek") {
    if (strlen($ind) > 0) {
        foreach ($array as $key => $row) {
            $row = explode('|', $row);
            if ($row[1] == $ind) {
                $id = $key;
            }
        }
    }
    if (strlen($id) > 0) {
        print writeKeyItem($array[$id], false);
    } else {
        print "Link parameter is not set.";
    }
} elseif ($category == "XML") {
    $curType = urldecode($action);
    print $curType."<br />\r\n";
    $write = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd" version="0.3" xml:lang="en">
  <siteinfo>
    <sitename>BGWiki</sitename>
    <base>http://wiki.bluegartrls.com/Main_Page</base>
    <generator>MediaWiki 1.11.0</generator>
    <case>first-letter</case>
      <namespaces>
      <namespace key="-2">Media</namespace>
      <namespace key="-1">Special</namespace>
      <namespace key="0" />
      <namespace key="1">Talk</namespace>
      <namespace key="2">User</namespace>
      <namespace key="3">User talk</namespace>
      <namespace key="4">BGWiki</namespace>
      <namespace key="5">BGWiki talk</namespace>
      <namespace key="6">Image</namespace>
      <namespace key="7">Image talk</namespace>
      <namespace key="8">MediaWiki</namespace>
      <namespace key="9">MediaWiki talk</namespace>
      <namespace key="10">Template</namespace>
      <namespace key="11">Template talk</namespace>
      <namespace key="12">Help</namespace>
      <namespace key="13">Help talk</namespace>
      <namespace key="14">Category</namespace>
      <namespace key="15">Category talk</namespace>
      <namespace key="100">Relation</namespace>
      <namespace key="101">Relation talk</namespace>
      <namespace key="102">Attribute</namespace>
      <namespace key="103">Attribute talk</namespace>
      <namespace key="104">Type</namespace>
      <namespace key="105">Type talk</namespace>
    </namespaces>
  </siteinfo>';
    //filewrite ($write);
    foreach ($array as $count => $row) {
        print writeKeyItem($row, true);
    }
    

    $write .= "
</mediawiki>";
    filewrite($write);
}
    

