setName('mining:tags') ->setDescription('Mine tags data') ->addOption('all', null, InputOption::VALUE_NONE, 'Selectionne tous les utilisateurs') ->addOption(self::MINE_DIFFUSION, null, InputOption::VALUE_NONE, 'Ne traite que les diffusions') ->addOption(self::MINE_FAVORITE, null, InputOption::VALUE_NONE, 'Ne traite que les favoris') ->addOption(self::MINE_PLAYLIST, null, InputOption::VALUE_NONE, 'Ne traite que les playlists') ->addOption(self::MINE_TAGS, null, InputOption::VALUE_NONE, 'Ne traite que les tags') ->addOption(self::MINE_TOPS, null, InputOption::VALUE_NONE, 'Ne traite que les top tags') ; } /** @return TagMiner */ protected function getTagMiner() { return $this->tag_miner; } /** @return \Doctrine\ORM\EntityManager */ protected function getEntityManager() { return $this->em; } protected function init(InputInterface $input, OutputInterface $output) { $this->em = $this->getContainer()->get('doctrine')->getEntityManager(); $this->tag_miner = $this->getContainer()->get('muzich.mining.tag.miner'); // Experimental $this->tag_miner->setLogger($this); $this->progress = $this->getHelperSet()->get('progress'); $this->input = $input; $this->output = $output; } protected function execute(InputInterface $input, OutputInterface $output) { $this->init($input, $output); if ($this->canIMineThat(self::MINE_DIFFUSION)) $this->mineDiffusions(); if ($this->canIMineThat(self::MINE_FAVORITE)) $this->mineFavorites(); if ($this->canIMineThat(self::MINE_PLAYLIST)) $this->minePlaylists(); if ($this->canIMineThat(self::MINE_TAGS)) $this->mineTags(); if ($this->canIMineThat(self::MINE_TOPS)) $this->mineTopTags(); $this->output->writeln('Terminé !'); } /** @return QueryBuilder */ protected function getUserQueryBuilder() { $user_query_builder = $this->getEntityManager()->createQueryBuilder() ->from('MuzichCoreBundle:User', 'user'); $this->tag_miner->adaptQueryBuilderSelectorsForUser($user_query_builder, 'user'); return $user_query_builder; } protected function mineDiffusions() { $users = $this->getUsersToProceed(User::DATA_DIFF_UPDATED); $this->output->writeln('Diffusions: '.count($users).' utilisateurs'); $this->progress->start($this->output, count($users)); $this->getTagMiner()->mineDiffusionTagsForUsers($users); $this->addUsersToUsersMineds($users); } protected function mineFavorites() { $users = $this->getUsersToProceed(User::DATA_FAV_UPDATED); $this->output->writeln('Favoris: '.count($users).' utilisateurs'); $this->progress->start($this->output, count($users)); $this->getTagMiner()->mineFavoriteTagsForUsers($users); $this->addUsersToUsersMineds($users); } protected function minePlaylists() { $users = $this->getUsersToProceed(User::DATA_PLAY_UPDATED); $this->output->writeln('Playlists: '.count($users).' utilisateurs'); $this->progress->start($this->output, count($users)); $this->getTagMiner()->minePlaylistTagsForUsers($users); $this->addUsersToUsersMineds($users); } protected function mineTags() { $this->output->writeln('Tags: '.count($this->users_mineds).' utilisateurs'); $this->progress->start($this->output, count($this->users_mineds)); $this->getTagMiner()->mineTagsForUsers($this->users_mineds); } protected function mineTopTags() { $this->output->writeln('TopTags: '.count($this->users_mineds).' utilisateurs'); $this->progress->start($this->output, count($this->users_mineds)); $this->getTagMiner()->mineTopTagsForUsers($this->users_mineds); } protected function getUsersToProceed($condition) { $users_query_builder = $this->getUserQueryBuilder(); if (!$this->input->getOption('all')) { $users_query_builder->andWhere('user.datas NOT LIKE :condition OR user.datas IS NULL'); $users_query_builder->setParameter('condition', '%"'.$condition.'":false%'); } return $users_query_builder->getQuery()->getResult(); } protected function canIMineThat($mine_type_asked) { $mine_type_specified = false; foreach (self::$mine_types as $mine_type) { if ($this->input->getOption($mine_type)) $mine_type_specified = true; } if (!$mine_type_specified) return true; if ($this->input->getOption($mine_type_asked)) return true; return false; } public function logUserProceed() { $this->progress->advance(); } public function logSavingInDatabase() { $this->output->writeln(''); $this->output->writeln('Saving in database ...'); } protected function addUsersToUsersMineds($users_to_add) { foreach ($users_to_add as $user_to_add) { $found = false; foreach ($this->users_mineds as $user_mined) { if ($user_mined->getId() == $user_to_add->getId()) $found = true; } if (!$found) $this->users_mineds[] = $user_to_add; } } }