PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/ginatrapani/ThinkUp
PHP | 168 lines | 109 code | 15 blank | 44 comment | 11 complexity | b08b8257ef87a5947c52a9aad18e71d9 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Controversial Topics (End of Year)
  4. Description: Which controversial topics did you mention or avoid this year.
  5. When: December 16
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/eoycontroversialtopics.php
  10. *
  11. * Copyright (c) 2012-2016 Gina Trapani
  12. *
  13. * LICENSE:
  14. *
  15. * This file is part of ThinkUp (http://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. * Copyright (c) 2014-2016 Chris Moyer
  29. *
  30. * @author Chris Moyer chris@inarow.net
  31. * @license http://www.gnu.org/licenses/gpl.html
  32. * @copyright 2014-2016 Chris Moyer
  33. */
  34. class EOYControversialTopicsInsight extends InsightPluginParent implements InsightPlugin {
  35. /**
  36. * Slug for this insight
  37. **/
  38. var $slug = 'eoy_controversial_topics';
  39. /**
  40. * Date to run this insight
  41. **/
  42. var $run_date = '12-16';
  43. //staging
  44. //var $run_date = '11-06';
  45. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  46. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  47. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  48. if ($instance->network != 'facebook') {
  49. $this->logger->logInfo("Done generating insight (Skipped Non-Facebook)", __METHOD__.','.__LINE__);
  50. return;
  51. }
  52. $year = date('Y');
  53. $regenerate = false;
  54. //testing
  55. //$regenerate = true;
  56. $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight(
  57. $this->slug,
  58. $instance,
  59. $insight_date = "$year-$this->run_date",
  60. $regenerate,
  61. $day_of_year = $this->run_date
  62. );
  63. if ($should_generate_insight) {
  64. $this->logger->logInfo("Should generate", __METHOD__.','.__LINE__);
  65. $post_dao = DAOFactory::getDAO('PostDAO');
  66. $last_year_of_posts = $post_dao->getThisYearOfPostsIterator(
  67. $author_id = $instance->network_user_id,
  68. $network = $instance->network
  69. );
  70. $topics = array(
  71. 'ferguson' => array("Ferguson", "Mike Brown", "Michael Brown"),
  72. 'obamacare' => array("Obamacare"),
  73. 'immigration' => array("immigration"),
  74. 'gamergate' => array("GamerGate"),
  75. 'isis' => array("ISIS"),
  76. 'israel' => array("Israel"),
  77. 'palestine' => array("Palestine"),
  78. 'hamas' => array("Hamas"),
  79. 'donaldsterling' => array("Donald Sterling"),
  80. 'marriage' => array("gay marriage","marriage equality","same-sex marriage"),
  81. 'ebola' => array("ebola"),
  82. 'climatechange' => array("climate change","global warming"),
  83. );
  84. $matches = array();
  85. foreach ($last_year_of_posts as $post) {
  86. foreach ($topics as $key => $strings) {
  87. foreach ($strings as $string) {
  88. if (preg_match_all('/\b'.$string.'\b/i', $post->post_text) > 0) {
  89. $matches[$key] = array('term'=>$string, 'post'=>$post);
  90. unset($topics[$key]);
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. $insight = new Insight();
  97. if (count($matches) == 0) {
  98. $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance);
  99. $qualified_year = "";
  100. if (date('Y', strtotime($earliest_pub_date)) == date('Y')) {
  101. if (date('n', strtotime($earliest_pub_date)) > 1 ) { //not January
  102. //Earliest post was this year; figure out what month we have data since this year
  103. $since = date('F', strtotime($earliest_pub_date));
  104. $qualified_year = " (at least since ".$since.")";
  105. }
  106. }
  107. $headline = $this->username.' kept the drama off Facebook in 2014';
  108. $insight_text = $this->username." avoided contentious topics like immigration and ebola in $year"
  109. . $qualified_year. ', which can be a good way to keep Facebook more friendly.';
  110. $posts = null;
  111. //Show avatar if there are no posts
  112. $insight->header_image = $user->avatar;
  113. }
  114. else {
  115. $headline = $this->username." took on $year's hot-button issues";
  116. $posts = array();
  117. $mentioned = array();
  118. foreach ($matches as $m) {
  119. if (count($posts) < 3) {
  120. $posts[] = $m['post'];
  121. }
  122. $mentioned[] = $m['term'];
  123. }
  124. $num = count($mentioned);
  125. if ($num > 1) {
  126. $mentioned[$num-1] = 'and '.$mentioned[$num-1];
  127. }
  128. $mention_string = join($num==2?' ':', ', $mentioned);
  129. $insight_text = $this->username." mentioned $mention_string on Facebook in $year. ".
  130. "It's great to use Facebook to discuss issues that matter.";
  131. }
  132. $insight->instance_id = $instance->id;
  133. $insight->slug = $this->slug;
  134. $insight->date = "$year-$this->run_date";
  135. $insight->headline = $headline;
  136. $insight->text = $insight_text;
  137. $insight->filename = basename(__FILE__, ".php");
  138. $insight->emphasis = Insight::EMPHASIS_HIGH;
  139. if ($posts) {
  140. $posts = array_slice($posts, 0, 12);
  141. $link_dao = DAOFactory::getDAO('LinkDAO');
  142. foreach ($posts as $post) {
  143. $post->links = $link_dao->getLinksForPost($post->post_id, $post->network);
  144. }
  145. $insight->setPosts($posts);
  146. }
  147. $this->insight_dao->insertInsight($insight);
  148. }
  149. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  150. }
  151. }
  152. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  153. $insights_plugin_registrar->registerInsightPlugin('EOYControversialTopicsInsight');