RelatedRorController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Drupal\related_ror\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Drupal\Core\Cache\CacheableJsonResponse;
  7. use Drupal\Core\Cache\CacheableMetadata;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Drupal\Core\Template\TwigEnvironment;
  10. use Drupal\node\NodeInterface;
  11. class RelatedRorController extends ControllerBase {
  12. protected $state;
  13. protected $twig;
  14. public function __construct($state, TwigEnvironment $twig) {
  15. $this->state = $state;
  16. $this->twig = $twig;
  17. }
  18. public static function create(ContainerInterface $container) {
  19. return new static(
  20. $container->get('state'),
  21. $container->get('twig')
  22. );
  23. }
  24. private function nodeToLinkdata($node_entity) {
  25. $arr = $node_entity->toArray();
  26. if(count($arr['body']) > 0) { // c'e' gente che pubblica articoli senza body
  27. $body = $arr['body'][0]['value'];
  28. } else {
  29. $body = '';
  30. }
  31. return array(
  32. 'nid' => intval($arr['nid'] [0] ['value']),
  33. 'title' => $arr['title'][0]['value'],
  34. //'body' => $arr['body'][0] ['value'],
  35. 'summary' => htmlspecialchars(substr(
  36. html_entity_decode(strip_tags($body)), 0, 3500), ENT_XML1, 'UTF-8'),
  37. 'url' => $arr['path'][0] ['alias']
  38. );
  39. }
  40. private function queryNearInTime(int $time, int $days = 7, int $limit = 0): array {
  41. $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
  42. $query = \Drupal::entityQuery('node');
  43. $query = \Drupal::database()->select('node', 'n');
  44. $query->addJoin('INNER', 'node__field_tx_date', 'dt', 'n.nid=dt.entity_id');
  45. $query->addJoin('INNER', 'node_field_data', 'field', 'n.nid=field.nid');
  46. $query ->addField('n', 'nid');
  47. $query->addExpression("CAST(UNIX_TIMESTAMP(field_tx_date_value) as UNSIGNED)", 'ts');
  48. $query->addExpression("ABS(cast(UNIX_TIMESTAMP(field_tx_date_value) as signed) - $time)", 'dist');
  49. $query->condition('field.status', '1');
  50. $query->condition('n.type', $arg, 'IN');
  51. $query->where('CAST(UNIX_TIMESTAMP(dt.field_tx_date_value) as UNSIGNED) > :from', array('from' => $time - 3600*24*$days));
  52. $query->where('CAST(UNIX_TIMESTAMP(dt.field_tx_date_value) as UNSIGNED) < :to', array('to' => $time + 3600*24*$days));
  53. $query->orderBy('dist', 'ASC');
  54. if($limit > 0) {
  55. $query->range(0, $limit);
  56. }
  57. if($query->preExecute() !== TRUE) {
  58. return null;
  59. }
  60. $nids = $query->execute()->fetchCol(0);
  61. return $nids;
  62. }
  63. private function presentNids(array $nids) {
  64. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  65. $data = ['nodes' => []];
  66. foreach($nodes_e as $nid => $node) {
  67. $nodedata = $this->nodeToLinkdata($node);
  68. array_push($data['nodes'], $nodedata);
  69. }
  70. return $data;
  71. }
  72. public function relatedTime() {
  73. $nid = \Drupal::request()->query->get('nid');
  74. if($nid == null) {
  75. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  76. }
  77. if(!is_numeric($nid)) {
  78. return new Response("NID must be integer, not `$nid`", 400, array('Content-Type' => 'text/plain'));
  79. }
  80. $nid = intval($nid);
  81. $node = \Drupal\node\Entity\Node::load($nid);
  82. if($node == null) {
  83. return new Response("Node not found", 404, array('Content-Type' => 'text/plain'));
  84. }
  85. if($this->isFilteredOut($node)) {
  86. return new Response("Node has no similarity", 404, array('Content-Type' => 'text/plain'));
  87. }
  88. $time = intval($node->getCreatedTime());
  89. $nearnodes = $this->presentNids(array_filter(
  90. $this->queryNearInTime($time, $days=4, $limit=20),
  91. function ($n) use ($nid): bool { return intval($n) != $nid; }));
  92. $resp = $this->cachedJsonResp(array('time' => $nearnodes));
  93. return $resp;
  94. }
  95. public function relatedTrx() {
  96. // TODO: get node time
  97. // TODO: queryNearInTime($time)
  98. $nid = \Drupal::request()->query->get('nid');
  99. if($nid == null) {
  100. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  101. }
  102. $content = "asd $nid";
  103. $resp = new Response($content, 500, array( 'Content-Type' => 'text/plain'));
  104. return $resp;
  105. }
  106. private function getTermWeight(int $termid): int {
  107. $term = \Drupal\taxonomy\Entity\Term::load($termid);
  108. $query = \Drupal::database()->select('taxonomy_index', 'ti');
  109. $query->fields('ti', ['nid']);
  110. $query->condition('ti.tid', $termid);
  111. $cnt = $query->countQuery()->execute()->fetchField();
  112. if($cnt < 100) {
  113. return 2;
  114. }
  115. return 1;
  116. }
  117. private function getSimilarity(array $orig, array $other): int {
  118. if(!array_key_exists('field_tags', $orig) || count($orig['field_tags']) == 0 ||
  119. !array_key_exists('field_tags', $other) || count($other['field_tags']) == 0) {
  120. return 0;
  121. }
  122. $orig_tags = array_map(function($t) { return $t['target_id']; }, $orig['field_tags']);
  123. $other_tags = array_map(function($t) { return $t['target_id']; }, $other['field_tags']);
  124. $both = array_intersect($orig_tags, $other_tags);
  125. $weighted = array_map([$this, 'getTermWeight'], $both);
  126. return array_sum($weighted) || 0;
  127. }
  128. private function cachedJsonResp(array $data): Response {
  129. $resp = new JsonResponse($data, 200, ['cache-control' => 'public, max-age=900']);
  130. return $resp;
  131. }
  132. private function isFilteredOut(\Drupal\node\Entity\Node $node) {
  133. if($node->getType() === 'page')
  134. return true;
  135. return false;
  136. }
  137. public function relatedTopic() {
  138. // TODO: get node time
  139. // TODO: queryNearInTime($time)
  140. $nid = \Drupal::request()->query->get('nid');
  141. if($nid == null) {
  142. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  143. }
  144. if(!is_numeric($nid)) {
  145. return new Response("NID must be integer, not `$nid`", 400, array('Content-Type' => 'text/plain'));
  146. }
  147. $nid = intval($nid);
  148. $node = \Drupal\node\Entity\Node::load($nid);
  149. if($node == null) {
  150. return new Response("Node not found", 404, array('Content-Type' => 'text/plain'));
  151. }
  152. if($this->isFilteredOut($node)) {
  153. return new Response("Node similarity disallowed", 403, array('Content-Type' => 'text/plain'));
  154. }
  155. $orig_arr = $node->toArray();
  156. $time = intval($node->getCreatedTime());
  157. $goodnids = [];
  158. $scores = [];
  159. $other_e = \Drupal\node\Entity\Node::loadMultiple($this->queryNearInTime($time, $days=30, $limit=100));
  160. foreach($other_e as $other_nid => $other_node) {
  161. if(intval($other_nid) === $nid) {
  162. continue;
  163. }
  164. $score = $this->getSimilarity($orig_arr, $other_node->toArray());
  165. if($score > 0) {
  166. $scores[$other_nid] = $score;
  167. }
  168. }
  169. arsort($scores);
  170. foreach($scores as $nid => $score) {
  171. array_push($goodnids, $nid);
  172. }
  173. $resp = $this->cachedJsonResp(['topic' => $this->presentNids($goodnids)]);
  174. return $resp;
  175. }
  176. }