GithubIssueBridge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. default: return parent::getName();
  48. }
  49. return $name;
  50. }
  51. public function getURI(){
  52. if(!is_null($this->getInput('u')) && !is_null($this->getInput('p'))) {
  53. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues';
  54. if($this->queriedContext === 'Issue comments') {
  55. $uri .= '/' . $this->getInput('i');
  56. } elseif($this->getInput('c')) {
  57. $uri .= '?q=is%3Aissue+sort%3Aupdated-desc';
  58. }
  59. return $uri;
  60. }
  61. return parent::getURI();
  62. }
  63. protected function extractIssueComment($issueNbr, $title, $comment){
  64. $class = $comment->getAttribute('class');
  65. $classes = explode(' ', $class);
  66. $event = false;
  67. if(in_array('discussion-item', $classes)) {
  68. $event = true;
  69. }
  70. $author = 'unknown';
  71. if($comment->find('.author', 0)) {
  72. $author = $comment->find('.author', 0)->plaintext;
  73. }
  74. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues/' . $issueNbr;
  75. $comment = $comment->firstChild();
  76. if(!$event) {
  77. $comment = $comment->nextSibling();
  78. }
  79. if($event) {
  80. $title .= ' / ' . substr($class, strpos($class, 'discussion-item-') + strlen('discussion-item-'));
  81. if(!$comment->hasAttribute('id')) {
  82. $items = array();
  83. $timestamp = strtotime($comment->find('relative-time', 0)->getAttribute('datetime'));
  84. $content = $comment->innertext;
  85. while($comment = $comment->nextSibling()) {
  86. $item = array();
  87. $item['author'] = $author;
  88. $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  89. $item['timestamp'] = $timestamp;
  90. $item['content'] = $content . '<p>' . $comment->children(1)->innertext . '</p>';
  91. $item['uri'] = $uri . '#' . $comment->children(1)->getAttribute('id');
  92. $items[] = $item;
  93. }
  94. return $items;
  95. }
  96. $content = $comment->parent()->innertext;
  97. } else {
  98. $title .= ' / ' . trim($comment->firstChild()->plaintext);
  99. $content = '<pre>' . $comment->find('.comment-body', 0)->innertext . '</pre>';
  100. }
  101. $item = array();
  102. $item['author'] = $author;
  103. $item['uri'] = $uri . '#' . $comment->getAttribute('id');
  104. $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  105. $item['timestamp'] = strtotime($comment->find('relative-time', 0)->getAttribute('datetime'));
  106. $item['content'] = $content;
  107. return $item;
  108. }
  109. protected function extractIssueComments($issue){
  110. $items = array();
  111. $title = $issue->find('.gh-header-title', 0)->plaintext;
  112. $issueNbr = trim(substr($issue->find('.gh-header-number', 0)->plaintext, 1));
  113. $comments = $issue->find('.js-discussion', 0);
  114. foreach($comments->children() as $comment) {
  115. $classes = explode(' ', $comment->getAttribute('class'));
  116. if(in_array('discussion-item', $classes)
  117. || in_array('timeline-comment-wrapper', $classes)) {
  118. $item = $this->extractIssueComment($issueNbr, $title, $comment);
  119. if(array_keys($item) !== range(0, count($item) - 1)) {
  120. $item = array($item);
  121. }
  122. $items = array_merge($items, $item);
  123. }
  124. }
  125. return $items;
  126. }
  127. public function collectData(){
  128. $html = getSimpleHTMLDOM($this->getURI())
  129. or returnServerError('No results for Github Issue ' . $this->getURI());
  130. switch($this->queriedContext) {
  131. case 'Issue comments':
  132. $this->items = $this->extractIssueComments($html);
  133. break;
  134. case 'Project Issues':
  135. foreach($html->find('.js-active-navigation-container .js-navigation-item') as $issue) {
  136. $info = $issue->find('.opened-by', 0);
  137. $issueNbr = substr(trim($info->plaintext), 1, strpos(trim($info->plaintext), ' '));
  138. $item = array();
  139. $item['content'] = '';
  140. if($this->getInput('c')) {
  141. $uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p') . '/issues/' . $issueNbr;
  142. $issue = getSimpleHTMLDOMCached($uri, static::CACHE_TIMEOUT);
  143. if($issue) {
  144. $this->items = array_merge($this->items, $this->extractIssueComments($issue));
  145. continue;
  146. }
  147. $item['content'] = 'Can not extract comments from ' . $uri;
  148. }
  149. $item['author'] = $info->find('a', 0)->plaintext;
  150. $item['timestamp'] = strtotime($info->find('relative-time', 0)->getAttribute('datetime'));
  151. $item['title'] = html_entity_decode(
  152. $issue->find('.js-navigation-open', 0)->plaintext,
  153. ENT_QUOTES,
  154. 'UTF-8'
  155. );
  156. $comments = $issue->find('.col-5', 0)->plaintext;
  157. $item['content'] .= "\n" . 'Comments: ' . ($comments ? $comments : '0');
  158. $item['uri'] = self::URI . $issue->find('.js-navigation-open', 0)->getAttribute('href');
  159. $this->items[] = $item;
  160. }
  161. break;
  162. }
  163. array_walk($this->items, function(&$item){
  164. $item['content'] = preg_replace('/\s+/', ' ', $item['content']);
  165. $item['content'] = str_replace('href="/', 'href="' . static::URI, $item['content']);
  166. $item['content'] = str_replace(
  167. 'href="#',
  168. 'href="' . substr($item['uri'], 0, strpos($item['uri'], '#') + 1),
  169. $item['content']
  170. );
  171. $item['title'] = preg_replace('/\s+/', ' ', $item['title']);
  172. });
  173. }
  174. }