| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Dec 2011 17:50:04 +0100 | |
| changeset 65 | ba6b8e38d90e |
| parent 63 | 774ba82dca59 |
| child 67 | 989d9e117586 |
| permissions | -rw-r--r-- |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* This file is part of the WikiTagBundle package. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
* For the full copyright and license information, please view the LICENSE |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
* file that was distributed with this source code. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
namespace IRI\Bundle\WikiTagBundle\Services; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
| 63 | 12 |
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag; |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
use Symfony\Component\DependencyInjection\ContainerAware; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
| 63 | 15 |
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
class DocumentService extends ContainerAware |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
* Get the container associated with this service. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
* @return ContainerInterface |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
public function getContainer() |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
return $this->container; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
* Public constructor with container as parameter for contruct injection. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
* @param ContainerInterface $container |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
public function __construct(ContainerInterface $container) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
$this->setContainer($container); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
private $doctrine; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
public function getDoctrine() |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
if(is_null($this->doctrine)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
$this->doctrine = $this->getContainer()->get('doctrine'); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
return $this->doctrine; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
* Copy the list of tags of one document to another. |
| 63 | 51 |
* The ids are the ids of the "host" document. |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
* @param mixed $id_doc_src the source document id |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
* @param mixed $id_doc_tgt the target document id |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
public function copyTags($id_doc_src, $id_doc_tgt) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
$doctrine = $this->getDoctrine(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
$em = $doctrine->getEntityManager(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
$doc_rep = $em->getRepository("WikiTagBundle:Document"); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
$src_doc = $doc_rep->findOneByExternalId($id_doc_src); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
if(is_null($src_doc)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
throw new Exception("cloneTags: no source doc"); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
$tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
if(is_null($tgt_doc)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
throw new Exception("cloneTags: no target doc"); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
$doc_rep->copyTags($src_doc, $tgt_doc); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
$em->flush(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
|
| 63 | 83 |
/** |
84 |
* |
|
85 |
* Add a new tag to a "host" document. |
|
86 |
* If the label already exists, an exception is raised. |
|
87 |
* |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
88 |
* @param mixed $doc |
| 63 | 89 |
* @param string|array $tag_label : the label of the new tag |
90 |
*/ |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
91 |
public function addTags($doc, $tag_labels) |
| 63 | 92 |
{ |
93 |
// We get the DocumentTags |
|
94 |
$em = $this->getDoctrine()->getEntityManager(); |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
95 |
|
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
96 |
$class = $this->getContainer()->getParameter("wiki_tag.document_class"); |
| 63 | 97 |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
98 |
if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
99 |
if(is_a($doc, $class)) { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
100 |
$doc_id = $doc->getId(); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
101 |
} |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
102 |
else { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
103 |
$doc_id = $doc; |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
104 |
} |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
105 |
$doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
106 |
} |
| 63 | 107 |
|
108 |
||
109 |
if(!is_array($tag_labels)) { |
|
110 |
$tag_labels = array($tag_labels); |
|
111 |
} |
|
112 |
||
113 |
foreach ($tag_labels as $tag_label) { |
|
114 |
||
115 |
$normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); |
|
116 |
||
117 |
$query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label"); |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
118 |
$query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label)); |
| 63 | 119 |
|
120 |
$nb_tags = $query->getSingleScalarResult(); |
|
121 |
||
122 |
// If the label was found, we sent a bad request |
|
123 |
if($nb_tags > 0){ |
|
124 |
throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); |
|
125 |
} |
|
126 |
// returns array($tag, $revision_id, $created) |
|
127 |
try { |
|
128 |
$ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label); |
|
129 |
} |
|
130 |
catch (\Exception $e){ |
|
131 |
throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); |
|
132 |
} |
|
133 |
||
134 |
$tag = $ar[0]; |
|
135 |
$revision_id = $ar[1]; |
|
136 |
$created = $ar[2]; |
|
137 |
||
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
138 |
if(!$created) { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
139 |
$query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag"); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
140 |
$query->setParameters(array("id_doc"=>$doc, "tag"=>$tag)); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
141 |
$nb_tags = $query->getSingleScalarResult(); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
142 |
} |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
143 |
|
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
144 |
if($created || $nb_tags==0){ |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
145 |
$new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc); |
| 63 | 146 |
// The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned. |
147 |
$a1 = reset($new_order_ar); |
|
148 |
$new_order = intval(reset($a1)) + 1; |
|
149 |
$new_DT = new DocumentTag(); |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
150 |
$new_DT->setDocument($doc); |
| 63 | 151 |
$new_DT->setTag($tag); |
152 |
$new_DT->setOriginalOrder($new_order); |
|
153 |
$new_DT->setTagOrder($new_order); |
|
154 |
$new_DT->setWikipediaRevisionId($revision_id); |
|
155 |
$em->persist($new_DT); |
|
156 |
} |
|
157 |
} |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
158 |
|
| 63 | 159 |
} |
160 |
||
161 |
public function getTagLabels($id_doc) |
|
162 |
{ |
|
163 |
$rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); |
|
164 |
$doc = $rep->findOneByExternalId($id_doc); |
|
165 |
||
166 |
if(is_null($doc)) { |
|
167 |
throw new WikiTagServiceException("Unknown document id"); |
|
168 |
} |
|
169 |
||
170 |
return $rep->getTagsStr($doc); |
|
171 |
||
172 |
} |
|
173 |
||
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
174 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
175 |
} |