Skip to content
Snippets Groups Projects

Hotline#100972 minsoc tess

Merged efalcy requested to merge hotline#100972_minsoc_tess into minsoc_prod
Compare and
1 file
+ 196
0
Preferences
Compare changes
+ 196
0
<?php
error_reporting(E_ERROR | E_PARSE);
require(__DIR__.'/../console.php');
echo "\n\nWelcome\n\n";
$matching = [ 'MOTC1096' => 'MOTC9719',
'MOTC1056' => 'MOTC9240', // inspection du w => travail,
'MOTC2690' => 'MOTC9360', // management -> management public
'MOTC9112' => 'MOTC9360', // lean management => management public
];
$force_matching = [ 'MOTC9859' => 'MOTC9283',
'MOTC0566' =>'MOTC9741',
'MOTC9887' => 'MOTC9778',
'MOTC0641' =>'MOTC9815'];
Class Script_Thesaurus_Cleaner {
use Trait_MemoryCleaner;
protected $without_authorities,
$not_found_matching = [],
$patch;
public function __construct () {
$this->patch = fopen("/tmp/patch_update_thesaurus.php", "w") or die("Unable to open file!");
}
public function closeFile() {
fclose($this->patch);
}
public function findAllThesaurusWithoutAuthorities () {
if ($this->without_authorities)
return $this->without_authorities;
$notfound = [];
$found = [];
return $this->without_authorities = array_filter(Class_CodifThesaurus::findChildrenOfWith('MOTC',10000,'id'),
function($codif) {
return (!Class_Exemplaire::findFirstAuthorityItemByOriginId($codif->getIdOrigine()));
});
}
public function deleteFacetInNotice($codif,$matching) {
return $this->changeFacetInNotice($codif->getIdThesaurus(), $codif->getFacetteIndex(),$matching);
}
public function changeFacetInNotice($facet_id, $facet_index,$matching) {
echo '.';
if (!Class_Notice::findAllBy(['where' => 'match(facettes) against(\'' . $facet_index . '\')']))
return;
$replace_facet="";
if ($replace_facet = $matching[$facet_id])
$replace_facet = 'H'.$replace_facet;
$sql = "UPDATE notices SET facettes = REPLACE(facettes,'".$facet_index."','".$replace_facet."') where facettes like '%".$facet_index."%';";
fwrite($this->patch, $sql."\n");
// sqlExecute($sql);
}
public function findAllWithSameLabel($codif) {
return Class_CodifThesaurus::findAllBy(['where' => 'code="MOTC" and id_thesaurus != "'.$codif->getIdThesaurus().'" and libelle like "'.str_replace('"','\"', $codif->getLibelle()).'"' ]);
}
public function findAllWithSameLabelFacet($codif) {
return Class_CodifThesaurus::findAllBy(['where' => 'code="MOTC" and id_thesaurus != "'.$codif->getIdThesaurus().'" and libelle_facette like "'.str_replace('"','\"', $codif->getLibelleFacette()).'"' ]);
}
public function findAllStartingLikeLabel($codif) {
return Class_CodifThesaurus::findAllBy(['where' => 'code="MOTC" and id_thesaurus != "'.$codif->getIdThesaurus().'" and libelle not like "" and libelle like "'.str_replace('"','\"', $codif->getLibelle()).'%"' ]);
}
public function findAllStartingLikeLabelFacet($codif) {
return Class_CodifThesaurus::findAllBy(['where' => 'code="MOTC" and id_thesaurus != "'.$codif->getIdThesaurus().'" and libelle_facette not like "" and libelle_facette like "'.str_replace('"','\"', $codif->getLibelleFacette()).'%"' ]);
}
public function filterThesaurusWithAuthorities($thesaurus) {
$without_authorities = $this->findAllThesaurusWithoutAuthorities();
return array_filter($thesaurus ,
function($thesauri) use ($without_authorities) {
return !in_array($thesauri, $without_authorities);
});
}
public function matchingThesaurus($codif) {
if (trim($codif->getLibelle()==""))
return;
$without_authorities = $this->findAllThesaurusWithoutAuthorities();
if (!$thesaurus = $this->findAllWithSameLabelFacet($codif)) {
if (!$thesaurus = $this->findAllStartingLikeLabelFacet($codif)) {
$this->not_found_matching[] = $codif->getIdThesaurus();
return;
}
}
if (count($thesaurus)==1 &&
(!in_array($thesaurus[0], $without_authorities)))
return $thesaurus[0];
if (count($thesaurus)==1)
return ;
if (!$found = $this->filterThesaurusWithAuthorities($thesaurus))
return;
if (sizeof($found)>1)
echo "\nCorrespondances multiples pour ". $codif->getIdThesaurus().':'.current($found)->getIdThesaurus().','.end($found)->getIdThesaurus()."\n";
return $found[0];
}
public function replaceDomains($matching) {
echo "\nREPLACE DOMAINS : \n";
foreach (Class_Catalogue::findAllBy(['where' => 'thesaurus like "%MOTC%"']) as $domain) {
$thesaurus = explode(';',$domain->getThesaurus());
foreach ($thesaurus as $thesauri) {
if ($replaced = $this->replace($thesauri,$matching)) {
echo 'replace '.$domain->getThesaurus().' by '.$replaced."\n";
$domain->setThesaurus(str_replace($thesauri, $replaced, $domain->getThesaurus()));
$domain->save();
}
}
}
}
public function replace( $subject, $matching) {
preg_match('/MOTC([0-9]+)/',$subject,$matches);
if (!isset($matches[0]))
return;
if (!$val = $matching[$matches[0]]) {
if (!in_array($matches[0],$matching))
echo "\nImpossible to found matching : ".$matches[0];
return ;
}
return str_replace($matches[0],$val,$subject);
}
public function replaceBookmarks($matching) {
echo "\nREPLACE BOOKMARK : \n";
foreach (Class_User_BookmarkedSearch::findAllBy(['where' => "criterias like '%HMOTC%'"]) as $bookmark) {
$replaced = $this->replace($bookmark->getCriterias(),$matching);
if (strlen($bookmark->getCriterias()) == strlen($replaced))
$bookmark->setCriterias($replaced)->save();
}
}
}
$script = new Script_Thesaurus_Cleaner();
/*$without_authorities =$script->findAllThesaurusWithoutAuthorities();
foreach($without_authorities as $codif) {
if ($corr = $script->matchingThesaurus($codif))
$matching[$codif->getIdThesaurus()] = $corr->getIdThesaurus();
}
*/
//$script->replaceBookmarks($matching);
//$script->replaceDomains($matching);
/*echo 'Update Facettes in notices'."\n";
echo count($without_authorities);exit;
foreach($without_authorities as $codif) {
$script->deleteFacetInNotice($codif, $matching);
$codif->deleteMeAndMyChildren();
}
*/
foreach ($force_matching as $key => $match) {
$script->changeFacetInNotice($key, 'H'.$key,$force_matching);
}
$script->closeFile();
/*$myfile = fopen("/tmp/changes.php", "w") or die("Unable to open file!");
foreach ($matching as $key => $value) {
fwrite($myfile, $key.' =>'.$value.",\n");
}
fclose($myfile);*/
echo "\n\nDONE !!!!\n\n";