Document.php
4.42 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* SearchWP Document.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
use SearchWP\Parser;
if ( ! defined( 'ABSPATH' ) ) {
die();
}
/**
* Class Document is responsible for modeling an attachment WP_Post.
*
* @since 4.0
*/
class Document extends Parser {
/**
* Meta key that stores parsed Document content.
*
* @since 4.0
* @var string
*/
public static $meta_key = SEARCHWP_PREFIX . 'content';
/**
* Retrieve the file content from a Media entry.
*
* @since 4.0
* @param WP_Post $post The Media entry.
* @return string
*/
public static function get_content( \WP_Post $post ) {
$mime_type = $post->post_mime_type;
$filename = get_attached_file( $post->ID );
// It's possible that this fires too early to return the file. See #226.
// Returning here will cause the indexing process to try again.
if ( empty( $filename ) || ! file_exists( $filename ) ) {
return '';
}
$content = apply_filters( 'searchwp\document\content\stored', self::get_stored_content( $post ), $post );
$skipped = apply_filters( 'searchwp\document\skip',
get_post_meta( $post->ID, self::$meta_key . '_skipped', true ), $post );
if (
empty( $content )
&& ! $skipped
&& ( did_action( 'searchwp\indexer\batch' ) || did_action( 'searchwp\indexer\init' ) )
) {
$extracted = self::extract_text( $filename, $mime_type, $post );
$content = apply_filters( 'searchwp\document\content\extracted', $extracted, $post );
if ( empty( $content ) ) {
// There was an actual error or there's no content.
// Flag this to omit skip from further extraction attempts.
update_post_meta( $post->ID, self::$meta_key . '_skipped', true );
}
$content = Utils::get_string_from( $content );
// Prevent sanitization malfunction due to angle brackets.
$content = str_replace( '<', '<', $content );
$content = str_replace( '>', '>', $content );
update_post_meta( $post->ID, self::$meta_key, $content );
}
return (string) apply_filters( 'searchwp\document\content', $content, $post );
}
/**
* Retrieve stored document content.
*
* @since 4.0
* @return mixed|string
*/
public static function get_stored_content( \WP_Post $post ) {
return get_post_meta( $post->ID, self::$meta_key, true );
}
/**
* Retrieve PDF metadata from a Media entry.
*
* @since 4.0
* @param WP_Post $post The Media entry.
* @return string
*/
public static function get_pdf_metadata( \WP_Post $post ) {
$filename = get_attached_file( $post->ID );
$skipped = apply_filters(
'searchwp\document\pdf_metadata\skip',
get_post_meta( $post->ID, self::$meta_key . '_skipped_pdf_metadata', true ),
$post
);
if ( $skipped || ! file_exists( $filename ) || 'application/pdf' !== $post->post_mime_type ) {
return null;
}
// TODO: If an external process is extracting this data, that external process is
// run each time which adds overhead.
$metadata = apply_filters( 'searchwp\document\pdf_metadata',
get_post_meta( $post->ID, self::$meta_key . '_pdf_metadata', true ), $post );
if ( ! empty( $metadata ) ) {
return $metadata;
}
try {
if ( did_action( 'searchwp\indexer\batch' ) || did_action( 'searchwp\indexer\init' ) ) {
$pdf_parser = new \SearchWP\Dependencies\Smalot\PdfParser\Parser();
$metadata = apply_filters(
'searchwp\document\pdf_metadata\parsed',
$pdf_parser->parseFile( $filename )->getDetails(),
[ 'post_id' => $post->ID ]
);
$result = update_post_meta( $post->ID, self::$meta_key . '_pdf_metadata', $metadata );
if ( ! $result ) {
// Something went wrong in trying to save the metadata, likely invalid characters.
do_action( 'searchwp\debug\log', 'PDF metadata saving failed, attempting re-save after parsing', 'parser' );
// Clean it up and try again.
$metadata = array_map( function( $value ) {
return Utils::get_string_from( $value );
}, $metadata );
$result = update_post_meta( $post->ID, self::$meta_key . '_pdf_metadata', $metadata );
if ( ! $result ) {
do_action( 'searchwp\debug\log', 'PDF metadata parsing failed for ' . $post->ID, 'parser' );
}
}
}
} catch (\Exception $e) {
do_action(
'searchwp\debug\log',
'PDF metadata extraction failed: ' . sanitize_text_field( $e->getMessage() ),
'parser'
);
update_post_meta( $post->ID, self::$meta_key . '_skipped_pdf_metadata', true );
$metadata = null;
}
return $metadata;
}
}