Skip to content
Snippets Groups Projects
Commit 5c08c2e7 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

Merge branch 'hotline#119099_desindexer_les_auteurs_d_articles_de_bokeh' into 'hotline'

hotline #119099 : add clear unused authors script

See merge request !3868
parents 1bc1633f 4bc673eb
Branches
Tags
2 merge requests!3875Master,!3868hotline #119099 : add clear unused authors script
Pipeline #12523 passed with stage
in 49 minutes and 43 seconds
- ticket #119099 : Administration : Ajout d'un script de suppression des auteurs inutilisés dans les notices
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Clean_UnusedAuthors {
use Trait_MemoryCleaner;
protected
$_last_id = 0,
$_page = 1,
$_deleted = 0;
public function clean() {
while ($records = Class_CodifAuteur::findAllBy(['where' => 'id_auteur > ' . (int)$this->_last_id,
'limit' => '1000']))
$this->_runPage($records);
echo "\nDELETED: ". $this->_deleted ."\n";
return $this;
}
protected function _runPage($records) {
echo "\npage: ". $this->_page ."\n";
$this->_page++;
foreach($records as $record)
$this->_runOne($record);
$this->_last_id = $record->getId();
$this->_cleanMemory();
return $this;
}
protected function _runOne($record) {
if ($this->_isUsedInRecord($record->getFacetCode())) {
echo '.';
return $this;
}
echo 'D';
$record->delete();
$this->_deleted++;
return $this;
}
protected function _isUsedInRecord($facet) {
return 0 < Class_Notice::countBy(['where' => 'match(facettes) against("+' . $facet . '" in boolean mode)']);
}
}
<?php
error_reporting(E_ALL^E_DEPRECATED);
require __DIR__ . '/../console.php';
echo "\n\nWelcome to the iClean Author 2077 Pro tool by @patbator\n\n";
echo "\n\nWill delete unused authors from " . Class_CodifAuteur::count() . " authors in database\n\n";
(new Class_Clean_UnusedAuthors)->clean();
echo "\n\nDONE !!!!\n\n";
<?php
/**
* Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Clean_UnusedAuthorsTest extends ModelTestCase {
protected
$_asimov,
$_tolkien,
$_output;
public function setUp() {
parent::setUp();
$this->_tolkien = $this->fixture('Class_CodifAuteur',
['id' => 1, 'libelle' => 'tolkien']);
$this->_asimov = $this->fixture('Class_CodifAuteur',
['id' => 2, 'libelle' => 'asimov']);
$this
->onLoaderOfModel('Class_CodifAuteur')
->whenCalled('findAllBy')
->with(['where' => 'id_auteur > 0', 'limit' => '1000'])
->answers([$this->_tolkien, $this->_asimov])
->whenCalled('findAllBy')->answers([]);
$this
->onLoaderOfModel('Class_Notice')
->whenCalled('countBy')
->with(['where' => 'match(facettes) against("+A1" in boolean mode)'])
->answers(0)
->whenCalled('countBy')
->with(['where' => 'match(facettes) against("+A2" in boolean mode)'])
->answers(1);
ob_start();
(new Class_Clean_UnusedAuthors)
->setMemoryCleaner(function() {})
->clean();
$this->_output = ob_get_contents();
ob_end_clean();
}
/** @test */
public function shouldDeleteUnusedTolkien() {
$this->assertTrue(Class_CodifAuteur::methodHasBeenCalledWithParams('delete', [$this->_tolkien]));
}
/** @test */
public function shouldNotDeleteUsedAsimov() {
$this->assertFalse(Class_CodifAuteur::methodHasBeenCalledWithParams('delete', [$this->_asimov]));
}
/** @test */
public function outputShouldContainsPageOne() {
$this->assertContains('page: 1', $this->_output);
}
/** @test */
public function outputShouldContainsProgressOneDeletedOneSkipped() {
$this->assertContains('D.', $this->_output);
}
/** @test */
public function outputShouldContainsDeletionsSummary() {
$this->assertContains('DELETED: 1', $this->_output);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment