1 | <?php |
||
2 | |||
3 | namespace Elgg\Groups\Upgrades; |
||
4 | |||
5 | use Elgg\Upgrade\Batch; |
||
6 | use Elgg\Upgrade\Result; |
||
7 | |||
8 | /** |
||
9 | * Moves group icons owned by user to directory owned by the groups itself. |
||
10 | * |
||
11 | * BEFORE: /dataroot/<bucket>/<owner_guid>/groups/<group_guid><size>.jpg |
||
12 | * AFTER: /dataroot/<bucket>/<group_guid>/icons/icon/<size>.jpg |
||
13 | */ |
||
14 | class GroupIconTransfer implements Batch { |
||
15 | |||
16 | public function getVersion() { |
||
17 | return 2016101900; |
||
18 | } |
||
19 | |||
20 | public function needsIncrementOffset() { |
||
21 | return true; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function shouldBeSkipped() { |
||
28 | $groups = elgg_get_entities([ |
||
29 | 'types' => 'group', |
||
30 | 'metadata_names' => 'icontime', |
||
31 | ]); |
||
32 | |||
33 | if (empty($groups)) { |
||
34 | return true; |
||
35 | } |
||
36 | |||
37 | $group = array_pop($groups); |
||
38 | |||
39 | $sizes = elgg_get_icon_sizes('group', $group->getSubtype()); |
||
40 | |||
41 | $dataroot = elgg_get_config('dataroot'); |
||
42 | $dir = (new \Elgg\EntityDirLocator($group->owner_guid))->getPath(); |
||
43 | $prefix = 'groups/'; |
||
44 | |||
45 | // Check whether there are icons that are still saved under the |
||
46 | // group's owner instead of the group itself. |
||
47 | foreach ($sizes as $size => $opts) { |
||
48 | if ($size == 'original') { |
||
49 | $filename = "{$group->guid}.jpg"; |
||
50 | } else { |
||
51 | $filename = "{$group->guid}{$size}.jpg"; |
||
52 | } |
||
53 | $filestorename = "{$dataroot}{$dir}{$prefix}{$filename}"; |
||
54 | if (file_exists($filestorename)) { |
||
55 | // A group icon was found meaning that the upgrade is needed. |
||
56 | return false; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return true; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function countItems() { |
||
67 | return elgg_get_entities([ |
||
68 | 'types' => 'group', |
||
69 | 'count' => true, |
||
70 | ]); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function run(Result $result, $offset) { |
||
77 | |||
78 | $groups = elgg_get_entities([ |
||
79 | 'types' => 'group', |
||
80 | 'offset' => $offset, |
||
81 | 'limit' => 10, |
||
82 | ]); |
||
83 | |||
84 | foreach ($groups as $group) { |
||
85 | $result = $this->transferIcons($group, $result); |
||
86 | } |
||
87 | |||
88 | return $result; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Transfer group icons to new filestore location |
||
93 | * Before 3.0, group icons where owned by the group owner |
||
94 | * and located in /groups/<guid><size>.jpg |
||
95 | * relative to group owner's filestore directory |
||
96 | * In 3.0, we are moving these to default filestore location |
||
97 | * relative to group's filestore directory |
||
98 | * |
||
99 | * @param \ElggGroup $group Group entity |
||
100 | * @param Result $result Upgrade result |
||
101 | * @return Result |
||
102 | */ |
||
103 | public function transferIcons(\ElggGroup $group, Result $result) { |
||
104 | |||
105 | $sizes = elgg_get_icon_sizes('group', $group->getSubtype()); |
||
106 | |||
107 | $dataroot = elgg_get_config('dataroot'); |
||
108 | $dir = (new \Elgg\EntityDirLocator($group->owner_guid))->getPath(); |
||
109 | $prefix = 'groups/'; |
||
110 | |||
111 | foreach ($sizes as $size => $opts) { |
||
112 | if ($size == 'original') { |
||
113 | $filename = "{$group->guid}.jpg"; |
||
114 | } else { |
||
115 | $filename = "{$group->guid}{$size}.jpg"; |
||
116 | } |
||
117 | $filestorename = "{$dataroot}{$dir}{$prefix}{$filename}"; |
||
118 | if (!file_exists($filestorename)) { |
||
119 | // nothing to move |
||
120 | continue; |
||
121 | } |
||
122 | |||
123 | $icon = $group->getIcon($size); |
||
124 | |||
125 | // before transferring the file, we need to make sure |
||
126 | // the directory structure of the new filestore location exists |
||
127 | $icon->open('write'); |
||
128 | $icon->close(); |
||
129 | |||
130 | if (!rename($filestorename, $icon->getFilenameOnFilestore())) { |
||
131 | $result->addError(" |
||
132 | Failed to transfer file from '$filestorename' |
||
133 | to {$icon->getFilenameOnFilestore()} |
||
134 | "); |
||
135 | $error = true; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if ($error) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
140 | $result->addFailures(); |
||
141 | } else { |
||
142 | $result->addSuccesses(); |
||
143 | } |
||
144 | |||
145 | return $result; |
||
146 | } |
||
147 | |||
148 | } |
||
149 |