IndexDocument.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\FullTextSearch\Model;
  25. use JsonSerializable;
  26. use OCP\FullTextSearch\Exceptions\FullTextSearchIndexNotAvailableException;
  27. use OCP\FullTextSearch\Model\IDocumentAccess;
  28. use OCP\FullTextSearch\Model\IIndex;
  29. use OCP\FullTextSearch\Model\IIndexDocument;
  30. /**
  31. * Class IndexDocument
  32. *
  33. * This is one of the main class of the FullTextSearch, used as a data transfer
  34. * object. An IndexDocument is created to manage documents around FullTextSearch,
  35. * during an index and during a search.
  36. * The uniqueness of an IndexDocument is made by the Id of the Content Provider
  37. * and the Id of the original document within the Content Provider.
  38. *
  39. * We will call original document the source from which the IndexDocument is
  40. * generated. As an example, an original document can be a file, a mail, ...
  41. *
  42. * @since 15.0.0
  43. *
  44. * @package OC\FullTextSearch\Model
  45. */
  46. class IndexDocument implements IIndexDocument, JsonSerializable {
  47. protected string $id = '';
  48. protected DocumentAccess $access;
  49. protected ?IIndex $index = null;
  50. protected int $modifiedTime = 0;
  51. protected string $source = '';
  52. protected array $tags = [];
  53. protected array $metaTags = [];
  54. protected array $subTags = [];
  55. protected string $title = '';
  56. protected string $content = '';
  57. protected string $hash = '';
  58. protected array $parts = [];
  59. protected string $link = '';
  60. protected array $more = [];
  61. protected array $excerpts = [];
  62. protected string $score = '';
  63. protected array $info = [];
  64. protected int $contentEncoded = 0;
  65. /**
  66. * IIndexDocument constructor.
  67. *
  68. * On creation, we assure the uniqueness of the object using the providerId
  69. * and the Id of the original document.
  70. *
  71. * @since 15.0.0
  72. */
  73. public function __construct(
  74. protected string $providerId,
  75. string $documentId,
  76. ) {
  77. $this->id = $documentId;
  78. }
  79. /**
  80. * Returns the Id of the original document.
  81. *
  82. * @since 15.0.0
  83. */
  84. final public function getId(): string {
  85. return $this->id;
  86. }
  87. /**
  88. * Returns the Id of the provider.
  89. *
  90. * @since 15.0.0
  91. */
  92. final public function getProviderId(): string {
  93. return $this->providerId;
  94. }
  95. /**
  96. * Set the Index related to the IIndexDocument.
  97. *
  98. * @see IIndex
  99. *
  100. * @since 15.0.0
  101. */
  102. final public function setIndex(IIndex $index): IIndexDocument {
  103. $this->index = $index;
  104. return $this;
  105. }
  106. /**
  107. * Get the Index.
  108. *
  109. * @throws FullTextSearchIndexNotAvailableException
  110. * @since 15.0.0
  111. */
  112. final public function getIndex(): IIndex {
  113. if ($this->index === null) {
  114. throw new FullTextSearchIndexNotAvailableException('No IIndex generated');
  115. }
  116. return $this->index;
  117. }
  118. /**
  119. * return if Index is defined.
  120. *
  121. * @since 16.0.0
  122. */
  123. final public function hasIndex(): bool {
  124. return $this->index !== null;
  125. }
  126. /**
  127. * Set the modified time of the original document.
  128. *
  129. * @since 15.0.0
  130. */
  131. final public function setModifiedTime(int $modifiedTime): IIndexDocument {
  132. $this->modifiedTime = $modifiedTime;
  133. return $this;
  134. }
  135. /**
  136. * Get the modified time of the original document.
  137. *
  138. * @since 15.0.0
  139. */
  140. final public function getModifiedTime(): int {
  141. return $this->modifiedTime;
  142. }
  143. /**
  144. * Check if the original document of the IIndexDocument is older than $time.
  145. *
  146. * @since 15.0.0
  147. */
  148. final public function isOlderThan(int $time): bool {
  149. return ($this->modifiedTime < $time);
  150. }
  151. /**
  152. * Set the read rights of the original document using a IDocumentAccess.
  153. *
  154. * @see IDocumentAccess
  155. *
  156. * @since 15.0.0
  157. */
  158. final public function setAccess(IDocumentAccess $access): IIndexDocument {
  159. $this->access = $access;
  160. return $this;
  161. }
  162. /**
  163. * Get the IDocumentAccess related to the original document.
  164. *
  165. * @since 15.0.0
  166. */
  167. final public function getAccess(): IDocumentAccess {
  168. return $this->access;
  169. }
  170. /**
  171. * Add a tag to the list.
  172. *
  173. * @since 15.0.0
  174. */
  175. final public function addTag(string $tag): IIndexDocument {
  176. $this->tags[] = $tag;
  177. return $this;
  178. }
  179. /**
  180. * Set the list of tags assigned to the original document.
  181. *
  182. * @since 15.0.0
  183. */
  184. final public function setTags(array $tags): IIndexDocument {
  185. $this->tags = $tags;
  186. return $this;
  187. }
  188. /**
  189. * Get the list of tags assigned to the original document.
  190. *
  191. * @since 15.0.0
  192. */
  193. final public function getTags(): array {
  194. return $this->tags;
  195. }
  196. /**
  197. * Add a meta tag to the list.
  198. *
  199. * @since 15.0.0
  200. */
  201. final public function addMetaTag(string $tag): IIndexDocument {
  202. $this->metaTags[] = $tag;
  203. return $this;
  204. }
  205. /**
  206. * Set the list of meta tags assigned to the original document.
  207. *
  208. * @since 15.0.0
  209. */
  210. final public function setMetaTags(array $tags): IIndexDocument {
  211. $this->metaTags = $tags;
  212. return $this;
  213. }
  214. /**
  215. * Get the list of meta tags assigned to the original document.
  216. *
  217. * @since 15.0.0
  218. */
  219. final public function getMetaTags(): array {
  220. return $this->metaTags;
  221. }
  222. /**
  223. * Add a sub tag to the list.
  224. *
  225. * @since 15.0.0
  226. */
  227. final public function addSubTag(string $sub, string $tag): IIndexDocument {
  228. if (!array_key_exists($sub, $this->subTags)) {
  229. $this->subTags[$sub] = [];
  230. }
  231. $this->subTags[$sub][] = $tag;
  232. return $this;
  233. }
  234. /**
  235. * Set the list of sub tags assigned to the original document.
  236. *
  237. * @since 15.0.0
  238. */
  239. final public function setSubTags(array $tags): IIndexDocument {
  240. $this->subTags = $tags;
  241. return $this;
  242. }
  243. /**
  244. * Get the list of sub tags assigned to the original document.
  245. * If $formatted is true, the result will be formatted in a one
  246. * dimensional array.
  247. *
  248. * @since 15.0.0
  249. */
  250. final public function getSubTags(bool $formatted = false): array {
  251. if ($formatted === false) {
  252. return $this->subTags;
  253. }
  254. $subTags = [];
  255. $ak = array_keys($this->subTags);
  256. foreach ($ak as $source) {
  257. $tags = $this->subTags[$source];
  258. foreach ($tags as $tag) {
  259. $subTags[] = $source . '_' . $tag;
  260. }
  261. }
  262. return $subTags;
  263. }
  264. /**
  265. * Set the source of the original document.
  266. *
  267. * @since 15.0.0
  268. */
  269. final public function setSource(string $source): IIndexDocument {
  270. $this->source = $source;
  271. return $this;
  272. }
  273. /**
  274. * Get the source of the original document.
  275. *
  276. * @since 15.0.0
  277. */
  278. final public function getSource(): string {
  279. return $this->source;
  280. }
  281. /**
  282. * Set the title of the original document.
  283. *
  284. * @since 15.0.0
  285. */
  286. final public function setTitle(string $title): IIndexDocument {
  287. $this->title = $title;
  288. return $this;
  289. }
  290. /**
  291. * Get the title of the original document.
  292. *
  293. * @since 15.0.0
  294. */
  295. final public function getTitle(): string {
  296. return $this->title;
  297. }
  298. /**
  299. * Set the content of the document.
  300. * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or
  301. * encoded in base64.
  302. *
  303. * @since 15.0.0
  304. */
  305. final public function setContent(string $content, int $encoded = 0): IIndexDocument {
  306. $this->content = $content;
  307. $this->contentEncoded = $encoded;
  308. return $this;
  309. }
  310. /**
  311. * Get the content of the original document.
  312. *
  313. * @since 15.0.0
  314. */
  315. final public function getContent(): string {
  316. return $this->content;
  317. }
  318. /**
  319. * Returns the type of the encoding on the content.
  320. *
  321. * @since 15.0.0
  322. */
  323. final public function isContentEncoded(): int {
  324. return $this->contentEncoded;
  325. }
  326. /**
  327. * Return the size of the content.
  328. *
  329. * @since 15.0.0
  330. */
  331. final public function getContentSize(): int {
  332. return strlen($this->getContent());
  333. }
  334. /**
  335. * Generate a hash, based on the content of the original document.
  336. *
  337. * @since 15.0.0
  338. */
  339. final public function initHash(): IIndexDocument {
  340. if ($this->getContent() === '' || is_null($this->getContent())) {
  341. return $this;
  342. }
  343. $this->hash = hash("md5", $this->getContent());
  344. return $this;
  345. }
  346. /**
  347. * Set the hash of the original document.
  348. *
  349. * @since 15.0.0
  350. */
  351. final public function setHash(string $hash): IIndexDocument {
  352. $this->hash = $hash;
  353. return $this;
  354. }
  355. /**
  356. * Get the hash of the original document.
  357. *
  358. * @since 15.0.0
  359. */
  360. final public function getHash(): string {
  361. return $this->hash;
  362. }
  363. /**
  364. * Add a part, identified by a string, and its content.
  365. *
  366. * It is strongly advised to use alphanumerical chars with no space in the
  367. * $part string.
  368. *
  369. * @since 15.0.0
  370. */
  371. final public function addPart(string $part, string $content): IIndexDocument {
  372. $this->parts[$part] = $content;
  373. return $this;
  374. }
  375. /**
  376. * Set all parts and their content.
  377. *
  378. * @since 15.0.0
  379. */
  380. final public function setParts(array $parts): IIndexDocument {
  381. $this->parts = $parts;
  382. return $this;
  383. }
  384. /**
  385. * Get all parts of the IIndexDocument.
  386. *
  387. * @since 15.0.0
  388. */
  389. final public function getParts(): array {
  390. return $this->parts;
  391. }
  392. /**
  393. * Add a link, usable by the frontend.
  394. *
  395. * @since 15.0.0
  396. */
  397. final public function setLink(string $link): IIndexDocument {
  398. $this->link = $link;
  399. return $this;
  400. }
  401. /**
  402. * Get the link.
  403. *
  404. * @since 15.0.0
  405. */
  406. final public function getLink(): string {
  407. return $this->link;
  408. }
  409. /**
  410. * Set more information that couldn't be set using other method.
  411. *
  412. * @since 15.0.0
  413. */
  414. final public function setMore(array $more): IIndexDocument {
  415. $this->more = $more;
  416. return $this;
  417. }
  418. /**
  419. * Get more information.
  420. *
  421. * @since 15.0.0
  422. */
  423. final public function getMore(): array {
  424. return $this->more;
  425. }
  426. /**
  427. * Add some excerpt of the content of the original document, usually based
  428. * on the search request.
  429. *
  430. * @since 16.0.0
  431. */
  432. final public function addExcerpt(string $source, string $excerpt): IIndexDocument {
  433. $this->excerpts[] =
  434. [
  435. 'source' => $source,
  436. 'excerpt' => $this->cleanExcerpt($excerpt)
  437. ];
  438. return $this;
  439. }
  440. /**
  441. * Set all excerpts of the content of the original document.
  442. *
  443. * @since 16.0.0
  444. */
  445. final public function setExcerpts(array $excerpts): IIndexDocument {
  446. $new = [];
  447. foreach ($excerpts as $entry) {
  448. $new[] = [
  449. 'source' => $entry['source'],
  450. 'excerpt' => $this->cleanExcerpt($entry['excerpt'])
  451. ];
  452. }
  453. $this->excerpts = $new;
  454. return $this;
  455. }
  456. /**
  457. * Get all excerpts of the content of the original document.
  458. *
  459. * @since 15.0.0
  460. */
  461. final public function getExcerpts(): array {
  462. return $this->excerpts;
  463. }
  464. /**
  465. * Clean excerpt.
  466. *
  467. * @since 16.0.0
  468. */
  469. private function cleanExcerpt(string $excerpt): string {
  470. $excerpt = str_replace("\\n", ' ', $excerpt);
  471. $excerpt = str_replace("\\r", ' ', $excerpt);
  472. $excerpt = str_replace("\\t", ' ', $excerpt);
  473. $excerpt = str_replace("\n", ' ', $excerpt);
  474. $excerpt = str_replace("\r", ' ', $excerpt);
  475. $excerpt = str_replace("\t", ' ', $excerpt);
  476. return $excerpt;
  477. }
  478. /**
  479. * Set the score to the result assigned to this document during a search
  480. * request.
  481. *
  482. * @since 15.0.0
  483. */
  484. final public function setScore(string $score): IIndexDocument {
  485. $this->score = $score;
  486. return $this;
  487. }
  488. /**
  489. * Get the score.
  490. *
  491. * @since 15.0.0
  492. */
  493. final public function getScore(): string {
  494. return $this->score;
  495. }
  496. /**
  497. * Set some information about the original document that will be available
  498. * to the front-end when displaying search result. (as string)
  499. * Because this information will not be indexed, this method can also be
  500. * used to manage some data while filling the IIndexDocument before its
  501. * indexing.
  502. *
  503. * @since 15.0.0
  504. */
  505. final public function setInfo(string $info, string $value): IIndexDocument {
  506. $this->info[$info] = $value;
  507. return $this;
  508. }
  509. /**
  510. * Get an information about a document. (string)
  511. *
  512. * @since 15.0.0
  513. */
  514. final public function getInfo(string $info, string $default = ''): string {
  515. if (!key_exists($info, $this->info)) {
  516. return $default;
  517. }
  518. return $this->info[$info];
  519. }
  520. /**
  521. * Set some information about the original document that will be available
  522. * to the front-end when displaying search result. (as array)
  523. * Because this information will not be indexed, this method can also be
  524. * used to manage some data while filling the IIndexDocument before its
  525. * indexing.
  526. *
  527. * @since 15.0.0
  528. */
  529. final public function setInfoArray(string $info, array $value): IIndexDocument {
  530. $this->info[$info] = $value;
  531. return $this;
  532. }
  533. /**
  534. * Get an information about a document. (array)
  535. *
  536. * @since 15.0.0
  537. */
  538. final public function getInfoArray(string $info, array $default = []): array {
  539. if (!key_exists($info, $this->info)) {
  540. return $default;
  541. }
  542. return $this->info[$info];
  543. }
  544. /**
  545. * Set some information about the original document that will be available
  546. * to the front-end when displaying search result. (as int)
  547. * Because this information will not be indexed, this method can also be
  548. * used to manage some data while filling the IIndexDocument before its
  549. * indexing.
  550. *
  551. * @since 15.0.0
  552. */
  553. final public function setInfoInt(string $info, int $value): IIndexDocument {
  554. $this->info[$info] = $value;
  555. return $this;
  556. }
  557. /**
  558. * Get an information about a document. (int)
  559. *
  560. * @since 15.0.0
  561. */
  562. final public function getInfoInt(string $info, int $default = 0): int {
  563. if (!key_exists($info, $this->info)) {
  564. return $default;
  565. }
  566. return $this->info[$info];
  567. }
  568. /**
  569. * Set some information about the original document that will be available
  570. * to the front-end when displaying search result. (as bool)
  571. * Because this information will not be indexed, this method can also be
  572. * used to manage some data while filling the IIndexDocument before its
  573. * indexing.
  574. *
  575. * @since 15.0.0
  576. */
  577. final public function setInfoBool(string $info, bool $value): IIndexDocument {
  578. $this->info[$info] = $value;
  579. return $this;
  580. }
  581. /**
  582. * Get an information about a document. (bool)
  583. *
  584. * @since 15.0.0
  585. */
  586. final public function getInfoBool(string $info, bool $default = false): bool {
  587. if (!key_exists($info, $this->info)) {
  588. return $default;
  589. }
  590. return $this->info[$info];
  591. }
  592. /**
  593. * Get all info.
  594. *
  595. * @since 15.0.0
  596. */
  597. final public function getInfoAll(): array {
  598. $info = [];
  599. foreach ($this->info as $k => $v) {
  600. if (str_starts_with($k, '_')) {
  601. continue;
  602. }
  603. $info[$k] = $v;
  604. }
  605. return $info;
  606. }
  607. /**
  608. * @since 15.0.0
  609. *
  610. * On some version of PHP, it is better to force destruct the object.
  611. * And during the index, the number of generated IIndexDocument can be
  612. * _huge_.
  613. */
  614. public function __destruct() {
  615. unset($this->id);
  616. unset($this->providerId);
  617. unset($this->access);
  618. unset($this->modifiedTime);
  619. unset($this->title);
  620. unset($this->content);
  621. unset($this->hash);
  622. unset($this->link);
  623. unset($this->source);
  624. unset($this->tags);
  625. unset($this->metaTags);
  626. unset($this->subTags);
  627. unset($this->more);
  628. unset($this->excerpts);
  629. unset($this->score);
  630. unset($this->info);
  631. unset($this->contentEncoded);
  632. }
  633. /**
  634. * @since 15.0.0
  635. */
  636. public function jsonSerialize(): array {
  637. return [
  638. 'id' => $this->getId(),
  639. 'providerId' => $this->getProviderId(),
  640. 'access' => $this->access,
  641. 'modifiedTime' => $this->getModifiedTime(),
  642. 'title' => $this->getTitle(),
  643. 'link' => $this->getLink(),
  644. 'index' => $this->index,
  645. 'source' => $this->getSource(),
  646. 'info' => $this->getInfoAll(),
  647. 'hash' => $this->getHash(),
  648. 'contentSize' => $this->getContentSize(),
  649. 'tags' => $this->getTags(),
  650. 'metatags' => $this->getMetaTags(),
  651. 'subtags' => $this->getSubTags(),
  652. 'more' => $this->getMore(),
  653. 'excerpts' => $this->getExcerpts(),
  654. 'score' => $this->getScore()
  655. ];
  656. }
  657. }