Conditions | 49 |
Paths | > 20000 |
Total Lines | 303 |
Code Lines | 167 |
Lines | 0 |
Ratio | 0 % |
Changes | 19 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
113 | protected function exportXml($member_info) |
||
114 | { |
||
115 | global $smcFunc, $sourcedir, $context, $modSettings, $settings, $user_info, $mbname; |
||
116 | global $user_profile, $txt, $scripturl, $query_this_board; |
||
117 | |||
118 | // For convenience... |
||
119 | $uid = $this->_details['uid']; |
||
120 | $lang = $this->_details['lang']; |
||
121 | $included = $this->_details['included']; |
||
122 | $start = $this->_details['start']; |
||
123 | $latest = $this->_details['latest']; |
||
124 | $datatype = $this->_details['datatype']; |
||
125 | |||
126 | if (!isset($included[$datatype]['func']) || !isset($included[$datatype]['langfile'])) |
||
127 | return; |
||
128 | |||
129 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'News.php'); |
||
130 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'ScheduledTasks.php'); |
||
131 | |||
132 | // Setup. |
||
133 | $done = false; |
||
134 | $delay = 0; |
||
135 | $func = $included[$datatype]['func']; |
||
|
|||
136 | $context['xmlnews_uid'] = $uid; |
||
137 | $context['xmlnews_limit'] = !empty($modSettings['export_rate']) ? $modSettings['export_rate'] : 250; |
||
138 | $context[$datatype . '_start'] = $start[$datatype]; |
||
139 | $datatypes = array_keys($included); |
||
140 | |||
141 | // Fake a wee bit of $user_info so that loading the member data & language doesn't choke. |
||
142 | $user_info = $member_info; |
||
143 | |||
144 | loadEssentialThemeData(); |
||
145 | $settings['actual_theme_dir'] = $settings['theme_dir']; |
||
146 | $context['user']['id'] = $uid; |
||
147 | $context['user']['language'] = $lang; |
||
148 | loadMemberData($uid); |
||
149 | loadLanguage(implode('+', array_unique(array('index', 'Modifications', 'Stats', 'Profile', $included[$datatype]['langfile']))), $lang); |
||
150 | |||
151 | // @todo Ask lawyers whether the GDPR requires us to include posts in the recycle bin. |
||
152 | $query_this_board = '{query_see_message_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' AND m.id_board != ' . $modSettings['recycle_board'] : ''); |
||
153 | |||
154 | // We need a valid export directory. |
||
155 | if (empty($modSettings['export_dir']) || !file_exists($modSettings['export_dir'])) |
||
156 | { |
||
157 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'Profile-Export.php'); |
||
158 | if (create_export_dir() === false) |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | $export_dir_slash = $modSettings['export_dir'] . DIRECTORY_SEPARATOR; |
||
163 | |||
164 | $idhash = hash_hmac('sha1', $uid, get_auth_secret()); |
||
165 | $idhash_ext = $idhash . '.' . $this->_details['format_settings']['extension']; |
||
166 | |||
167 | // Increment the file number until we reach one that doesn't exist. |
||
168 | $filenum = 1; |
||
169 | $realfile = $export_dir_slash . $filenum . '_' . $idhash_ext; |
||
170 | while (file_exists($realfile)) |
||
171 | $realfile = $export_dir_slash . ++$filenum . '_' . $idhash_ext; |
||
172 | |||
173 | $tempfile = $export_dir_slash . $idhash_ext . '.tmp'; |
||
174 | $progressfile = $export_dir_slash . $idhash_ext . '.progress.json'; |
||
175 | |||
176 | $feed_meta = array( |
||
177 | 'title' => sprintf($txt['profile_of_username'], $user_profile[$uid]['real_name']), |
||
178 | 'desc' => sentence_list(array_map(function ($datatype) use ($txt) { return $txt[$datatype]; }, array_keys($included))), |
||
179 | 'author' => $mbname, |
||
180 | 'source' => $scripturl . '?action=profile;u=' . $uid, |
||
181 | 'self' => '', // Unused, but can't be null. |
||
182 | 'page' => &$filenum, |
||
183 | ); |
||
184 | |||
185 | // Some paranoid hosts disable or hamstring the disk space functions in an attempt at security via obscurity. |
||
186 | $check_diskspace = !empty($modSettings['export_min_diskspace_pct']) && function_exists('disk_free_space') && function_exists('disk_total_space') && intval(@disk_total_space($modSettings['export_dir']) >= 1440); |
||
187 | $minspace = $check_diskspace ? ceil(disk_total_space($modSettings['export_dir']) * $modSettings['export_min_diskspace_pct'] / 100) : 0; |
||
188 | |||
189 | // If a necessary file is missing, we need to start over. |
||
190 | if (!file_exists($tempfile) || !file_exists($progressfile) || filesize($progressfile) == 0) |
||
191 | { |
||
192 | foreach (array_merge(array($tempfile, $progressfile), glob($export_dir_slash . '*_' . $idhash_ext)) as $fpath) |
||
193 | @unlink($fpath); |
||
194 | |||
195 | $filenum = 1; |
||
196 | $realfile = $export_dir_slash . $filenum . '_' . $idhash_ext; |
||
197 | |||
198 | buildXmlFeed('smf', array(), $feed_meta, 'profile'); |
||
199 | file_put_contents($tempfile, implode('', $context['feed']), LOCK_EX); |
||
200 | |||
201 | $progress = array_fill_keys($datatypes, 0); |
||
202 | file_put_contents($progressfile, $smcFunc['json_encode']($progress)); |
||
203 | } |
||
204 | else |
||
205 | $progress = $smcFunc['json_decode'](file_get_contents($progressfile), true); |
||
206 | |||
207 | // Get the data, always in ascending order. |
||
208 | $xml_data = call_user_func($included[$datatype]['func'], 'smf', true); |
||
209 | |||
210 | // No data retrived? Just move on then. |
||
211 | if (empty($xml_data)) |
||
212 | $datatype_done = true; |
||
213 | |||
214 | // Basic profile data is quick and easy. |
||
215 | elseif ($datatype == 'profile') |
||
216 | { |
||
217 | buildXmlFeed('smf', $xml_data, $feed_meta, 'profile'); |
||
218 | file_put_contents($tempfile, implode('', $context['feed']), LOCK_EX); |
||
219 | |||
220 | $progress[$datatype] = time(); |
||
221 | $datatype_done = true; |
||
222 | |||
223 | // Cache for subsequent reuse. |
||
224 | $profile_basic_items = $context['feed']['items']; |
||
225 | cache_put_data('export_profile_basic-' . $uid, $profile_basic_items, MAX_CLAIM_THRESHOLD); |
||
226 | } |
||
227 | |||
228 | // Posts and PMs... |
||
229 | else |
||
230 | { |
||
231 | // We need the basic profile data in every export file. |
||
232 | $profile_basic_items = cache_get_data('export_profile_basic-' . $uid, MAX_CLAIM_THRESHOLD); |
||
233 | if (empty($profile_basic_items)) |
||
234 | { |
||
235 | $profile_data = call_user_func($included['profile']['func'], 'smf', true); |
||
236 | buildXmlFeed('smf', $profile_data, $feed_meta, 'profile'); |
||
237 | $profile_basic_items = $context['feed']['items']; |
||
238 | cache_put_data('export_profile_basic-' . $uid, $profile_basic_items, MAX_CLAIM_THRESHOLD); |
||
239 | unset($context['feed']); |
||
240 | } |
||
241 | |||
242 | $per_page = $this->_details['format_settings']['per_page']; |
||
243 | $prev_item_count = empty($this->_details['item_count']) ? 0 : $this->_details['item_count']; |
||
244 | |||
245 | // If the temp file has grown enormous, save it so we can start a new one. |
||
246 | clearstatcache(); |
||
247 | if (file_exists($tempfile) && filesize($tempfile) >= 1024 * 1024 * 250) |
||
248 | { |
||
249 | rename($tempfile, $realfile); |
||
250 | $realfile = $export_dir_slash . ++$filenum . '_' . $idhash_ext; |
||
251 | |||
252 | if (empty($context['feed']['header'])) |
||
253 | buildXmlFeed('smf', array(), $feed_meta, 'profile'); |
||
254 | |||
255 | file_put_contents($tempfile, implode('', array($context['feed']['header'], $profile_basic_items, $context['feed']['footer'])), LOCK_EX); |
||
256 | |||
257 | $prev_item_count = 0; |
||
258 | } |
||
259 | |||
260 | // Split $xml_data into reasonably sized chunks. |
||
261 | if (empty($prev_item_count)) |
||
262 | { |
||
263 | $xml_data = array_chunk($xml_data, $per_page); |
||
264 | } |
||
265 | else |
||
266 | { |
||
267 | $first_chunk = array_splice($xml_data, 0, $per_page - $prev_item_count); |
||
268 | $xml_data = array_merge(array($first_chunk), array_chunk($xml_data, $per_page)); |
||
269 | unset($first_chunk); |
||
270 | } |
||
271 | |||
272 | foreach ($xml_data as $chunk => $items) |
||
273 | { |
||
274 | unset($new_item_count, $last_id); |
||
275 | |||
276 | // Remember the last item so we know where to start next time. |
||
277 | $last_item = end($items); |
||
278 | if (isset($last_item['content'][0]['content']) && $last_item['content'][0]['tag'] === 'id') |
||
279 | $last_id = $last_item['content'][0]['content']; |
||
280 | |||
281 | // Build the XML string from the data. |
||
282 | buildXmlFeed('smf', $items, $feed_meta, 'profile'); |
||
283 | |||
284 | // If disk space is insufficient, pause for a day so the admin can fix it. |
||
285 | if ($check_diskspace && disk_free_space($modSettings['export_dir']) - $minspace <= strlen(implode('', $context['feed']) . self::$xslt_info['stylesheet'])) |
||
286 | { |
||
287 | loadLanguage('Errors'); |
||
288 | log_error(sprintf($txt['export_low_diskspace'], $modSettings['export_min_diskspace_pct'])); |
||
289 | |||
290 | $delay = 86400; |
||
291 | } |
||
292 | else |
||
293 | { |
||
294 | // We need a file to write to, of course. |
||
295 | if (!file_exists($tempfile)) |
||
296 | file_put_contents($tempfile, implode('', array($context['feed']['header'], $profile_basic_items, $context['feed']['footer'])), LOCK_EX); |
||
297 | |||
298 | // Insert the new data before the feed footer. |
||
299 | $handle = fopen($tempfile, 'r+'); |
||
300 | if (is_resource($handle)) |
||
301 | { |
||
302 | flock($handle, LOCK_EX); |
||
303 | |||
304 | fseek($handle, strlen($context['feed']['footer']) * -1, SEEK_END); |
||
305 | |||
306 | $bytes_written = fwrite($handle, $context['feed']['items'] . $context['feed']['footer']); |
||
307 | |||
308 | // If we couldn't write everything, revert the changes and consider the write to have failed. |
||
309 | if ($bytes_written > 0 && $bytes_written < strlen($context['feed']['items'] . $context['feed']['footer'])) |
||
310 | { |
||
311 | fseek($handle, $bytes_written * -1, SEEK_END); |
||
312 | $pointer_pos = ftell($handle); |
||
313 | ftruncate($handle, $pointer_pos); |
||
314 | rewind($handle); |
||
315 | fseek($handle, 0, SEEK_END); |
||
316 | fwrite($handle, $context['feed']['footer']); |
||
317 | |||
318 | $bytes_written = false; |
||
319 | } |
||
320 | |||
321 | flock($handle, LOCK_UN); |
||
322 | fclose($handle); |
||
323 | } |
||
324 | |||
325 | // Write failed. We'll try again next time. |
||
326 | if (empty($bytes_written)) |
||
327 | { |
||
328 | $delay = MAX_CLAIM_THRESHOLD; |
||
329 | break; |
||
330 | } |
||
331 | |||
332 | // All went well. |
||
333 | else |
||
334 | { |
||
335 | // Track progress by ID where appropriate, and by time otherwise. |
||
336 | $progress[$datatype] = !isset($last_id) ? time() : $last_id; |
||
337 | file_put_contents($progressfile, $smcFunc['json_encode']($progress)); |
||
338 | |||
339 | // Are we done with this datatype yet? |
||
340 | if (!isset($last_id) || (count($items) < $per_page && $last_id >= $latest[$datatype])) |
||
341 | $datatype_done = true; |
||
342 | |||
343 | // Finished the file for this chunk, so move on to the next one. |
||
344 | if (count($items) >= $per_page - $prev_item_count) |
||
345 | { |
||
346 | rename($tempfile, $realfile); |
||
347 | $realfile = $export_dir_slash . ++$filenum . '_' . $idhash_ext; |
||
348 | |||
349 | file_put_contents($tempfile, implode('', array($context['feed']['header'], $profile_basic_items, $context['feed']['footer'])), LOCK_EX); |
||
350 | |||
351 | $prev_item_count = $new_item_count = 0; |
||
352 | } |
||
353 | // This was the last chunk. |
||
354 | else |
||
355 | { |
||
356 | // Should we append more items to this file next time? |
||
357 | $new_item_count = isset($last_id) ? $prev_item_count + count($items) : 0; |
||
358 | } |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | |||
364 | if (!empty($datatype_done)) |
||
365 | { |
||
366 | $datatype_key = array_search($datatype, $datatypes); |
||
367 | $done = !isset($datatypes[$datatype_key + 1]); |
||
368 | |||
369 | if (!$done) |
||
370 | $datatype = $datatypes[$datatype_key + 1]; |
||
371 | } |
||
372 | |||
373 | // Remove the .tmp extension from the final tempfile so the system knows it's done. |
||
374 | if (!empty($done)) |
||
375 | { |
||
376 | rename($tempfile, $realfile); |
||
377 | } |
||
378 | |||
379 | // Oops. Apparently some sneaky monkey cancelled the export while we weren't looking. |
||
380 | elseif (!file_exists($progressfile)) |
||
381 | { |
||
382 | @unlink($tempfile); |
||
383 | return; |
||
384 | } |
||
385 | |||
386 | // We have more work to do again later. |
||
387 | else |
||
388 | { |
||
389 | $start[$datatype] = $progress[$datatype]; |
||
390 | |||
391 | $new_details = array( |
||
392 | 'format' => $this->_details['format'], |
||
393 | 'uid' => $uid, |
||
394 | 'lang' => $lang, |
||
395 | 'included' => $included, |
||
396 | 'start' => $start, |
||
397 | 'latest' => $latest, |
||
398 | 'datatype' => $datatype, |
||
399 | 'format_settings' => $this->_details['format_settings'], |
||
400 | 'last_page' => $this->_details['last_page'], |
||
401 | 'dlfilename' => $this->_details['dlfilename'], |
||
402 | ); |
||
403 | if (!empty($new_item_count)) |
||
404 | $new_details['item_count'] = $new_item_count; |
||
405 | |||
406 | $this->next_task = array('$sourcedir/tasks/ExportProfileData.php', 'ExportProfileData_Background', $smcFunc['json_encode']($new_details), time() - MAX_CLAIM_THRESHOLD + $delay); |
||
407 | |||
408 | if (!file_exists($tempfile)) |
||
409 | { |
||
410 | buildXmlFeed('smf', array(), $feed_meta, 'profile'); |
||
411 | file_put_contents($tempfile, implode('', array($context['feed']['header'], !empty($profile_basic_items) ? $profile_basic_items : '', $context['feed']['footer'])), LOCK_EX); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | file_put_contents($progressfile, $smcFunc['json_encode']($progress)); |
||
416 | } |
||
747 | ?> |