Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
storage_track
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 8
380
0.00% covered (danger)
0.00%
0 / 1
 effectively_installed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 depends_on
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 update_schema
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 revert_schema
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 track_avatars
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 track_attachments
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 track_backups
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration\data\v400;
15
16use phpbb\db\migration\container_aware_migration;
17use phpbb\storage\exception\storage_exception;
18use phpbb\storage\storage;
19
20class storage_track extends container_aware_migration
21{
22    public function effectively_installed()
23    {
24        return $this->db_tools->sql_table_exists($this->tables['storage']);
25    }
26
27    public static function depends_on()
28    {
29        return [
30            '\phpbb\db\migration\data\v400\storage_attachment',
31            '\phpbb\db\migration\data\v400\storage_avatar',
32            '\phpbb\db\migration\data\v400\storage_backup',
33        ];
34    }
35
36    public function update_schema()
37    {
38        return [
39            'add_tables' => [
40                $this->table_prefix . 'storage'    => [
41                    'COLUMNS' => [
42                        'file_id'            => ['UINT', null, 'auto_increment'],
43                        'file_path'            => ['VCHAR', ''],
44                        'storage'            => ['VCHAR', ''],
45                        'filesize'            => ['UINT:20', 0],
46                    ],
47                    'PRIMARY_KEY'    => 'file_id',
48                ],
49            ],
50        ];
51    }
52
53    public function revert_schema()
54    {
55        return [
56            'drop_tables'    => [
57                $this->table_prefix . 'storage',
58            ],
59        ];
60    }
61
62    public function update_data()
63    {
64        return [
65            ['custom', [[$this, 'track_avatars']]],
66            ['custom', [[$this, 'track_attachments']]],
67            ['custom', [[$this, 'track_backups']]],
68        ];
69    }
70
71    public function track_avatars()
72    {
73        /** @var storage $storage */
74        $storage = $this->container->get('storage.avatar');
75
76        $sql = 'SELECT user_avatar
77            FROM ' . USERS_TABLE . "
78            WHERE user_avatar_type = 'avatar.driver.upload'";
79
80        $result = $this->db->sql_query($sql);
81
82        while ($row = $this->db->sql_fetchrow($result))
83        {
84            $avatar_group = false;
85            $filename = $row['user_avatar'];
86
87            if (isset($filename[0]) && $filename[0] === 'g')
88            {
89                $avatar_group = true;
90                $filename = substr($filename, 1);
91            }
92
93            $ext        = substr(strrchr($filename, '.'), 1);
94            $filename    = (int) $filename;
95
96            try
97            {
98                $storage->track_file($this->config['avatar_salt'] . '_' . ($avatar_group ? 'g' : '') . $filename . '.' . $ext);
99            }
100            catch (storage_exception $e)
101            {
102                // If file doesn't exist, don't track it
103            }
104        }
105        $this->db->sql_freeresult($result);
106    }
107
108    public function track_attachments()
109    {
110        /** @var storage $storage */
111        $storage = $this->container->get('storage.attachment');
112
113        $sql = 'SELECT physical_filename, thumbnail
114            FROM ' . ATTACHMENTS_TABLE;
115
116        $result = $this->db->sql_query($sql);
117
118        while ($row = $this->db->sql_fetchrow($result))
119        {
120            try
121            {
122                $storage->track_file($row['physical_filename']);
123            }
124            catch (storage_exception $e)
125            {
126                // If file doesn't exist, don't track it
127            }
128
129            if ($row['thumbnail'] == 1)
130            {
131                try
132                {
133                    $storage->track_file('thumb_' . $row['physical_filename']);
134                }
135                catch (storage_exception $e)
136                {
137                    // If file doesn't exist, don't track it
138                }
139            }
140        }
141        $this->db->sql_freeresult($result);
142    }
143
144    public function track_backups()
145    {
146        /** @var storage $storage */
147        $storage = $this->container->get('storage.backup');
148
149        $sql = 'SELECT filename
150            FROM ' . BACKUPS_TABLE;
151
152        $result = $this->db->sql_query($sql);
153
154        while ($row = $this->db->sql_fetchrow($result))
155        {
156            try
157            {
158                $storage->track_file($row['filename']);
159            }
160            catch (storage_exception $e)
161            {
162                // If file doesn't exist, don't track it
163            }
164        }
165
166        $this->db->sql_freeresult($result);
167    }
168}