Просмотр исходного кода

Logiciel: Evolution #838: Base de connaissance

Bastien Sevajol 11 лет назад
Родитель
Сommit
b4b326f05f

+ 162 - 14
src/Muzich/CoreBundle/Command/MineTagsDataCommand.php Просмотреть файл

@@ -4,31 +4,54 @@ namespace Muzich\CoreBundle\Command;
4 4
 
5 5
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6 6
 use Symfony\Component\Console\Input\InputInterface;
7
+use Symfony\Component\Console\Input\InputOption;
7 8
 use Symfony\Component\Console\Output\OutputInterface;
8 9
 use Muzich\CoreBundle\Mining\Tag\TagMiner;
10
+use Doctrine\ORM\QueryBuilder;
11
+use Muzich\CoreBundle\Entity\User;
9 12
 
10 13
 class MineTagsDataCommand extends ContainerAwareCommand
11 14
 {
12 15
   
13
-  protected $sitemap_content;
14
-  protected $sitemap_urls;
15
-  protected $router;
16
-  protected $locales;
17
-  protected $siteurl_prefix;
16
+  const MINE_DIFFUSION = 'diffusion';
17
+  const MINE_FAVORITE = 'favorite';
18
+  const MINE_PLAYLIST = 'playlist';
19
+  const MINE_TAGS = 'tags';
20
+  const MINE_TOPS = 'tops';
21
+  
22
+  static $mine_types = array(
23
+    self::MINE_DIFFUSION,
24
+    self::MINE_FAVORITE,
25
+    self::MINE_PLAYLIST,
26
+    self::MINE_TAGS,
27
+    self::MINE_TOPS
28
+  );
29
+  
18 30
   protected $em;
31
+  protected $tag_miner;
32
+  protected $input;
33
+  protected $output;
34
+  protected $progress;
35
+  protected $users_mineds = array();
19 36
   
20 37
   protected function configure()
21 38
   {
22 39
     $this
23 40
       ->setName('mining:tags')
24 41
       ->setDescription('Mine tags data')
42
+      ->addOption('all', null, InputOption::VALUE_NONE, 'Selectionne tous les utilisateurs')
43
+      ->addOption(self::MINE_DIFFUSION, null, InputOption::VALUE_NONE, 'Ne traite que les diffusions')
44
+      ->addOption(self::MINE_FAVORITE, null, InputOption::VALUE_NONE, 'Ne traite que les favoris')
45
+      ->addOption(self::MINE_PLAYLIST, null, InputOption::VALUE_NONE, 'Ne traite que les playlists')
46
+      ->addOption(self::MINE_TAGS, null, InputOption::VALUE_NONE, 'Ne traite que les tags')
47
+      ->addOption(self::MINE_TOPS, null, InputOption::VALUE_NONE, 'Ne traite que les top tags')
25 48
     ;
26 49
   }
27 50
   
28 51
   /** @return TagMiner */
29 52
   protected function getTagMiner()
30 53
   {
31
-    return $this->getContainer()->get('muzich.mining.tag.miner');
54
+    return $this->tag_miner;
32 55
   }
33 56
   
34 57
   /** @return \Doctrine\ORM\EntityManager */
@@ -37,22 +60,147 @@ class MineTagsDataCommand extends ContainerAwareCommand
37 60
     return $this->em;
38 61
   }
39 62
 
40
-  protected function init()
63
+  protected function init(InputInterface $input, OutputInterface $output)
41 64
   {
42 65
     $this->em = $this->getContainer()->get('doctrine')->getEntityManager();
66
+    $this->tag_miner = $this->getContainer()->get('muzich.mining.tag.miner');
67
+    
68
+    // Experimental
69
+    $this->tag_miner->setLogger($this);
70
+    
71
+    $this->progress = $this->getHelperSet()->get('progress');
72
+    $this->input = $input;
73
+    $this->output = $output;
43 74
   }
44 75
   
45 76
   protected function execute(InputInterface $input, OutputInterface $output)
46 77
   {
47
-    $this->init();
78
+    $this->init($input, $output);
79
+    
80
+    if ($this->canIMineThat(self::MINE_DIFFUSION))
81
+      $this->mineDiffusions();
82
+    
83
+    if ($this->canIMineThat(self::MINE_FAVORITE))
84
+      $this->mineFavorites();
85
+    
86
+    if ($this->canIMineThat(self::MINE_PLAYLIST))
87
+      $this->minePlaylists();
88
+    
89
+    if ($this->canIMineThat(self::MINE_TAGS))
90
+      $this->mineTags();
91
+    
92
+    if ($this->canIMineThat(self::MINE_TOPS))
93
+      $this->mineTopTags();
94
+    
95
+    $this->output->writeln('<info>Terminé !</info>');
96
+  }
97
+  
98
+  /** @return QueryBuilder */
99
+  protected function getUserQueryBuilder()
100
+  {
101
+    $user_query_builder = $this->getEntityManager()->createQueryBuilder()
102
+      ->from('MuzichCoreBundle:User', 'user');
103
+    $this->tag_miner->adaptQueryBuilderSelectorsForUser($user_query_builder, 'user');
104
+    
105
+    return $user_query_builder;
106
+  }
107
+  
108
+  protected function mineDiffusions()
109
+  {
110
+    $users = $this->getUsersToProceed(User::DATA_DIFF_UPDATED);
111
+    $this->output->writeln('<info>Diffusions: '.count($users).' utilisateurs</info>');
112
+    $this->progress->start($this->output, count($users));
113
+    $this->getTagMiner()->mineDiffusionTagsForUsers($users);
114
+    $this->addUsersToUsersMineds($users);
115
+  }
116
+  
117
+  protected function mineFavorites()
118
+  {
119
+    $users = $this->getUsersToProceed(User::DATA_FAV_UPDATED);
120
+    $this->output->writeln('<info>Favoris: '.count($users).' utilisateurs</info>');
121
+    $this->progress->start($this->output, count($users));
122
+    $this->getTagMiner()->mineFavoriteTagsForUsers($users);
123
+    $this->addUsersToUsersMineds($users);
124
+  }
125
+  
126
+  protected function minePlaylists()
127
+  {
128
+    $users = $this->getUsersToProceed(User::DATA_PLAY_UPDATED);
129
+    $this->output->writeln('<info>Playlists: '.count($users).' utilisateurs</info>');
130
+    $this->progress->start($this->output, count($users));
131
+    $this->getTagMiner()->minePlaylistTagsForUsers($users);
132
+    $this->addUsersToUsersMineds($users);
133
+  }
134
+  
135
+  protected function mineTags()
136
+  {
137
+    $this->output->writeln('<info>Tags: '.count($this->users_mineds).' utilisateurs</info>');
138
+    $this->progress->start($this->output, count($this->users_mineds));
139
+    $this->getTagMiner()->mineTagsForUsers($this->users_mineds);
140
+  }
141
+  
142
+  protected function mineTopTags()
143
+  {
144
+    $this->output->writeln('<info>TopTags: '.count($this->users_mineds).' utilisateurs</info>');
145
+    $this->progress->start($this->output, count($this->users_mineds));
146
+    $this->getTagMiner()->mineTopTagsForUsers($this->users_mineds);
147
+  }
148
+  
149
+  protected function getUsersToProceed($condition)
150
+  {
151
+    $users_query_builder = $this->getUserQueryBuilder();
152
+    if (!$this->input->getOption('all'))
153
+    {
154
+      $users_query_builder->andWhere('user.datas NOT LIKE :condition OR user.datas IS NULL');
155
+      $users_query_builder->setParameter('condition', '%"'.$condition.'":false%');
156
+    }
48 157
     
49
-    $tag_miner = $this->getTagMiner();
50
-    $user_query_builder = $this->getEntityManager()->createQueryBuilder()->from('MuzichCoreBundle:User', 'user');
51
-    $tag_miner->adaptQueryBuilderSelectorsForUser($user_query_builder, 'user');
52
-    // $user_query_builder->where en fonction des inputes etc
53
-    $this->getTagMiner()->mineTagsForUsers($user_query_builder->getQuery()->getResult());
158
+    return $users_query_builder->getQuery()->getResult();
159
+  }
160
+  
161
+  protected function canIMineThat($mine_type_asked)
162
+  {
163
+    $mine_type_specified = false;
164
+    foreach (self::$mine_types as $mine_type)
165
+    {
166
+      if ($this->input->getOption($mine_type))
167
+        $mine_type_specified = true;
168
+    }
54 169
     
55
-    $output->writeln('<info>Terminé !</info>');
170
+    if (!$mine_type_specified)
171
+      return true;
172
+    
173
+    if ($this->input->getOption($mine_type_asked))
174
+      return true;
175
+    
176
+    return false;
177
+  }
178
+  
179
+  public function logUserProceed()
180
+  {
181
+    $this->progress->advance();
182
+  }
183
+  
184
+  public function logSavingInDatabase()
185
+  {
186
+    $this->output->writeln('');
187
+    $this->output->writeln('<info>Saving in database ...</info>');
188
+  }
189
+  
190
+  protected function addUsersToUsersMineds($users_to_add)
191
+  {
192
+    foreach ($users_to_add as $user_to_add)
193
+    {
194
+      $found = false;
195
+      foreach ($this->users_mineds as $user_mined)
196
+      {
197
+        if ($user_mined->getId() == $user_to_add->getId())
198
+          $found = true;
199
+      }
200
+      
201
+      if (!$found)
202
+        $this->users_mineds[] = $user_to_add;
203
+    }
56 204
   }
57 205
   
58 206
 }

+ 0 - 135
src/Muzich/CoreBundle/Command/TagOrderCommand.php Просмотреть файл

@@ -1,135 +0,0 @@
1
-<?php
2
-
3
-namespace Muzich\CoreBundle\Command;
4
-
5
-use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
-use Symfony\Component\Console\Input\InputArgument;
7
-use Symfony\Component\Console\Input\InputInterface;
8
-use Symfony\Component\Console\Input\InputOption;
9
-use Symfony\Component\Console\Output\OutputInterface;
10
-
11
-use Muzich\CoreBundle\Entity\User;
12
-use Muzich\CoreBundle\Searcher\ElementSearcher;
13
-use Muzich\CoreBundle\lib\Tag as TagLib;
14
-
15
-class TagOrderCommand extends ContainerAwareCommand
16
-{
17
-  protected function configure()
18
-  {
19
-    $this
20
-      ->setName('tagengine:order')
21
-      ->setDescription('Ordonne la liste des tags sur les pages utilisateurs')
22
-      ->addOption('force', null, InputOption::VALUE_NONE, 'Forcer les opérations sur tous les utilisateurs')
23
-    ;
24
-  }
25
-  
26
-  protected function execute(InputInterface $input, OutputInterface $output)
27
-  {
28
-    $doctrine = $this->getContainer()->get('doctrine');
29
-    $em = $doctrine->getEntityManager();
30
-    $tag_lib = new TagLib();
31
-
32
-    $output->writeln('#');
33
-    $output->writeln('## Script d\'ordonance des listes de tags sur les pages'
34
-      .' utilisateurs ##');
35
-    $output->writeln('#');
36
-
37
-    // Premier point on récupère les utilisateurs qui ont mis a jour leurs 
38
-    // favoris
39
-    $output->writeln('<info>Gestion de l\'ordre des tags sur les pages de '
40
-      .'partages favoris</info>');
41
- 
42
-    if ($input->getOption('force'))
43
-    {
44
-      $users = $em->createQuery("SELECT u FROM MuzichCoreBundle:User u")
45
-        ->getResult()
46
-      ;
47
-    }
48
-    else
49
-    {
50
-      $users = $em->createQuery(
51
-          "SELECT u FROM MuzichCoreBundle:User u"
52
-          . " WHERE u.datas LIKE :favupd"
53
-        )->setParameter('favupd', '%"'.User::DATA_FAV_UPDATED.'":true%')
54
-        ->getResult()
55
-      ;
56
-    }
57
-    
58
-    if (count($users))
59
-    {
60
-      $output->writeln('<info>Traitement de '.count($users).' utilisateurs</info>');
61
-      foreach ($users as $user)
62
-      {
63
-        // On récupère ses éléments favoris
64
-        $search_object = new ElementSearcher();
65
-        $search_object->init(array(
66
-          'user_id'  => $user->getId(),
67
-          'favorite' => true
68
-        ));
69
-        $elements_favorite = $search_object->getElements($doctrine, $user->getId());
70
-        
71
-        // On récupère la nouvelle liste de tags ordonnés
72
-        $tags_ordered = $tag_lib->getOrderedTagsWithElements($elements_favorite);
73
-        // On enregistre ça en base
74
-        $user->setData(User::DATA_TAGS_ORDER_PAGE_FAV, $tags_ordered);
75
-        $user->setData(User::DATA_FAV_UPDATED, false);
76
-        $em->persist($user);
77
-        $em->flush();
78
-      }
79
-    
80
-    }
81
-    else
82
-    {
83
-      $output->writeln('<info>Aucun utilisateur a traiter</info>');
84
-    }
85
-    
86
-    // Deuxième point on récupère les utilisateurs qui ont mis a jour leurs 
87
-    // diffusion
88
-    $output->writeln('<info>Gestion de l\'ordre des tags sur les diffusions</info>');
89
- 
90
-    if ($input->getOption('force'))
91
-    {
92
-      $users = $em->createQuery("SELECT u FROM MuzichCoreBundle:User u")
93
-        ->getResult()
94
-      ;
95
-    }
96
-    else
97
-    {
98
-      $users = $em->createQuery(
99
-          "SELECT u FROM MuzichCoreBundle:User u"
100
-          . " WHERE u.datas LIKE :favupd"
101
-        )->setParameter('favupd', '%"'.User::DATA_DIFF_UPDATED.'":true%')
102
-        ->getResult()
103
-      ;
104
-    }
105
-    
106
-    if (count($users))
107
-    {
108
-      $output->writeln('<info>Traitement de '.count($users).' utilisateurs</info>');
109
-      foreach ($users as $user)
110
-      {
111
-        // On récupère ses éléments diffusés
112
-        $search_object = new ElementSearcher();
113
-        $search_object->init(array(
114
-          'user_id'  => $user->getId()
115
-        ));
116
-        $elements_diffused = $search_object->getElements($doctrine, $user->getId());
117
-        
118
-        // On récupère la nouvelle liste de tags ordonnés
119
-        $tags_ordered = $tag_lib->getOrderedTagsWithElements($elements_diffused);
120
-        // On enregistre ça en base
121
-        $user->setData(User::DATA_TAGS_ORDER_DIFF, $tags_ordered);
122
-        $user->setData(User::DATA_DIFF_UPDATED, false);
123
-        $em->persist($user);
124
-        $em->flush();
125
-      }
126
-    
127
-    }
128
-    else
129
-    {
130
-      $output->writeln('<info>Aucun utilisateur a traiter</info>');
131
-    }
132
-    
133
-    $output->writeln('<info>Terminé !</info>');
134
-  }
135
-}

+ 0 - 4
src/Muzich/CoreBundle/Controller/ElementController.php Просмотреть файл

@@ -218,10 +218,6 @@ class ElementController extends Controller
218 218
       $em->persist($element->getOwner());
219 219
       $em->remove($element);
220 220
       
221
-      /**
222
-      * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
223
-      * Docrine le voit si on faire une requete directe.
224
-      */
225 221
       $user = $this->getUser();
226 222
       
227 223
       // On signale que cet user a modifié ses diffusions

+ 12 - 3
src/Muzich/CoreBundle/Document/UserTags.php Просмотреть файл

@@ -11,22 +11,25 @@ class UserTags extends EntityTags
11 11
 {
12 12
 
13 13
   /**
14
-   * @MongoDB\Hash
14
+   * @MongoDB\Collection
15 15
    */
16 16
   protected $element_diffusion_tags;
17 17
 
18 18
   /**
19
-   * @MongoDB\Hash
19
+   * @MongoDB\Collection
20 20
    */
21 21
   protected $element_favorite_tags;
22 22
 
23 23
   /**
24
-   * @MongoDB\Hash
24
+   * @MongoDB\Collection
25 25
    */
26 26
   protected $element_playlist_tags;
27 27
   
28 28
   public function getElementDiffusionTags()
29 29
   {
30
+    if (!$this->element_diffusion_tags)
31
+      return array();
32
+      
30 33
     return $this->element_diffusion_tags;
31 34
   }
32 35
   
@@ -37,6 +40,9 @@ class UserTags extends EntityTags
37 40
   
38 41
   public function getElementFavoriteTags()
39 42
   {
43
+    if (!$this->element_favorite_tags)
44
+      return array();
45
+      
40 46
     return $this->element_favorite_tags;
41 47
   }
42 48
   
@@ -47,6 +53,9 @@ class UserTags extends EntityTags
47 53
   
48 54
   public function getElementPlaylistTags()
49 55
   {
56
+    if (!$this->element_playlist_tags)
57
+      return array();
58
+      
50 59
     return $this->element_playlist_tags;
51 60
   }
52 61
   

+ 18 - 23
src/Muzich/CoreBundle/Entity/User.php Просмотреть файл

@@ -25,29 +25,9 @@ use Muzich\CoreBundle\Managers\UserPrivacy as PrivacyManager;
25 25
 class User extends BaseUser
26 26
 {
27 27
   
28
-  /**
29
-   * Data ordre des tags de sa page favoris
30
-   * @var string 
31
-   */
32
-  const DATA_TAGS_ORDER_PAGE_FAV = "data_tags_order_page_fav";
33
-  
34
-  /**
35
-   * Data ordre des tags de ses diffusions
36
-   * @var string 
37
-   */
38
-  const DATA_TAGS_ORDER_DIFF     = "data_tags_order_diff";
39
-  
40
-  /**
41
-   * Data, les favoris ont ils été modifiés
42
-   * @var string 
43
-   */
44
-  const DATA_FAV_UPDATED         = "data_fav_updated";
45
-  
46
-  /**
47
-   * Data, les favoris ont ils été modifiés
48
-   * @var string 
49
-   */
50
-  const DATA_DIFF_UPDATED        = "data_diff_updated";
28
+  const DATA_FAV_UPDATED = "data_fav_updated";
29
+  const DATA_DIFF_UPDATED = "data_diff_updated";
30
+  const DATA_PLAY_UPDATED = "data_play_updated";
51 31
   
52 32
   const HELP_TOUR_HOME = "home";
53 33
   
@@ -1312,4 +1292,19 @@ class User extends BaseUser
1312 1292
     return $privacy_manager->set(PrivacyManager::CONF_FAVORITES_PUBLIC, $public);
1313 1293
   }
1314 1294
   
1295
+  public function setDataFavoriteNoMoreUpdated()
1296
+  {
1297
+    $this->setData(self::DATA_FAV_UPDATED, false);
1298
+  }
1299
+  
1300
+  public function setDataDiffusionsNoMoreUpdated()
1301
+  {
1302
+    $this->setData(self::DATA_DIFF_UPDATED, false);
1303
+  }
1304
+  
1305
+  public function setDataPlaylistNoMoreUpdated()
1306
+  {
1307
+    $this->setData(self::DATA_PLAY_UPDATED, false);
1308
+  }
1309
+  
1315 1310
 }

+ 39 - 0
src/Muzich/CoreBundle/Mining/Tag/Tag.php Просмотреть файл

@@ -0,0 +1,39 @@
1
+<?php
2
+namespace Muzich\CoreBundle\Mining\Tag;
3
+
4
+use Doctrine\ORM\EntityManager;
5
+use Doctrine\Bundle\MongoDBBundle\ManagerRegistry as MongoManagerRegistry;
6
+use Doctrine\ODM\MongoDB\DocumentRepository;
7
+use Doctrine\ODM\MongoDB\DocumentManager;
8
+
9
+class Tag
10
+{
11
+  
12
+  protected $doctrine_entity_manager;
13
+  protected $mongo_manager_registry;
14
+  
15
+  public function __construct(EntityManager $doctrine_entity_manager, MongoManagerRegistry $mongo_manager_registry)
16
+  {
17
+    $this->doctrine_entity_manager = $doctrine_entity_manager;
18
+    $this->mongo_manager_registry = $mongo_manager_registry;
19
+  }
20
+  
21
+  /** @return EntityManager */
22
+  protected function getDoctrineEntityManager()
23
+  {
24
+    return $this->doctrine_entity_manager;
25
+  }
26
+  
27
+  /** @return DocumentRepository */
28
+  protected function getMongoRepository($repository)
29
+  {
30
+    return $this->mongo_manager_registry->getRepository($repository);
31
+  }
32
+  
33
+  /** @return DocumentManager */
34
+  protected function getMongoManager()
35
+  {
36
+    return $this->mongo_manager_registry->getManager();
37
+  }
38
+  
39
+}

+ 39 - 0
src/Muzich/CoreBundle/Mining/Tag/TagData.php Просмотреть файл

@@ -0,0 +1,39 @@
1
+<?php
2
+namespace Muzich\CoreBundle\Mining\Tag;
3
+
4
+use Muzich\CoreBundle\Mining\Tag\Tag as Base;
5
+use Muzich\CoreBundle\Entity\User;
6
+
7
+class TagData extends Base
8
+{
9
+  
10
+  public function getTagOrderForFavorites(User $user)
11
+  {
12
+    $user_tags = $this->getUserTagsTags($user, 'element_favorite_tags');
13
+    
14
+   if (count($tags_ordereds = $user_tags->getElementFavoriteTags()))
15
+     return $tags_ordereds;
16
+   
17
+   return array();
18
+  }
19
+  
20
+  public function getTagOrderForDiffusions(User $user)
21
+  {
22
+    $user_tags = $this->getUserTagsTags($user, 'element_diffusion_tags');
23
+    
24
+   if (count($tags_ordereds = $user_tags->getElementDiffusionTags()))
25
+     return $tags_ordereds;
26
+   
27
+   return array();
28
+  }
29
+  
30
+  protected function getUserTagsTags(User $user, $field)
31
+  {
32
+    return $this->getMongoManager()->createQueryBuilder('MuzichCoreBundle:UserTags')
33
+      ->select($field)
34
+      ->field('ref')->equals((int)$user->getId())
35
+      ->getQuery()->getSingleResult()
36
+    ;
37
+  }
38
+  
39
+}

+ 70 - 48
src/Muzich/CoreBundle/Mining/Tag/TagMiner.php Просмотреть файл

@@ -3,8 +3,6 @@ namespace Muzich\CoreBundle\Mining\Tag;
3 3
 
4 4
 use Doctrine\ORM\EntityManager;
5 5
 use Doctrine\Bundle\MongoDBBundle\ManagerRegistry as MongoManagerRegistry;
6
-use Doctrine\ODM\MongoDB\DocumentRepository;
7
-use Doctrine\ODM\MongoDB\DocumentManager;
8 6
 use Muzich\CoreBundle\Document\EntityTags;
9 7
 use Muzich\CoreBundle\Document\UserTags;
10 8
 use Muzich\CoreBundle\Document\GroupTags;
@@ -15,40 +13,22 @@ use Muzich\CoreBundle\lib\TagScorer;
15 13
 use Muzich\CoreBundle\Entity\User;
16 14
 use Muzich\CoreBundle\Managers\PlaylistManager;
17 15
 
18
-class TagMiner
16
+use Muzich\CoreBundle\Mining\Tag\Tag as Base;
17
+
18
+class TagMiner extends Base
19 19
 {
20 20
   
21
-  protected $doctrine_entity_manager;
22
-  protected $mongo_manager_registry;
23 21
   protected $tag_scorer;
24 22
   protected $tag_orderer;
23
+  protected $logger_parent;
25 24
   
26 25
   public function __construct(EntityManager $doctrine_entity_manager, MongoManagerRegistry $mongo_manager_registry)
27 26
   {
28
-    $this->doctrine_entity_manager = $doctrine_entity_manager;
29
-    $this->mongo_manager_registry = $mongo_manager_registry;
27
+    parent::__construct($doctrine_entity_manager, $mongo_manager_registry);
30 28
     $this->tag_scorer = new TagScorer();
31 29
     $this->tag_orderer = new TagOrderer();
32 30
   }
33 31
   
34
-  /** @return EntityManager */
35
-  protected function getDoctrineEntityManager()
36
-  {
37
-    return $this->doctrine_entity_manager;
38
-  }
39
-  
40
-  /** @return DocumentRepository */
41
-  protected function getMongoRepository($repository)
42
-  {
43
-    return $this->mongo_manager_registry->getRepository($repository);
44
-  }
45
-  
46
-  /** @return DocumentManager */
47
-  protected function getMongoManager()
48
-  {
49
-    return $this->mongo_manager_registry->getManager();
50
-  }
51
-  
52 32
   /** @return TagScorer */
53 33
   protected function getTagsScorer()
54 34
   {
@@ -61,6 +41,21 @@ class TagMiner
61 41
     return $this->tag_orderer;
62 42
   }
63 43
   
44
+  // Experimental
45
+  public function setLogger($parent)
46
+  {
47
+    $this->logger_parent = $parent;
48
+  }
49
+  
50
+  // Experimental
51
+  protected function log($action_name)
52
+  {
53
+    $action_name_complete = 'log'.ucfirst($action_name);
54
+    
55
+    if ($this->logger_parent)
56
+      $this->logger_parent->$action_name_complete();
57
+  }
58
+  
64 59
   /** 
65 60
    * @param QueryBuilder $query_builder
66 61
    * @param string $user_alias
@@ -81,22 +76,52 @@ class TagMiner
81 76
   /**
82 77
    * @param array $users
83 78
    */
84
-  public function mineTagsForUsers($users)
79
+  public function mineForUsers($users, $mining_action, $user_action = null)
85 80
   {
86
-    foreach ($users as $user)
81
+    if (count($users))
87 82
     {
88
-      $user_tags = $this->getEntityTagsDocument($user->getId(), EntityTags::TYPE_USER);
89
-      
90
-      $this->scoreUserDiffusionsTags($user_tags, $user);
91
-      $this->scoreUserFavoritesTags($user_tags, $user);
92
-      $this->scoreUserPlaylistsTags($user_tags, $user);
93
-      $this->scoreUserTags($user_tags, $user);
94
-      $this->determineTagsTops($user_tags);
83
+      foreach ($users as $user)
84
+      {
85
+        $user_tags = $this->getEntityTagsDocument($user->getId(), EntityTags::TYPE_USER);
86
+
87
+        $this->log('userProceed');
88
+        $this->$mining_action($user_tags, $user);
89
+        if ($user_action)
90
+          $user->$user_action();
91
+
92
+        $this->getMongoManager()->persist($user_tags);
93
+        $this->getDoctrineEntityManager()->persist($user);
94
+      }
95 95
       
96
-      $this->getMongoManager()->persist($user_tags);
96
+      $this->log('savingInDatabase');
97
+      $this->getMongoManager()->flush();
98
+      $this->getDoctrineEntityManager()->flush();
97 99
     }
98
-    
99
-    $this->getMongoManager()->flush();
100
+  }
101
+  
102
+  public function mineDiffusionTagsForUsers($users)
103
+  {
104
+    $this->mineForUsers($users, 'orderUserDiffusionsTags', 'setDataDiffusionsNoMoreUpdated');
105
+  }
106
+  
107
+  public function mineFavoriteTagsForUsers($users)
108
+  {
109
+    $this->mineForUsers($users, 'orderUserFavoritesTags', 'setDataFavoriteNoMoreUpdated');
110
+  }
111
+  
112
+  public function minePlaylistTagsForUsers($users)
113
+  {
114
+    $this->mineForUsers($users, 'orderUserPlaylistsTags', 'setDataPlaylistNoMoreUpdated');
115
+  }
116
+  
117
+  public function mineTagsForUsers($users)
118
+  {
119
+    $this->mineForUsers($users, 'orderUserTags');
120
+  }
121
+  
122
+  public function mineTopTagsForUsers($users)
123
+  {
124
+    $this->mineForUsers($users, 'determineTagsTops');
100 125
   }
101 126
   
102 127
   /** @return EntityTags */
@@ -131,26 +156,23 @@ class TagMiner
131 156
     }
132 157
   }
133 158
   
134
-  protected function scoreUserDiffusionsTags(EntityTags $user_tags, User $user)
159
+  protected function orderUserDiffusionsTags(EntityTags $user_tags, User $user)
135 160
   {
136 161
     $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($user->getElements());
137
-    $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
138
-    $user_tags->setElementDiffusionTags($scoreds_tags_ids);
162
+    $user_tags->setElementDiffusionTags($tags_ids_ordereds);
139 163
   }
140 164
   
141
-  protected function scoreUserFavoritesTags(EntityTags $user_tags, User $user)
165
+  protected function orderUserFavoritesTags(EntityTags $user_tags, User $user)
142 166
   {
143 167
     $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($user->getElementsFavoritesElements());
144
-    $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
145
-    $user_tags->setElementFavoriteTags($scoreds_tags_ids);
168
+    $user_tags->setElementFavoriteTags($tags_ids_ordereds);
146 169
   }
147 170
   
148
-  protected function scoreUserPlaylistsTags(EntityTags $user_tags, User $user)
171
+  protected function orderUserPlaylistsTags(EntityTags $user_tags, User $user)
149 172
   {
150 173
     $playlist_manager = new PlaylistManager($this->getDoctrineEntityManager());
151 174
     $tags_ids_ordereds = $this->getTagOrderer()->getOrderedTagsWithElements($playlist_manager->getElementsOfPlaylists($this->getUserPlaylists($user)));
152
-    $scoreds_tags_ids = $this->getTagsScorer()->scoreOrderedsTagsIds($tags_ids_ordereds);
153
-    $user_tags->setElementPlaylistTags($scoreds_tags_ids);
175
+    $user_tags->setElementPlaylistTags($tags_ids_ordereds);
154 176
   }
155 177
   
156 178
   protected function getUserPlaylists(User $user)
@@ -174,7 +196,7 @@ class TagMiner
174 196
     return $playlists;
175 197
   }
176 198
   
177
-  protected function scoreUserTags(EntityTags $user_tags, User $user)
199
+  protected function orderUserTags(EntityTags $user_tags, User $user)
178 200
   {
179 201
     $all_tags_ordered = $this->getTagsScorer()->scoreEntireOrderedTagsIds(array(
180 202
       $user_tags->getElementDiffusionTags(),
@@ -185,7 +207,7 @@ class TagMiner
185 207
     $user_tags->setTagsAll($all_tags_ordered);
186 208
   }
187 209
   
188
-  protected function determineTagsTops(EntityTags $user_tags)
210
+  protected function determineTagsTops(EntityTags $user_tags, User $user)
189 211
   {
190 212
     $user_tags->setTagsTop1($this->getTopTagsRange($user_tags->getTagsAll(), 1));
191 213
     $user_tags->setTagsTop2($this->getTopTagsRange($user_tags->getTagsAll(), 2));

+ 24 - 0
src/Muzich/CoreBundle/lib/Controller.php Просмотреть файл

@@ -531,6 +531,18 @@ class Controller extends BaseController
531 531
     return $this->getDoctrine()->getManager();
532 532
   }
533 533
   
534
+  /** @return Doctrine\ODM\MongoDB\DocumentManager */
535
+  public function getMongoManager()
536
+  {
537
+    return $this->get('doctrine_mongodb')->getManager();
538
+  }
539
+  
540
+  /** @return Doctrine\ODM\MongoDB\DocumentRepository */
541
+  public function getMongoRepository($repository)
542
+  {
543
+    return $this->get('doctrine_mongodb')->getRepository($repository);
544
+  }
545
+  
534 546
   /**
535 547
    *
536 548
    * @param object $entity 
@@ -723,4 +735,16 @@ class Controller extends BaseController
723 735
     return $this->container->get('muzich_user_manager');
724 736
   }
725 737
   
738
+  /** @return \Muzich\CoreBundle\Mining\Tag\TagMiner */
739
+  protected function getMineTagMiner()
740
+  {
741
+    return $this->container->get('muzich.mining.tag.miner');
742
+  }
743
+  
744
+  /** @return \Muzich\CoreBundle\Mining\Tag\TagData */
745
+  protected function getMineTagData()
746
+  {
747
+    return $this->container->get('muzich.mining.tag.data');
748
+  }
749
+  
726 750
 }

+ 3 - 2
src/Muzich/CoreBundle/lib/TagScorer.php Просмотреть файл

@@ -5,7 +5,7 @@ namespace Muzich\CoreBundle\lib;
5 5
 class TagScorer
6 6
 {
7 7
   
8
-  public function scoreOrderedsTagsIds($ordered_tags)
8
+  protected function scoreOrderedsTagsIds($ordered_tags)
9 9
   {
10 10
     $tags_ordered_by_score = array();
11 11
     $score_max = count($ordered_tags);
@@ -25,7 +25,8 @@ class TagScorer
25 25
     
26 26
     foreach ($ordereds_elements_tags_ids as $ordereds_tags_ids)
27 27
     {
28
-      foreach ($ordereds_tags_ids as $score_tag => $tag_id)
28
+      $scored_tags_ids = $this->scoreOrderedsTagsIds($ordereds_tags_ids);
29
+      foreach ($scored_tags_ids as $score_tag => $tag_id)
29 30
       {
30 31
         if (!array_key_exists($tag_id, $tags_score))
31 32
           $tags_score[$tag_id] = 0;

+ 4 - 2
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Просмотреть файл

@@ -176,8 +176,7 @@ class FavoriteController extends Controller
176 176
     
177 177
     // Organisation des tags en fonction de leurs utilisation
178 178
     $tag_lib = new TagLib();
179
-    $tags = $tag_lib->sortTagWithOrderedReference($tags, 
180
-      $user->getData(User::DATA_TAGS_ORDER_PAGE_FAV, array()));
179
+    $tags = $tag_lib->sortTagWithOrderedReference($tags, $this->getMineTagData()->getTagOrderForFavorites($user));
181 180
     
182 181
     $tags_id = array();
183 182
     foreach ($tags as $tag)
@@ -218,6 +217,9 @@ class FavoriteController extends Controller
218 217
         ->getTags($viewed_user->getId(), $this->getUserId(true))      
219 218
       ;
220 219
 
220
+      $tag_lib = new TagLib();
221
+      $tags = $tag_lib->sortTagWithOrderedReference($tags, $this->getMineTagData()->getTagOrderForFavorites($viewed_user));
222
+      
221 223
       $tags_id = array();
222 224
       foreach ($tags as $tag)
223 225
       {

+ 1 - 2
src/Muzich/HomeBundle/Controller/ShowController.php Просмотреть файл

@@ -34,8 +34,7 @@ class ShowController extends Controller
34 34
     
35 35
     // Organisation des tags en fonction de leurs utilisation
36 36
     $tag_lib = new TagLib();
37
-    $tags = $tag_lib->sortTagWithOrderedReference($tags, 
38
-    $viewed_user->getData(User::DATA_TAGS_ORDER_DIFF, array()));
37
+    $tags = $tag_lib->sortTagWithOrderedReference($tags, $this->getMineTagData()->getTagOrderForDiffusions($viewed_user));
39 38
     
40 39
     $tags_id = array();
41 40
     foreach ($tags as $tag)

+ 9 - 0
src/Muzich/PlaylistBundle/Controller/EditController.php Просмотреть файл

@@ -8,6 +8,7 @@ use Muzich\CoreBundle\Security\Context as SecurityContext;
8 8
 use Muzich\CoreBundle\Propagator\EventElement;
9 9
 use Muzich\CoreBundle\Form\Playlist\PrivateLinksForm;
10 10
 use Muzich\CoreBundle\Entity\Playlist;
11
+use Muzich\CoreBundle\Entity\User;
11 12
 
12 13
 class EditController extends Controller
13 14
 {
@@ -40,6 +41,7 @@ class EditController extends Controller
40 41
       return $this->jsonNotFoundResponse();
41 42
     
42 43
     $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
44
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
43 45
     
44 46
     $event = new EventElement($this->container);
45 47
     $event->removedFromPlaylist($element, $this->getUser(), $playlist);
@@ -77,6 +79,7 @@ class EditController extends Controller
77 79
       return $this->jsonNotFoundResponse();
78 80
     
79 81
     $playlist_manager->addElementToPlaylist($element, $playlist);
82
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
80 83
     
81 84
     $event = new EventElement($this->container);
82 85
     $event->addedToPlaylist($element, $this->getUser(), $playlist);
@@ -102,6 +105,7 @@ class EditController extends Controller
102 105
       
103 106
       $event = new EventElement($this->container);
104 107
       $event->addedToPlaylist($element, $this->getUser(), $form->getData());
108
+      $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
105 109
 
106 110
       $this->persist($element);
107 111
       $this->flush();
@@ -133,6 +137,7 @@ class EditController extends Controller
133 137
     
134 138
     $event = new EventElement($this->container);
135 139
     $event->addedToPlaylist($element, $this->getUser(), $new_playlist);
140
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
136 141
     
137 142
     $this->persist($element);
138 143
     $this->flush();
@@ -149,6 +154,7 @@ class EditController extends Controller
149 154
       throw $this->createNotFoundException();
150 155
     
151 156
     $this->getPlaylistManager()->deletePlaylist($playlist);
157
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
152 158
     $this->flush();
153 159
     $this->setFlash('success', 'playlist.delete.success');
154 160
     
@@ -166,6 +172,7 @@ class EditController extends Controller
166 172
       throw $this->createNotFoundException();
167 173
     
168 174
     $playlist_manager->removePickedPlaylistToUser($this->getUser(), $playlist);
175
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
169 176
     $this->flush();
170 177
     $this->setFlash('success', 'playlist.delete.success');
171 178
     
@@ -190,6 +197,7 @@ class EditController extends Controller
190 197
       throw $this->createNotFoundException();
191 198
     }
192 199
     $this->getPlaylistManager()->addPickedPlaylistToUser($this->getUser(), $playlist);
200
+    $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
193 201
     $this->flush();
194 202
     
195 203
     if ($this->getRequest()->isXmlHttpRequest())
@@ -212,6 +220,7 @@ class EditController extends Controller
212 220
       if ($playlist_form->isValid())
213 221
       {
214 222
         $this->persist($playlist_form->getData());
223
+        $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
215 224
         $this->flush();
216 225
         
217 226
         $this->setFlash('success', $this->trans('playlist.create.success', array(), 'flash'));