MineTagsDataCommand.php 6.1KB

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