0
I have the following code:
<?php
set_time_limit(0);
ignore_user_abort(1);
ini_set('memory_limit', -1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/* CONECTION */
$conn = mysqli_connect('foo','foo','foo','foo') or die(mysqli_error($conn));
mysqli_query($conn, 'SET NAMES UTF8;') or die(mysqli_error($conn));
/* GENERATE SLUG */
function _slug($string) {
return strtolower(preg_replace(['/[[:punct:]]+/', '/[[:space:]\s]+/'], ['', '-'], trim(stripslashes(iconv('UTF-8', 'ASCII//TRANSLIT', $string)))));
}
$url = 'enderecodosistema:porta'; // url sem a barra final
$limit = 50000; // limite total
$i = 1;
$file = 1;
$query = mysqli_query($conn,"select matricula, aluno, data_matricula from escola") or die( mysqli_error($conn) );
$total = mysqli_num_rows($query);
while($asc = mysqli_fetch_assoc($query)) {
$student = _slug($asc['aluno']);
$enrollment = $asc['matricula'];
$date = date("Y-m-d h:i:s", strtotime($asc['data_matricula']));
if($i == 1) {
$sitemap = "sitemap_{$file}.xml";
$dom = new \DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->appendChild($dom->createElement( "urlset" ));
$root->setAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
$root->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
$sxe = simplexml_import_dom( $dom );
}
$child = $sxe->addChild('url');
$child->addChild('loc', $url.'/'.$student.'/'.$enrollment);
$child->addChild('lastmod', date(DATE_ATOM, strtotime($date)));
if($i == $limit) {
$dom->save($sitemap);
$i = 0;
$file++;
}
$i++;
}
if($limit >= $total) {
$dom->save($sitemap);
$i = 0;
}
if($file-1) {
$sitemap = "sitemap.xml";
$dom = new \DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->appendChild($dom->createElement( "sitemapindex" ));
$root->setAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
$root->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
$sxe = simplexml_import_dom( $dom );
for($j = 1; $j <= $file-1; $j++) {
$child = $sxe->addChild('sitemap');
$child->addChild('loc', $url.'/sitemap_'.$j.'.xml');
}
$dom->save($sitemap);
}
?>
It creates a sitemap (with a limit of 50,000 urls per file) of students, pulling from a database.
It turns out that the database is too big and the script takes too long and does not run to the end.
It is possible to adapt the code (with any solution) so that it is "executed in parts", avoiding that it is terminated without completing?
sorry my ignorance, but how would it look in my code? I could not add.
– Marcelo
@Marcelo later I will make the change.
– Andrei Coelho
@Marcelo did the editing
– Andrei Coelho
did not work. The script runs but does not create the files. That limit in the query, what is the reason? The $limit 50k above, is the limit of urls for each file (I believe this is the error, right?) Anyway it didn’t work. I made all the changes, including at the end of the script
– Marcelo
@Marcelo the limit is the limit in the database... It will pull 50 in 50 records.
– Andrei Coelho
@Marcelo as you pull the bank table plates... each second will pull 50 plates
– Andrei Coelho
got it! Refiz again and nothing. No errors, loads too fast (which is weird) but does not create the files .
– Marcelo
@Marcelo just edited the code... there was an error here
if($total == 0)
– Andrei Coelho
@Marcelo was like this :
if(!$total == 0)
– Andrei Coelho
Got it! Now he’s counting the pages, but he only creates 1 sitemap (with 10 results) and the final sitemap.xml (Indice). In the browser, paging keeps changing, but without creating the files.
– Marcelo
Let’s go continue this discussion in chat.
– Andrei Coelho
Someone who gave me -1 could please explain why?
– Andrei Coelho
Just for the record, the code worked perfectly! Thank you Andrei!
– Marcelo