| author | ymh <ymh.work@gmail.com> |
| Fri, 26 Sep 2014 10:04:40 +0200 | |
| changeset 136 | f209fcb0df6c |
| parent 75 | ca2a145e67f3 |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
|
74
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
2 |
/* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
3 |
* This file is part of the WikiTagBundle package. |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
4 |
* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
5 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
6 |
* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
7 |
* For the full copyright and license information, please view the LICENSE |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
8 |
* file that was distributed with this source code. |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
9 |
*/ |
| 2 | 10 |
|
11 |
namespace IRI\Bundle\WikiTagBundle\Entity; |
|
12 |
||
13 |
use Doctrine\ORM\EntityRepository; |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
14 |
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
15 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
| 2 | 16 |
|
17 |
/** |
|
18 |
* TagRepository |
|
19 |
* |
|
20 |
* This class was generated by the Doctrine ORM. Add your own custom |
|
21 |
* repository methods below. |
|
22 |
*/ |
|
23 |
class TagRepository extends EntityRepository |
|
24 |
{ |
|
| 34 | 25 |
public function getTagCloud($max_tags) |
26 |
{ |
|
27 |
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
28 |
$qb->select('t.id', 't.label', 'COUNT( dt.id ) AS nb_docs'); |
|
29 |
$qb->from('WikiTagBundle:Tag','t'); |
|
30 |
$qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); |
|
31 |
$qb->addGroupBy('t.id'); |
|
32 |
$qb->addOrderBy('nb_docs','DESC'); |
|
33 |
$qb->setMaxResults($max_tags); |
|
34 |
||
35 |
$query = $qb->getQuery(); |
|
36 |
return $query->getResult(); |
|
37 |
} |
|
38 |
||
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
39 |
/** |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
40 |
* returns a list of tags label containing the given seed. |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
41 |
* The seed is either at the beggining, the end of the label or at the beggining of a word. |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
42 |
* @param string $seed |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
43 |
*/ |
| 34 | 44 |
public function getCompletion($seed) |
45 |
{ |
|
46 |
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
|
75
ca2a145e67f3
Completion service add the nb of doc + test
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
47 |
$qb->select('t.label', 'COUNT(dt.id) AS nb_docs'); |
| 34 | 48 |
$qb->from('WikiTagBundle:Tag','t'); |
|
75
ca2a145e67f3
Completion service add the nb of doc + test
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
49 |
$qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); |
|
ca2a145e67f3
Completion service add the nb of doc + test
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
50 |
$qb->addGroupBy('t.label'); |
| 34 | 51 |
$qb->where($qb->expr()->orx( |
| 63 | 52 |
$qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")), |
| 34 | 53 |
$qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))), |
54 |
$qb->expr()->like('t.label',$qb->expr()->literal("% ".addcslashes(mysql_real_escape_string($seed),"%_")."%")) |
|
55 |
)); |
|
56 |
||
57 |
$query = $qb->getQuery(); |
|
58 |
||
59 |
return $query->getResult(); |
|
60 |
||
61 |
} |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
62 |
|
| 67 | 63 |
private function findSemantizedTag($entityList, $normalized_label) |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
64 |
{ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
65 |
$tag = null; |
| 67 | 66 |
foreach ($entityList as $entity) { |
67 |
if(!is_null($normalized_label) && (!is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\TagInterface") || $normalized_label !== $entity->getNormalizedLabel())) { |
|
68 |
continue; |
|
69 |
} |
|
70 |
$t = $entity; |
|
| 58 | 71 |
if($tag==null |
72 |
|| $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized'] |
|
73 |
|| ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) { |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
74 |
$tag = $t; |
| 58 | 75 |
if($tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['unsemantized'] && $tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) { |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
76 |
break; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
77 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
78 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
79 |
} |
| 67 | 80 |
|
81 |
return $tag; |
|
82 |
||
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean) |
|
87 |
* @param $tag_label |
|
88 |
* @param $doctrine |
|
89 |
* @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)) |
|
90 |
*/ |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
91 |
public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false, $logger=null) |
| 67 | 92 |
{ |
93 |
$tag_label_normalized = WikiTagUtils::normalizeTag($tag_label); |
|
94 |
// We get the wikipedia references for the tag_label |
|
95 |
// We get or create the tag object |
|
96 |
$tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized)); |
|
97 |
$tag = $this->findSemantizedTag($tags, null); |
|
98 |
$uow = $this->getEntityManager()->getUnitOfWork(); |
|
99 |
if(is_null($tag)) { |
|
100 |
$tag = $this->findSemantizedTag($uow->getScheduledEntityInsertions(), $tag_label_normalized); |
|
101 |
} |
|
102 |
if(is_null($tag)) { |
|
103 |
$tag = $this->findSemantizedTag($uow->getScheduledEntityUpdates(), $tag_label_normalized); |
|
104 |
} |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
105 |
$wp_request_done = false; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
106 |
if($tag==null) { |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
107 |
$tag = new Tag(); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
108 |
$tag->setLabel($tag_label_normalized); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
109 |
$tag->setOriginalLabel($tag_label); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
110 |
$tag->setNormalizedLabel($tag_label_normalized); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
111 |
$created = true; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
112 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
113 |
else { |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
114 |
$created = false; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
115 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
116 |
|
| 58 | 117 |
// We request Wikipedia if the tag is created or if this is a null result |
118 |
if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) { |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
119 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
120 |
if($wp_request_done==false) { |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
121 |
$wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info, $logger); |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
122 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
123 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
124 |
$tag->setWikipediaInfo($wp_response); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
125 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
126 |
// Save datas. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
127 |
$em = $this->getEntityManager(); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
128 |
$em->persist($tag); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
129 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
130 |
$wikipedia_revision_id = $wp_response['revision_id']; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
131 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
132 |
} |
| 60 | 133 |
elseif($tag!=null && $tag->getWikipediaPageId()!=null) { |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
134 |
$wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info, $logger); |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
135 |
$wikipedia_revision_id = $wp_response['revision_id']; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
136 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
137 |
else { |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
138 |
$wikipedia_revision_id = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
139 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
140 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
141 |
return array($tag, $wikipedia_revision_id, $created);//, $wpReponse); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
142 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
143 |
|
| 59 | 144 |
} |