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
;
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.
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
21 OptionEditor
::import([
22 'like_show_summary' => 1,
26 WCF
::getDB()->beginTransaction();
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']);
36 $reactions[] = 'thumbsDown';
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();
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,
59 $sql = "INSERT IGNORE INTO wcf".WCF_N
."_reaction_type
60 (reactionTypeID, title, showOrder, iconFile)
62 $statement = WCF
::getDB()->prepareStatement($sql);
63 for ($i = 0, $length = count($reactions); $i < $length; $i++
) {
64 $reactionTypeID = $i +
1;
68 "wcf.reactionType.title{$reactionTypeID}",
70 "{$reactions[$i]}.svg",
74 // Update the existing (dis)likes.
75 $likeValues = [Like
::LIKE
=> 1];
76 if (LIKE_ENABLE_DISLIKE
) $likeValues[Like
::DISLIKE
] = 6;
78 $sql = "UPDATE wcf".WCF_N
."_like
79 SET reactionTypeID = ?
81 $statement = WCF
::getDB()->prepareStatement($sql);
82 foreach ($likeValues as $likeValue => $reactionTypeID) {
89 // Delete outdated or unsupported likes.
90 WCF
::getDB()->prepareStatement("DELETE FROM wcf".WCF_N
."_like WHERE reactionTypeID = 0")->execute();
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,
97 WCF
::getDB()->prepareStatement($sql)->execute();
99 $dbEditor = WCF
::getDB()->getEditor();
100 $foreignKeys = $dbEditor->getForeignKeys('wcf'.WCF_N
.'_like');
101 $expectedKey = 'fe5076ee92a558ce8177e3afbfc3dafc_fk';
102 $hasExpectedKey = false;
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;
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);
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',
127 WCF
::getDB()->commitTransaction();
129 catch (Exception
$e) {
130 WCF
::getDB()->rollBackTransaction();