From 38c39b93416f4b89bc9afda782693941215a81fd Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 8 May 2015 18:09:38 +0200 Subject: [PATCH] Add update script for 2.1.4 to update image attachment filesizes --- ...uild_attachments_com.woltlab.wcf_2.1.4.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 wcfsetup/install/files/acp/rebuild_attachments_com.woltlab.wcf_2.1.4.php diff --git a/wcfsetup/install/files/acp/rebuild_attachments_com.woltlab.wcf_2.1.4.php b/wcfsetup/install/files/acp/rebuild_attachments_com.woltlab.wcf_2.1.4.php new file mode 100644 index 0000000000..a891d723ae --- /dev/null +++ b/wcfsetup/install/files/acp/rebuild_attachments_com.woltlab.wcf_2.1.4.php @@ -0,0 +1,86 @@ + + * @package com.woltlab.wcf + * @category Community Framework + */ +$minimumAttachmentTime = 1425219870; // time of the release note + +$attachmentsPerRun = 100; +$rebuildData = WCF::getSession()->getVar('__wcfUpdateRebuildAttachments'); +if ($rebuildData === null) { + $sql = "SELECT COUNT(*) + FROM wcf".WCF_N."_attachment + WHERE isImage = ? + AND uploadTime >= ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + 1, + $minimumAttachmentTime + )); + $count = $statement->fetchColumn(); + + $rebuildData = array( + 'i' => 0, + 'max' => 0 + ); + + if ($count) { + $rebuildData['max'] = ceil($count / $attachmentsPerRun); + } +} + +if ($rebuildData['max']) { + // get attachment file data + $attachmentList = new AttachmentList(); + $attachmentList->getConditionBuilder()->add('isImage = ?', array(1)); + $attachmentList->getConditionBuilder()->add('uploadTime >= ?', array($minimumAttachmentTime)); + $attachmentList->sqlOffset = $rebuildData['i'] * $attachmentsPerRun; + $attachmentList->sqlLimit = $attachmentsPerRun; + $attachmentList->readObjects(); + + if (!count($attachmentList)) { + // all relevant attachments have been processed + WCF::getSession()->unregister('__wcfUpdateRebuildAttachments'); + } + else { + $attachmentUpdates = array(); + foreach ($attachmentList as $attachment) { + $filesize = filesize($attachment->getLocation()); + if ($filesize != $attachment->filesize) { + $attachmentUpdates[$attachment->attachmentID] = $filesize; + } + } + + if (!empty($attachmentUpdates)) { + $sql = "UPDATE wcf".WCF_N."_attachment + SET filesize = ? + WHERE attachmentID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + + WCF::getDB()->beginTransaction(); + foreach ($attachmentUpdates as $attachmentID => $filesize) { + $statement->execute(array( + $filesize, + $attachmentID + )); + } + WCF::getDB()->commitTransaction(); + } + + // update rebuiled data + $rebuildData['i']++; + WCF::getSession()->register('__wcfUpdateRebuildAttachments', $rebuildData); + + throw new SplitNodeException(); + } +} -- 2.20.1