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
8class Search_ContentSource_FileSource implements Search_ContentSource_Interface, Tiki_Profile_Writer_ReferenceProvider
9{
10	private $db;
11
12	function __construct()
13	{
14		$this->db = TikiDb::get();
15	}
16
17	function getReferenceMap()
18	{
19		return [
20			'gallery_id' => 'file_gallery',
21		];
22	}
23
24	function getDocuments()
25	{
26		$files = $this->db->table('tiki_files');
27		return $files->fetchColumn(
28			'fileId',
29			[
30				'archiveId' => 0,
31			],
32			-1,
33			-1,
34			'ASC'
35		);
36	}
37
38	function getDocument($objectId, Search_Type_Factory_Interface $typeFactory)
39	{
40		$filegallib = Tikilib::lib('filegal');
41
42		$file = $filegallib->get_file_info($objectId, true, false);
43
44		if (! $file) {
45			return false;
46		}
47
48		if (! empty($file['name'])) {
49			// Many files when uploaded have underscore in the file name and makes search difficult
50			$file['name'] = str_replace('_', ' ', $file['name']);
51		}
52
53		$data = [
54			'title' => $typeFactory->sortable(empty($file['name']) ? $file['filename'] : $file['name']),
55			'title_unstemmed' => $typeFactory->simpletext(empty($file['name']) ? $file['filename'] : $file['name']),
56			'language' => $typeFactory->identifier('unknown'),
57			'creation_date' => $typeFactory->timestamp($file['created']),
58			'modification_date' => $typeFactory->timestamp($file['lastModif']),
59			'date' => $typeFactory->timestamp($file['created']),
60			'contributors' => $typeFactory->multivalue(array_unique([$file['author'], $file['user'], $file['lastModifUser']])),
61			'description' => $typeFactory->plaintext($file['description']),
62			'filename' => $typeFactory->identifier($file['filename']),
63			'filetype' => $typeFactory->sortable(preg_replace('/^([\w-]+)\/([\w-]+).*$/', '$1/$2', $file['filetype'])),
64			'filesize' => $typeFactory->plaintext($file['filesize']),
65			'hits' => $typeFactory->numeric($file['hits']),
66
67			'gallery_id' => $typeFactory->identifier($file['galleryId']),
68			'file_comment' => $typeFactory->plaintext($file['comment']),
69			'file_content' => $typeFactory->plaintext($file['search_data']),
70			'ocr_content' => $typeFactory->plaintext($file['ocr_data']),
71
72			'parent_object_type' => $typeFactory->identifier('file gallery'),
73			'parent_object_id' => $typeFactory->identifier($file['galleryId']),
74			'parent_view_permission' => $typeFactory->identifier('tiki_p_download_files'),
75		];
76
77		return $data;
78	}
79
80	function getProvidedFields()
81	{
82		return [
83			'title',
84			'title_unstemmed',
85			'language',
86			'creation_date',
87			'modification_date',
88			'date',
89			'contributors',
90			'description',
91			'filename',
92			'filetype',
93			'filesize',
94			'hits',
95
96			'gallery_id',
97			'file_comment',
98			'file_content',
99			'ocr_content',
100
101			'parent_view_permission',
102			'parent_object_id',
103			'parent_object_type',
104		];
105	}
106
107	function getGlobalFields()
108	{
109		return [
110			'title' => true,
111			'description' => true,
112			'date' => true,
113			'filename' => true,
114
115			'file_comment' => false,
116			'file_content' => false,
117			'ocr_content' => false,
118		];
119	}
120}
121