Add `PersonRebuildDataWorker` to part 5 of tutorial series
authorMatthias Schmidt <gravatronics@live.com>
Mon, 3 May 2021 06:19:14 +0000 (08:19 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 3 May 2021 06:19:14 +0000 (08:19 +0200)
Close #168

docs/tutorial/series/part_5.md
snippets/tutorial/tutorial-series/part-5/files/lib/system/worker/PersonRebuildDataWorker.class.php [new file with mode: 0644]
snippets/tutorial/tutorial-series/part-5/objectType.xml

index a9fb8431d90e09b5fc683bea442714209423c028..12f84425be5c01a7c129ed806611bffbf1c327cb 100644 (file)
@@ -33,14 +33,17 @@ The package will have the following file structure _excluding_ unchanged files f
 │   │           └── Controller
 │   │               └── Person.js
 │   └── lib
-│       └── data
-│           └── person
-│               ├── Person.class.php
-│               └── information
-│                   ├── PersonInformation.class.php
-│                   ├── PersonInformationAction.class.php
-│                   ├── PersonInformationEditor.class.php
-│                   └── PersonInformationList.class.php
+│       ├── data
+│       │   └── person
+│       │       ├── Person.class.php
+│       │       └── information
+│       │           ├── PersonInformation.class.php
+│       │           ├── PersonInformationAction.class.php
+│       │           ├── PersonInformationEditor.class.php
+│       │           └── PersonInformationList.class.php
+│       └── system
+│           └── worker
+│               └── PersonRebuildDataWorker.class.php
 ├── language
 │   ├── de.xml
 │   └── en.xml
@@ -198,6 +201,17 @@ Instead of the id of the person, however, we now pass the id of the edited piece
 After editing a piece of information, we do not reload the page but dynamically update the text of the information in the TypeScript code so that we return the updated rendered information text and id of the edited pieced of information in `submitAddDialog()`.
 
 
+## Rebuild Data Worker
+
+To ensure the integrity of the person data, `PersonRebuildDataWorker` updates the `informationCount` counter:
+
+{jinja{ codebox(
+title="files/lib/system/worker/PersonRebuildDataWorker.class.php",
+language="php",
+filepath="tutorial/tutorial-series/part-5/files/lib/system/worker/PersonRebuildDataWorker.class.php"
+) }}
+
+
 ## Username and IP Address Event Listeners
 
 As we store the name of the user who create a new piece of information and store their IP address, we have to add event listeners to properly handle the following scenarios:
diff --git a/snippets/tutorial/tutorial-series/part-5/files/lib/system/worker/PersonRebuildDataWorker.class.php b/snippets/tutorial/tutorial-series/part-5/files/lib/system/worker/PersonRebuildDataWorker.class.php
new file mode 100644 (file)
index 0000000..2dd1823
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+namespace wcf\system\worker;
+
+use wcf\data\person\PersonList;
+use wcf\system\WCF;
+
+/**
+ * Worker implementation for updating people.
+ *
+ * @author  Matthias Schmidt
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Worker
+ *
+ * @method  PersonList  getObjectList()
+ */
+class PersonRebuildDataWorker extends AbstractRebuildDataWorker
+{
+    /**
+     * @inheritDoc
+     */
+    protected $limit = 500;
+
+    /**
+     * @inheritDoc
+     */
+    protected $objectListClassName = PersonList::class;
+
+    /**
+     * @inheritDoc
+     */
+    protected function initObjectList()
+    {
+        parent::initObjectList();
+
+        $this->objectList->sqlOrderBy = 'person.personID';
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function execute()
+    {
+        parent::execute();
+
+        if (!\count($this->objectList)) {
+            return;
+        }
+        
+        $sql = "UPDATE  wcf" . WCF_N . "_person person
+                SET     informationCount = (
+                            SELECT  COUNT(*)
+                            FROM    wcf" . WCF_N . "_person_information person_information
+                            WHERE   person_information.personID = person.personID
+                        )
+                WHERE   person.personID = ?";
+        $statement = WCF::getDB()->prepareStatement($sql);
+        
+        WCF::getDB()->beginTransaction();
+        foreach ($this->getObjectList() as $person) {
+            $statement->execute([$person->personID]);
+        }
+        WCF::getDB()->commitTransaction();
+    }
+}
index 120c761d8ad9b099af101efa5033de226083a683..1576313071050f601c4b8ca0f030f3751219568c 100644 (file)
                        <name>com.woltlab.wcf.people.information</name>
                        <definitionname>com.woltlab.wcf.message</definitionname>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.people.person</name>
+                       <definitionname>com.woltlab.wcf.rebuildData</definitionname>
+                       <classname>wcf\system\worker\PersonRebuildDataWorker</classname>
+               </type>
        </import>
 </data>