<?php

ini_set("memory_limit", "160M");
set_time_limit(30 * 60);
$dbhost = "localhost";
$dbuname = "root";
$dbupass = "";
$dbname = "wowbeez_db";
//database connection
$dbi = mysql_connect($dbhost, $dbuname, $dbupass, true) or die("Couldn't connect to database server!");
mysql_select_db($dbname, $dbi) or die("Q 200603201239" . mysql_error($dbi));
$drop_type = 1;
$fout = fopen("out.sql", "wt");
$fout_clean = fopen("out_clean.sql", "wt");
process_link();
fclose($fout);
fclose($fout_clean);
function process_link()
{
    $drop_type = 1;
    $link = "http://www.wowhead.com/spell=31252/prospecting";
    $file = @fopen($link, "r");
    if ($file) {
        //echo "have file<br>";
        while (!feof($file)) {
            $line = fgets($file);
            //echo $line;
            if (strpos("#" . $line, "{template: 'item', id: 'prospected-from'") == 0) {
                continue;
            }
            //new Listview({template: 'item', id: 'prospected-from', name: LANG.tab_prospectedfrom, tabs: tabsRelated, parent: 'lkljbjkb574', hiddenCols: ['side', 'slot', 'source', 'type', 'level', 'reqlevel'], extraCols: [Listview.extraCols.yield, Listview.extraCols.reqskill, Listview.extraCols.count, Listview.extraCols.percent], sort:['reqskill', '-percent', 'name'], computeDataFunc: Listview.funcBox.initLootTable, data: [{"classs":7,"id":52185,"level":83,"name":"6Elementium Ore","reqlevel":75,"slot":0,"source":[2,3],"sourcemore":[{"p":3}],"subclass":7,count:381739,outof:2102071,yield:52182,reqskill:475},{"classs":7,"id":52185,"level":83,"name":"6Elementium Ore","reqlevel":75,"slot":0,"source":[2,3],"sourcemore":[{"p":3}],"subclass":7,count:381375,outof:2102071,yield:52181,reqskill:475},{"classs":7,"id":52185,"level":83,"name":"6Elementium Ore","reqlevel":75,"slot":0,"source":[2,3],"sourcemore":
function process_itunes_link($post)
{
    preg_match_all("/<a href=\"(http|https):\\/\\/itunes.apple.com(\\S*)\\/app\\S*\\/id(\\d+)(\\?mt\\=\\d+){0,1}[\\s\\S]+<\\/a>/i", $post, $matches);
    if ($matches == null || $matches[0] == null) {
        return $post;
    }
    for ($i = 0; $i < count($matches[0]); $i++) {
        $link = $matches[0][$i];
        $protocol = $matches[1][$i];
        $country = $matches[2][$i];
        if ($country == null || $country == "") {
            $country = "us";
        } else {
            $country = substr($country, 1, 2);
        }
        $appid = $matches[3][$i];
        $appfile = "app/" . $appid . "_" . $country . ".txt";
        if (!file_exists($appfile)) {
            $pageContents = file_get_contents("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $appid . "&country=" . $country);
            if ($pageContents != null && trim($pageContents) != "") {
                // 写缓存
                $fhandler = fopen($appfile, 'a');
                if ($fhandler && fwrite($fhandler, trim($pageContents))) {
                    fclose($fhandler);
                }
                return str_replace($link, process_link($link) . get($pageContents, $country), $post);
            }
            break;
        }
        return str_replace($link, get(file_get_contents($appfile), $country) . process_link($link), $post);
    }
    return $post;
}