PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/retired/olympics2014.php

http://github.com/ginatrapani/ThinkUp
PHP | 141 lines | 84 code | 22 blank | 35 comment | 8 complexity | 38da9ce39ede4142cb5b536766e79c3b MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Olympics 2014
  4. Description: Did you mention the Olympics?
  5. When: Sunday, February 23, 2014
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/olympics2014.php
  10. *
  11. * Copyright (c) 2014-2016 Gina Trapani, Anil Dash
  12. *
  13. * LICENSE:
  14. *
  15. * This file is part of ThinkUp (https://thinkup.com).
  16. *
  17. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  18. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  19. * later version.
  20. *
  21. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  22. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  26. * <http://www.gnu.org/licenses/>.
  27. *
  28. * @license http://www.gnu.org/licenses/gpl.html
  29. * @copyright 2014-2016 Gina Trapani, Anil Dash
  30. * @author Anil Dash <anil[at]thinkup[dot]com>
  31. */
  32. class Olympics2014Insight extends InsightPluginParent implements InsightPlugin {
  33. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  34. if (Utils::isTest() || date("Y-m-d") == '2014-02-23') {
  35. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  36. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  37. $hero_image = array(
  38. 'url' => 'https://www.thinkup.com/assets/images/insights/2014-02/olympics2014.jpg',
  39. 'alt_text' => 'The Olympic rings in Sochi',
  40. 'credit' => 'Photo: Atos International',
  41. 'img_link' => 'http://www.flickr.com/photos/atosorigin/12568057033/'
  42. );
  43. $post_dao = DAOFactory::getDAO('PostDAO');
  44. $last_month_of_posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username,
  45. $network=$instance->network, $count=0, $order_by="pub_date", $in_last_x_days = 30,
  46. $iterator = false, $is_public = false);
  47. if (self::shouldGenerateWeeklyInsight('olympics_2014', $instance, $insight_date='today',
  48. $regenerate_existing_insight=true, $day_of_week=0, count($last_month_of_posts))) {
  49. $event_count = 0;
  50. foreach ($last_month_of_posts as $post) {
  51. $event_count += self::countOlympicReferences($post->post_text);
  52. }
  53. $this->logger->logInfo("There are $event_count Olympic-related mentions", __METHOD__.','.__LINE__);
  54. if ($event_count > 0) {
  55. $headline = "Do they give out medals for ".$this->terms->getNoun('post', InsightTerms::PLURAL)."?";
  56. $insight_text = "$this->username mentioned ";
  57. if ($event_count > 0) {
  58. $this->logger->logInfo("There are event mentions", __METHOD__.','.__LINE__);
  59. $insight_text .= "the Olympics ";
  60. if ($event_count > 1) {
  61. $this->logger->logInfo("there is more than one event mention", __METHOD__.','.__LINE__);
  62. $insight_text .= "$event_count times since they started.";
  63. $insight_text .= " That's kind of like winning $event_count gold medals in " .
  64. ucfirst($instance->network) . ", right?";
  65. } else {
  66. $insight_text .= "just as the whole world's attention was focused on the Games.";
  67. $insight_text .= " That's a pretty great way to join a global conversation.";
  68. }
  69. }
  70. $my_insight = new Insight();
  71. $my_insight->slug = 'olympics_2014'; //slug to label this insight's content
  72. $my_insight->instance_id = $instance->id;
  73. $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date
  74. $my_insight->headline = $headline; // or just set a string like 'Ohai';
  75. $my_insight->text = $insight_text; // or just set a strong like "Greetings humans";
  76. $my_insight->filename = basename(__FILE__, ".php"); //Same for every insight, must be set this way
  77. $my_insight->emphasis = Insight::EMPHASIS_HIGH; //Optional emphasis, default is Insight::EMPHASIS_LOW
  78. $my_insight->setHeroImage($hero_image);
  79. $this->insight_dao->insertInsight($my_insight);
  80. }
  81. }
  82. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  83. }
  84. }
  85. /**
  86. * Count the number of times Olympic-related terms appear in text.
  87. * @param str $text
  88. * @return int Total occurences of the event names in $text
  89. */
  90. public static function countOlympicReferences($text) {
  91. $count = 0;
  92. $matches = array();
  93. $url_free_text = preg_replace('!https?://[\S]+!', ' ', $text);
  94. $depunctuated_text = " ". preg_replace('/[^a-z0-9]+/i', ' ', $url_free_text) ." ";
  95. preg_match_all("/\bolympic/i", $depunctuated_text, $matches);
  96. $count += sizeof($matches[0]);
  97. preg_match_all("/\bolympian/i", $depunctuated_text, $matches);
  98. $count += sizeof($matches[0]);
  99. preg_match_all("/\bsochi/i", $depunctuated_text, $matches);
  100. $count += sizeof($matches[0]);
  101. preg_match_all("/\bopening ceremony\b/i", $depunctuated_text, $matches);
  102. $count += sizeof($matches[0]);
  103. preg_match_all("/\bopening ceremonies\b/i", $depunctuated_text, $matches);
  104. $count += sizeof($matches[0]);
  105. preg_match_all("/\bclosing ceremony\b/i", $depunctuated_text, $matches);
  106. $count += sizeof($matches[0]);
  107. preg_match_all("/\bclosing ceremonies\b/i", $depunctuated_text, $matches);
  108. $count += sizeof($matches[0]);
  109. preg_match_all("/\bgold medal/i", $depunctuated_text, $matches);
  110. $count += sizeof($matches[0]);
  111. preg_match_all("/\bsilver medal/i", $depunctuated_text, $matches);
  112. $count += sizeof($matches[0]);
  113. preg_match_all("/\bbronze medal/i", $depunctuated_text, $matches);
  114. $count += sizeof($matches[0]);
  115. return $count;
  116. }
  117. }
  118. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  119. $insights_plugin_registrar->registerInsightPlugin('Olympics2014Insight');