/** * SaveAsXML * */ public function SaveAsXML($filelist, $isRoot = false) { $adjustPath = $isRoot ? ISC_BASE_PATH . '/' : self::$sitemap_folder . '/'; $uQuery = "SELECT * FROM [|PREFIX|]sitemap_setting limit 0,1"; $uResult = $GLOBALS['ISC_CLASS_DB']->Query($uQuery); if ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($uResult)) { $this->xmlConfig['IndexRoot'] = $row['indexroot']; $this->xmlConfig['IndexUrl'] = $row['indexurl']; $this->xmlConfig['NormalRoot'] = $row['normalroot']; $this->xmlConfig['NormalUrl'] = $row['normalurl']; $this->xmlConfig['childNodes'] = explode(',', $row['childnodes']); } else { $this->xmlConfig["MaxmumRecords"] = "50,000"; $this->xmlConfig["IndexRootName"] = "sitemapindex"; $this->xmlConfig["NormalRootName"] = "urlset"; $this->xmlConfig["IndexUrlName"] = "sitemap"; $this->xmlConfig["NormalUrlName"] = "url"; $this->xmlConfig['childNodes'] = array('loc'); } $rootTag = $isRoot ? $this->xmlConfig['IndexRoot'] : $this->xmlConfig['NormalRoot']; $urlTag = $isRoot ? $this->xmlConfig['IndexUrl'] : $this->xmlConfig['NormalUrl']; $childTags = array(); foreach ($filelist as $filename => $datas) { //echo "$filename.xml is generating...<br/>"; if (count($datas) > 100000) { $tmpDatas = array_chunk($datas, 10000); foreach ($tmpDatas as $tmps) { $xmlDoc = new OpXML(); $urlset = $xmlDoc->addRoot($rootTag, array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9')); foreach ($tmps as $data) { $url = $xmlDoc->addChild($urlset, $urlTag); foreach ($this->xmlConfig['childNodes'] as $tag) { if ($tag == 'loc') { $childTags[$tag] = htmlspecialchars($data, ENT_QUOTES, "UTF-8"); } else { if ($tag == 'lastmod') { $childTags[$tag] = date("Y-m-d"); } else { if ($tag == 'changefreq') { $childTags[$tag] = 'weekly'; } else { if ($tag == 'priority') { $childTags[$tag] = '0.5'; } } } } } foreach ($childTags as $childTag => $childVal) { $xmlDoc->addChild($url, $childTag, $childVal); } } $saveResult = $xmlDoc->save("{$adjustPath}{$filename}.xml", "a"); } } else { $xmlDoc = new OpXML(); $urlset = $xmlDoc->addRoot($rootTag, array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9')); foreach ($datas as $data) { $url = $xmlDoc->addChild($urlset, $urlTag); foreach ($this->xmlConfig['childNodes'] as $tag) { if ($tag == 'loc') { $childTags[$tag] = htmlspecialchars($data, ENT_QUOTES, "UTF-8"); //var_dump($childTags[$tag]);echo "<br>"; } else { if ($tag == 'lastmod') { $childTags[$tag] = date("Y-m-d"); } else { if ($tag == 'changefreq') { $childTags[$tag] = 'weekly'; } else { if ($tag == 'priority') { $childTags[$tag] = '0.5'; } } } } } foreach ($childTags as $childTag => $childVal) { $xmlDoc->addChild($url, $childTag, $childVal); } } $saveResult = $xmlDoc->save("{$adjustPath}{$filename}.xml"); } if ($saveResult) { $filepath = htmlspecialchars(self::$website . "/{$adjustPath}{$filename}.xml"); array_push($this->sitemapIndex, self::$website . "/{$adjustPath}{$filename}.xml"); //echo 'Save '.htmlspecialchars($filename).".xml Successful.<br/>Total: $saveResult Bytes.<br/>"; $filepath = str_replace(ISC_BASE_PATH . '/', '', $filepath); //return $filepath; } else { return false; } } }
public function SaveAsXML($filelist, $isRoot = false) { $adjustPath = $isRoot ? '' : self::$sitemap_folder . '/'; $rootTag = $isRoot ? 'sitemapindex' : 'urlset'; $urlTag = $isRoot ? 'sitemap' : 'url'; $locTag = 'loc'; foreach ($filelist as $filename => $datas) { echo "{$filename}.xml is generating...<br/>"; /* $xmlDoc = new DOMDocument("1.0","UTF-8"); $xmlDoc->preserveWhiteSpace = false; $xmlDoc->formatOutput = true; $urlset = $xmlDoc->createElement("urlset"); $urlset = $xmlDoc->appendChild($urlset); */ if (count($datas) > 100000) { $tmpDatas = array_chunk($datas, 10000); foreach ($tmpDatas as $tmps) { $xmlDoc = new OpXML(); $urlset = $xmlDoc->addRoot($rootTag, array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9')); foreach ($tmps as $data) { $url = $xmlDoc->addChild($urlset, $urlTag); $xmlDoc->addChild($url, $locTag, htmlspecialchars($data, ENT_COMPAT, "UTF-8")); //$xmlDoc->addChild($url, 'lastmod', $this->Last_modification); } //unset($xmlDoc); $saveResult = $xmlDoc->save("{$adjustPath}{$filename}.xml", "a"); } } else { $xmlDoc = new OpXML(); $urlset = $xmlDoc->addRoot($rootTag, array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9')); foreach ($datas as $data) { $url = $xmlDoc->addChild($urlset, $urlTag); //$xmlDoc->addChild($url, $locTag, $data); $xmlDoc->addChild($url, $locTag, htmlspecialchars($data, ENT_QUOTES, "UTF-8")); //$xmlDoc->addChild($url, 'lastmod', $this->Last_modification); //$xmlDoc->addChild($url, 'changefreq', $this->Changefreq); //$xmlDoc->addChild($url, 'priority', $this->Priority); /* //create nodes $url = $xmlDoc->createElement("url"); $url = $urlset->appendChild($url); //create attribute to a node //$name_value = $xmlDoc->createAttribute("value"); //$name_value =$url->appendChild($name_value); $loc = $xmlDoc->createElement("loc"); $loc = $url->appendChild($loc); $loc->appendChild($xmlDoc->createTextNode($data)); $lastmod = $xmlDoc->createElement("lastmod"); $lastmod = $url->appendChild($lastmod); $lastmod->appendChild($xmlDoc->createTextNode($this->Last_modification)); $changefreq = $xmlDoc->createElement("changefreq"); $changefreq = $url->appendChild($changefreq); $changefreq->appendChild($xmlDoc->createTextNode($this->Changefreq)); $priority = $xmlDoc->createElement("priority"); $priority = $url->appendChild($priority); $priority->appendChild($xmlDoc->createTextNode($this->Priority)); */ } $saveResult = $xmlDoc->save("{$adjustPath}{$filename}.xml"); //$xmlDoc->save("$adjustPath$filename.xml"); } if ($saveResult) { echo UsedMemory() . "<br/>"; $filepath = htmlspecialchars(self::$website . "/{$adjustPath}{$filename}.xml"); array_push($this->sitemapIndex, self::$website . "/{$adjustPath}{$filename}.xml"); echo 'Save ' . htmlspecialchars($filename) . ".xml Successful.<br/>Total: {$saveResult} Bytes.<br/>"; echo "<a href='{$filepath}'>{$filepath}</a><br/><br/>"; } else { return false; } } return true; }