-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.php
More file actions
96 lines (90 loc) · 3.03 KB
/
Copy pathhelper.php
File metadata and controls
96 lines (90 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
use dokuwiki\plugin\totext\Extractor\ExtractionResult;
use dokuwiki\plugin\totext\Extractor\ExtractorFactory;
/**
* DokuWiki Plugin totext (Helper Component)
*
* Gives other plugins a simple API to extract plain text and metadata from
* documents.
*
* @license GPL-2.0-only
* @author Andreas Gohr <gohr@cosmocode.de>
*/
class helper_plugin_totext extends \dokuwiki\Extension\Plugin
{
/**
* Extract body text and metadata from the given file.
*
* @param string $path absolute path to the file
* @return ExtractionResult the extracted body text and canonical metadata
* @throws \dokuwiki\plugin\totext\Exception\ExtractionException on failure
* @throws \dokuwiki\plugin\totext\Exception\UnsupportedFormatException on unsupported format
*/
public function extract($path)
{
return ExtractorFactory::extract($path);
}
/**
* Extract plain text from the given file.
*
* Unlike extract(), this throws when text extraction itself failed (even if
* metadata was salvaged), so callers that want only the text — e.g. the
* docsearch plugin, which falls back to its own converters on failure — keep
* their throw-on-failure contract.
*
* @param string $path absolute path to the file
* @return string the extracted plain text
* @throws \dokuwiki\plugin\totext\Exception\ExtractionException on failure
* @throws \dokuwiki\plugin\totext\Exception\UnsupportedFormatException on unsupported format
*/
public function extractText($path)
{
$result = $this->extract($path);
if ($result->textError !== null) {
throw $result->textError;
}
return $result->text;
}
/**
* Extract the canonical metadata map from the given file.
*
* Throws when metadata extraction itself failed (even if the body text was
* extracted), mirroring extractText().
*
* @param string $path absolute path to the file
* @return array<string, string> canonical key => value map
* @throws \dokuwiki\plugin\totext\Exception\ExtractionException on failure
* @throws \dokuwiki\plugin\totext\Exception\UnsupportedFormatException on unsupported format
*/
public function extractMetadata($path)
{
$result = $this->extract($path);
if ($result->metadataError !== null) {
throw $result->metadataError;
}
return $result->metadata;
}
/**
* List the file extensions this plugin can handle.
*
* @return string[] supported extensions (without leading dot)
*/
public function supportedExtensions()
{
return ExtractorFactory::supportedExtensions();
}
/**
* Whether the given file is supported, based on its extension.
*
* @param string $path file name or path
* @return bool
*/
public function isSupported($path)
{
return in_array(
strtolower(pathinfo($path, PATHINFO_EXTENSION)),
$this->supportedExtensions(),
true
);
}
}