GithubIssueBridge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 DESCRIPTION = 'Returns the issues or comments of an issue of a github project';
  7. const PARAMETERS=array(
  8. 'global'=>array (
  9. 'u'=>array(
  10. 'name'=>'User name',
  11. 'required'=>true
  12. ),
  13. 'p'=>array(
  14. 'name'=>'Project name',
  15. 'required'=>true
  16. )
  17. ),
  18. 'Project Issues'=>array(
  19. 'c'=>array(
  20. 'name'=>'Show Issues Comments',
  21. 'type'=>'checkbox'
  22. )
  23. ),
  24. 'Issue comments'=>array(
  25. 'i'=>array(
  26. 'name'=>'Issue number',
  27. 'type'=>'number',
  28. 'required'=>'true'
  29. )
  30. )
  31. );
  32. public function getName(){
  33. $name=$this->getInput('u').'/'.$this->getInput('p');
  34. switch($this->queriedContext){
  35. case 'Project Issues':
  36. if($this->getInput('c')){
  37. $prefix=static::NAME.'s comments for ';
  38. }else{
  39. $prefix=static::NAME.'s for ';
  40. }
  41. $name=$prefix.$name;
  42. break;
  43. case 'Issue comments':
  44. $name=static::NAME.' '.$name.' #'.$this->getInput('i');
  45. break;
  46. }
  47. return $name;
  48. }
  49. public function getURI(){
  50. $uri = static::URI.$this->getInput('u').'/'.$this->getInput('p').'/issues';
  51. if($this->queriedContext==='Issue comments'){
  52. $uri.='/'.$this->getInput('i');
  53. }else if($this->getInput('c')){
  54. $uri.='?q=is%3Aissue+sort%3Aupdated-desc';
  55. }
  56. return $uri;
  57. }
  58. protected function extractIssueComment($issueNbr,$title,$comment){
  59. $class=$comment->getAttribute('class');
  60. $classes=explode(' ',$class);
  61. $event=false;
  62. if(in_array('discussion-item',$classes)){
  63. $event=true;
  64. }
  65. $author='unknown';
  66. if($comment->find('.author',0)){
  67. $author=$comment->find('.author',0)->plaintext;
  68. }
  69. $uri=static::URI.$this->getInput('u').'/'.$this->getInput('p').'/issues/'
  70. .$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. ){
  115. $item=$this->extractIssueComment($issueNbr,$title,$comment);
  116. if(array_keys($item)!==range(0,count($item)-1)){
  117. $item=array($item);
  118. }
  119. $items=array_merge($items,$item);
  120. }
  121. }
  122. return $items;
  123. }
  124. public function collectData(){
  125. $html = getSimpleHTMLDOM($this->getURI())
  126. or returnServerError('No results for Github Issue '.$this->getURI());
  127. switch($this->queriedContext){
  128. case 'Issue comments':
  129. $this->items=$this->extractIssueComments($html);
  130. break;
  131. case 'Project Issues':
  132. foreach($html->find('.js-active-navigation-container .js-navigation-item') as $issue){
  133. $info=$issue->find('.opened-by',0);
  134. $issueNbr=substr(trim($info->plaintext),1,strpos(trim($info->plaintext),' '));
  135. $item=array();
  136. $item['content']='';
  137. if($this->getInput('c')){
  138. $uri=static::URI.$this->getInput('u').'/'.$this->getInput('p').'/issues/'.$issueNbr;
  139. $issue=getSimpleHTMLDOMCached($uri,1800);
  140. if($issue){
  141. $this->items=array_merge($this->items,$this->extractIssueComments($issue));
  142. continue;
  143. }
  144. $item['content']='Can not extract comments from '.$uri;
  145. }
  146. $item['author']=$info->find('a',0)->plaintext;
  147. $item['timestamp']=strtotime($info->find('relative-time',0)->getAttribute('datetime'));
  148. $item['title']=html_entity_decode(
  149. $issue->find('.js-navigation-open',0)->plaintext,
  150. ENT_QUOTES,
  151. 'UTF-8'
  152. );
  153. $comments=$issue->find('.col-5',0)->plaintext;
  154. $item['content'].="\n".'Comments: '.($comments?$comments:'0');
  155. $item['uri']=self::URI.$issue->find('.js-navigation-open',0)->getAttribute('href');
  156. $this->items[]=$item;
  157. }
  158. break;
  159. }
  160. array_walk($this->items, function(&$item){
  161. $item['content']=preg_replace('/\s+/',' ',$item['content']);
  162. $item['content']=str_replace('href="/','href="'.static::URI,$item['content']);
  163. $item['content']=str_replace(
  164. 'href="#',
  165. 'href="'.substr($item['uri'],0,strpos($item['uri'],'#')+1),
  166. $item['content']
  167. );
  168. $item['title']=preg_replace('/\s+/',' ',$item['title']);
  169. });
  170. }
  171. public function getCacheDuration(){
  172. return 600; // ten minutes
  173. }
  174. }