1
+ <?php
2
+ /**
3
+ * River
4
+ * @author Nikolai Shcherbin
5
+ * @package Plugin
6
+ * @license GNU Affero General Public License version 3
7
+ * @copyright (c) Nikolai Shcherbin 2019
8
+ * @link https://wzm.me
9
+ **/
10
+ namespace Market \Upgrades ;
11
+
12
+ use Elgg \Database \Select ;
13
+ use Elgg \Database \Update ;
14
+ use Elgg \Upgrade \AsynchronousUpgrade ;
15
+ use Elgg \Upgrade \Result ;
16
+
17
+ class MigrateMarketFiles implements AsynchronousUpgrade {
18
+ /**
19
+ * Version of the upgrade
20
+ *
21
+ * @return int
22
+ */
23
+ public function getVersion (): int {
24
+ return 2023011200 ;
25
+ }
26
+
27
+ /**
28
+ * Should the run() method receive an offset representing all processed items?
29
+ *
30
+ * @return bool
31
+ */
32
+ public function needsIncrementOffset (): bool {
33
+ return false ;
34
+ }
35
+
36
+ /**
37
+ * Should this upgrade be skipped?
38
+ *
39
+ * @return bool
40
+ */
41
+ public function shouldBeSkipped (): bool {
42
+ return empty ($ this ->countItems ());
43
+ }
44
+
45
+ /**
46
+ * The total number of items to process in the upgrade
47
+ *
48
+ * @return int
49
+ */
50
+ public function countItems (): int {
51
+ return elgg_call (ELGG_SHOW_DISABLED_ENTITIES , function () {
52
+ return elgg_count_entities ([
53
+ 'type ' => 'object ' ,
54
+ 'subtype ' => 'file ' ,
55
+ 'metadata_name_value_pairs ' => [
56
+ [
57
+ 'name ' => 'origin ' ,
58
+ 'value ' => 'market ' ,
59
+ ],
60
+ ],
61
+ ]);
62
+ });
63
+ }
64
+
65
+ /**
66
+ * Runs upgrade on a single batch of items
67
+ *
68
+ * @param Result $result Result of the batch (this must be returned)
69
+ * @param int $offset Number to skip when processing
70
+ *
71
+ * @return Result Instance of \Elgg\Upgrade\Result
72
+ */
73
+ public function run (Result $ result , $ offset ): Result {
74
+ $ select = Select::fromTable ('metadata ' , 'md ' );
75
+ $ select ->select ('entity_guid ' )
76
+ ->where ($ select ->compare ('md.name ' , '= ' , 'origin ' , ELGG_VALUE_STRING ))
77
+ ->andWhere ($ select ->compare ('md.value ' , '= ' , 'market ' , ELGG_VALUE_STRING ));
78
+
79
+ $ guids = _elgg_services ()->db ->getData ($ select , function ($ row ) {
80
+ return (int ) $ row ->entity_guid ;
81
+ });
82
+
83
+ if (!empty ($ guids )) {
84
+ $ update = Update::table ('entities ' );
85
+ $ update ->set ('subtype ' , '"market_file" ' )
86
+ ->where ('subtype = "file" ' )
87
+ ->andWhere ($ update ->compare ('guid ' , 'in ' , $ guids , ELGG_VALUE_GUID ));
88
+
89
+ _elgg_services ()->db ->updateData ($ update );
90
+ }
91
+
92
+ $ result ->addSuccesses (count ($ guids ));
93
+
94
+ return $ result ;
95
+ }
96
+
97
+ }
0 commit comments