Added stat rebuild worker
authorMarcel Werk <burntime@woltlab.com>
Sat, 19 Apr 2014 22:51:31 +0000 (00:51 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sat, 19 Apr 2014 22:51:31 +0000 (00:51 +0200)
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/worker/StatDailyRebuildDataWorker.class.php [new file with mode: 0644]

index 94738961e9d5e2adb7fa9d88cf9875852a3dc97b..95d095a02f818dcd5e036fa37ec92daf0723b277 100644 (file)
                        <classname><![CDATA[wcf\system\worker\AttachmentRebuildDataWorker]]></classname>
                        <nicevalue>100</nicevalue>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.statDaily</name>
+                       <definitionname>com.woltlab.wcf.rebuildData</definitionname>
+                       <classname><![CDATA[wcf\system\worker\StatDailyRebuildDataWorker]]></classname>
+                       <nicevalue>110</nicevalue>
+               </type>
                <!-- /rebuild data workers -->
                
                <!-- stat handlers -->
diff --git a/wcfsetup/install/files/lib/system/worker/StatDailyRebuildDataWorker.class.php b/wcfsetup/install/files/lib/system/worker/StatDailyRebuildDataWorker.class.php
new file mode 100644 (file)
index 0000000..e3916b1
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+namespace wcf\system\worker;
+use wcf\data\object\type\ObjectTypeCache;
+use wcf\system\event\EventHandler;
+use wcf\system\WCF;
+use wcf\util\DateUtil;
+
+/**
+ * Worker implementation for updating daily statistics.
+ * 
+ * @author     Marcel Werk
+ * @copyright  2001-2014 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.worker
+ * @category   Community Framework
+ */
+class StatDailyRebuildDataWorker extends AbstractRebuildDataWorker {
+       /**
+        * @see \wcf\system\worker\AbstractRebuildDataWorker::$objectListClassName
+        */
+       protected $objectListClassName = 'wcf\data\attachment\AttachmentList';
+       
+       /**
+        * @see \wcf\system\worker\AbstractWorker::$limit
+        */
+       protected $limit = 30;
+       
+       /**
+        * start timestamp
+        * @var integer
+        */
+       protected $startDate = 0;
+       
+       /**
+        * @see \wcf\system\worker\AbstractRebuildDataWorker::initObjectList()
+        */
+       protected function initObjectList() {}
+       
+       /**
+        * @see \wcf\system\worker\IWorker::countObjects()
+        */
+       public function countObjects() {
+               $this->getStartDate();
+               
+               $this->count = ceil((TIME_NOW - $this->startDate) / 86400);
+       }
+       
+       /**
+        * @see \wcf\system\worker\IWorker::execute()
+        */
+       public function execute() {
+               EventHandler::getInstance()->fireAction($this, 'execute');
+               
+               if (!$this->loopCount) {
+                       // delete existing stat
+                       $sql = "DELETE FROM     wcf".WCF_N."_stat_daily";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute();
+               }
+               
+               // prepare insert statement
+               $sql = "INSERT IGNORE INTO      wcf".WCF_N."_stat_daily
+                                               (objectTypeID, date, counter, total)
+                       VALUES                  (?, ?, ?, ?)";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               
+               $this->getStartDate();
+               $d = DateUtil::getDateTimeByTimestamp($this->startDate);
+               $d->setTimezone(new \DateTimeZone(TIMEZONE));
+               $d->setTime(0, 0);
+               if ($this->loopCount) {
+                       $d->add(new \DateInterval('P'.($this->loopCount * $this->limit).'D'));
+               }
+               for ($i = 0; $i < $this->limit; $i++) {
+                       // get object types
+                       foreach (ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.statDailyHandler') as $objectType) {
+                               $data = $objectType->getProcessor()->getData($d->getTimestamp());
+                               $statement->execute(array($objectType->objectTypeID, $d->format('Y-m-d'), $data['counter'], $data['total']));
+                       }
+                       
+                       $d->add(new \DateInterval('P1D'));
+               }
+       }
+       
+       /**
+        * Gets the start timestamp.
+        * 
+        * @return      integer
+        */
+       protected function getStartDate() {
+               if ($this->startDate) return;
+               $sql = "SELECT  MIN(registrationDate)
+                       FROM    wcf".WCF_N."_user";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute();
+               $this->startDate = $statement->fetchColumn();
+       }
+}