| Conditions | 49 | 
| Paths | > 20000 | 
| Total Lines | 307 | 
| Code Lines | 170 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 16 | ||
| 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']), LOCK_EX); | ||
| 177 | |||
| 178 | $progress = array_fill_keys($datatypes, 0); | ||
| 179 | file_put_contents($progressfile, $smcFunc['json_encode']($progress), LOCK_EX); | ||
| 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']), LOCK_EX); | ||
| 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'])), LOCK_EX); | ||
| 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'])), LOCK_EX); | ||
| 274 | |||
| 275 | // Insert the new data before the feed footer. | ||
| 276 | $handle = fopen($tempfile, 'r+'); | ||
| 277 | if (is_resource($handle)) | ||
| 278 | 					{ | ||
| 279 | flock($sfhandle, LOCK_EX); | ||
| 280 | |||
| 281 | fseek($handle, strlen($context['feed']['footer']) * -1, SEEK_END); | ||
| 282 | |||
| 283 | $bytes_written = fwrite($handle, $context['feed']['items'] . $context['feed']['footer']); | ||
| 284 | |||
| 285 | // If we couldn't write everything, revert the changes and consider the write to have failed. | ||
| 286 | if ($bytes_written > 0 && $bytes_written < strlen($context['feed']['items'] . $context['feed']['footer'])) | ||
| 287 | 						{ | ||
| 288 | fseek($handle, $bytes_written * -1, SEEK_END); | ||
| 289 | $pointer_pos = ftell($handle); | ||
| 290 | ftruncate($handle, $pointer_pos); | ||
| 291 | rewind($handle); | ||
| 292 | fseek($handle, 0, SEEK_END); | ||
| 293 | fwrite($handle, $context['feed']['footer']); | ||
| 294 | |||
| 295 | $bytes_written = false; | ||
| 296 | } | ||
| 297 | |||
| 298 | flock($sfhandle, LOCK_UN); | ||
| 299 | fclose($handle); | ||
| 300 | } | ||
| 301 | |||
| 302 | // Write failed. We'll try again next time. | ||
| 303 | if (empty($bytes_written)) | ||
| 304 | 					{ | ||
| 305 | $delay = MAX_CLAIM_THRESHOLD; | ||
| 306 | break; | ||
| 307 | } | ||
| 308 | |||
| 309 | // All went well. | ||
| 310 | else | ||
| 311 | 					{ | ||
| 312 | // Track progress by ID where appropriate, and by time otherwise. | ||
| 313 | $progress[$datatype] = !isset($last_id) ? time() : $last_id; | ||
| 314 | file_put_contents($progressfile, $smcFunc['json_encode']($progress), LOCK_EX); | ||
| 315 | |||
| 316 | // Are we done with this datatype yet? | ||
| 317 | if (!isset($last_id) || (count($items) < $per_page && $last_id >= $latest[$datatype])) | ||
| 318 | $datatype_done = true; | ||
| 319 | |||
| 320 | // Finished the file for this chunk, so move on to the next one. | ||
| 321 | if (count($items) >= $per_page - $prev_item_count) | ||
| 322 | 						{ | ||
| 323 | rename($tempfile, $realfile); | ||
| 324 | $realfile = $export_dir_slash . ++$filenum . '_' . $idhash_ext; | ||
| 325 | |||
| 326 | 							file_put_contents($tempfile, implode('', array($context['feed']['header'], $profile_basic_items, $context['feed']['footer'])), LOCK_EX); | ||
| 327 | |||
| 328 | $prev_item_count = $new_item_count = 0; | ||
| 329 | } | ||
| 330 | // This was the last chunk. | ||
| 331 | else | ||
| 332 | 						{ | ||
| 333 | // Should we append more items to this file next time? | ||
| 334 | $new_item_count = isset($last_id) ? $prev_item_count + count($items) : 0; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | } | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | if (!empty($datatype_done)) | ||
| 342 | 		{ | ||
| 343 | $datatype_key = array_search($datatype, $datatypes); | ||
| 344 | $done = !isset($datatypes[$datatype_key + 1]); | ||
| 345 | |||
| 346 | if (!$done) | ||
| 347 | $datatype = $datatypes[$datatype_key + 1]; | ||
| 348 | } | ||
| 349 | |||
| 350 | // Remove the .tmp extension from the final tempfile so the system knows it's done. | ||
| 351 | if (!empty($done)) | ||
| 352 | 		{ | ||
| 353 | rename($tempfile, $realfile); | ||
| 354 | } | ||
| 355 | |||
| 356 | // Oops. Apparently some sneaky monkey cancelled the export while we weren't looking. | ||
| 357 | elseif (!file_exists($progressfile)) | ||
| 358 | 		{ | ||
| 359 | @unlink($tempfile); | ||
| 360 | return; | ||
| 361 | } | ||
| 362 | |||
| 363 | // We have more work to do again later. | ||
| 364 | else | ||
| 365 | 		{ | ||
| 366 | $start[$datatype] = $progress[$datatype]; | ||
| 367 | |||
| 368 | $new_details = array( | ||
| 369 | 'format' => $this->_details['format'], | ||
| 370 | 'uid' => $uid, | ||
| 371 | 'lang' => $lang, | ||
| 372 | 'included' => $included, | ||
| 373 | 'start' => $start, | ||
| 374 | 'latest' => $latest, | ||
| 375 | 'datatype' => $datatype, | ||
| 376 | 'format_settings' => $this->_details['format_settings'], | ||
| 377 | 'last_page' => $this->_details['last_page'], | ||
| 378 | 'dlfilename' => $this->_details['dlfilename'], | ||
| 379 | ); | ||
| 380 | if (!empty($new_item_count)) | ||
| 381 | $new_details['item_count'] = $new_item_count; | ||
| 382 | |||
| 383 | 			$smcFunc['db_insert']('insert', '{db_prefix}background_tasks', | ||
| 384 | 				array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'), | ||
| 385 | 				array('$sourcedir/tasks/ExportProfileData.php', 'ExportProfileData_Background', $smcFunc['json_encode']($new_details), time() - MAX_CLAIM_THRESHOLD + $delay), | ||
| 386 | array() | ||
| 387 | ); | ||
| 388 | |||
| 389 | if (!file_exists($tempfile)) | ||
| 390 | 			{ | ||
| 391 | 				buildXmlFeed('smf', array(), $feed_meta, 'profile'); | ||
| 392 | 				file_put_contents($tempfile, implode('', array($context['feed']['header'], !empty($profile_basic_items) ? $profile_basic_items : '', $context['feed']['footer'])), LOCK_EX); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | file_put_contents($progressfile, $smcFunc['json_encode']($progress), LOCK_EX); | ||
| 397 | } | ||
| 694 | ?> |