Company Web site is a question and answer Wikipedia website, SEO engineers put forward the requirements of the site based on the problem to generate XML files. Each XML file contains 5,000 Setmap format data. The online site now has about 70w problems, so basically generates 140 XML files. There is also an index file. For example, the name of a file begins with a number. The index file contains the path and name of each XML file.
Why do you want to store 5,000 data per file, because this is a threshold value for MySQL, which can affect online user access or slow down each time you take more. Each file stores 5,000 data, but the MySQL Selsect can not take 5,000 each time, now write 1000 each time. So the logic is a little complicated.
First say implementation:
First Take out 1000 data (you can be flexible to survive, easy to modify later), and then loop to generate XML format files. File_puts_contens write to the file. Then the resulting XML file name, the smallest ID to remove the problem, remove the maximum ID of the problem, take out the problem of the number of the key to write an index query TXT file, the format is probably like this.
0,3146886,3145887,1000
finds that the last side of the bar is 1000, the first select takes out 1000 data, and then writes to the 0.xml file. Writes the extracted XML file name, the minimum ID, the maximum ID, and the number of bars into the index query txt. For the first time, 1000 data was written to 0.xml, and the number of bars generated was 1000. The SELECT statement becomes the second time the query is made. Where ID > Maximum ID taken out (current MySQL is positive order query, if reverse, to less than) limit 1000 then take out 1000, and then modify the index query TXT's minimum ID, maximum ID, the number of generated bars plus to 2000. And so on, and so on, when the generation bar is 5000, another row is written to the index file, and similar to
0,3146886,3145887,5000
1,3148886,3147887,1000
This is written to ease the pressure on the server.
The implementation code is posted below (style is a bit messy):
The code is as follows |
Copy Code |
<?php /* * Sitemap Interface class */
Class Sitemapaction extends action{ private static $baseURL = '; URL address private static $ASKMOBILEURL = ' http://m.xxx.cn/ask/'; Question and answer mobile version address private static $ASKPCURL = "http://www.xxx.cn/ask/"; Answer PC Address private static $ASKZONEPCURL = "http://www.xxx.cn/ask/jingxuan/"; Featured PC Links private static $ASKZONEMOBILEURL = "http://m.xxx.cn/ask/jx/"; Featured Mobile Links Question and Answer Setmaps Public Function Asksetmap () { Header (' Content-type:text/html;charset=utf-8 '); Get a list of issues $maxid = 0; Index file Maximum ID $minid = 0; Index file Minimum ID $psize = 1000; Number of databases per fetch $MAXXML = 5000; Number of XML Write records $where = Array (); Reading index files $index = App_path. ' Setmapxml/index.txt '; Associating setmaps paths $ASKXML = ". /siteditu/ask/ask.xml "; if (!file_exists ($index)) { $FP =fopen ("$index", "w+"); if (!is_writable ($index)) { Die ("File:". $index. " Not written, please check! "); } Fclose ($FP); }else{ Index.txt File Description 0:xml file name (starting from 1), 1: File Max ID, 2: File Minimum ID, 3: File current record number $fp = file ($index); $string = $fp [Count ($fp) -1];//displays the last line $arr = Explode (', ', $string); } Whether the number of index files is less than $maxxml If you are running for the first time if (! $arr [1]) { $bs = 1; $filename = 0; }else{ if ($arr && $arr [3]< $maxXml) { $filename = $arr [0]; $psize = $maxXml-$arr [3]> $psize? $psize:($maxXml-$arr [3]); $bs = 0; }else{ $filename = $arr [0]+1; $bs = 1; } } $maxid = Empty ($arr [1])? 0: $arr [1]; $minid = Empty ($arr [2])? 0: $arr [2]; echo "File name:". $filename. ". XML "." <br/> "; echo "Max ID:". $maxid. " <br/> "; echo "min ID:". $minid. " <br/> "; Echo writes maximum record: ". $maxXml." <br/> "; echo "Database per Read quantity:". $psize. " <br/> "; $list = self:: $QUESTIONOBJ->getquestionsetmap ($where, $maxid, $psize); if (count ($list) <=0) { Echo 1;exit; } $record = $arr [3]+count ($list); Index file Write record number $INDEXARR = Array (' filename ' => $filename, ' Maxid ' => $maxid, ' MiniD ' => $minid, ' maxxml ' => $record); $start = ' <?xml version= ' 1.0 ' encoding= ' UTF-8 '?> '. chr (10); $start. = "<urlset xmlns=\" http://www.sitemaps.org/schemas/sitemap/0.9\ "xmlns:mobile=\" http://www.baidu.com/ Schemas/sitemap-mobile/1/\ ">" Chr (10); $start. = "</urlset>"; foreach ($list as $k => $qinfo) { if ($k ==0) $INDEXARR [' MiniD ']= $qinfo [' id ']; $qinfo [' lastmod '] = substr ($qinfo [' lasttime '],0,10); $qinfo [' mobielurl '] = self:: $askMobileUrl. $qinfo [' id ']. HTML '; Link to mobile version $qinfo [' pcurl '] = self:: $askPcUrl. $qinfo [' id ']. ' -p1.html '; PC version link $xml. = $this->askmapmobileurl ($qinfo); Mobile version $xml. = $this->askmappcurl ($qinfo); PC version } $maxid = End ($list); $INDEXARR [' maxid '] = $MAXID [' id ']; Update index file if ($bs ==0) { Update last line $txt = file ($index); $txt [Count ($txt)-1] = $INDEXARR [filename]. $indexArr [Maxid]. $indexArr [' MiniD ']. ', '. $indexArr [' Maxxml ']. " \ r \ n "; $str = Join ($txt); if (is_writable ($index)) { if (! $handle = fopen ($index, ' W ')) { echo "Cannot open file $index"; exit; Exit } if (fwrite ($handle, $str) = = FALSE) { echo "cannot be written to file $index"; exit; Exit } echo "successfully written to file $index"; Fclose ($handle); } else { echo "File $index not writable"; exit; } Fclose ($index); }elseif ($bs ==1) { Join a new line $fp = fopen ($index, ' a '); $num = count ($list); $string = $INDEXARR [filename]. $indexArr [Maxid]. '. $INDEXARR [' MiniD ']. $num. " \ r \ n "; if (fwrite ($fp, $string) ===false) { Echo failed to append new line ... "; exit; }else{ echo "Append success <br/>"; Update Sitemap Index file $xmlData = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>". Chr (10); $xmlData. = "<sitemapindex>". Chr (10); $xmlData. = "</sitemapindex>"; if (!file_exists ($ASKXML)) File_put_contents ($ASKXML, $xmlData); $fileList = file ($askXml); $fileCount = count ($fileList); $setmapxml = "http://www.xxx.cn/ask/setmapxml/{$filename}.xml";//Normal problem link $txt = $this->setmapindex ($setmapxml); $fileList [$fileCount -1]= $txt.] </sitemapindex> "; $newContent = '; foreach ($fileList as $v) { $newContent. = $v; } if (!file_put_contents ($ASKXML, $newContent)) exit (' cannot write data '); Echo ' has been written to the document '. $ASKXML; } Fclose ($FP); } $filename = App_path. ' setmapxml/'. $filename. XML '; Update to XML file, add end if (!file_exists ($filename)) File_put_contents ($filename, $start); $xmlList = file ($filename); $xmlCount = count ($fileList); $xmlList [$xmlCount -1]= $xml.] </urlset> "; $newXml = '; foreach ($xmlList as $v) { $newXml. = $v; } if (!file_put_contents ($filename, $newXml)) exit ("Write data Error"); Else echo "Write data successfully <br/>"; } Question and answer mobile version xml Private Function Askmapmobileurl ($data) { $xml = '; if (Is_array ($data) &&!empty ($data)) { $xml. = "<url>". Chr (10); if ($data [' ID ']) $xml. = ' <loc> ' $data [' Mobielurl ']. </loc> '. chr (10);//Mobile Version link $xml. = "<mobile:mobile type=\" mobile\ "/>". Chr (10); if ($data [' lastmod ']) $xml. = ' <lastmod> ' $data [' lastmod ']. </lastmod> '. chr (10); $xml. = ' <changefreq>daily</changefreq> '. chr (10); $xml. = ' <priority>0.8</priority> '. chr (10); $xml. = "</url>". Chr (10); return $xml; } } Question and answer PC version xml Private Function Askmappcurl ($data) { $xml = '; if (Is_array ($data) &&!empty ($data)) { $xml. = ' <url> '. chr (10); if ($data [' ID ']) $xml. = ' <loc> ' $data [' Pcurl ']. </loc> ' Chr (a)//PC edition link if ($data [' lastmod ']) $xml. = ' <lastmod> ' $data [' lastmod ']. </lastmod> '. chr (10); $xml. = ' <changefreq>daily</changefreq> '. chr (10); $xml. = ' <priority>0.8</priority> '. chr (10); $xml. = ' </url> '. chr (10); return $xml; } } Setmaps Index File Private Function Setmapindex ($filename) { $xml = '; $xml. = "<sitemap>". Chr (10); $xml. = "<loc>{$filename}</loc>". Chr (10); $xml. = "<lastmod>". Date ("Y-m-d", Time ()). " </lastmod> ". Chr (10); $xml. = "</sitemap>". Chr (10); return $xml; } } ?>
XML index file format <?xml version= "1.0" encoding= "UTF-8"?> <sitemapindex> <sitemap> <loc>http://www.xxx.cn/ask/setmapxml/0.xml</loc> <lastmod>2014-05-12</lastmod> </sitemap> <sitemap> <loc>http://www.xxx.cn/ask/setmapxml/1.xml</loc> <lastmod>2014-05-12</lastmod> </sitemap> </sitemapindex>
XML file format (each file needs to store 5,000, now show 1 examples) <?xml version= "1.0" encoding= "UTF-8"?> <urlset xmlns= "http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile= "http://www.baidu.com/schemas/ Sitemap-mobile/1/"> <url> <loc>http://m.xxx.cn/ask/7460.html</loc> <mobile:mobile type= "mobile"/> <lastmod>2013-01-11</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url> <url> </urlset> |
The SQL code is not pasted, just a select.