| author | ymh <ymh.work@gmail.com> |
| Fri, 09 Dec 2011 02:21:17 +0100 | |
| changeset 59 | ea562f37d7a5 |
| parent 50 | e967654e90cb |
| parent 58 | 87bf6ec8af90 |
| child 60 | 984ba20c150b |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
2 |
||
3 |
namespace IRI\Bundle\WikiTagBundle\Entity; |
|
4 |
||
5 |
use Doctrine\ORM\EntityRepository; |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
6 |
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
|
7 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
| 2 | 8 |
|
9 |
/** |
|
10 |
* TagRepository |
|
11 |
* |
|
12 |
* This class was generated by the Doctrine ORM. Add your own custom |
|
13 |
* repository methods below. |
|
14 |
*/ |
|
15 |
class TagRepository extends EntityRepository |
|
16 |
{ |
|
| 34 | 17 |
public function getTagCloud($max_tags) |
18 |
{ |
|
19 |
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
20 |
$qb->select('t.id', 't.label', 'COUNT( dt.id ) AS nb_docs'); |
|
21 |
$qb->from('WikiTagBundle:Tag','t'); |
|
22 |
$qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); |
|
23 |
$qb->addGroupBy('t.id'); |
|
24 |
$qb->addOrderBy('nb_docs','DESC'); |
|
25 |
$qb->setMaxResults($max_tags); |
|
26 |
||
27 |
$query = $qb->getQuery(); |
|
28 |
return $query->getResult(); |
|
29 |
} |
|
30 |
||
31 |
public function getCompletion($seed) |
|
32 |
{ |
|
33 |
$qb = $this->getEntityManager()->createQueryBuilder(); |
|
34 |
$qb->select('t.label'); |
|
35 |
$qb->from('WikiTagBundle:Tag','t'); |
|
36 |
$qb->where($qb->expr()->orx( |
|
37 |
$qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))), |
|
38 |
$qb->expr()->like('t.label',$qb->expr()->literal("% ".addcslashes(mysql_real_escape_string($seed),"%_")."%")) |
|
39 |
)); |
|
40 |
||
41 |
$query = $qb->getQuery(); |
|
42 |
||
43 |
return $query->getResult(); |
|
44 |
||
45 |
} |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
46 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
47 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
48 |
* Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean) |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
49 |
* @param $tag_label |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
50 |
* @param $doctrine |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
51 |
* @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)) |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
52 |
*/ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
53 |
public function getOrCreateTag($tag_label) |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
54 |
{ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
55 |
$tag_label_normalized = WikiTagUtils::normalizeTag($tag_label); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
56 |
// We get the wikipedia references for the tag_label |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
57 |
// We get or create the tag object |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
58 |
$tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized)); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
59 |
$tag = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
60 |
foreach ($tags as $t) { |
| 58 | 61 |
if($tag==null |
62 |
|| $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized'] |
|
63 |
|| ($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
|
64 |
$tag = $t; |
| 58 | 65 |
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
|
66 |
break; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
67 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
68 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
69 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
70 |
$wp_request_done = false; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
71 |
if($tag==null) { |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
72 |
$tag = new Tag(); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
73 |
$tag->setLabel($tag_label_normalized); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
74 |
$tag->setOriginalLabel($tag_label); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
75 |
$tag->setNormalizedLabel($tag_label_normalized); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
76 |
$created = true; |
|
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 |
else { |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
79 |
$created = false; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
80 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
81 |
|
| 58 | 82 |
// We request Wikipedia if the tag is created or if this is a null result |
83 |
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
|
84 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
85 |
if($wp_request_done==false) { |
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
86 |
try { |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
87 |
$wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized); |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
88 |
} |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
89 |
catch (\Exception $e){ |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
90 |
throw new \Exception($e->getMessage()); |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
91 |
} |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
92 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
93 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
94 |
$tag->setWikipediaInfo($wp_response); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
95 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
96 |
// Save datas. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
97 |
$em = $this->getEntityManager(); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
98 |
$em->persist($tag); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
99 |
$em->flush(); |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
100 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
101 |
$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
|
102 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
103 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
104 |
else if($tag!=null && $tag->getWikipediaPageId()!=null) { |
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
105 |
try { |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
106 |
$wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId()); |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
107 |
} |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
108 |
catch (\Exception $e){ |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
109 |
throw new \Exception($e->getMessage()); |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
42
diff
changeset
|
110 |
} |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
111 |
$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
|
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 |
$wikipedia_revision_id = null; |
|
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 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
117 |
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
|
118 |
} |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
119 |
|
| 59 | 120 |
} |