ApiIndexer.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\apidoc\helpers;
  8. use cebe\jssearch\Indexer;
  9. use cebe\jssearch\tokenizer\StandardTokenizer;
  10. use cebe\jssearch\TokenizerInterface;
  11. use yii\helpers\StringHelper;
  12. /**
  13. * ApiIndexer indexes framework API
  14. */
  15. class ApiIndexer extends Indexer
  16. {
  17. /**
  18. * @param string $file
  19. * @param string $contents
  20. * @param string $basePath
  21. * @param string $baseUrl
  22. * @return array
  23. */
  24. protected function generateFileInfo($file, $contents, $basePath, $baseUrl)
  25. {
  26. // create file entry
  27. if (preg_match('~<h1>(.*?)</h1>~s', $contents, $matches)) {
  28. $title = str_replace('&para;', '', strip_tags($matches[1]));
  29. } elseif (preg_match('~<title>(.*?)</title>~s', $contents, $matches)) {
  30. $title = strip_tags($matches[1]);
  31. } else {
  32. $title = '<i>No title</i>';
  33. }
  34. if (preg_match('~<div id="classDescription">\s*<strong>(.*?)</strong>~s', $contents, $matches)) {
  35. $description = strip_tags($matches[1]);
  36. } elseif (preg_match('~<p>(.*?)</p>~s', $contents, $matches)) {
  37. $description = StringHelper::truncate(strip_tags($matches[1]), 1000, '...', 'UTF-8');
  38. } else {
  39. $description = '';
  40. }
  41. return [
  42. 'u' => $baseUrl . str_replace('\\', '/', substr($file, strlen(rtrim($basePath, '\\/')))),
  43. 't' => $title,
  44. 'd' => $description,
  45. ];
  46. }
  47. /**
  48. * @return TokenizerInterface
  49. */
  50. public function getTokenizer()
  51. {
  52. $tokenizer = parent::getTokenizer();
  53. if ($tokenizer instanceof StandardTokenizer) {
  54. // yii is part of every doc and makes weird search results
  55. $tokenizer->stopWords[] = 'yii';
  56. $tokenizer->stopWords = array_unique($tokenizer->stopWords);
  57. }
  58. return $tokenizer;
  59. }
  60. }