Пример #1
0
 public static function fromEntries(array $entries = [])
 {
     $dist = new self();
     Model_MixedUserMedia::attachGenres($entries);
     foreach ($entries as $entry) {
         $dist->addEntry($entry);
     }
     $dist->finalize();
     return $dist;
 }
 public static function work(&$controllerContext, &$viewContext)
 {
     $viewContext->viewName = 'user-profile';
     $viewContext->meta->title = $viewContext->user->name . '\'s profile — ' . Config::$title;
     $viewContext->meta->description = $viewContext->user->name . '\'s profile.';
     WebMediaHelper::addEntries($viewContext);
     WebMediaHelper::addMiniSections($viewContext);
     WebMediaHelper::addCustom($viewContext);
     $viewContext->yearsOnMal = null;
     if (intval($viewContext->user->join_date)) {
         list($year, $month, $day) = explode('-', $viewContext->user->join_date);
         $time = mktime(0, 0, 0, $month, $day, $year);
         $diff = time() - $time;
         $diff /= 3600 * 24;
         $viewContext->yearsOnMal = $diff / 361.25;
     }
     $viewContext->friends = $viewContext->user->getFriends();
     $viewContext->finished = [];
     $viewContext->meanUserScore = [];
     $viewContext->meanGlobalScore = [];
     $viewContext->franchiseCount = [];
     $viewContext->mismatchedCount = [];
     foreach (Media::getConstList() as $media) {
         $list = $viewContext->user->getMixedUserMedia($media);
         $listFinished = UserMediaFilter::doFilter($list, UserMediaFilter::finished());
         $viewContext->finished[$media] = count($listFinished);
         unset($listFinished);
         $listNonPlanned = UserMediaFilter::doFilter($list, UserMediaFilter::nonPlanned());
         $viewContext->meanUserScore[$media] = RatingDistribution::fromEntries($listNonPlanned)->getMeanScore();
         $franchises = Model_MixedUserMedia::getFranchises($listNonPlanned);
         $viewContext->franchiseCount[$media] = count(array_filter($franchises, function ($franchise) {
             return count($franchise->ownEntries) > 1;
         }));
         unset($franchises);
         unset($listNonPlanned);
         if ($media == Media::Anime) {
             $viewContext->episodes = array_sum(array_map(function ($mixedMediaEntry) {
                 return $mixedMediaEntry->finished_episodes;
             }, $list));
         } else {
             $viewContext->chapters = array_sum(array_map(function ($mixedMediaEntry) {
                 return $mixedMediaEntry->finished_chapters;
             }, $list));
         }
         $mismatched = $viewContext->user->getMismatchedUserMedia($list);
         $viewContext->mismatchedCount[$media] = count($mismatched);
         unset($mismatched);
         unset($list);
         $globalsCache = file_exists(Config::$globalsCachePath) ? TextHelper::loadJson(Config::$globalsCachePath, true) : [];
         $viewContext->meanGlobalScore[$media] = array_map(function ($v) {
             return RatingDistribution::fromArray($v);
         }, $globalsCache['rating-dist'])[$media]->getMeanScore();
     }
 }
 public static function work(&$controllerContext, &$viewContext)
 {
     $viewContext->viewName = 'user-profile';
     $viewContext->meta->title = 'MALgraph - ' . $viewContext->user->name . '’s profile';
     $viewContext->meta->description = $viewContext->user->name . '’s profile on MALgraph, an online tool that extends your MyAnimeList profile.';
     $viewContext->meta->keywords = array_merge($viewContext->meta->keywords, ['profile', 'list', 'achievements', 'ratings', 'history', 'favorites', 'suggestions', 'recommendations']);
     WebMediaHelper::addEntries($viewContext);
     WebMediaHelper::addMiniSections($viewContext);
     WebMediaHelper::addCustom($viewContext);
     $viewContext->yearsOnMal = null;
     if (intval($viewContext->user->join_date)) {
         list($year, $month, $day) = explode('-', $viewContext->user->join_date);
         $time = mktime(0, 0, 0, $month, $day, $year);
         $diff = time() - $time;
         $diff /= 3600 * 24;
         $viewContext->yearsOnMal = $diff / 361.25;
     }
     $viewContext->friends = $viewContext->user->getFriends();
     $viewContext->clubs = $viewContext->user->getClubs();
     $viewContext->finished = [];
     $viewContext->meanUserScore = [];
     $viewContext->meanGlobalScore = [];
     $viewContext->franchiseCount = [];
     $viewContext->mismatchedCount = [];
     foreach (Media::getConstList() as $media) {
         $list = $viewContext->user->getMixedUserMedia($media);
         $listFinished = UserMediaFilter::doFilter($list, UserMediaFilter::finished());
         $viewContext->finished[$media] = count($listFinished);
         unset($listFinished);
         $listNonPlanned = UserMediaFilter::doFilter($list, UserMediaFilter::nonPlanned());
         $viewContext->meanUserScore[$media] = RatingDistribution::fromEntries($listNonPlanned)->getMeanScore();
         $viewContext->meanGlobalScore[$media] = Model_MixedUserMedia::getRatingDistribution($media)->getMeanScore();
         $franchises = Model_MixedUserMedia::getFranchises($listNonPlanned);
         $viewContext->franchiseCount[$media] = count(array_filter($franchises, function ($franchise) {
             return count($franchise->ownEntries) > 1;
         }));
         unset($franchises);
         unset($listNonPlanned);
         if ($media == Media::Anime) {
             $viewContext->episodes = array_sum(array_map(function ($mixedMediaEntry) {
                 return $mixedMediaEntry->finished_episodes;
             }, $list));
         } else {
             $viewContext->chapters = array_sum(array_map(function ($mixedMediaEntry) {
                 return $mixedMediaEntry->finished_chapters;
             }, $list));
         }
         $mismatched = $viewContext->user->getMismatchedUserMedia($list);
         $viewContext->mismatchedCount[$media] = count($mismatched);
         unset($mismatched);
         unset($list);
     }
 }
Пример #4
0
 public static function genre($genreIds, $list)
 {
     if (empty($list)) {
         return [];
     }
     if (!is_array($genreIds)) {
         $genreIds = [$genreIds];
     }
     $tblName = Model_MixedUserMedia::createTemporaryTable($list);
     $query = 'SELECT * FROM mediagenre mg INNER JOIN ' . $tblName . ' ON mg.media_id = ' . $tblName . '.media_id WHERE mg.mal_id IN (' . R::genSlots($genreIds) . ')';
     $data = R::getAll($query, $genreIds);
     Model_MixedUserMedia::dropTemporaryTable($tblName);
     $data = array_map(function ($x) {
         return $x['media_id'];
     }, $data);
     $data = array_flip($data);
     return function ($row) use($data) {
         return isset($data[$row->media_id]);
     };
 }
 public function getNewRecommendations($goal)
 {
     $dontRecommend = [];
     foreach ($this->allFranchises as $franchise) {
         foreach ($franchise->allEntries as $entry) {
             $key = $entry->media . $entry->mal_id;
             $dontRecommend[$key] = true;
         }
     }
     $selectedEntries = [];
     $weights1 = self::addRecsFromRecommendations($this->list);
     $weights1 = self::filterKeys($weights1, $dontRecommend);
     $weights1 = self::trimByValue($weights1, $goal * 10);
     $weights2 = self::addRecsFromStaticRecommendations($this->media);
     $weights2 = self::filterKeys($weights2, $dontRecommend);
     $allWeights = array_merge($weights2, $weights1);
     $selectedEntries = self::getRecsFromWeights($allWeights);
     //echo json_encode($selectedEntries);
     $selectedEntries = self::trimByScore($selectedEntries, $goal * 10);
     $selectedEntries = self::filterBannedGenres($selectedEntries);
     $selectedEntries = self::trimByScore($selectedEntries, $goal * 3);
     $selectedEntries = self::filterFranchises($selectedEntries);
     $selectedEntries = self::trimByScore($selectedEntries, $goal);
     //reattach stuff that might have been lost due to whatever reason
     foreach ($selectedEntries as $entry) {
         $entry->media_id = $entry->id;
     }
     Model_MixedUserMedia::attachGenres($selectedEntries);
     return $selectedEntries;
 }
 public static function work(&$controllerContext, &$viewContext)
 {
     $sender = $_GET['sender'];
     $filterParam = isset($_GET['filter-param']) ? $_GET['filter-param'] : null;
     if (isset($_GET['media']) and in_array($_GET['media'], Media::getConstList())) {
         $viewContext->media = $_GET['media'];
     }
     $viewContext->viewName = 'user-entries-' . $sender;
     $viewContext->layoutName = 'layout-ajax';
     $viewContext->filterParam = $filterParam;
     $list = $viewContext->user->getMixedUserMedia($viewContext->media);
     $computeMeanScore = null;
     switch ($sender) {
         case 'ratings':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::score($filterParam));
             break;
         case 'length':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::nonMovie(), UserMediaFilter::lengthGroup($filterParam));
             $computeMeanScore = true;
             break;
         case 'type':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::type($filterParam));
             $computeMeanScore = true;
             break;
         case 'year':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::publishedYear($filterParam));
             $computeMeanScore = true;
             break;
         case 'decade':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::publishedDecade($filterParam));
             $computeMeanScore = true;
             break;
         case 'creator':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::creator($filterParam, $list));
             switch ($viewContext->media) {
                 case Media::Anime:
                     $table = 'animeproducer';
                     break;
                 case Media::Manga:
                     $table = 'mangaauthor';
                     break;
                 default:
                     throw new BadMediaException();
             }
             $computeMeanScore = true;
             break;
         case 'genre':
             $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::genre($filterParam, $list));
             $computeMeanScore = true;
             break;
         case 'franchises':
             $filter = UserMediaFilter::nonPlanned();
             break;
         case 'mismatches':
             $filter = null;
             break;
         case 'watched':
             $filterParam = explode(":", $filterParam);
             $filterParam[2] = boolval($filterParam[2]);
             if ($filterParam[2]) {
                 $yearOrDecade = UserMediaFilter::publishedDecade($filterParam[1]);
             } else {
                 $yearOrDecade = UserMediaFilter::publishedYear($filterParam[1]);
             }
             if ($filterParam[0] == 1) {
                 $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::nonDropped(), UserMediaFilter::notOnHold(), $yearOrDecade, UserMediaFilter::type($filterParam[0]));
             } else {
                 if ($filterParam[0] == 2) {
                     $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::nonDropped(), UserMediaFilter::notOnHold(), $yearOrDecade, UserMediaFilter::notType(1));
                 } else {
                     if ($filterParam[0] == 3) {
                         $filter = UserMediaFilter::combine(UserMediaFilter::nonPlanned(), UserMediaFilter::notCompleting(), UserMediaFilter::notFinished(), $yearOrDecade);
                     }
                 }
             }
             $computeMeanScore = true;
             $viewContext->filterParam = $filterParam;
             break;
         default:
             throw new Exception('Unknown sender (' . $sender . ')');
     }
     $list = UserMediaFilter::doFilter($list, $filter);
     $isPrivate = $viewContext->user->isUserMediaPrivate($viewContext->media);
     if (!$isPrivate) {
         if ($computeMeanScore) {
             $dist = RatingDistribution::fromEntries($list);
             $viewContext->meanScore = $dist->getMeanScore();
         }
         if ($sender == 'franchises') {
             $franchises = Model_MixedUserMedia::getFranchises($list);
             foreach ($franchises as &$franchise) {
                 $dist = RatingDistribution::fromEntries($franchise->ownEntries);
                 $franchise->meanScore = $dist->getMeanScore();
             }
             unset($franchise);
             DataSorter::sort($franchises, DataSorter::MeanScore);
             $viewContext->franchises = array_filter($franchises, function ($franchise) {
                 return count($franchise->ownEntries) > 1;
             });
         } elseif ($sender == 'mismatches') {
             $entries = $viewContext->user->getMismatchedUserMedia($list);
             DataSorter::sort($entries, DataSorter::Title);
             $viewContext->entries = $entries;
         } else {
             DataSorter::sort($list, DataSorter::Title);
             $viewContext->entries = $list;
         }
     }
     $viewContext->isPrivate = $isPrivate;
 }
Пример #7
0
<?php

require_once __DIR__ . '/../src/core.php';
CronRunner::run(__FILE__, function ($logger) {
    $userCount = Model_User::getCount();
    $userCountActive = Model_User::getCountActive();
    $mediaCount = [];
    $distArr = [];
    foreach (Media::getConstList() as $media) {
        $distArr[$media] = [];
        $mediaCount[$media] = Model_Media::getCount($media);
    }
    foreach (Media::getConstList() as $media) {
        $localDist = Model_MixedUserMedia::getRatingDistribution($media);
        foreach ($localDist->getGroupsKeys() as $key) {
            if (!isset($distArr[$media][$key])) {
                $distArr[$media][$key] = 0;
            }
            $distArr[$media][$key] += $localDist->getGroupSize($key);
        }
    }
    $globalsCache = ['user-count' => $userCount, 'user-count-active' => $userCountActive, 'media-count' => $mediaCount, 'rating-dist' => $distArr];
    TextHelper::putJson(Config::$globalsCachePath, $globalsCache);
});