public function attachments ()
{
static $types = array(
1 => 'GIF',
2 => 'JPEG',
3 => 'PNG'
);
$globalStart = microtime(true);
$start = $this->db->fetchOne('
SELECT MAX(attachment_id)
FROM xf_attachment
');
$maxvB = $this->db->fetchOne('SELECT MAX(attachmentid) FROM ' . $this->sourceDb . '.attachment');
$fileName = '' . self::$dataDir . 'attachment_tmp';
$attachmentModel = XenForo_Model::create('XenForo_Model_Attachment');
$emptyBatch = false;
while (
// WHERE attachment.attachmentid BETWEEN ' . intval($start) . ' AND ' . intval($start + 999) . ' AND attachment.contenttypeid = 1
$attachments = $this->db->fetchAll('
SELECT attachment.attachmentid AS attachment_id, attachment.userid AS user_id, ' . (self::$isVB4 ? 'contentid' : 'postid') . ' AS content_id, attachment.dateline AS attachment_date, ' . (self::$isVB4 ? 'filedata.filedata' : 'attachment.filedata') . ' AS file, ' . (self::$isVB4 ? 'filedata.thumbnail_filesize' : 'attachment.thumbnail_filesize') . ', attachment.filename, ' . (self::$isVB4 ? 'filedata.extension' : 'attachment.extension') . ' AS extension, ' . (self::$isVB4 ? 'filedata.filedataid' : 'attachment.attachmentid') . ' AS filesysid
FROM ' . $this->sourceDb . '.attachment
' . (self::$isVB4 ? 'LEFT JOIN ' . $this->sourceDb . '.filedata ON (filedata.filedataid = attachment.filedataid)' : '') . '
WHERE attachment.attachmentid BETWEEN ' . intval($start) . ' AND ' . intval($start + 999) . (self::$isVB4 ? ' AND attachment.contenttypeid = 1' : '') . '
' . (self::$isVB4 ? 'GROUP BY attachment.filedataid' : '') . '
ORDER BY attachment.attachmentid
')
OR $emptyBatch = (($start + 999) < $maxvB)
)
{
if ($emptyBatch)
{
echo "\r\n EMPTY BATCH (no 'post' attachments in range)\r\n";
$start = $start + 1000;
$emptyBatch = false;
continue;
}
echo "\r\n " . number_format(count($attachments)) . " new attachments\r\n";
foreach ($attachments as $attachment)
{
echo $attachment['attachment_id'] . ' ';
if (self::$attachFile AND file_exists(self::$attachFile . implode('/', preg_split('//', $attachment['user_id'], -1, PREG_SPLIT_NO_EMPTY)) . '/' . strval($attachment['filesysid']) . '.attach'))
{
$attachment['file'] = file_get_contents(self::$attachFile . implode('/', preg_split('//', $attachment['user_id'], -1, PREG_SPLIT_NO_EMPTY)) . '/' . strval($attachment['filesysid']) . '.attach');
}
else
{
$attachment['file'] = $attachment['file'];
}
if (!$attachment['file'])
{
echo '!!MISSING DATA!!';
continue;
}
if ($attachment['thumbnail_filesize'] > 0 AND $attachment['extension'] != 'bmp')
{
file_put_contents($fileName, $attachment['file']);
$tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
$imageInfo = getimagesize($fileName);
if ($imageInfo === false)
{
echo '!!BAD!!';
$attachment['width'] = 0;
$attachment['height'] = 0;
$attachment['thumbnail_width'] = 0;
$attachment['thumbnail_height'] = 0;
$attachment['thumbnail_file'] = '';
$attachment['file_type'] = '';
}
else
{
$attachment['width'] = $imageInfo[0];
$attachment['height'] = $imageInfo[1];
$attachment['thumbnail_width'] = 0;
$attachment['thumbnail_height'] = 0;
$attachment['thumbnail_file'] = '';
$attachment['file_type'] = strtoupper(image_type_to_extension($imageInfo[2], false));
$image = XenForo_Image_Abstract::createFromFile($fileName, $imageInfo[2]);
if ($image)
{
if ($image->thumbnail(XenForo_Application::get('options')->attachmentThumbnailDimensions))
{
$image->output($imageInfo[2], $tempThumbFile);
$attachment['thumbnail_file'] = file_get_contents($tempThumbFile);
unlink($tempThumbFile);
}
else
{
$attachment['thumbnail_file'] = $attachment['file'];
}
$attachment['thumbnail_width'] = $image->getWidth();
$attachment['thumbnail_height'] = $image->getHeight();
unset($image);
}
}
}
else
{
$attachment['width'] = 0;
$attachment['height'] = 0;
$attachment['thumbnail_width'] = 0;
$attachment['thumbnail_height'] = 0;
$attachment['thumbnail_file'] = '';
$attachment['file_type'] = '';
}
if (self::$extraOutCommand)
{
$attachFileNameTmp = array();
exec('echo $\'' . addslashes($attachment['filename']) . '\' ' . self::$extraOutCommand , $attachFileNameTmp);
$attachFileName = $attachFileNameTmp[0];
}
else
{
$attachFileName = $attachment['filename'];
}
$this->db->query('
INSERT INTO xf_attachment_data
(user_id, upload_date, filename, file_size, file_hash, width, height, thumbnail_width, thumbnail_height, attach_count)
VALUES
(' . $attachment['user_id'] . ', ' . $attachment['attachment_date'] . ', "' . addslashes($attachFileName) . '", ' . strlen($attachment['file']) . ', "' . addslashes(md5($attachment['file'])) . '", ' . $attachment['width'] . ', ' . $attachment['height'] . ', ' . $attachment['thumbnail_width'] . ', ' . $attachment['thumbnail_height'] . ', 1)
');
$attachmentDataId = $this->db->lastInsertId();
$this->db->query('
INSERT INTO xf_attachment
(attachment_id, data_id, content_type, content_id, attach_date, temp_hash, unassociated, view_count)
VALUES
(' . $attachment['attachment_id'] . ', ' . $attachmentDataId . ', "post", ' . $attachment['content_id'] . ', ' . $attachment['attachment_date'] . ', "", 0, 0)
ON DUPLICATE KEY UPDATE
data_id = VALUES(data_id), content_type = VALUES(content_type), content_id = VALUES(content_id), attach_date = VALUES(attach_date), temp_hash = VALUES(temp_hash), unassociated = VALUES(unassociated), view_count = VALUES(view_count)
');
// SKIP THIS AND WRITE TO FILE INSTEAD
$path = $attachmentModel->getAttachmentDataFilePath(array(
'data_id' => $attachmentDataId,
'file_hash' => md5($attachment['file']),
));
XenForo_Helper_File::createDirectory(dirname($path));
file_put_contents($path, $attachment['file']);
if ($attachment['thumbnail_width'])
{
$path = $attachmentModel->getAttachmentThumbnailFilePath(array(
'data_id' => $attachmentDataId,
'file_hash' => md5($attachment['file']),
));
XenForo_Helper_File::createDirectory(dirname($path), true);
file_put_contents($path, $attachment['thumbnail_file']);
}
}
unset ($attachments);
$start = $start + 1000;
//break;
}
echo "\r\n updating attachment view count";
$this->db->query('
UPDATE xf_attachment
LEFT JOIN ' . $this->sourceDb . '.attachment ON (attachment.attachmentid = xf_attachment.attachment_id)
SET xf_attachment.view_count = attachment.counter
');
$this->db->closeConnection();
echo "\r\nTotal time for attachments: " . number_format(microtime(true) - $globalStart, 2) . "s\r\n";
}