--- a/Command/QueryWikipediaCommand.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Command/QueryWikipediaCommand.php Mon Jan 23 00:48:55 2012 +0100
@@ -24,7 +24,7 @@
private function processTag($tag, $em)
{
$tag_label_normalized = WikiTagUtils::normalizeTag($tag->getLabel());
- $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'));
+ $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'), $this->getContainer()->get('logger'));
$tag->setWikipediaInfo($wp_response);
@@ -45,7 +45,6 @@
->addOption("all","a",InputOption::VALUE_NONE, "Search all tags")
->addOption("null","n",InputOption::VALUE_NONE, "Treat only non processed tags")
->addOption("redirection",null,InputOption::VALUE_NONE, "Treat redirections")
- ->addOption("random","r",InputOption::VALUE_NONE, "randomize query on tags")
->addOption("site","S",InputOption::VALUE_OPTIONAL, "the url for the wikipedia site", "http://fr.wikipedia.org/w/api.php")
->addOption("limit","l",InputOption::VALUE_OPTIONAL, "number of tag to process", -1)
->addOption("start",null,InputOption::VALUE_OPTIONAL, "number of tag to ignore", 0);
@@ -56,7 +55,6 @@
$force = $input->getOption('force');
$all = $input->getOption('all');
- $random = $input->getOption('random');
$redirection = $input->getOption('redirection');
$site = $input->getOption('site');
$limit = intval($input->getOption('limit'));
--- a/Controller/WikiTagController.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Controller/WikiTagController.php Mon Jan 23 00:48:55 2012 +0100
@@ -201,7 +201,7 @@
}
// We create the new tag or get the already existing tag. $tag, $revision_id, $created
try {
- $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->container->getParameter('wiki_tag.ignore_wikipedia_error'));
+ $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->container->getParameter('wiki_tag.ignore_wikipedia_error'), $this->container->get('logger'));
}
catch (\Exception $e){
return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
@@ -252,9 +252,7 @@
$doc->setManualOrder(false);
$this->getDoctrine()->getEntityManager()->persist($doc);
- $search_service = $this->get('wiki_tag.search');
-
- $search_service->reorderTagsForDocument($doc);
+ $doc_service = $this->get('wiki_tag.document')->reorderTags($doc);
$this->getDoctrine()->getEntityManager()->flush();
@@ -563,7 +561,7 @@
if($tag!=null && $label!=null){
// We get the Wikipedia informations for the sent label
$tag_label_normalized = WikiTagUtils::normalizeTag($label);
- $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $this->container->getParameter('wiki_tag.ignore_wikipedia_error'));
+ $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $this->container->getParameter('wiki_tag.ignore_wikipedia_error'), $this->container->get('logger'));
$tag->setWikipediaInfo($wp_response);
// Save datas.
$em = $this->getDoctrine()->getEntityManager();
--- a/DependencyInjection/Configuration.php Mon Dec 26 22:53:50 2011 +0100
+++ b/DependencyInjection/Configuration.php Mon Jan 23 00:48:55 2012 +0100
@@ -38,7 +38,7 @@
->performNoDeepMerging()
->children()
->scalarNode('type')->defaultValue('text')->end()
- ->scalarNode('length')
+ ->scalarNode('length')->defaultValue(1024)
->beforeNormalization()
->ifString()
->then(function($v) { return intval($v); })
--- a/Entity/DocumentRepository.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Entity/DocumentRepository.php Mon Jan 23 00:48:55 2012 +0100
@@ -58,7 +58,7 @@
$this->reflection_doc_class = new ReflectionClass(get_class($object));
}
- $set_method = NULL;
+ $set_method = null;
if($this->reflection_doc_class->hasMethod($method_name))
{
$set_method = $this->reflection_doc_class->getMethod($method_name);
@@ -154,7 +154,7 @@
return $this->getClassMetadata()->columnNames[$field_name];
}
- $res = $field_name;
+ $res = null;
if(isset($this->getClassMetadata()->associationMappings[$field_name]))
{
$association_mapping = $this->getClassMetadata()->associationMappings[$field_name];
@@ -168,6 +168,9 @@
}
}
+ if(is_null($res)) {
+ throw new \Exception("WikiTag.DocumentRepository: Unknown field $field_name");
+ }
return $res;
}
@@ -271,18 +274,29 @@
* Search wikitag documents using the index.
*
* @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight)
- * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"", "value"=>value)
+ * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value)
+ * @param array $fields : array : a list of field name to include in the result
* @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]]
*/
- function search(array $values, array $conditions=NULL)
+ function search(array $values, array $conditions=null, array $fields=null)
{
$em = $this->getEntityManager();
+ if(is_null($fields)) {
+ $fieldnamelist = array();
+ }
+ else {
+ $fieldnamelist = $fields;
+ }
+
$rsm = new ResultSetMapping();
$rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d");
$rsm->addFieldResult("d", "id", "id");
+ foreach($fieldnamelist as $fieldname) {
+ $rsm->addFieldResult("d", $fieldname, $fieldname);
+ }
$rsm->addScalarResult("score", "score");
- $rsm->addMetaResult("d", "external_id", "externalId");
+ $rsm->addMetaResult("d", "external_id", "external_id");
$score = array();
@@ -315,6 +329,9 @@
$i = 0;
foreach ($conditions as $field => $conddef)
{
+ if(!$field) {
+ continue;
+ }
$i++;
$col = $this->getColumnName($field);
if(is_array($conddef) && isset($conddef['operator']))
@@ -332,6 +349,10 @@
$operator = "=";
$values = $conddef;
}
+
+ if(!in_array( strtolower($operator), array("=","!=","<".">","<=".">=","like","ilike"))) {
+ throw new Exception('DocumentRepository.search : operator must be in "=","!=","<".">","<=".">=","like","ilike"');
+ }
if($operator === "IN")
{
@@ -356,8 +377,11 @@
}
}
-
- $query = $em->createNativeQuery("SELECT d.id, d.external_id, $score_def AS score FROM wikitag_document d WHERE $score_def > 0 $conditions_str ORDER BY score DESC", $rsm);
+ $fieldnamequery = "d.id, d.external_id";
+ if(count($fieldnamelist) > 0) {
+ $fieldnamequery .= ", d.".join(", d.", $fieldnamelist);
+ }
+ $query = $em->createNativeQuery("SELECT $fieldnamequery, $score_def AS score FROM wikitag_document d WHERE $score_def > 0 $conditions_str ORDER BY score DESC", $rsm);
$query->setParameters($parameters);
--- a/Entity/TagRepository.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Entity/TagRepository.php Mon Jan 23 00:48:55 2012 +0100
@@ -28,6 +28,11 @@
return $query->getResult();
}
+ /**
+ * returns a list of tags label containing the given seed.
+ * The seed is either at the beggining, the end of the label or at the beggining of a word.
+ * @param string $seed
+ */
public function getCompletion($seed)
{
$qb = $this->getEntityManager()->createQueryBuilder();
@@ -73,7 +78,7 @@
* @param $doctrine
* @return multitype:boolean Ambigous <NULL, \IRI\Bundle\WikiTagBundle\Entity\Tag> Ambigous <NULL, unknown, mixed, string> (array(\IRI\Bundle\WikiTagBundle\Model\TagInterface, revision_id=int, created:Boolean))
*/
- public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false)
+ public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false, $logger=null)
{
$tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
// We get the wikipedia references for the tag_label
@@ -103,7 +108,7 @@
if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
if($wp_request_done==false) {
- $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info);
+ $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info, $logger);
}
$tag->setWikipediaInfo($wp_response);
@@ -116,7 +121,7 @@
}
elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
- $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info);
+ $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info, $logger);
$wikipedia_revision_id = $wp_response['revision_id'];
}
else {
--- a/Listener/DocumentListener.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Listener/DocumentListener.php Mon Jan 23 00:48:55 2012 +0100
@@ -141,7 +141,7 @@
{
$doc = $entity->getDocument();
$state = $args->getEntityManager()->getUnitOfWork()->getEntityState($doc);
- if($state != UnitOfWork::STATE_REMOVED) {
+ if($state !== UnitOfWork::STATE_REMOVED) {
$this->updateTagsStr($doc);
}
}
@@ -150,14 +150,16 @@
private function handleEvent(LifecycleEventArgs $args)
{
-
$logger = $this->container->get('logger');
$entity = $args->getEntity();
if (is_a($entity, "IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface"))
{
$doc = $entity->getDocument();
- $this->updateTagsStr($doc);
+ $state = $args->getEntityManager()->getUnitOfWork()->getEntityState($doc);
+ if($state !== UnitOfWork::STATE_REMOVED) {
+ $this->updateTagsStr($doc);
+ }
}
elseif (is_a($entity, "IRI\Bundle\WikiTagBundle\Model\TagInterface"))
{
@@ -166,7 +168,11 @@
{
foreach($documents as $doctag)
{
- $this->updateTagsStr($doctag->getDocument());
+ $doc = $doctag->getDocument();
+ $state = $args->getEntityManager()->getUnitOfWork()->getEntityState($doc);
+ if($state !== UnitOfWork::STATE_REMOVED) {
+ $this->updateTagsStr($doc);
+ }
}
}
}
--- a/README.md Mon Dec 26 22:53:50 2011 +0100
+++ b/README.md Mon Jan 23 00:48:55 2012 +0100
@@ -45,7 +45,7 @@
\_\_DIR\_\_.'/cache/task/wikitag',
));
-* Since WikiTagBundle builds its own document class from the host app's document class, you need to tell in config.yml what _text_ fields will be used in this class.
+* Since WikiTagBundle builds its own document class from the host app's document class, you need to tell in config.yml what is the host app's document class and what _text_ fields will be used in this class.
These fields are used for searching and to calculate the tag's weight. Example :
wiki_tag:
@@ -60,6 +60,13 @@
description:
type: text
weight: 0.5
+The 'document_id_column' option is used to indicate the primary key column used by the host app's document class. We are currently limited to non composite primary keys.
+A field definition has the following format:
+<field name>:
+ type: <string or text> : field type. default : text
+ length: <int> : the length of the field. ignored if field type is text
+ accessor: <field name or method name> : the field name in the host app's document class, or the name of the method used to access the field value. If not found it will try ta add 'get' in frint of the name. Default : the field name
+ weight: <float> : the weight used for this field to calculate the score of each tag. default : 1.0
* Add the WikiTag routes to your routing.yml :
@@ -70,8 +77,8 @@
* Run the commands :
php app/console wikitag:generate-document-class (no need to explain)
- php app/console wikitag:schema:update (also runs php app/console doctrine:schema:update)
- php app/console wikitag:sync-doc (fills the database with the datas from the host document class to the wikitag document class)
+ php app/console wikitag:schema:update (also replace and runs php app/console doctrine:schema:update)
+ php app/console wikitag:sync-doc (fills the database with the datas from the host document class to the wikitag document class. this command is needed only if the database was not empty)
* Your database is ready. You can now include the table of tags in a template. Do not forget css and javascript (and php app/console assets:install). Example :
@@ -179,7 +186,62 @@
{% render "WikiTagBundle:WikiTag:addJavascriptForContextSearch" with {'context_name': 'some_divs'} %}
{% endblock %}
+## More configuration 3 : ignore wikipedia errors
+This option allows to ignore wikipedia errors instead of raising an exception. The error is logged with an ERROR level and the tag is added/updated but not semantized.
+
+ wiki_tag:
+ ...
+ ignore_wikipedia_error: true
+
+## Services
+
+### Document Service : wiki_tag.document - IRI\Bundle\WikiTagBundle\Services\DocumentService
+The document service gathers methods to manage tags on a given document. all these methods must be called after the host app's document(s) object has been created and flushed.
+
+* addTags : add a tag or a list pf tags to a document. For each new tag, wikipedia will be queried.
+
+* copyTags : copy the tags list from one document to the other.
+
+* reorderTags : reorder a document tags.
+
+* getTagsLabels : get the list of tag labels from one document.
+### Search service : wiki_tag.search - IRI\Bundle\WikiTagBundle\Services\SearchService
+The search service allows searching documents
+
+* getTagCloud : returns a weighted list of tag labels. The weight of the label is the number of documents tagged with this label. The list is sorted by descending weights.
+
+* search : search documents.
+
+## Commands
+This bundle provides a number of commans that helps in the tags management.
+
+### wikitag:schema:create
+Command to create the database schema. Equivalent to the doctrine:schema:create command.
+It fully replaces the doctrine:schema:create and *must* be run instead of the doctrine:schema:create command.
+### wikitag:schema:update
+Command to update the database schema. Equivalent to the doctrine:schema:update command.
+It fully replaces the doctrine:schema:update and *must* be run instead of the doctrine:schema:update command.
+
+
+### wikitag:create-fulltext-indexes
+Generate the sql to create the full text index on the database. This command is not destined to be directly called.
+
+### wikitag:generate-document-class
+Generate the WikiTagBundle document class. This command should not be directly called.
+
+### wikitag:purge-tags
+Removes tags associated to no documents.
+
+### wikitag:query-wikipedia
+Query wikipedia for informations on tags.
+
+### wikitag:reorder-tags
+Automatically reorder documents tags. For each documents treated, each tag of the document is scored according to the fields definition in the bundle configuration. The sorting of the tags is done document by document according to these scores.
+
+### wikitag:sync-doc
+Synchronize the wikiTag Bundle documents with the host bundle. This synchronization is made according to the fields defined in the bundle configuration.
+
--- a/Services/DocumentService.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Services/DocumentService.php Mon Jan 23 00:48:55 2012 +0100
@@ -147,7 +147,7 @@
}
// returns array($tag, $revision_id, $created)
try {
- $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'));
+ $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'), $this->getContainer()->get('logger'));
}
catch (\Exception $e){
throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed");
@@ -196,5 +196,50 @@
return $rep->getTagsStr($doc);
}
+
+ /**
+ * Service to reorder the tags using their notes in the index search.
+ * This service is configured in the configuration file by affecting the weight in each field definition.
+ *
+ * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
+ */
+ public function reorderTags($document)
+ {
+ $doctrine = $this->getContainer()->get('doctrine');
+
+ $tags_score = array();
+
+ foreach($document->getTags() as $tag)
+ {
+ $label = $tag->getTag()->getLabel();
+
+ $score_res = $this->search($label, array("id"=>$document->getId()));
+
+ if(count($score_res)>0)
+ {
+ $score = floatval($score_res[0]['score']);
+ }
+ else
+ {
+ $score = 0.0;
+ }
+ $tags_score[] = array($score,$tag);
+ }
+ // sort tags based on score
+ $i=1;
+ usort($tags_score, function($a, $b) {
+ return $a[0]<$b[0]?1:-1;
+ });
+
+ foreach($tags_score as $item)
+ {
+ $tag = $item[1];
+ $tag->setTagOrder($i++);
+ $tag->setIndexNote($item[0]);
+ $doctrine->getEntityManager()->persist($tag);
+ }
+
+ }
+
}
\ No newline at end of file
--- a/Services/SearchService.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Services/SearchService.php Mon Jan 23 00:48:55 2012 +0100
@@ -34,6 +34,10 @@
private $doctrine;
+ /**
+ * doctrine object getter
+ * @return object
+ */
public function getDoctrine()
{
if(is_null($this->doctrine))
@@ -42,87 +46,108 @@
}
return $this->doctrine;
}
+
+
/**
*
* Enter description here ...
- * @param string $value
- * @param array $conditions
- * @param array $fields
+ * @param mixed $value : either a string or the list of fields to search into ; format : ['<field_name>' => ['weight'=><relative weight of the field>, 'value' => value]]
+ * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operator is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value)
+ * @param array $fields : The field of field names to export, one of the list specified in the configuration file
+ * @return array []
*/
- public function search($value, array $conditions, array $fields=null)
+ public function search($value, array $conditions = null, array $fields = null)
{
+ if(is_null($value) || (is_string($value) && strlen($value) == 0) || count($value)==0 ) {
+ return null;
+ }
+
+ $fielddeflist = $this->getContainer()->getParameter("wiki_tag.fields");
+ $fieldquery = array();
+
+ // build filed query
+ if(is_string($value)) {
+ foreach ($fielddeflist as $fieldname => $fielddef) {
+ if(!is_null($fielddef) && isset($fielddef['weight'])) {
+ $weight = $fielddef['weight'];
+ }
+ else
+ {
+ $weight = 1.0;
+ }
+ $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
+ }
+ }
+ else {
+ foreach ($value as $fieldname => $fielddef) {
+ if(is_null($fielddef) || !isset($fielddef['value']) || strlen($fielddef['value']) == 0) {
+ continue;
+ }
+ $valuefield = $fielddef['value'];
+ if(isset($fielddef['weight'])) {
+ $weight = $fielddef['weight'];
+ }
+ else
+ {
+ $weight = 1.0;
+ }
+
+ $fieldquery[] = array("columns"=>$fieldname, "value"=>$valuefield, "weight"=>$weight);
+ }
+ }
+
+ // buildf
if(is_null($fields))
{
- $fields = $this->getContainer()->getParameter("wiki_tag.fields");
+ $fieldnamelist = array();
+ foreach ($fielddeflist as $fieldname => $fielddef) {
+ $fieldnamelist[] = $fieldname;
+ }
}
- $doctrine = $this->getContainer()->get('doctrine');
- $res = $doctrine->getRepository('WikiTagBundle:Document');
- $fieldquery = array();
- foreach ($fields as $fieldname => $fielddef) {
- if(isset($fielddef['weight']))
- {
- $weight = $fielddef['weight'];
- }
- else
- {
- $weight = 1.0;
- }
- $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
+ else {
+ $fieldnamelist = $fields;
}
- $score_res = $res->search($fieldquery, $conditions);
+ $doctrine = $this->getContainer()->get('doctrine');
+ $rep = $doctrine->getRepository('WikiTagBundle:Document');
+
+ $score_res = $rep->search($fieldquery, $conditions, $fieldnamelist);
+
+ $res = array();
+ foreach($score_res as $single_res) {
+ $res_entry = array();
+ $res_entry['host_doc_id'] = $single_res[0]->getExternalId()->getId();
+ $res_entry['wikitag_doc_id'] = $single_res[0]->getId();
+ foreach($fieldnamelist as $fieldname) {
+ $accessor_name = "get".ucfirst($fieldname);
+ $res_entry[$fieldname] = $single_res[0]->{$accessor_name}();
+ }
+ $res_entry['_score'] = $single_res['score'];
+ $res_entry['wikitag_doc'] = $single_res[0];
+ $res[] = $res_entry;
+ }
- return $score_res;
+ return $res;
}
+
/**
- * Service to reorder the tags using their notes in the index search
- * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
+ * get a list of tags label sprted by their number of documents tagged.
+ * @param int $max_tags the max number of tags to return
+ * @return array of array of tags,
*/
- public function reorderTagsForDocument($document)
- {
- $doctrine = $this->getContainer()->get('doctrine');
-
- $tags_score = array();
-
- foreach($document->getTags() as $tag)
- {
- $label = $tag->getTag()->getLabel();
-
- $score_res = $this->search($label, array("id"=>$document->getId()));
-
- if(count($score_res)>0)
- {
- $score = floatval($score_res[0]['score']);
- }
- else
- {
- $score = 0.0;
- }
- $tags_score[] = array($score,$tag);
- }
- // sort tags based on score
- $i=1;
- usort($tags_score, function($a, $b) {
- return $a[0]<$b[0]?1:-1;
- });
-
- foreach($tags_score as $item)
- {
- $tag = $item[1];
- $tag->setTagOrder($i++);
- $tag->setIndexNote($item[0]);
- $doctrine->getEntityManager()->persist($tag);
- }
-
- }
-
public function getTagCloud($max_tags)
{
$rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
return $rep->getTagCloud($max_tags);
}
+ /**
+ * List the tag label containing the seed given as an argument.
+ * The seed is either at the beggining, the end of the label or at the beggining of a word.
+ * @param unknown_type $seed
+ * @return : an array containing the possible tag labels
+ */
public function completion($seed)
{
$rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
@@ -133,7 +158,6 @@
}
return $res;
-
}
--- a/Tests/Services/SearchServiceTest.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Tests/Services/SearchServiceTest.php Mon Jan 23 00:48:55 2012 +0100
@@ -16,14 +16,36 @@
{
protected $_container;
+ protected $_application;
+ protected $_kernel;
+
public function __construct()
{
- $kernel = new \AppKernel("test", true);
- $kernel->boot();
- $this->_container = $kernel->getContainer();
+ $this->_kernel = new \AppKernel("test", true);
+ $this->_kernel->boot();
+ $this->_container = $this->_kernel->getContainer();
parent::__construct();
}
+
+ protected function runConsole($command, Array $options = array())
+ {
+
+ $options["-e"] = "test";
+ $options["-q"] = null;
+ $options = array_merge($options, array('command' => $command));
+ return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
+ }
+
+ public function setUp()
+ {
+ $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);
+ $this->_application->setAutoExit(false);
+ $this->runConsole("doctrine:schema:drop", array("--force" => true));
+ $this->runConsole("wikitag:schema:create");
+ $this->runConsole("cache:warmup");
+ $this->runConsole("doctrine:fixtures:load"/*, array("--fixtures" => __DIR__ . "/../../../../../../../src/Company/BaseBundle/DataFixtures")*/);
+ }
protected function get($service)
{
@@ -33,27 +55,122 @@
public function testTagCloud()
{
-
$search_service = $this->get("wiki_tag.search");
- $result = $search_service->getTagCloud(30);
+ $result = $search_service->getTagCloud(4);
$this->assertNotNull($result, "tag cloud should not be null");
- $this->assertLessThanOrEqual(30, count($result));
+ $this->assertLessThanOrEqual(4, count($result));
+ $this->assertEquals(4, count($result));
+
+ // 0->tag4,6 ; 0->tag3,5 ; 0->tag2,4 ; 0->tag1,2
+ $expected = array('Tag1'=>2,'Tag2'=>4, 'Tag3'=>5, 'Tag4'=>6);
+ for ($i = 0; $i < 4; $i++) {
+ $tagname = "Tag".(4-$i);
+ $this->assertEquals($tagname,$result[$i]['label']);
+ $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
+ }
+
}
+
+ public function testTagCloudLimit()
+ {
+ $search_service = $this->get("wiki_tag.search");
+
+ $result = $search_service->getTagCloud(2);
+
+ $this->assertNotNull($result, "tag cloud should not be null");
+ $this->assertEquals(2, count($result));
+
+ $expected = array('Tag3'=>5, 'Tag4'=>6);
+ for ($i = 0; $i < 2; $i++) {
+ $tagname = "Tag".(4-$i);
+ $this->assertEquals($tagname,$result[$i]['label']);
+ $this->assertEquals($expected[$tagname],$result[$i]['nb_docs']);
+ }
+
+ }
public function testCompletion()
{
-
$search_service = $this->get("wiki_tag.search");
$result = $search_service->completion("tag");
- $this->assertNotNull($result, "tag cloud should not be null");
+ $this->assertNotNull($result, "completion should not be null");
$this->assertEquals(4, count($result));
+
+ foreach ($result as $tagname) {
+ $this->assertEquals(0,strpos($tagname,"tag"));
+ }
}
+ public function testSearch() {
+
+ $search_service = $this->get("wiki_tag.search");
+ $result = $search_service->search("Suspendisse");
+
+ $this->assertNotNull($result, "search result should not be null");
+ $this->assertEquals(1, count($result));
+ foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
+ $this->assertArrayHasKey($key,$result[0]);
+ $this->assertNotNull($result[0][$key]);
+ }
+ $this->assertTrue($result[0]['_score']>0);
+ $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
+ $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
+ $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
+
+ }
+
+ public function testSearchCondition() {
+
+ $search_service = $this->get("wiki_tag.search");
+ $result = $search_service->search("ullamcorper");
+
+ $this->assertNotNull($result, "search result should not be null");
+ $this->assertEquals(2, count($result));
+
+ $result = $search_service->search("ullamcorper", array("tagsStr"=>array("operator"=>"like","value"=>"barfoo")));
+
+ foreach (array("_score","host_doc_id", "wikitag_doc_id", "wikitag_doc", "title", "description", "categories") as $key) {
+ $this->assertArrayHasKey($key,$result[0]);
+ $this->assertNotNull($result[0][$key]);
+ }
+ $this->assertTrue($result[0]['_score']>0);
+ $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['title']);
+ $this->assertTrue(is_a($result[0]['wikitag_doc'], "\IRI\Bundle\WikiTagBundle\Model\DocumentInterface"));
+ $this->assertEquals("Lorem ipsum dolor sit amet", $result[0]['wikitag_doc']->getTitle());
+ $this->assertEquals("Lorem ipsum ullamcorper", $result[0]['description']);
+
+ }
+
+ public function testSearchWeight() {
+ $search_service = $this->get("wiki_tag.search");
+
+ $result = $search_service->search(array('title'=>array('weight'=>0.9, 'value'=>'caveat'), 'description'=>array('weight'=>0.1, 'value'=>'caveat')));
+
+ $this->assertNotNull($result, "search result should not be null");
+ $this->assertEquals(2, count($result));
+ $this->assertTrue($result[0]['_score']>$result[1]['_score']);
+
+ $this->assertEquals('caveat', $result[0]['title']);
+ $this->assertEquals('emptor', $result[0]['description']);
+
+ $result = $search_service->search(array('title'=>array('weight'=>0.1, 'value'=>'caveat'), 'description'=>array('weight'=>0.9, 'value'=>'caveat')));
+
+ $this->assertNotNull($result, "search result should not be null");
+ $this->assertEquals(2, count($result));
+ $this->assertTrue($result[0]['_score']>$result[1]['_score']);
+
+ $this->assertEquals('caveat', $result[0]['description']);
+ $this->assertEquals('emptor', $result[0]['title']);
+
+
+ }
+
+
}
--- a/Utils/WikiTagUtils.php Mon Dec 26 22:53:50 2011 +0100
+++ b/Utils/WikiTagUtils.php Mon Jan 23 00:48:55 2012 +0100
@@ -47,10 +47,9 @@
* @param bigint $page_id
* @return array
*/
- public static function getWikipediaInfo($tag_label_normalized, $page_id=null, $ignore_wikipedia_error=false)
+ public static function getWikipediaInfo($tag_label_normalized, $page_id=null, $ignore_wikipedia_error=false, $logger = null)
{
- // get ignore_wikipedia_error parameter
-
+
$params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json');
if($tag_label_normalized!=null){
$params['titles'] = urlencode($tag_label_normalized);
@@ -67,6 +66,9 @@
}
catch(\Exception $e) {
if($ignore_wikipedia_error) {
+ if(!is_null($logger)) {
+ $logger->err("Error when querying wikipedia : ".$e->getMessage()." with trace : ".$e->getTraceAsString());
+ }
return WikiTagUtils::returnNullResult(null);
}
else {
@@ -116,6 +118,9 @@
}
catch(\Exception $e) {
if($ignore_wikipedia_error) {
+ if(!is_null($logger)) {
+ $logger->error("Error when querying wikipedia for redirection : ".$e->getMessage()." with trace : ".$e->getTraceAsString());
+ }
return WikiTagUtils::returnNullResult(null);
}
else {