GithubIssueBridge.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. class GithubIssueBridge extends BridgeAbstract {
  3. const MAINTAINER = 'Pierre Mazière';
  4. const NAME = 'Github Issue';
  5. const URI = 'https://github.com/';
  6. const CACHE_TIMEOUT = 600; // 10min
  7. const DESCRIPTION = 'Returns the issues or comments of an issue of a github project';
  8. const PARAMETERS = array(
  9. 'global' => array(
  10. 'u' => array(
  11. 'name' => 'User name',
  12. 'required' => true
  13. ),
  14. 'p' => array(
  15. 'name' => 'Project name',
  16. 'required' => true
  17. )
  18. ),
  19. 'Project Issues' => array(
  20. 'c' => array(
  21. 'name' => 'Show Issues Comments',
  22. 'type' => 'checkbox'
  23. )
  24. ),
  25. 'Issue comments' => array(
  26. 'i' => array(
  27. 'name' => 'Issue number',
  28. 'type' => 'number',
  29. 'required' => 'true'
  30. )
  31. )
  32. );
  33. public function getName(){
  34. $name = $this->getInput('u') . '/' . $this->getInput('p');
  35. switch($this->queriedContext){
  36. case 'Project Issues':
  37. if($this->getInput('c')){
  38. $prefix = static::NAME . 's comments for ';
  39. } else {
  40. $prefix = static::NAME . 's for ';
  41. }
  42. $name = $prefix . $name;
  43. break;
  44. case 'Issue comments':
  45. $name = static::NAME . ' ' . $name . ' #' . $this->getInput('i');
  46. break;
  47. }
  48. return $name;
  49. }
  50. public function getURI(){
  51. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues';
  52. if($this->queriedContext === 'Issue comments'){
  53. $uri .= '/' . $this->getInput('i');
  54. } elseif($this->getInput('c')){
  55. $uri .= '?q=is%3Aissue+sort%3Aupdated-desc';
  56. }
  57. return $uri;
  58. }
  59. protected function extractIssueComment($issueNbr, $title, $comment){
  60. $class = $comment->getAttribute('class');
  61. $classes = explode(' ', $class);
  62. $event = false;
  63. if(in_array('discussion-item', $classes)){
  64. $event = true;
  65. }
  66. $author = 'unknown';
  67. if($comment->find('.author', 0)){
  68. $author = $comment->find('.author', 0)->plaintext;
  69. }
  70. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues/' . $issueNbr;
  71. $comment = $comment->firstChild();
  72. if(!$event){
  73. $comment = $comment->nextSibling();
  74. }
  75. if($event){
  76. $title .= ' / ' . substr($class, strpos($class, 'discussion-item-') + strlen('discussion-item-'));
  77. if(!$comment->hasAttribute('id')){
  78. $items = array();
  79. $timestamp = strtotime($comment->find('relative-time', 0)->getAttribute('datetime'));
  80. $content = $comment->innertext;
  81. while($comment = $comment->nextSibling()){
  82. $item = array();
  83. $item['author'] = $author;
  84. $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  85. $item['timestamp'] = $timestamp;
  86. $item['content'] = $content . '<p>' . $comment->children(1)->innertext . '</p>';
  87. $item['uri'] = $uri . '#' . $comment->children(1)->getAttribute('id');
  88. $items[] = $item;
  89. }
  90. return $items;
  91. }
  92. $content = $comment->parent()->innertext;
  93. } else {
  94. $title .= ' / ' . trim($comment->firstChild()->plaintext);
  95. $content = "<pre>" . $comment->find('.comment-body', 0)->innertext . "</pre>";
  96. }
  97. $item = array();
  98. $item['author'] = $author;
  99. $item['uri'] = $uri . '#' . $comment->getAttribute('id');
  100. $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  101. $item['timestamp'] = strtotime($comment->find('relative-time', 0)->getAttribute('datetime'));
  102. $item['content'] = $content;
  103. return $item;
  104. }
  105. protected function extractIssueComments($issue){
  106. $items = array();
  107. $title = $issue->find('.gh-header-title', 0)->plaintext;
  108. $issueNbr = trim(substr($issue->find('.gh-header-number', 0)->plaintext, 1));
  109. $comments = $issue->find('.js-discussion', 0);
  110. foreach($comments->children() as $comment){
  111. $classes = explode(' ', $comment->getAttribute('class'));
  112. if(in_array('discussion-item', $classes)
  113. || in_array('timeline-comment-wrapper', $classes)){
  114. $item = $this->extractIssueComment($issueNbr, $title, $comment);
  115. if(array_keys($item) !== range(0, count($item) - 1)){
  116. $item = array($item);
  117. }
  118. $items = array_merge($items, $item);
  119. }
  120. }
  121. return $items;
  122. }
  123. public function collectData(){
  124. $html = getSimpleHTMLDOM($this->getURI())
  125. or returnServerError('No results for Github Issue ' . $this->getURI());
  126. switch($this->queriedContext){
  127. case 'Issue comments':
  128. $this->items = $this->extractIssueComments($html);
  129. break;
  130. case 'Project Issues':
  131. foreach($html->find('.js-active-navigation-container .js-navigation-item') as $issue){
  132. $info = $issue->find('.opened-by', 0);
  133. $issueNbr = substr(trim($info->plaintext), 1, strpos(trim($info->plaintext), ' '));
  134. $item = array();
  135. $item['content'] = '';
  136. if($this->getInput('c')){
  137. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues/' . $issueNbr;
  138. $issue = getSimpleHTMLDOMCached($uri, static::CACHE_TIMEOUT);
  139. if($issue){
  140. $this->items = array_merge($this->items, $this->extractIssueComments($issue));
  141. continue;
  142. }
  143. $item['content'] = 'Can not extract comments from ' . $uri;
  144. }
  145. $item['author'] = $info->find('a', 0)->plaintext;
  146. $item['timestamp'] = strtotime($info->find('relative-time', 0)->getAttribute('datetime'));
  147. $item['title'] = html_entity_decode(
  148. $issue->find('.js-navigation-open', 0)->plaintext,
  149. ENT_QUOTES,
  150. 'UTF-8'
  151. );
  152. $comments = $issue->find('.col-5', 0)->plaintext;
  153. $item['content'] .= "\n" . 'Comments: ' . ($comments ? $comments : '0');
  154. $item['uri'] = self::URI . $issue->find('.js-navigation-open', 0)->getAttribute('href');
  155. $this->items[] = $item;
  156. }
  157. break;
  158. }
  159. array_walk($this->items, function(&$item){
  160. $item['content'] = preg_replace('/\s+/', ' ', $item['content']);
  161. $item['content'] = str_replace('href="/', 'href="' . static::URI, $item['content']);
  162. $item['content'] = str_replace(
  163. 'href="#',
  164. 'href="' . substr($item['uri'], 0, strpos($item['uri'], '#') + 1),
  165. $item['content']
  166. );
  167. $item['title'] = preg_replace('/\s+/', ' ', $item['title']);
  168. });
  169. }
  170. }