RelatedRorController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. return array(
  27. 'nid' => intval($arr['nid'] [0] ['value']),
  28. 'title' => $arr['title'][0]['value'],
  29. //'body' => $arr['body'][0] ['value'],
  30. 'summary' => htmlspecialchars(substr(
  31. html_entity_decode(strip_tags($arr['body'][0]['value'])), 0, 3500), ENT_XML1, 'UTF-8'),
  32. 'url' => $arr['path'][0] ['alias']
  33. );
  34. }
  35. private function queryNearInTime(int $time, int $days = 7, int $limit = 0): array {
  36. $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
  37. $query = \Drupal::entityQuery('node');
  38. $query = \Drupal::database()->select('node', 'n');
  39. $query->addJoin('INNER', 'node__field_tx_date', 'dt', 'n.nid=dt.entity_id');
  40. $query->addJoin('INNER', 'node_field_data', 'field', 'n.nid=field.nid');
  41. $query ->addField('n', 'nid');
  42. $query->addExpression("CAST(UNIX_TIMESTAMP(field_tx_date_value) as UNSIGNED)", 'ts');
  43. $query->addExpression("ABS(cast(UNIX_TIMESTAMP(field_tx_date_value) as signed) - $time)", 'dist');
  44. $query->condition('field.status', '1');
  45. $query->condition('n.type', $arg, 'IN');
  46. $query->where('CAST(UNIX_TIMESTAMP(dt.field_tx_date_value) as UNSIGNED) > :from', array('from' => $time - 3600*24*$days));
  47. $query->where('CAST(UNIX_TIMESTAMP(dt.field_tx_date_value) as UNSIGNED) < :to', array('to' => $time + 3600*24*$days));
  48. $query->orderBy('dist', 'ASC');
  49. if($limit > 0) {
  50. $query->range(0, $limit);
  51. }
  52. if($query->preExecute() !== TRUE) {
  53. return null;
  54. }
  55. $nids = $query->execute()->fetchCol(0);
  56. return $nids;
  57. }
  58. private function presentNids(array $nids) {
  59. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  60. $data = ['nodes' => []];
  61. foreach($nodes_e as $nid => $node) {
  62. $nodedata = $this->nodeToLinkdata($node);
  63. array_push($data['nodes'], $nodedata);
  64. }
  65. return $data;
  66. }
  67. public function relatedTime() {
  68. $nid = \Drupal::request()->query->get('nid');
  69. if($nid == null) {
  70. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  71. }
  72. if(!is_numeric($nid)) {
  73. return new Response("NID must be integer, not `$nid`", 400, array('Content-Type' => 'text/plain'));
  74. }
  75. $nid = intval($nid);
  76. $node = \Drupal\node\Entity\Node::load($nid);
  77. if($node == null) {
  78. return new Response("Node not found", 400, array('Content-Type' => 'text/plain'));
  79. }
  80. $time = intval($node->getCreatedTime());
  81. $nearnodes = $this->presentNids(array_filter(
  82. $this->queryNearInTime($time, $days=4, $limit=20),
  83. function ($n) use ($nid): bool { return intval($n) != $nid; }));
  84. $resp = $this->cachedJsonResp(array('time' => $nearnodes));
  85. return $resp;
  86. }
  87. public function relatedTrx() {
  88. // TODO: get node time
  89. // TODO: queryNearInTime($time)
  90. $nid = \Drupal::request()->query->get('nid');
  91. if($nid == null) {
  92. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  93. }
  94. $content = "asd $nid";
  95. $resp = new Response($content, 500, array( 'Content-Type' => 'text/plain'));
  96. return $resp;
  97. }
  98. private function getTermWeight(int $termid): int {
  99. $term = \Drupal\node\Entity\Term::load($termid);
  100. $query = \Drupal::database()->select('taxonomy_index', 'ti');
  101. $query->fields('ti', ['nid']);
  102. $query->condition('ti.tid', $term_id);
  103. $cnt = $query->execute()->rowCount();
  104. if($cnt < 100) {
  105. return 2;
  106. }
  107. return 1;
  108. }
  109. private function getSimilarity(array $orig, array $other): int {
  110. $orig_tags = array_map(function($t) { return $t['target_id']; }, $orig['field_tags']);
  111. $other_tags = array_map(function($t) { return $t['target_id']; }, $other['field_tags']);
  112. $both = array_intersect($orig_tags, $other_tags);
  113. $weighted = array_map($this->getTermWeight, $both);
  114. return array_sum($weighted);
  115. }
  116. private function cachedJsonResp(array $data): Response {
  117. $resp = new JsonResponse($data, 200, ['cache-control' => 'public, max-age=900']);
  118. return $resp;
  119. }
  120. public function relatedTopic() {
  121. // TODO: get node time
  122. // TODO: queryNearInTime($time)
  123. $nid = \Drupal::request()->query->get('nid');
  124. if($nid == null) {
  125. return new Response("Must supply a NID", 400, array('Content-Type' => 'text/plain'));
  126. }
  127. if(!is_numeric($nid)) {
  128. return new Response("NID must be integer, not `$nid`", 400, array('Content-Type' => 'text/plain'));
  129. }
  130. $nid = intval($nid);
  131. $node = \Drupal\node\Entity\Node::load($nid);
  132. if($node == null) {
  133. return new Response("Node not found", 400, array('Content-Type' => 'text/plain'));
  134. }
  135. $orig_arr = $node->toArray();
  136. $time = intval($node->getCreatedTime());
  137. $goodnids = [];
  138. $scores = [];
  139. $other_e = \Drupal\node\Entity\Node::loadMultiple($this->queryNearInTime($time, $days=30, $limit=100));
  140. foreach($other_e as $other_nid => $other_node) {
  141. if(intval($other_nid) === $nid) {
  142. continue;
  143. }
  144. $score = $this->getSimilarity($orig_arr, $other_node->toArray());
  145. if($score > 0) {
  146. $scores[$other_nid] = $score;
  147. }
  148. }
  149. arsort($scores);
  150. foreach($scores as $nid => $score) {
  151. array_push($goodnids, $nid);
  152. }
  153. $resp = $this->cachedJsonResp(['topic' => $this->presentNids($goodnids)]);
  154. return $resp;
  155. }
  156. }