GithubIssueBridge.php 6.0 KB

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