I
I
Ilya123452020-12-09 14:45:30
PHP
Ilya12345, 2020-12-09 14:45:30

Seo php + mysql Modx?

Good day, at work, there was a task to do mass SEO for the site in sales using a template:
for a page with "such a template" in cms and a category, then make a generated header according to a template from Excel.
In the semi-manual mode, I did the task, 6000+ records, for one of the cities.
Then I was told to do the same with other cities. (If I had been told at once I would have done everything at once).
I did the last task for a week.
Deadline 2 days. The only logical solution is to duplicate the content from the completed pages to those that are not yet ready.
The template for generating title and description is essentially the same.
And then I looked at the number of unfinished records, there are more than 230+ thousand of them.
Here is the code I used:

<?php
ini_set("memory_limit", "4012M");
function sear($text,$word){
$pos = strpos($text, $word);
if ($pos === false) {
    return false;
} else {
    return true;
} 
return false;
}
$sql = "
SELECT id,type,contentType,pagetitle,longtitle,alias,isfolder,template,class_key,content_type,uri FROM modx_site_content WHERE context_key <> 'contname' and context_key <> 'web' and template <> 172 and template <> 136 and template <> 138 and template <> 169 and template <> 148 and template <> 142 and template <> 0 and template <> 145 and template <> 135 and template <> 70 and template <> 38 and template <> 13
";
$sql1 = "SELECT id,type,contentType,pagetitle,longtitle,alias,isfolder,template,class_key,content_type,uri FROM modx_site_content WHERE context_key = 'contname' and template <> 172 and template <> 136 and template <> 138 and template <> 169 and template <> 148 and template <> 142 and template <> 0 and template <> 145 and template <> 135 and template <> 70 and template <> 38 and template <> 13"; //Выборка готовых полей
$statement = $modx->query($sql);
$out = $statement->fetchAll(PDO::FETCH_ASSOC);
$stat = $modx->query($sql1);
$ouo = $stat->fetchAll(PDO::FETCH_ASSOC);
foreach ($ouo as $ou){
    foreach ($out as $data) {
if($data['type'] === $ou['type'] and
$data['contentType'] === $ou['contentType'] and $data['pagetitle'] === $ou['pagetitle'] and $data['longtitle'] === $ou['longtitle'] and $data['alias']=== $ou['alias'] and $data['isfolder']=== $ou['isfolder'] and $data['template'] === $ou['template'] and $data['class_key'] === $ou['class_key'] and $data['content_type'] === $ou['content_type'] and $data['uri'] === $ou['uri']){
    $res1 = $modx->getObject('modResource',intval($ou['id']));
    $t1 = $res1->getTVValue('MetaTitle');
    $d1 = $res1->getTVValue('MetaDescription');
    if(!sear($t1,'&#10148')){continue;} ////Если тайтл вообще есть
    $res2 = $modx->getObject('modResource',intval($data['id']));
    $t2 = $res2->getTVValue('MetaTitle');
    $d2 = $res2->getTVValue('MetaDescription');
    if($t1 === $t2 and $d1 === $d2){continue;} //Если ещё не отредактирован
    if(sear($t2,'&#10148')){continue;} //Если ещё не отредактирован
    $t1 = str_replace('','',$t1);
    $d1 = str_replace('','',$d1);
    $res2->setTVValue('MetaTitle',$t1);
    $res2->setTVValue('MetaDescription',$d1);
    $res2->save();
}
}
}
die;


The code logic is simple:
  • the first cycle goes through 200,000+ records;
  • the second cycle inside the first one goes through the processed 6000+ records;
  • further compares so that the resources are identical from the point of view of the CMS;
  • then it checks that the entries are not identical;
  • then it checks that the received record does not contain the trigger "➤";
  • and edits if all conditions are met;

If you are not familiar with Modx, then this cms stores additional variable fields (in my case, MetaTitle and MetsDescription) in a separate SQL table, which is accessed via api:
$res1 = $modx->getObject('modResource',intval($ou['id']));
 $t1 = $res1->getTVValue('MetaTitle');
  $d1 = $res1->getTVValue('MetaDescription');


It has already processed over 27,253 records. Then he just puts the site and freezes and does not work, apparently due to a memory leak.
I don't have access to the hosting admin panel.
Deep down, I understand that this can be done in pure SQL, but there is not enough knowledge on it.
Please help make this crap work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question