From 8916b6918a90cf6ee3b10e5524678a939d72ba0a Mon Sep 17 00:00:00 2001
From: llaffont <laurent.laffont@gmail.com>
Date: Tue, 4 Mar 2014 14:37:38 +0100
Subject: [PATCH] Album indexation: for audio records track titles now indexed
 as fulltext

---
 library/Class/AlbumRessource.php              | 23 ++++++--
 library/Class/File/Info.php                   | 52 +++++++++++++++++++
 library/Class/Indexation/PseudoNotice.php     |  5 +-
 ...echercheControllerAlbumAudioRecordTest.php | 27 +++++++++-
 tests/library/Class/File/InfoTest.php         | 46 ++++++++++++++++
 5 files changed, 146 insertions(+), 7 deletions(-)
 create mode 100644 library/Class/File/Info.php
 create mode 100644 tests/library/Class/File/InfoTest.php

diff --git a/library/Class/AlbumRessource.php b/library/Class/AlbumRessource.php
index 5521bc47ffb..71078d5b162 100644
--- a/library/Class/AlbumRessource.php
+++ b/library/Class/AlbumRessource.php
@@ -816,6 +816,14 @@ class Class_AlbumRessource extends Storm_Model_Abstract {
 	}
 
 
+	/** 
+	 * @return string
+	 */
+	protected function humanizeFilename() {
+		return (new Class_File_Info($this->getFichier()))->humanizeFilename();
+	}
+
+
 	/**
 	 * @return string
 	 */
@@ -1044,12 +1052,19 @@ class Class_AlbumRessource extends Storm_Model_Abstract {
 	}
 
 
+	public function findTitle() {
+		return 
+			$this->getTitre() 
+			? $this->getTitre() 
+			: ($this->getFichier() 
+				 ? $this->humanizeFilename()
+				 : $this->getFolio());
+	}
+
+
 	public function acceptVisitor($visitor) {
 		$view_helper = (new ZendAfi_Controller_Action_Helper_View());
-		if (!$titre = $this->getTitre())
-			$titre = $this->getFolio();
-
-		$visitor->visitRessourceDatas($titre, 
+		$visitor->visitRessourceDatas($this->findTitle(), 
 																	$this->getDuration(), 
 																	$this->getAuthorsNames(),
 																	$view_helper->album_PlayRessourceUrl($this));
diff --git a/library/Class/File/Info.php b/library/Class/File/Info.php
new file mode 100644
index 00000000000..452a13b3ff3
--- /dev/null
+++ b/library/Class/File/Info.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class Class_File_Info {
+	protected $_filepath;
+
+	/** @param filepath string */
+	public function __construct($filepath) {
+		$this->_filepath = (string)$filepath;
+	}
+
+
+
+	public function humanizeFilename() {
+		if (!$file = $this->fileNameWithoutExtension())
+			return '';
+		
+		$parts = array_filter(preg_split('/[^a-zA-Z\s]+/', $file));
+		return implode(' ', array_map('trim',$parts));
+	}
+
+
+	/** 
+	 * @return string
+	 */
+	public function fileNameWithoutExtension() {
+		return $this->_filepath
+			? pathinfo($this->_filepath)['filename']
+			: '';
+	}
+}
+
+
+?>
\ No newline at end of file
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index c82fff3c909..ca827b5b070 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -368,8 +368,9 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 
 	protected function extractTitles() {
 		$titles = [parent::extractTitles()];
-		foreach($this->_model->getRessources() as $ressource) 
-			$titles[] = $ressource->getTitre();
+		foreach($this->_model->getRessources() as $ressource) {
+			$titles[] = $ressource->findTitle();
+		}
 		return implode(' ', $titles);
 	}
 
diff --git a/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php b/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
index 7794d8dfead..69f81d8be00 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerAlbumAudioRecordTest.php
@@ -59,6 +59,9 @@ abstract class RechercheControllerAlbumAudioRecordTestCase extends AbstractContr
 			->addRessource($this->fixture('Class_AlbumRessource',
 																		['id' => 3,
 																		 'fichier' => 'unknown.mp3']))
+			->addRessource($this->fixture('Class_AlbumRessource',
+																		['id' => 4,
+																		 'fichier' => '502_05_the_prophecy.mp3']))
 			->assertSave();
 
 		$album->index();
@@ -136,7 +139,29 @@ class RechercheControllerAlbumAudioRecordViewNoticeTest extends RechercheControl
 
 	/** @test */
 	public function titresFulltextShouldContainsMOONCHILD() {
-		$this->assertContains('MOONCHILD', $this->_notice->getRawAttributes()['titres']);
+		$this->assertContains('MOONCHILD', 
+													explode(' ', $this->_notice->getRawAttributes()['titres']));
+	}
+
+
+	/** @test */
+	public function titresFulltextShouldContainsUNKNOWN() {
+		$this->assertContains('UNKNOWN', 
+													explode(' ', $this->_notice->getRawAttributes()['titres']));
+	}
+
+
+	/** @test */
+	public function titresFulltextShouldContainsPROPHECY() {
+		$this->assertContains('PROPHECY', 
+													explode(' ', $this->_notice->getRawAttributes()['titres']));
+	}
+
+
+	/** @test */
+	public function titresFulltextShouldNotContains502() {
+		$this->assertNotContains('502', 
+														 explode(' ', $this->_notice->getRawAttributes()['titres']));
 	}
 }
 
diff --git a/tests/library/Class/File/InfoTest.php b/tests/library/Class/File/InfoTest.php
new file mode 100644
index 00000000000..eab7f9562f6
--- /dev/null
+++ b/tests/library/Class/File/InfoTest.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class FileInfoTest extends PHPUnit_Framework_TestCase {
+	public function fixtures() {
+		return [ 
+			['one.mp3', 'one' ],
+			['one', 'one'],
+			['.mp3', ''],
+			['', ''],
+			[null, ''],
+			['dark_vador-the*vilain', 'dark vador the vilain'],
+			['666 beast 111', 'beast']
+			];
+	}
+
+
+	/** 
+	 * @test 
+	 * @dataProvider fixtures
+	 */
+	public function humanizeFilenameShouldAnswer($file, $result) {
+		$this->assertEquals($result, (new Class_File_Info($file))->humanizeFilename());
+	}
+		 
+}
+
+?>
\ No newline at end of file
-- 
GitLab