| author | ymh <ymh.work@gmail.com> |
| Wed, 14 Dec 2011 23:28:57 +0100 | |
| changeset 63 | 774ba82dca59 |
| parent 57 | 186c4121c7b3 |
| child 65 | ba6b8e38d90e |
| 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 |
* |
|
88 |
* @param mixed $id_doc |
|
89 |
* @param string|array $tag_label : the label of the new tag |
|
90 |
*/ |
|
91 |
public function addTags($id_doc, $tag_labels) |
|
92 |
{ |
|
93 |
// We get the DocumentTags |
|
94 |
$em = $this->getDoctrine()->getEntityManager(); |
|
95 |
||
96 |
$need_flush = false; |
|
97 |
||
98 |
||
99 |
if(!is_array($tag_labels)) { |
|
100 |
$tag_labels = array($tag_labels); |
|
101 |
} |
|
102 |
||
103 |
foreach ($tag_labels as $tag_label) { |
|
104 |
||
105 |
$normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); |
|
106 |
||
107 |
$query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label"); |
|
108 |
$query->setParameters(array("id_doc"=>$id_doc, "label"=>$normalized_tag_label)); |
|
109 |
||
110 |
$nb_tags = $query->getSingleScalarResult(); |
|
111 |
||
112 |
// If the label was found, we sent a bad request |
|
113 |
if($nb_tags > 0){ |
|
114 |
throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); |
|
115 |
} |
|
116 |
// returns array($tag, $revision_id, $created) |
|
117 |
try { |
|
118 |
$ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label); |
|
119 |
} |
|
120 |
catch (\Exception $e){ |
|
121 |
throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); |
|
122 |
} |
|
123 |
||
124 |
$tag = $ar[0]; |
|
125 |
$revision_id = $ar[1]; |
|
126 |
$created = $ar[2]; |
|
127 |
||
128 |
$tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId())); |
|
129 |
$nb_tags = count($tags); |
|
130 |
||
131 |
if($created==true || $nb_tags==0){ |
|
132 |
$new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc); |
|
133 |
// The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned. |
|
134 |
$a1 = reset($new_order_ar); |
|
135 |
$new_order = intval(reset($a1)) + 1; |
|
136 |
$new_DT = new DocumentTag(); |
|
137 |
$new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc)); |
|
138 |
$new_DT->setTag($tag); |
|
139 |
$new_DT->setOriginalOrder($new_order); |
|
140 |
$new_DT->setTagOrder($new_order); |
|
141 |
$new_DT->setWikipediaRevisionId($revision_id); |
|
142 |
$em->persist($new_DT); |
|
143 |
$need_flush = true; |
|
144 |
||
145 |
} |
|
146 |
} |
|
147 |
||
148 |
if($need_flush) { |
|
149 |
$em->flush(); |
|
150 |
} |
|
151 |
||
152 |
||
153 |
} |
|
154 |
||
155 |
public function getTagLabels($id_doc) |
|
156 |
{ |
|
157 |
$rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); |
|
158 |
$doc = $rep->findOneByExternalId($id_doc); |
|
159 |
||
160 |
if(is_null($doc)) { |
|
161 |
throw new WikiTagServiceException("Unknown document id"); |
|
162 |
} |
|
163 |
||
164 |
return $rep->getTagsStr($doc); |
|
165 |
||
166 |
} |
|
167 |
||
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
168 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
169 |
} |