Merge branch '3.1' into 5.2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / acp / update_com.woltlab.wcf_5.2_reactionUpdate.php
1 <?php
2 use wcf\data\language\item\LanguageItemEditor;
3 use wcf\data\like\Like;
4 use wcf\data\option\OptionEditor;
5 use wcf\system\language\LanguageFactory;
6 use wcf\system\WCF;
7
8 // !!!!!!!!!
9 // HEADS UP! The columns for wcf1_like, wcf1_like_object and the wcf1_reaction_type table must already exists, before calling this script.
10 // HEADS UP! The foreign key for the wcf1_like table will be created within this script, after providing real values for the reactionTypeID
11 // HEADS UP! column. Also this script provides the basic reactionTypes.
12 // !!!!!!!!!
13
14 /**
15 * @author Joshua Ruesweg
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core
19 */
20
21 OptionEditor::import([
22 'like_show_summary' => 1,
23 ]);
24
25 try {
26 WCF::getDB()->beginTransaction();
27
28 $reactions = ['like', 'thanks', 'haha', 'confused', 'sad'];
29 if (LIKE_ENABLE_DISLIKE) {
30 // Remove the existing phrase in case a previous upgrade attempt has failed.
31 $sql = "DELETE FROM wcf".WCF_N."_language_item
32 WHERE languageItem = ?";
33 $statement = WCF::getDB()->prepareStatement($sql);
34 $statement->execute(['wcf.reactionType.title6']);
35
36 $reactions[] = 'thumbsDown';
37
38 $sql = "SELECT languageCategoryID
39 FROM wcf".WCF_N."_language_category
40 WHERE languageCategory = ?";
41 $statement = WCF::getDB()->prepareStatement($sql, 1);
42 $statement->execute(['wcf.reactionType']);
43 $languageCategoryID = $statement->fetchSingleColumn();
44
45 // Create a custom phrase for this reaction, it needs to be "manually" added
46 // because it would otherwise conflict with the next reaction created by the
47 // user, *if* there are no dislikes.
48 foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
49 LanguageItemEditor::create([
50 'languageID' => $language->languageID,
51 'languageItem' => 'wcf.reactionType.title6',
52 'languageItemValue' => ($language->getFixedLanguageCode() === 'de' ? 'Gefällt mir nicht' : 'Dislike'),
53 'languageCategoryID' => $languageCategoryID,
54 'packageID' => 1,
55 ]);
56 }
57 }
58
59 $sql = "INSERT IGNORE INTO wcf".WCF_N."_reaction_type
60 (reactionTypeID, title, showOrder, iconFile)
61 VALUES (?, ?, ?, ?)";
62 $statement = WCF::getDB()->prepareStatement($sql);
63 for ($i = 0, $length = count($reactions); $i < $length; $i++) {
64 $reactionTypeID = $i + 1;
65
66 $statement->execute([
67 $reactionTypeID,
68 "wcf.reactionType.title{$reactionTypeID}",
69 $reactionTypeID,
70 "{$reactions[$i]}.svg",
71 ]);
72 }
73
74 // Update the existing (dis)likes.
75 $likeValues = [Like::LIKE => 1];
76 if (LIKE_ENABLE_DISLIKE) $likeValues[Like::DISLIKE] = 6;
77
78 $sql = "UPDATE wcf".WCF_N."_like
79 SET reactionTypeID = ?
80 WHERE likeValue = ?";
81 $statement = WCF::getDB()->prepareStatement($sql);
82 foreach ($likeValues as $likeValue => $reactionTypeID) {
83 $statement->execute([
84 $reactionTypeID,
85 $likeValue,
86 ]);
87 }
88
89 // Delete outdated or unsupported likes.
90 WCF::getDB()->prepareStatement("DELETE FROM wcf".WCF_N."_like WHERE reactionTypeID = 0")->execute();
91
92 // Adjust the like objects by moving all dislikes into regular likes/cumulativeLikes.
93 $sql = "UPDATE wcf".WCF_N."_like_object
94 SET likes = likes + dislikes,
95 cumulativeLikes = likes,
96 dislikes = 0";
97 WCF::getDB()->prepareStatement($sql)->execute();
98
99 $dbEditor = WCF::getDB()->getEditor();
100 $foreignKeys = $dbEditor->getForeignKeys('wcf'.WCF_N.'_like');
101 $expectedKey = 'fe5076ee92a558ce8177e3afbfc3dafc_fk';
102 $hasExpectedKey = false;
103
104 // Find the previously added foreign key, in case the upgrade was interrupted before.
105 foreach ($foreignKeys as $indexName => $definition) {
106 if ($indexName === $expectedKey) {
107 $hasExpectedKey = true;
108 break;
109 }
110
111 if ($definition['referencedTable'] === 'wcf'.WCF_N.'_reaction_type') {
112 if (count($definition['columns']) === 1 && $definition['columns'][0] === 'reactionTypeID') {
113 $dbEditor->dropForeignKey('wcf'.WCF_N.'_like', $indexName);
114 }
115 }
116 }
117
118 if (!$hasExpectedKey) {
119 $dbEditor->addForeignKey('wcf' . WCF_N . '_like', $expectedKey, [
120 'columns' => 'reactionTypeID',
121 'referencedColumns' => 'reactionTypeID',
122 'referencedTable' => 'wcf'.WCF_N.'_reaction_type',
123 'ON DELETE' => 'CASCADE',
124 ]);
125 }
126
127 WCF::getDB()->commitTransaction();
128 }
129 catch (Exception $e) {
130 WCF::getDB()->rollBackTransaction();
131
132 throw $e;
133 }