@@ -93,22 +93,8 @@ public function processIter(
93
93
foreach ( $ crawl_responses as $ crawled ) {
94
94
$ content_type = $ crawled ->content_type ;
95
95
if ( $ content_type && $ this ->processContentType ( $ content_type ) ) {
96
- if ( $ crawled ->body ) {
97
- $ rewritten = $ this ->rewriteFileContents (
98
- $ crawled ->body ,
99
- );
100
- if ( $ rewritten !== $ crawled ->body ) {
101
- $ crawled = $ crawled ->withBody ( $ rewritten );
102
- }
103
- ++$ this ->processed ;
104
- } elseif ( $ crawled ->filename ) {
105
- $ file_contents = file_get_contents ( $ crawled ->filename );
106
- $ rewritten = $ this ->rewriteFileContents (
107
- $ file_contents
108
- );
109
- if ( $ rewritten !== $ file_contents ) {
110
- $ crawled = $ crawled ->withBody ( $ rewritten );
111
- }
96
+ if ( $ crawled ->body || $ crawled ->filename ) {
97
+ $ crawled = $ this ->rewriteFileContents ( $ crawled );
112
98
++$ this ->processed ;
113
99
}
114
100
} else {
@@ -122,25 +108,31 @@ public function processIter(
122
108
}
123
109
124
110
/**
125
- * Rewrite URLs in a string to destination_url
126
- *
127
- * @param string $file_contents
128
- * @return string
111
+ * Rewrite strings in the body of a PathInfo
112
+ * according to $this->config->replacement_patterns
129
113
*/
130
114
public function rewriteFileContents (
131
- string $ file_contents ,
132
- ): string {
133
- // TODO: allow empty file saving here? Exception for style.css
134
- if ( ! $ file_contents ) {
135
- return '' ;
115
+ PathInfo $ path_info
116
+ ): PathInfo {
117
+ if ( $ path_info ->body !== null ) {
118
+ $ s = $ path_info ->body ;
119
+ } elseif ( $ path_info ->filename ) {
120
+ $ s = file_get_contents ( $ path_info ->filename );
121
+ if ( $ s === false ) {
122
+ throw WsLog::ex ( 'Error reading file ' . $ path_info ->filename );
123
+ }
136
124
}
137
125
138
- $ rewritten_contents = strtr (
139
- $ file_contents ,
126
+ $ rewritten = strtr (
127
+ $ s ,
140
128
$ this ->config ->replacement_patterns
141
129
);
142
130
143
- return $ rewritten_contents ;
131
+ if ( $ rewritten !== $ s ) {
132
+ return $ path_info ->withBody ( $ rewritten );
133
+ }
134
+
135
+ return $ path_info ;
144
136
}
145
137
146
138
public function complete (): void {
0 commit comments