<?php
date_default_timezone_set('America/New_York');

function ucitem($str) {
    $safeListIn = array("San D'Oria", " Of ", " A ", " The ", " And ", "A L'Outrance", "Au ", "D'a", "D'o", "O'", "-De-Mer", '-Kazari', 'Bug-Eaten', 'Opo-Opo', '-Cookie', '-Boiled', '-Eyed', '-Zero', ' De ', 'Au-Feu', ' Des ', '-Nez', '-Splitter', '-Arashi', '-No-Ohabari', '-Os', '-Coeur', 'Ichi-An', 'Side-Sword', 'Fluoro-Flora', 'Sand-Caked', 'Fulm-Long', 'Choco-Delight', '-Oto', 'Spook-A-Swirl');
    $safeListOut = array("San d'Oria", " of ", " a ", " the ", " and ", "A l'Outrance", "au ", "d'A", "d'O", "o'", "-de-mer", '-kazari', 'Bug-eaten', 'Opo-opo', '-cookie', '-boiled', '-eyed', '-zero', ' de ', 'au-feu', ' des ', '-nez', '-splitter', '-arashi', '-no-ohabari', '-os', '-coeur', 'Ichi-an', 'Side-sword', 'Fluoro-flora', 'Sand-caked', 'Fulm-long', 'Choco-delight', '-oto', 'Spook-a-Swirl');
    $repSpecialIn = array('-', '"', '(');
    $repSpecialOut = array(' - - ', ' " " ', ' ( ( ');
    $str = str_replace($repSpecialIn, $repSpecialOut, $str);
    $str = ucwords($str);
    $str = str_replace($repSpecialOut, $repSpecialIn, $str);
    $str = str_replace($safeListIn, $safeListOut, $str);
    return ($str);
}
function getEnchantment($str, $type="s") {
    $day = ($tpye == "s") ? 0 : '00';
    $hour = ($tpye == "s") ? 0 : '00';
    $min = ($tpye == "s") ? 0 : '00';
    $sec = ($tpye == "s") ? 0 : '00';
    if (strpos($str, 'd') !== false) {
        $splode = explode('d', $str);
        $day = intval($splode[0]);
        $str = $splode[1];
    }
    if (strpos($str, 'h') !== false) {
        $splode = explode('h', $str);
        $hour = intval($splode[0]);
        $str = $splode[1];
    }
    if (strpos($str, 'm') !== false) {
        $splode = explode('m', $str);
        $min = intval($splode[0]);
        $str = $splode[1];
    }
    if (strpos($str, 's') !== false) {
        $splode = explode('s', $str);
        $sec = intval($splode[0]);
        $str = $splode[1];
    }
    if ($type == 's') {
        $total = 0;
        $total = ($day > 0) ? ($total + ($day * 86400)) : $total;
        $total = ($hour > 0) ? ($total + ($hour * 3600)) : $total;
        $total = ($min > 0) ? ($total + ($min * 60)) : $total;
        $total = ($sec > 0) ? ($total + $sec) : $total;
    } else {
        $total = '';
        if ($day != '00') {
            $hour += $day*24;
            $min = (strlen($min) == 1) ? '0'.$min : strval($min);
            $sec = (strlen($sec) == 1) ? '0'.$sec : strval($sec);
            $total = $hour.':'.$min.':'.$sec;
        } elseif ($hour != '00') {
            $min = (strlen($min) == 1) ? '0'.$min : strval($min);
            $sec = (strlen($sec) == 1) ? '0'.$sec : strval($sec);
            $total = $hour.':'.$min.':'.$sec;
        } elseif ($min != '00') {
            $sec = (strlen($sec) == 1) ? '0'.$sec : strval($sec);
            $total = $min.':'.$sec;
        } else {
            $sec = (strlen($sec) == 1) ? '0'.$sec : strval($sec);
            $total = '0:'.$sec;
        }
    }
    return ($total);
}
function getTP($d) {
    if ($d < 180) {
        return 5.0 + (($d - 180) * 1.5) / 180;
    } elseif ($d >= 180 && $d < 450) {
        return 5.0 + (($d - 180) * 6.50) / 270;
    } elseif ($d >= 450 && $d < 480) {
        return 11.5 + (($d - 450) * 1.5) / 30;
    } elseif ($d >= 480 && $d < 530) {
        return 13.0 + (($d - 480) * 1.5) / 50;
    } else {
        return 14.5 + (($d - 530) * 3.5) / 470;
    }
}
function getMatchingItems($wn, $ln, $ty) {
    $magianIn = array("Revenant Fists", "Ursine Claws", "Taipan Fangs", "Barracudas", "Daka", "Parazonium", "Kila", "Fusetto", "Badelaire", "Khanda", "Shamshir", "Machaera", "Espafut", "Prestelame", "Sombrelame", "Kalavejs", "Alard's Axe", "Richardet's Axe", "Guichard's Axe", "Renaud's Axe", "Maschu", "Kunlun", "Atlas", "Sumeru", "Penitence", "Maleficence", "Absolution", "Reckoning", "Cerastes", "Corsesca", "Langdebeve", "Stingray", "Tobi", "Enchu", "Sekka", "Uzura", "Hiradennotachi", "Kokushitsunotachi", "Hyogugusarinotachi", "Keitonotachi", "Canne de Combat", "Naboot", "Hoeroa", "Makhila", "Taiaha", "Yantok", "Shareeravadi", "Agni's Staff", "Varuna's Staff", "Vayu's Staff", "Kubera's Staff", "Indra's Staff", "Soma's Staff", "Surya's Staff", "Yama's Staff", "Sedikutchi", "Harrier", "Condor", "Bateleur", "Sparrowhawk",
    "Bedlam", "Spitfire", "Mayhem", "Anarchy");
    $magianOut = array("Dumuzis", "Ursa Majors", "Oxyuranis", "Sphyras", "Khandroma", "Praxidikai", "Thokcha", "Centovente", "Brunello", "Khadga", "Shikargar", "Thibron", "Xiphias", "Fulgurante", "Tenebreuse", "Kauriraris", "Sacripante", "Ganelon", "Astolfo", "Fernagu", "Shamash", "Liushi Shan", "Telamon", "Tavatimsa", "Umiliati", "Ogresse", "Cloacina", "Basanizo", "Daboya", "Chauve-Souris", "Nahvalr", "Sixgill", "Kasasagi", "Majimun", "Arisui", "Hitaki", "Torigashiranotachi", "Kawatsutsuminotachi", "Kabutsuchinotachi", "Kantonotachi", "Rose Couverte", "Tahtib", "Tutunui", "Ukaldi", "Paikea", "Urduja", "Tuahjebat", "Atar", "Vourukasha", "Vayuvata", "Vishrava", "Apamajas", "Haoma", "Arka", "Xsaeta", "Muruga", "Circinae", "Gymnogyps", "Terathopus", "Accipiter", "Mollfrith", "Aithon", "Erra", "Ataktos");
    $roman = array(" IV", " III", " II", " I",);
    $arr = @file('ultimateParse.txt');
    $output = array();
    $op = '';

    if ($ty = 'magians') {
        $com = explode("+", $ln);
        $com[0] = str_replace($roman, '', $com[0]);
        $comp = trim($com[0]);
        $comp = str_replace($magianOut, $magianIn, $comp);
        foreach ($arr as $key => $curarr) {
            $currow = explode('|', $curarr);
            $test = str_replace($magianOut, $magianIn, $currow[4]);
            $test = str_replace($roman, '', $test);
            if (stristr($test, $comp) !== false && $currow[2] != $wn) {
                array_push($output, $currow[2]);
            }
        }
    } else {
        foreach ($arr as $curarr) {
            $currow = explode('|', $curarr);
            $test = str_replace($roman, '', $test);
            if ($currow[4] == $ln && $currow[2] != $wn) {
                array_push($output, $currow[2]);
            }

        }
    }
    if (count($output) > 0) {
        $op = "\r\n<br />* [[" . implode("]]\r\n<br />* [[", $output) . "]]";
    }
    return $op;
}
function getTimeIndex() {
    return floor (microtime(true));
}
function xmlWrapper($na, $str) {
    $repIn = array('&', '"');
    $repOut = array('&amp;', '&quot;');
    global $fIndex;
    global $now;
    if ($fIndex == 0) {
        $fIndex = getTimeIndex();
    } else {
        $fIndex++;
    }
    $str = str_replace($repIn, $repOut, $str);
    $na = str_replace($repIn, $repOut, $na);
    $op = "&nbsp;&nbsp;&lt;page&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;$na&lt;/title&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;revision&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;text xml:space=\"preserve\"&gt;$str&lt;/text&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/revision&gt;
<br />&nbsp;&nbsp;&lt;/page&gt;<br />";
    return $op;
}

function xmlStart() {
    print "&lt;mediawiki xmlns=\"http://www.mediawiki.org/xml/export-0.5/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.mediawiki.org/xml/export-0.5/ http://www.mediawiki.org/xml/export-0.5.xsd\" version=\"0.5\" xml:lang=\"en\"&gt;
<br />&nbsp; &lt;siteinfo&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;sitename&gt;BGWiki&lt;/sitename&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;base&gt;http://wiki.bluegartr.com/bg/Main_Page&lt;/base&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;generator&gt;MediaWiki 1.17.0&lt;/generator&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;case&gt;first-letter&lt;/case&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;namespaces&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"-2\" case=\"first-letter\"&gt;Media&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"-1\" case=\"first-letter\"&gt;Special&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"0\" case=\"first-letter\" /&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"1\" case=\"first-letter\"&gt;Talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"2\" case=\"first-letter\"&gt;User&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"3\" case=\"first-letter\"&gt;User talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"4\" case=\"first-letter\"&gt;BGWiki&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"5\" case=\"first-letter\"&gt;BGWiki talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"6\" case=\"first-letter\"&gt;File&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"7\" case=\"first-letter\"&gt;File talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"8\" case=\"first-letter\"&gt;MediaWiki&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"9\" case=\"first-letter\"&gt;MediaWiki talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"10\" case=\"first-letter\"&gt;Template&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"11\" case=\"first-letter\"&gt;Template talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"12\" case=\"first-letter\"&gt;Help&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"13\" case=\"first-letter\"&gt;Help talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"14\" case=\"first-letter\"&gt;Category&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"15\" case=\"first-letter\"&gt;Category talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"100\" case=\"first-letter\"&gt;Math&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;namespace key=\"101\" case=\"first-letter\"&gt;Math talk&lt;/namespace&gt;
<br />&nbsp;&nbsp;&nbsp; &lt;/namespaces&gt;
<br />&nbsp; &lt;/siteinfo&gt;\r\n<br />";
}

function xmlEnd() {
    print "&lt;/mediawiki&gt;";
}

function writeTemplate($l, $p=false, $xml=false, $return=false) {
    $flagListIn = array('None', 'CanUse', 'CanTradeNPC', 'NoAuction', 'CanEquip', 'NoSale', 'NoDelivery', 'Ex', 'Flag04', 'PoL Send');
    $flagListOut = array('', 'Can Use', 'NPC tradable', 'Not auctionable', 'Equippable', 'Not vendorable', 'Not sendable', 'Not sendable, Exclusive', 'Sendable within PoL account', 'Sendable within PoL account');
    $typeListIn = array('UsableItem', 'QuestItem');
    $typeListOut = array('Usable Item', 'Quest Item');
    $targetListIn = array('PartyMember', 'Ally');
    $targetListOut = array('Party Member', 'Alliance Member');
    $jobListIn = array('WAR', 'MNK', 'WHM', 'BLM', 'RDM', 'THF', 'PLD', 'DRK', 'BST', 'BRD', 'RNG', 'SAM', 'NIN', 'DRG', 'SMN', 'BLU', 'COR', 'PUP', 'DNC', 'SCH');
    $jobListOut = array('Warrior', 'Monk', 'White Mage', 'Black Mage', 'Red Mage', 'Thief', 'Paladin', 'Dark Knight', 'Beastmaster', 'Bard', 'Ranger', 'Samurai', 'Ninja', 'Dragoon', 'Summoner', 'Blue Mage', 'Corsair', 'Puppetmaster', 'Dancer', 'Scholar');
    $raceListIn = array('HumeMale', 'HumeFemale', 'EvlaanMale', 'ElvaanFemale', 'TarutaruMale', 'TarutaruFemale');
    $raceListOut = array('Hume Male', 'Hume Female', 'Evlaan Male', 'Elvaan Female', 'Tarutaru Male', 'Tarutaru Female');
    $primativeIn = array('"', ':', '?');
    $primativeOut = array('_', '_', '_');
    $imageIn = array('"', ':');
    $imageOut = array('', '-');
    $ffxiahIn = array(':', '"', ' & ', ' &', '& ', '&', ' ');
    $ffxiahOut = array('', '', ' and ', ' and ', ' and ', ' and ', '-');
    $magianCategories = array();
    $magianCategories['Hand-to-Hand'] = 'Hand-to-Hand';
    $magianCategories['Dagger'] = 'Daggers';
    $magianCategories['Sword'] = 'Swords';
    $magianCategories['Great Sword'] = 'Great Swords';
    $magianCategories['Axe'] = 'Axes';
    $magianCategories['Great Axe'] = 'Great Axes';
    $magianCategories['Scythe'] = 'Scythes';
    $magianCategories['Polearm'] = 'Polearms';
    $magianCategories['Katana'] = 'Katanas';
    $magianCategories['Great Katana'] = 'Great Katanas';
    $magianCategories['Club'] = 'Clubs';
    $magianCategories['Staff'] = 'Staves';
    $magianCategories['Archery'] = 'Archery';
    $magianCategories['Marksmanship'] = 'Marksmanship';
    $magianCategories['Instrument'] = 'Instruments';
    $magianCategories['Shield'] = 'Shields';
    $magianCategories['Head'] = 'Heads';
    $magianCategories['Neck'] = 'Necks';
    $magianCategories['Body'] = 'Bodies';
    $magianCategories['Hands'] = 'Hands';
    $magianCategories['Waist'] = 'Waists';
    $magianCategories['Back'] = 'Backs';
    $magianCategories['Rings'] = 'Rings';
    $magianCategories['Earrings'] = 'Earrings';
    $magianCategories['Legs'] = 'Legs';
    $magianCategories['Feet'] = 'Feet';
    $relicArmorIn = array("Warrior's Mask", "Warrior's Lorica", "Warrior's Mufflers", "Warrior's Cuisses", "Warrior's Calligae", 'Melee Crown', 'Melee Cyclas', 'Melee Gloves', 'Melee Hose', 'Melee Gaiters', "Cleric's Cap", "Cleric's Briault", "Cleric's Mitts", "Cleric's Pantaloons", "Cleric's Duckbills", "Sorcerer's Petasos", "Sorcerer's Coat", "Sorcerer's Gloves", "Sorcerer's Tonban", "Sorcerer's Sabots", "Duelist's Chapeau", "Duelist's Tabard", "Duelist's Gloves", "Duelist's Tights", "Duelist's Boots", "Assassin's Bonnet", "Assassin's Vest", "Assassin's Armlets", "Assassin's Culottes", "Assassin's Poulaines", 'Valor Coronet', 'Valor Surcoat', 'Valor Gauntlets', 'Valor Breeches', 'Valor Leggings', 'Abyss Burgeonet', 'Abyss Cuirass', 'Abyss Gauntlets', 'Abyss Flanchard', 'Abyss Sollerets', 'Monster Helm', 'Monster Jackcoat', 'Monster Gloves', 'Monster Trousers', 'Monster Gaiters', "Bard's Roundlet", "Bard's Justaucorps", "Bard's Cuffs", "Bard's Cannions", "Bard's Slippers", "Scout's Beret", "Scout's Jerkin",
    "Scout's Bracers", "Scout's Braccae", "Scout's Socks", 'Saotome Kabuto', 'Saotome Domaru', 'Saotome Kote', 'Saotome Haidate', 'Saotome Sune-Ate', 'Koga Hatsuburi', 'Koga Chainmail', 'Koga Tekko', 'Koga Hakama', 'Koga Kyahan', 'Wyrm Armet', 'Wyrm Mail', 'Wyrm Finger Gauntlets', 'Wyrm Brais', 'Wyrm Greaves', "Summoner's Horn", "Summoner's Doublet", "Summoner's Bracers", "Summoner's Spats", "Summoner's Pigaches", 'Mirage Keffiyeh', 'Mirage Jubbah', 'Mirage Bazubands', 'Mirage Shalwar', 'Mirage Charuqs', 'Commodore Tricorne', 'Commodore Frac', 'Commodore Gants', 'Commodore Culottes', 'Commodore Bottes', 'Pantin Taj', 'Pantin Tobe', 'Pantin Dastanas', 'Pantin Churidars', 'Pantin Babouches', 'Etoile Tiara', 'Etoile Casaque', 'Etoile Bangles', 'Etoile Tights', 'Etoile Toe Shoes', 'Argute Mortarboard', 'Argute Gown', 'Argute Bracers', 'Argute Pants', 'Argute Loafers');
    $magianWeapon = array("Revenant Fists", "Ursine Claws", "Taipan Fangs", "Barracudas", "Daka", "Parazonium", "Kila", "Fusetto", "Badelaire", "Khanda", "Shamshir", "Machaera", "Espafut", "Prestelame", "Sombrelame", "Kalavejs", "Alard's Axe", "Richardet's Axe", "Guichard's Axe", "Renaud's Axe", "Maschu", "Kunlun", "Atlas", "Sumeru", "Penitence", "Maleficence", "Absolution", "Reckoning", "Cerastes", "Corsesca", "Langdebeve", "Stingray", "Tobi", "Enchu", "Sekka", "Uzura", "Hiradennotachi", "Kokushitsunotachi", "Hyogugusarinotachi", "Keitonotachi", "Canne de Combat", "Naboot", "Hoeroa", "Makhila", "Taiaha", "Yantok", "Shareeravadi", "Agni's Staff", "Varuna's Staff", "Vayu's Staff", "Kubera's Staff", "Indra's Staff", "Soma's Staff", "Surya's Staff", "Yama's Staff", "Sedikutchi", "Harrier", "Condor", "Bateleur", "Sparrowhawk",
    "Bedlam", "Spitfire", "Mayhem", "Anarchy", "Dumuzis", "Ursa Majors", "Oxyuranis", "Sphyras", "Khandroma", "Praxidikai", "Thokcha", "Centovente", "Brunello", "Khadga", "Shikargar", "Thibron", "Xiphias", "Fulgurante", "Tenebreuse", "Kauriraris", "Sacripante", "Ganelon", "Astolfo", "Fernagu", "Shamash", "Liushi Shan", "Telamon", "Tavatimsa", "Umiliati", "Ogresse", "Cloacina", "Basanizo", "Daboya", "Chauve-Souris", "Nahvalr", "Sixgill", "Kasasagi", "Majimun", "Arisui", "Hitaki", "Torigashiranotachi", "Kawatsutsuminotachi", "Kabutsuchinotachi", "Kantonotachi", "Rose Couverte", "Tahtib", "Tutunui", "Ukaldi", "Paikea", "Urduja", "Tuahjebat", "Atar", "Vourukasha", "Vayuvata", "Vishrava", "Apamajas", "Haoma", "Arka", "Xsaeta", "Muruga", "Circinae", "Gymnogyps", "Terathopus", "Accipiter", "Mollfrith", "Aithon", "Erra", "Ataktos");
    $woeWeapon = array("Revenant Fists", "Daka", "Badelaire", "Espafut", "Alard's Axe", "Maschu", "Penitence", "Cerastes", "Tobi", "Hiradennotachi", "Canne de Combat", "Taiaha", "Harrier", "Bedlam", "Dumuzis", "Khandroma", "Brunello", "Xiphias", "Sacripante", "Shamash", "Umiliati", "Daboya", "Kasasagi", "Torigashiranotachi", "Rose Couverte", "Paikea", "Circinae", "Mollfrith");
    $specialWeaponIn = array("Ochain", "Daurdabla", "Verethragna", "Twashtar", "Almace", "Caladbolg", "Farsha", "Ukonvasara", "Redemption", "Rhongomiant", "Kannagi", "Masamune", "Gambanteinn", "Hvergelmir", "Gandiva", "Armageddon", "Aegis", "Gjallarhorn", "Spharai", "Mandau", "Excalibur", "Ragnarok", "Guttler", "Bravura", "Gungnir", "Apocalypse", "Kikoku", "Amanomurakumo", "Mjollnir", "Claustrum", "Annihilator", "Yoichinoyumi", "Conqueror", "Glanzfaust", "Yagrush", "Laevateinn", "Murgleis", "Vajra", "Burtgang", "Liberator", "Aymur", "Carnwenhan", "Gastraphetes", "Kogarasumaru", "Nagi", "Ryunohige", "Nirvana", "Tizona", "Death Penalty", "Kenkonken", "Terpsichore", "Tupsimati");
    $specialWeaponOut = array("Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic");
    $relicArmorAugment = array("Adds \"Enhances 'Savagery' effect\"", "Adds \"Enhances 'Aggressive Aim' effect\"", "Adds \"Enhances 'Mighty Strikes' effect\"", "Adds \"Enhances 'Warrior's Charge' effect\"", "Adds \"Enhances 'Tomahawk' effect\"", "Adds \"Enhances 'Penance' effect\"", "Adds \"Enhances 'Formless Strikes' effect\"", "Adds \"Enhances 'Invigorate' effect\"", "Adds \"Enhances 'Hundred Fists' effect\"", "Adds \"Enhances 'Mantra' effect\"", "Adds \"Enhances 'Devotion V' effect\"", "Adds \"Enhances 'Benediction' effect\"", "Adds \"Enhances 'Martyr V' effect\"", "Adds \"Enhances 'Shellra V' effect\"", "Adds \"Enhances 'Protectra V' effect\"", "Adds \"Increases Ancient Magic II damage\"", "Adds \"Enhances 'Manafont' effect\"", "Adds \"Reduces Ancient Magic II casting time\"", "Adds \"Increases Ancient Magic II accuracy\"", "Adds \"Reduces Ancient Magic II MP cost\"", "Adds \"Enhances 'Dia III' effect\" and \"Enhances 'Slow II' effect\"", "Adds \"Enhances 'Chainspell' effect\"",
    "Adds \"Enhances 'Phalanx II' effect\"", "Adds \"Enhances 'Bio III' effect\" and \"Enhances 'Blind II' effect\"", "Adds \"Enhances 'Paralyze II' effect\"", "Adds \"Enhances 'Aura Steal' effect\"", "Adds \"Enhances 'Ambush' effect\"", "Adds \"Enhances 'Perfect Dodge' effect\"", "Adds \"Enhances 'Feint' effect\"", "Adds \"Enh. 'Assassin's Charge' effect\"", "Adds \"Enhances 'Iron Will' effect\"", "Adds \"Enhances 'Fealty' effect\"", "Adds \"Enhances 'Chivalry' effect\"", "Adds \"Enhances 'Invincible' effect\"", "Adds \"Enhances 'Guardian' effect\"", "Adds \"Enhances 'Dark Seal' effect\"", "Adds \"Enhances 'Blood Weapon' effect\"", "Adds \"Enhances 'Diabolic Eye' effect\"", "Adds \"Enhances 'Muted Soul' effect\"", "Adds \"Enhances 'Desperate Blows' effect\"", "Adds \"Enhances 'Killer Instinct' effect\"", "Adds \"Enhances 'Feral Howl' effect\"", "Adds \"Enhances 'Beast Affinity' effect\"", "Adds \"Enhances 'Familiar' effect\"", "Adds \"Enhances 'Beast Healer' effect\"",
    "Adds \"Enhances 'Foe Sirvente' effect\"", "Adds \"Enhances 'Troubador' effect\"", "Adds \"Enh. 'Adventurer's Dirge' effect\"", "Adds \"Enhances 'Soul Voice' effect\"", "Adds \"Enhances 'Nightingale' effect\"", "Adds \"Enhances 'Recycle' effect\"", "Adds \"Enhances 'Snapshot' effect\"", "Adds \"Enhances 'Flashy Shot' effect\"", "Adds \"Enhances 'Eagle Eye Shot' effect\"", "Adds \"Enhances 'Stealth Shot' effect\"", "Adds \"Enhances 'Ikishoten' effect\"", "Adds \"Enhances 'Overwhelm' effect\"", "Adds \"Enhances 'Blade Bash' effect\"", "Adds \"Enhances 'Shikikoyo' effect\"", "Adds \"Enhances 'Meikyo Shisui' effect\"", "Adds \"Increases elem. ninjutsu III damage\"", "Adds \"Enhances 'Sange' effect\"", "Adds \"Enh. 'Ninja Tool Expertise' effect\"", "Adds \"Enhances 'Mijin Gakure' effect\"", "Adds \"Reduces elem. ninjutsu III cast time\"", "Adds \"Enhances 'Deep Breathing' effect\"", "Adds \"Enhances 'Spirit Surge' effect\"", "Adds \"Enhances 'Angon' effect\"", "Adds \"Enhances 'Strafe' effect\"",
    "Adds \"Enhances 'Empathy' effect\"", "Adds \"Enhances 'Astral Flow' effect\"", "Adds \"Reduces Sp. 'Blood Pact' MP cost\"", "Adds \"Inc. Sp. 'Blood Pact magic burst dmg.\"", "Adds \"Increases Sp. 'Blood Pact accuracy\"", "Adds \"Increases Sp. 'Blood Pact crit. dmg.\"", "Adds \"Enhances 'Convergence' effect\"", "Adds \"Enhances 'Enchainment' effect\"", "Adds \"Enhances 'Azure Lore' effect\"", "Adds \"Enhances 'Assimilation' effect\"", "Adds \"Enhances 'Diffusion' effect\"", "Adds \"Enhances 'Winning Streak' effect\"", "Adds \"Enhances 'Loaded Deck' effect\"", "Adds \"Enhances 'Fold' effect\"", "Adds \"Enhances 'Snake Eye' effect\"", "Adds \"Enhances 'Wild Card' effect\"", "Adds \"Enhances 'Optimization' effect\"", "Adds \"Enhances 'Overdrive' effect\"", "Adds \"Enhances 'Fine-Tuning' effect\"", "Adds \"Enhances 'Ventriloquy' effect\"", "Adds \"Enhances 'Role Reversal' effect\"", "Adds \"Enhances 'Trance' effect\"", "Adds \"Enhances 'No Foot Rise' effect\"", "Adds \"Enhances 'Fan Dance' effect\"",
    "Adds \"Enhances 'Saber Dance' effect\"", "Adds \"Enhances 'Closed Position' effect\"", "Adds \"Enh. 'Altruism' and 'Focalization'\"", "Adds \"Enhances 'Enlightenment' effect\"", "Adds \"Enh. 'Tranquility' and 'Equanimity'\"", "Adds \"Enhances 'Tabula Rasa' effect\"", "Adds \"Enhances 'Stormsurge' effect\"");
    $specialText = '';
    $itemInfo = "\r\n<br />{{Item Info}}";
    $spCheck = '';
    $hq = '';
    $obtain = '';
    $related = '';
    $note = '';
    $roman = array(" IV", " III", " II", " I",);

    $stuff = explode('|', $l);
    $category = '';
    $type = str_replace('FFXI ', '', $stuff[0]);
    $type = str_replace($typeListIn, $typeListOut, $type);
    $strip = explode('+', ucitem($stuff[4]));
    $hq = trim($strip[1]);
    $strip[0] = str_replace($roman, '', $strip[0]);
    $baseName = trim($strip[0]);
    $baseName = $baseName;
    $specialCheck = str_replace($specialWeaponIn, $specialWeaponOut, $stuff[3]);
    $relicCheck = str_replace($relicArmorIn, 'Relic Armor', $baseName);
    if ($specialCheck == "Relic" || $specialCheck == 'Empyrean' || $specialCheck == 'Mythic') {
        $spCheck = 'special';
    } elseif ($relicCheck == 'Relic Armor') {
        if ($hq == 2) {
            $start = 4654;
            $id = array_search($baseName, $relicArmorIn);
            $relicAug = $relicArmorAugment[$id];
            $trial1 = ($id *2) + $start;
            $trial2 = $trial1 + 1;
            $trial3 = $start + $id + 300;
            $note = "* Upgraded from [[$baseName]] via [[Trial of the Magians|Magian Trial]] $trial1 or [[$baseName +1]] via [[Trial of the Magians|Magian Trial]] $trial2\r\n<br />* Can be further augmented with '''$relicAug''' via [[Trial of the Magians|Magian Trial]] $trial3\r\n<br />";
        }
        $specialCheck = 'Relic Armor';
        $spCheck = 'armor';
    } else {
        $specialCheck = str_replace($magianWeapon, "Magian", $baseName);
        if ($specialCheck == "Magian") {
            $sp = str_replace($woeWeapon, "Walk of Echoes", $baseName);
            $specialCheck = ($sp == "Walk of Echoes") ? "Walk of Echoes" : $specialCheck;
            $spCheck = 'magian';
        }
    }
    //print $strip[0] . " | $baseName | $specialCheck | $spCheck\r\n<br />";

    $dataType = $stuff[0];
    $id = $stuff[1];
    $primative = str_replace($primativeIn, $primativeOut, $stuff[3]);
    $icon = "[". substr('00000000', 0, (8-strlen($id))). "$id] $primative.png\r\n<br />";
    $wikiName = $stuff[2];
    $name = $stuff[3];
    $longName = $stuff[4];
    $ffxidb = getIDVal($id);
    $flags = str_replace('"', '', $stuff[6]);
    $ffxiahName = str_replace($ffxiahIn, $ffxiahOut, $name);
    $ffxiah = "$ffxidb/" . strtolower($ffxiahName);
    if (strstr($newRow[6], "Ex") !== false && strstr($newRow[6], "NoAuction") === false) {
        $flags = 'NoAuction, '.$flags;
    }
    $flags = str_replace($flagListIn, $flagListOut, $flags);
    if (strstr($flags, "Linkshell") !== false && stristr($longName, "link")) {
        $flags = str_replace('Linkshell', '', $flags);
        $flags = str_replace(', ,', ',', $flags);
    }
    $description = $stuff[7];
    $stack = $stuff[8];
    $target = str_replace($targetListIn, $targetListOut, $stuff[9]);
    if ($dataType == 'FFXI Armor' || $dataType == 'FFXI Weapon') {
        $level = $stuff[10];
        $slot = $stuff[11];
        $races = str_replace('"', '', $stuff[12]);
        $jobs = str_replace('"', '', $stuff[13]);
        $jobs = str_replace($jobListIn, $jobListOut, $jobs);
        if ($jobs != "All") {
            $jobs = "[[" . str_replace(', ', ']] / [[', $jobs) . "]]";
        } else {
            $jobs = "All Jobs";
        }
        if ($races == "All") {
            $races = "All Races";
        } elseif ($races == "Male") {
            $races = "[[Hume Male]] / [[Elvaan Male]] / [[Tarutaru Male]] / [[Galka]]";
        } elseif ($races == "Female") {
            $races = "[[Hume Female]] / [[Elvaan Female]] / [[Tarutaru Female]] / [[Mithra]]";
        }else {
            $races = str_replace($raceListIn, $raceListOut, $races);
            $races = "[[" . str_replace(', ', ']] / [[', $races) . "]]";
        }
        if ($dataType == 'FFXI Weapon') {
            $damage = $stuff[14];
            $delay = $stuff[15];
            $dps = $stuff[16];
            $weaponType = $stuff[17];
            $charges = $stuff[18];
            $castTime = $stuff[19];
            $chargeTime = $stuff[20];
            $rechargeTime = $stuff[21];
            if ($weaponType == 'String Instrument' || $weaponType == 'Wind Instrument' || $weaponType == "None" || $weaponType == "Pet Items") {
                $dps = 0;
                $damage = 0;
                $delay = 0;
                $tp = 0;
            } else {
                $tp = getTP($delay);
            }
            if ($weaponType == 'String Instrument' || $weaponType == 'Wind Instrument') {
                $category = '[[Category:Instrument]]';
            } elseif ($weaponType == 'None' && $slot == 'Sub') {
                $category = '[[Category:Grips]]';
            } elseif ($weaponType == 'None' || $weaponType == '-' || $weaponType == 'Pet Items') {
                $category = '[[Category:Ammo]]';
            } elseif ($weaponType == 'Archery' || $weaponType == 'Marksmanship') {
                $category = "[[Category:$weaponType]]";
                if ($slot == 'Ammo') {
                    $category .= ' [[Category:Ammo]]';
                }
            } else {
                $category = "[[Category:$weaponType]]";
            }
        } else {
            $defense = $stuff[14];
            $shieldSize = (strlen($stuff[15]) > 0) ? "\r\n<br />|shield size=".$stuff[15] : '';
            $category = "[[Category:$slot]]";
            $charges = $stuff[16];
            $castTime = $stuff[17];
            $chargeTime = $stuff[18];
            $rechargeTime = $stuff[19];
        }
        if ($charges > 0) {
            $castTime = getEnchantment($castTime, 's');
            $chargeTime = getEnchantment($chargeTime, 's');
            $rechargeTime = getEnchantment($rechargeTime, 's');
        }
        if ($spCheck != '') {
            if ($specialCheck != 'Magian' && $specialCheck != 'Relic Armor') {
                $category.= " [[Category:$specialCheck Weapons]]";
            } elseif ($specialCheck == 'Relic Armor') {
                if ($hq == '') {
                    $category .= " [[Category:$specialCheck]]";
                } else {
                    $category .= " [[Category:$specialCheck +" . $hq . "]]";
                }
            }
            if ($level > 75) {
                $itemType = ($dataType == 'FFXI Weapon') ? $weaponType : $slot;
                if ($itemType == "Shields") {
                    $itemType = "Shield";
                } elseif ($itemType == 'Ranged') {
                    $itemType = "Archery";
                } elseif ($itemType == 'Wind Instruments' || $itemType == 'String Instruments') {
                    $itemType = 'Instruments';
                }
                $specialText = "\r\n<br />{{Item NPC Table
<br />|Insert NPC List Template=
<br />{{Item NPC
<br />|NPC Name=Magian Moogle
<br />|Zone=Ru'Lude Gardens
<br />|position=H-5
<br />|Notes=Result of the [[Trial of the Magians]].  See the [[$itemType Trials]] page for more details.
<br />}}
<br />}}";
            $category.= " [[Category:Trial of the Magians ".$magianCategories[$itemType]."]]";
            }
            $obtain = "$specialText";
            if ($spCheck == 'special') {
                $related .= "<br />* [[".$stuff[3]."]]";
            }
            $related .= getMatchingItems($wikiName, $longName, $spCheck);
            $itemInfo = '';
        }
    } else if ($dataType == 'FFXI Furnishing' || $dataType == 'FFXI Mannequin' || $dataType == 'FFXI Flowerpot') {
        $doFurninshing = true;
        $furnEle = str_replace('Air', 'Wind', $stuff[10]);
        $furnNum = $stuff[11];
        $category .= ' [[Category:Furnishings]]';
        if ($dataType == 'FFXI Mannequin') {
            $repchar = (strpos($description, 'female') !== false) ? 'F' : 'M';
            $repSym = (strpos($description, 'female') !== false) ? "&#9792;" : "&#9794;";
            $wikiName = str_replace('?', $repchar, $wikiName);
            $name = str_replace('?', $repchar, $name);
            $longName = str_replace('?', $repSym, $longName);
            $ffxiah = str_replace('?', '', $ffxiah);
        }
    } else {
        $activationTime = substr(trim($stuff[10]), 0, -1);
        if ($activationTime == 1) {
            $activationTime .= " second";
        } else {
            $activationTime .= " seconds";
        }
    }
    $imageRename = str_replace($imageIn, $imageOut, $wikiName);
    $imageTag = ($wikiName != $imageRename)? "\r\n<br />|icon=[[Image:$imageRename icon.png]]\r\n<br />|description image=[[Image:$imageRename description.png]]" : $imageTag = '';

    if ($type == 'Fish') {
        $type = 'Usable Item';
        $category .= ' [[Category:Fish]]';
    }
    $description = str_replace('<br />', ' ', $description);
    if (strstr($description, "[br]Functionality:[br]") !== false) {
        $descExp = explode('Functionality:[br]', $description);
        $description = $descExp[0];
        $functionality = str_replace('?', '', $descExp[1]);
        $functionality = str_replace('°', '', $descExp[1]);
        $functionality = str_replace('[br]', ' ', $functionality);
        $type = 'Automaton Item';
        $category .= ' [[Category:Automaton Equipment]]';
    } else {
        $functionality = '';
    }
    if (strstr($stuff[2], " (Scroll)") !== false) {
        $category .= ' [[Category:Scrolls]]';
        if (strstr($description, "Teaches the summoning magic")) {
            $category .= ' [[Category:Summoner Pacts]]';
        } elseif (strstr($description, "Teaches the ninjutsu")) {
            $category .= ' [[Category:Ninjutsu Scrolls]]';
        } elseif (strstr($description, "Teaches the black magic")) {
            $category .= ' [[Category:Black Magic Scrolls]]';
        } elseif (strstr($description, "Teaches the white magic")) {
            $category .= ' [[Category:White Magic Scrolls]]';
        } elseif (strstr($description, "BRD Lv.")) {
            $category .= ' [[Category:Songs]]';
        }
    } else if (strstr($stuff[6], "Scroll") !== false && stristr($stuff[5], "dice") !== false){
        $category .= ' [[Category:Scrolls]] [[Category:Dice]]';
    }
    $description = str_replace('[br]', ' ', $description);
    $output = '';
    if ($p == true) {
        $output .= $icon;
    }
    if (($id >= 4096 && $id < 11265) && strstr($stuff[6], "CanUse") !== false) {
        $output .= "{{usable item
<br />|description=$description
<br />|type=$type
<br />|flags=$flags
<br />|stack size=$stack
<br />|singular log name=$longName
<br />|valid targets=$target
<br />|activation time=$activationTime
<br />|ffxiah=$ffxiah
<br />|ffxidb=$ffxidb
$imageTag<br />}}";
    } else {
        $output .= "{{item
<br />|description=$description
<br />|type=$type
<br />|flags=$flags
<br />|stack size=$stack
<br />|singular log name=$longName
<br />|ffxiah=$ffxiah
<br />|ffxidb=$ffxidb";
        if ($doFurninshing == true) {
            $output .= "\r\n<br />|storage=$furnNum
<br />|element=$furnEle
<br />|moghancement=";
        }  elseif ($functionality != '') {
            $output .= "\r\n<br />|functionality=".$functionality;
        }
        $output .= "$imageTag\r\n<br />}}";
    }
    if ($type == 'Armor') {
        $output .= "\r\n<br />{{armor
<br />|races=$races
<br />|slot=$slot$shieldSize
<br />|level=$level
<br />|jobs=$jobs
<br />}}";
    } elseif ($type == 'Weapon') {
        $output .= "\r\n<br />{{weapon
<br />|races=$races
<br />|weapon skill=$weaponType
<br />|damage=$damage
<br />|delay=$delay
<br />|damage per second=$dps
<br />|tp per hit=$tp
<br />|level=$level
<br />|jobs=$jobs
<br />}}";
    }
    if ($charges > 0) {
        $output .= "\r\n<br />{{enchantment
<br />|max charges=$charges
<br />|casting time=$castTime seconds
<br />|use delay=$chargeTime seconds
<br />|reuse delay=$rechargeTime seconds
<br />|valid targets=$target
<br />}}";
    }
    if (strlen($obtain) > 0) {
        //$output .= $obtain . "\r\n<br />";
    }
    if (strlen($note) > 0) {
        //$output .= "\r\n<br />== Notes ==\r\n<br />$note";
    }
    if (strlen($related) > 0) {
        //$output .= "\r\n<br />== Related Links ==\r\n$related";
    }
    if (strlen($category) > 0) {
        $output .= "\r\n<br />$category";
    }
    $output .= $itemInfo;
    if ($xml == true) {
        return $output;
    } elseif ($return == true) {
        return $output;
    } else {
        print $output;
    }
}
function writeKeyItem($st, $xml=false) {
    $op = '';
    $renameIn = array(':', '#', '"');
    $renameOut = array('', '', '');
    //$abysseaColors = array("Azure", "Crimson", "Emerald", "Indigo", "Jade", "Sapphire", "Scarlet", "Vermillion", "Viridian");
    if (is_string($st)) {
        $currow = explode('|', $st);
    } elseif (is_array($st)) {
        $currow = $st;
    } else {
        return 'Error';
    }
    $desc = explode('[br]', $currow[7], 2);
    if (count($desc) == 2) {
        $currow[7] = implode('|', $desc);
        $row = implode('|', $currow);
        $currow = explode('|', $row);
    }
    $description = $currow[8];
    $description = str_replace('[br]', '&lt;br /&gt; ', $description);
    $description = str_replace('  ', '&amp;nbsp;&amp;nbsp;', $description);
    $name = ucfirst($currow[6]);
    $name = str_replace('#', '', $name);
    $rename = str_replace($renameIn ,$renameOut, $name);
    $rename = ($name != $rename) ? "\r\n<br />|Image=$rename.jpg" : '';
    $category = '';
    if (strstr($name, "Atmacite of")) {
        $category = "\r\n<br />[[Category:Atmacite]]";
    } elseif (stristr($name, "periapt of")) {
        $category = "\r\n<br />[[Category:Periapt]]";
    } elseif (strstr($name, "stratum abyssite")) {
        $category = "\r\n<br />[[Category:Voidwatch]]";
    } elseif (strstr($name, "Atma of")) {
        $category = "\r\n<br />[[Category:Abyssea Atma]]";
    } elseif (strstr($name, "abyssite of")) {
        $category = "\r\n<br />[[Category:Abyssea Abyssite]]";
    }
    $op = "{{Key Item Header
<br />|Name=$name
<br />|Type=".$currow[9]."
<br />|Description=$description$rename
<br />}}
<br />{{Key Item Usage Table
<br />|type=Cost/Quest/BCNM/ISNM
<br />|Insert Key Item Acquisition Instance Template=
<br />{{Key Item Acquisition Instance
<br />|npcname=
<br />|zone=
<br />|cost=
<br />}}
<br />|Insert Key Item Use Instance Template=
<br />{{Key Item Use Instance
<br />|usage=
<br />|name=
<br />}}
<br />}}
<br />==Related Links==
<br />&lt;!--* List of links--&gt;$category";
    if ($xml == true) {
        $op = xmlWrapper($name, $op);
    }
    return $op;
}

function getIDVal($n) {
    $ex = intVal($n);
    if ($ex == 0) {
        $idNum = str_split(trim($n));
        $kill = false;
        while ($kill == false) {
                array_shift($idNum);
                $kill = (count($idNum) == 0 || $idNum[0] != 0) ? true : false;
        }
        $ex = (count($idNum) > 0) ? implode('', $idNum) : 0;
    }
    return ($ex);
}
?>
