/lib/core/Search/ContentSource/FileSource.php

https://gitlab.com/ElvisAns/tiki · PHP · 151 lines · 124 code · 21 blank · 6 comment · 6 complexity · 5527ef84173ec45628c52d0a20719065 MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. class Search_ContentSource_FileSource implements Search_ContentSource_Interface, Tiki_Profile_Writer_ReferenceProvider
  8. {
  9. private $db;
  10. public function __construct()
  11. {
  12. $this->db = TikiDb::get();
  13. }
  14. public function getReferenceMap()
  15. {
  16. return [
  17. 'gallery_id' => 'file_gallery',
  18. ];
  19. }
  20. public function getDocuments()
  21. {
  22. $files = $this->db->table('tiki_files');
  23. return $files->fetchColumn(
  24. 'fileId',
  25. [
  26. 'archiveId' => 0,
  27. ],
  28. -1,
  29. -1,
  30. 'ASC'
  31. );
  32. }
  33. public function getDocument($objectId, Search_Type_Factory_Interface $typeFactory)
  34. {
  35. global $prefs;
  36. $filegallib = Tikilib::lib('filegal');
  37. $file = $filegallib->get_file_info($objectId, true, false);
  38. if (! $file) {
  39. return false;
  40. }
  41. if (! empty($file['name'])) {
  42. // Many files when uploaded have underscore in the file name and makes search difficult
  43. $file['name'] = str_replace('_', ' ', $file['name']);
  44. }
  45. $data = [
  46. 'title' => $typeFactory->sortable(empty($file['name']) ? $file['filename'] : $file['name']),
  47. 'title_unstemmed' => $typeFactory->simpletext(empty($file['name']) ? $file['filename'] : $file['name']),
  48. 'language' => $typeFactory->identifier('unknown'),
  49. 'creation_date' => $typeFactory->timestamp($file['created']),
  50. 'modification_date' => $typeFactory->timestamp($file['lastModif']),
  51. 'date' => $typeFactory->timestamp($file['created']),
  52. 'contributors' => $typeFactory->multivalue(array_unique([$file['author'], $file['user'], $file['lastModifUser']])),
  53. 'description' => $typeFactory->plaintext($file['description']),
  54. 'filename' => $typeFactory->identifier($file['filename']),
  55. 'filetype' => $typeFactory->sortable(preg_replace('/^([\w-]+)\/([\w-]+).*$/', '$1/$2', $file['filetype'])),
  56. 'filesize' => $typeFactory->plaintext($file['filesize']),
  57. 'hits' => $typeFactory->numeric($file['hits']),
  58. 'gallery_id' => $typeFactory->identifier($file['galleryId']),
  59. 'file_comment' => $typeFactory->plaintext($file['comment']),
  60. 'file_content' => $typeFactory->plaintext($file['search_data']),
  61. 'ocr_content' => $typeFactory->plaintext($file['ocr_data']),
  62. 'parent_object_type' => $typeFactory->identifier('file gallery'),
  63. 'parent_object_id' => $typeFactory->identifier($file['galleryId']),
  64. 'view_permission' => $typeFactory->identifier('tiki_p_download_files'),
  65. ];
  66. if ($prefs['fgal_enable_email_indexing'] === 'y' && $file['filetype'] == 'message/rfc822') {
  67. $file_object = Tiki\FileGallery\File::id($file['fileId']);
  68. $parsed_fields = (new Tiki\FileGallery\Manipulator\EmailParser($file_object))->run();
  69. if ($parsed_fields) {
  70. $data += [
  71. 'email_subject' => $typeFactory->plaintext($parsed_fields['subject']),
  72. 'email_from' => $typeFactory->plaintext($parsed_fields['from']),
  73. 'email_sender' => $typeFactory->plaintext($parsed_fields['sender']),
  74. 'email_recipient' => $typeFactory->plaintext($parsed_fields['recipient']),
  75. 'email_date' => $typeFactory->timestamp($parsed_fields['date']),
  76. 'email_content_type' => $typeFactory->plaintext($parsed_fields['content_type']),
  77. 'email_body' => $typeFactory->plainmediumtext($parsed_fields['body']),
  78. 'email_plaintext' => $typeFactory->plainmediumtext($parsed_fields['plaintext']),
  79. 'email_html' => $typeFactory->plainmediumtext($parsed_fields['html']),
  80. ];
  81. }
  82. }
  83. return $data;
  84. }
  85. public function getProvidedFields()
  86. {
  87. return [
  88. 'title',
  89. 'title_unstemmed',
  90. 'language',
  91. 'creation_date',
  92. 'modification_date',
  93. 'date',
  94. 'contributors',
  95. 'description',
  96. 'filename',
  97. 'filetype',
  98. 'filesize',
  99. 'hits',
  100. 'gallery_id',
  101. 'file_comment',
  102. 'file_content',
  103. 'ocr_content',
  104. 'view_permission',
  105. 'parent_object_id',
  106. 'parent_object_type',
  107. 'email_subject',
  108. 'email_from',
  109. 'email_sender',
  110. 'email_recipient',
  111. 'email_date',
  112. 'email_content_type',
  113. 'email_body',
  114. 'email_plaintext',
  115. 'email_html',
  116. ];
  117. }
  118. public function getGlobalFields()
  119. {
  120. return [
  121. 'title' => true,
  122. 'description' => true,
  123. 'date' => true,
  124. 'filename' => true,
  125. 'file_comment' => false,
  126. 'file_content' => false,
  127. 'ocr_content' => false,
  128. ];
  129. }
  130. }