Created
December 26, 2014 16:12
-
-
Save joshuaadickerson/99fee0551787aea1673d to your computer and use it in GitHub Desktop.
Elkarte Hashtags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Hashtags\Model; | |
| class Trends | |
| { | |
| public function trending($min_tag_count = 100, $num_tags = 5, $num_hours = null) | |
| { | |
| // Get the number of hours | |
| $num_hours = $num_hours ?: $this->getNumHoursToStartTrend($min_tag_count); | |
| $tags = $this->getTagsByCountAndTime($num_hours); | |
| // Get the first $num_tags | |
| return array_slice($tags, 0, $num_tags); | |
| } | |
| protected function getNumHoursToStartTrend($min_tag_count = 100) | |
| { | |
| $hours = []; | |
| do | |
| { | |
| $hours += $this->getNumTagsByHour(); | |
| // Add a day to the start date. | |
| }while (count($hours) < $min_tag_count); | |
| return $num_hours; | |
| } | |
| protected function getNumTagsByHour(\DateTime $start_date = 'NOW()', $num_hours = 24) | |
| { | |
| /* | |
| * Group the number of tags by hour: | |
| * | |
| * SELECT HOUR(tag_dtg) AS tag_hour, COUNT(*) as hour_count | |
| * FROM post_tags | |
| * WHERE tag_dtg >= $start_date - $num_hours HOURS | |
| * GROUP BY HOUR(tag_dtg) | |
| * ORDER BY tag_hour | |
| */ | |
| return $hours; | |
| } | |
| protected function getTagsByCountAndTime($hours_from_now) | |
| { | |
| /* | |
| * Get all of the tags in this timeframe | |
| * | |
| * SELECT tag, COUNT(*) as tag_count | |
| * FROM post_tags | |
| * INNER JOIN tags USING(id_tag) | |
| * WHERE tag_dtg >= NOW() - $hours_from_now HOURS | |
| * GROUP BY id_tag | |
| * ORDER BY tag_count | |
| */ | |
| return $tags; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment