Add update script for 2.1.4 to update image attachment filesizes
authorMatthias Schmidt <gravatronics@live.com>
Fri, 8 May 2015 16:09:38 +0000 (18:09 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 8 May 2015 16:09:38 +0000 (18:09 +0200)
wcfsetup/install/files/acp/rebuild_attachments_com.woltlab.wcf_2.1.4.php [new file with mode: 0644]

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 (file)
index 0000000..a891d72
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+use wcf\data\attachment\AttachmentList;
+use wcf\system\package\SplitNodeException;
+use wcf\system\WCF;
+
+/**
+ * Checks the filesize of all image attachments since the release of WCF 2.1 due
+ * to issues with rotated images whose filesize is incorrect.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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();
+       }
+}