|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\DoctrineMigrations; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration; |
|
13
|
|
|
use Doctrine\DBAL\Migrations\SkipMigrationException; |
|
14
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
21
|
|
|
*/ |
|
22
|
|
|
class Version20131015113854_ChangeImagePaths extends AbstractMigration implements ContainerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Media dir. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $media_dir; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set container. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ContainerInterface $container |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setContainer(ContainerInterface $container = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->media_dir = $container->getParameter('kernel.root_dir').'/../web/media/'; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Schema $schema |
|
43
|
|
|
* |
|
44
|
|
|
* @throws SkipMigrationException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function up(Schema $schema) |
|
47
|
|
|
{ |
|
48
|
|
|
/* |
|
49
|
|
|
* Migration is not critical |
|
50
|
|
|
* Old format for storing image files to the new format |
|
51
|
|
|
* |
|
52
|
|
|
* Old format(date added image): |
|
53
|
|
|
* media/{Y}/{m}/ |
|
54
|
|
|
* |
|
55
|
|
|
* New Format(date added item): |
|
56
|
|
|
* media/{Y}/{m}/{d}/{His}/ |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
|
|
// move covers |
|
60
|
|
|
$items = $this->connection->fetchAll(' |
|
61
|
|
|
SELECT |
|
62
|
|
|
`id`, |
|
63
|
|
|
`cover`, |
|
64
|
|
|
`date_add` |
|
65
|
|
|
FROM |
|
66
|
|
|
`item` |
|
67
|
|
|
WHERE |
|
68
|
|
|
`cover` IS NOT NULL AND |
|
69
|
|
|
`cover` != "" AND |
|
70
|
|
|
`cover` NOT LIKE "example/%"' |
|
71
|
|
|
); |
|
72
|
|
View Code Duplication |
foreach ($items as $item) { |
|
|
|
|
|
|
73
|
|
|
$path = date('Y/m/d/His/', strtotime($item['date_add'])); |
|
74
|
|
|
$file = new File($this->media_dir.$item['cover']); |
|
75
|
|
|
$file->move($this->media_dir.$path); |
|
76
|
|
|
$this->addSql(' |
|
77
|
|
|
UPDATE |
|
78
|
|
|
`item` |
|
79
|
|
|
SET |
|
80
|
|
|
`cover` = ? |
|
81
|
|
|
WHERE |
|
82
|
|
|
`id` = ?', |
|
83
|
|
|
[ |
|
84
|
|
|
$path.$file->getBasename(), |
|
85
|
|
|
$item['id'], |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// move images |
|
91
|
|
|
$images = $this->connection->fetchAll(' |
|
92
|
|
|
SELECT |
|
93
|
|
|
im.`id`, |
|
94
|
|
|
im.`source`, |
|
95
|
|
|
i.`date_add` |
|
96
|
|
|
FROM |
|
97
|
|
|
`item` AS `i` |
|
98
|
|
|
INNER JOIN |
|
99
|
|
|
`image` AS `im` |
|
100
|
|
|
ON |
|
101
|
|
|
im.`item` = i.`id`' |
|
102
|
|
|
); |
|
103
|
|
View Code Duplication |
foreach ($images as $image) { |
|
|
|
|
|
|
104
|
|
|
$path = date('Y/m/d/His/', strtotime($image['date_add'])); |
|
105
|
|
|
$file = new File($this->media_dir.$image['source']); |
|
106
|
|
|
$file->move($this->media_dir.$path); |
|
107
|
|
|
$this->addSql(' |
|
108
|
|
|
UPDATE |
|
109
|
|
|
`image` |
|
110
|
|
|
SET |
|
111
|
|
|
`source` = ? |
|
112
|
|
|
WHERE |
|
113
|
|
|
`id` = ?', |
|
114
|
|
|
[ |
|
115
|
|
|
$path.$file->getBasename(), |
|
116
|
|
|
$image['id'], |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// skip if no data |
|
122
|
|
|
$this->skipIf(!($items && $images), 'No data to migrate'); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param Schema $schema |
|
127
|
|
|
* |
|
128
|
|
|
* @throws SkipMigrationException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function down(Schema $schema) |
|
131
|
|
|
{ |
|
132
|
|
|
// the down migration is not need |
|
133
|
|
|
$this->skipIf(true, 'The down migration is not need'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: