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