Skip to content
Snippets Groups Projects
Commit b6186422 authored by Alex Arnaud's avatar Alex Arnaud Committed by Patrick Barroca
Browse files

hotline #16604 clean articles urls

parent 81723901
Branches
Tags
3 merge requests!896Master,!795Wip test newopac3,!766Wip test newopac3
......@@ -16,7 +16,7 @@
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
......@@ -56,6 +56,20 @@ class Class_CmsUrlTransformer {
'$1'.BASE_URL.'/$2',
$rel);
}
public static function removeHost($basedir, $content, &$replace_count = null) {
$hosts = ['web.afi-sa.net',
'opac3.pergame.net',
'www.'.$basedir];
$regs = [];
foreach($hosts as $host) {
$regs []= '/(< *(a|img)[^>]*(href|src) *= *["\'])((http:\/\/)?' . $host. ')?\/'.$basedir.'(\/[^"\']*)/i';
}
return preg_replace($regs, '$1$6', $content, -1, $replace_count);
}
}
?>
\ No newline at end of file
<?php
if (!$argv[1]) {
echo "Missing argument site";
exit(1);
}
$basedir = $argv[1];
chdir($basedir);
require('console.php');
$search = 'http://web.afi-sa.net';
$replace = '';
$articles = Class_Article::findAllBy(['where' => 'concat(description,contenu) like "%'.$search.'%"']);
echo BASE_URL."\n";
$occurences = 0;
$articles = Class_Article::findAllBy(['where' => 'concat(description,contenu) like "%'. $basedir .'%"']);
foreach ($articles as $art) {
$art
->setDescription(str_replace($search, $replace, $art->getDescription()))
->setContenu(str_replace($search, $replace, $art->getContenu()))
->setDescription(Class_CmsUrlTransformer::removeHost($basedir, $art->getDescription(), $countDesc))
->setContenu(Class_CmsUrlTransformer::removeHost($basedir, $art->getContenu(), $countCont))
->save();
$occurences += $countDesc + $countCont;
}
$articles = Class_Article::findAllBy(['where' => 'concat(description,contenu) like "%' . $basedir . '%"']);
if (count($articles)) {
$ids = array_map(function($a) {return $a->getId();}, $articles);
echo "check articles: " . implode(', ', $ids) . "\n";
}
echo "$occurences occurence(s) replaced.\n"
?>
\ No newline at end of file
......@@ -16,7 +16,7 @@
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class CmsUrlTransformerTest extends PHPUnit_Framework_TestCase {
......@@ -61,6 +61,55 @@ class CmsUrlTransformerTest extends PHPUnit_Framework_TestCase {
'<img alt="" src="'.BASE_URL.BASE_URL.'/userfiles/image/Histoire/armistice.jpg" />'.
'<br /></a>'));
}
public function urlTransmormDataProvider() {
return [
['Concert <a class="popup" href="/cms/id/12">de maiden</a> à la bib !',
'Concert <a class="popup" href="http://www.genevois-biblio.fr/genevois-biblio.fr/cms/id/12">de maiden</a> à la bib !',
'genevois-biblio.fr'],
['Concert <a class="popup" href="/cms/id/12">de maiden</a> à la bib !',
'Concert <a class="popup" href="http://web.afi-sa.net/genevois-biblio.fr/cms/id/12">de maiden</a> à la bib !',
'genevois-biblio.fr'],
['Concert <a class="popup" href="/cms/id/12">de maiden</a> à la bib !',
'Concert <a class="popup" href="http://opac3.pergame.net/genevois-biblio.fr/cms/id/12">de maiden</a> à la bib !',
'genevois-biblio.fr'],
['Concert <a class="popup" href="/cms/id/12">de maiden</a> à la bib !',
'Concert <a class="popup" href="/genevois-biblio.fr/cms/id/12">de maiden</a> à la bib !',
'genevois-biblio.fr'],
['Concert <a class="popup" href="/cms/id/12">de maiden</a> à la bib !',
'Concert <a class="popup" href="www.genevois-biblio.fr/genevois-biblio.fr/cms/id/12">de maiden</a> à la bib !',
'genevois-biblio.fr'],
['Concert <img class="popup" src="/userfiles/maiden.jpg">de maiden à la bib !',
'Concert <img class="popup" src="www.genevois-biblio.fr/genevois-biblio.fr/userfiles/maiden.jpg">de maiden à la bib !',
'genevois-biblio.fr'],
['Concert <img class="popup" src="/userfiles/maiden.jpg" />de maiden à la bib !',
'Concert <img class="popup" src="www.genevois-biblio.fr/genevois-biblio.fr/userfiles/maiden.jpg" />de maiden à la bib !',
'genevois-biblio.fr'],
['Concert <img class="popup" src="/userfiles/maiden.jpg" ></img>de maiden à la bib !',
'Concert <img class="popup" src="www.genevois-biblio.fr/genevois-biblio.fr/userfiles/maiden.jpg" ></img>de maiden à la bib !',
'genevois-biblio.fr'],
];
}
/**
* @dataProvider urlTransmormDataProvider
* @test
*/
public function removeHostShouldTransformUrlsToRelative($expected, $content, $host) {
$this->assertEquals($expected,
$this->transformer->removeHost($host, $content));
}
}
......
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