From: Matthias Schmidt Date: Mon, 3 May 2021 06:19:14 +0000 (+0200) Subject: Add `PersonRebuildDataWorker` to part 5 of tutorial series X-Git-Tag: 5.6.final~249 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=82b63e4b6b6ca065500c3990bfc55563874172b3;p=GitHub%2FWoltLab%2Fwoltlab.github.io.git Add `PersonRebuildDataWorker` to part 5 of tutorial series Close #168 --- diff --git a/docs/tutorial/series/part_5.md b/docs/tutorial/series/part_5.md index a9fb8431..12f84425 100644 --- a/docs/tutorial/series/part_5.md +++ b/docs/tutorial/series/part_5.md @@ -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 index 00000000..2dd1823f --- /dev/null +++ b/snippets/tutorial/tutorial-series/part-5/files/lib/system/worker/PersonRebuildDataWorker.class.php @@ -0,0 +1,66 @@ + + * @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(); + } +} diff --git a/snippets/tutorial/tutorial-series/part-5/objectType.xml b/snippets/tutorial/tutorial-series/part-5/objectType.xml index 120c761d..15763130 100644 --- a/snippets/tutorial/tutorial-series/part-5/objectType.xml +++ b/snippets/tutorial/tutorial-series/part-5/objectType.xml @@ -25,5 +25,10 @@ com.woltlab.wcf.people.information com.woltlab.wcf.message + + com.woltlab.wcf.people.person + com.woltlab.wcf.rebuildData + wcf\system\worker\PersonRebuildDataWorker +