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