1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\controllers; |
13
|
|
|
|
14
|
|
|
use nystudio107\retour\Retour; |
15
|
|
|
use nystudio107\retour\assetbundles\retour\RetourImportAsset; |
16
|
|
|
use nystudio107\retour\helpers\MultiSite as MultiSiteHelper; |
17
|
|
|
use nystudio107\retour\helpers\Permission as PermissionHelper; |
18
|
|
|
use nystudio107\retour\helpers\Version as VersionHelper; |
19
|
|
|
|
20
|
|
|
use Craft; |
21
|
|
|
use craft\db\Query; |
22
|
|
|
use craft\helpers\ArrayHelper; |
23
|
|
|
use craft\helpers\UrlHelper; |
24
|
|
|
use craft\web\Controller; |
25
|
|
|
|
26
|
|
|
use yii\base\InvalidConfigException; |
27
|
|
|
use yii\web\Response; |
28
|
|
|
use yii\web\UploadedFile; |
29
|
|
|
|
30
|
|
|
use League\Csv\AbstractCsv; |
31
|
|
|
use League\Csv\Reader; |
32
|
|
|
use League\Csv\Statement; |
|
|
|
|
33
|
|
|
use League\Csv\Writer; |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @author nystudio107 |
|
|
|
|
37
|
|
|
* @package Retour |
|
|
|
|
38
|
|
|
* @since 3.0.0 |
|
|
|
|
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
class FileController extends Controller |
41
|
|
|
{ |
42
|
|
|
// Constants |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/'; |
46
|
|
|
|
47
|
|
|
const EXPORT_REDIRECTS_CSV_FIELDS = [ |
48
|
|
|
'redirectSrcUrl' => 'Legacy URL Pattern', |
49
|
|
|
'redirectDestUrl' => 'Redirect To', |
50
|
|
|
'redirectMatchType' => 'Match Type', |
51
|
|
|
'redirectHttpCode' => 'HTTP Status', |
52
|
|
|
'siteId' => 'Site ID', |
53
|
|
|
'redirectSrcMatch' => 'Legacy URL Match Type', |
54
|
|
|
'hitCount' => 'Hits', |
55
|
|
|
'hitLastTime' => 'Last Hit', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
const EXPORT_STATISTICS_CSV_FIELDS = [ |
59
|
|
|
'redirectSrcUrl' => '404 File Not Found URL', |
60
|
|
|
'referrerUrl' => 'Last Referrer URL', |
61
|
|
|
'remoteIp' => 'Remote IP', |
62
|
|
|
'hitCount' => 'Hits', |
63
|
|
|
'hitLastTime' => 'Last Hit', |
64
|
|
|
'handledByRetour' => 'Handled', |
65
|
|
|
'siteId' => 'Site ID', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
const IMPORT_REDIRECTS_CSV_FIELDS = [ |
69
|
|
|
'redirectSrcUrl', |
70
|
|
|
'redirectDestUrl', |
71
|
|
|
'redirectMatchType', |
72
|
|
|
'redirectHttpCode', |
73
|
|
|
'siteId', |
74
|
|
|
'redirectSrcMatch', |
75
|
|
|
'hitCount' |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
// Protected Properties |
79
|
|
|
// ========================================================================= |
80
|
|
|
|
81
|
|
|
protected $allowAnonymous = []; |
82
|
|
|
|
83
|
|
|
// Public Methods |
84
|
|
|
// ========================================================================= |
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* @throws \yii\web\BadRequestHttpException |
88
|
|
|
* @throws \yii\web\ForbiddenHttpException |
89
|
|
|
* @throws \craft\errors\MissingComponentException |
90
|
|
|
*/ |
|
|
|
|
91
|
|
|
public function actionImportCsvColumns() |
92
|
|
|
{ |
93
|
|
|
PermissionHelper::controllerPermissionCheck('retour:redirects'); |
94
|
|
|
// If your CSV document was created or is read on a Macintosh computer, |
95
|
|
|
// add the following lines before using the library to help PHP detect line ending in Mac OS X |
96
|
|
|
if (!ini_get('auto_detect_line_endings')) { |
97
|
|
|
ini_set('auto_detect_line_endings', '1'); |
98
|
|
|
} |
99
|
|
|
$this->requirePostRequest(); |
100
|
|
|
$filename = Craft::$app->getRequest()->getRequiredBodyParam('filename'); |
101
|
|
|
$columns = Craft::$app->getRequest()->getRequiredBodyParam('columns'); |
102
|
|
|
$headers = null; |
|
|
|
|
103
|
|
|
$csv = Reader::createFromPath($filename); |
104
|
|
|
try { |
105
|
|
|
$headers = array_flip($csv->fetchOne(0)); |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
// If this throws an exception, try to read the CSV file from the data cache |
108
|
|
|
// This can happen on load balancer setups where the Craft temp directory isn't shared |
109
|
|
|
$cache = Craft::$app->getCache(); |
110
|
|
|
$cachedFile = $cache->get($filename); |
111
|
|
|
if ($cachedFile !== false) { |
112
|
|
|
$csv = Reader::createFromString($cachedFile); |
113
|
|
|
$headers = array_flip($csv->fetchOne(0)); |
114
|
|
|
$cache->delete($filename); |
115
|
|
|
} else { |
116
|
|
|
Craft::error("Could not import ${$filename} from the file system, or the cache.", __METHOD__); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
// If we have headers, then we have a file, so parse it |
120
|
|
|
if ($headers !== null) { |
121
|
|
|
switch (VersionHelper::getLeagueCsvVersion()) { |
122
|
|
|
case 8: |
|
|
|
|
123
|
|
|
$this->importCsvApi8($csv, $columns, $headers); |
|
|
|
|
124
|
|
|
break; |
125
|
|
|
case 9: |
|
|
|
|
126
|
|
|
$this->importCsvApi9($csv, $columns, $headers); |
|
|
|
|
127
|
|
|
break; |
128
|
|
|
default: |
|
|
|
|
129
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('retour', 'Unknown league/csv package API version')); |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
@unlink($filename); |
|
|
|
|
133
|
|
|
Retour::$plugin->clearAllCaches(); |
134
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('retour', 'Redirects imported from CSV file.')); |
135
|
|
|
} else { |
136
|
|
|
Craft::$app->getSession()->setError(Craft::t('retour', 'Redirects could not be imported.')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->redirect('retour/redirects'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
|
|
|
|
143
|
|
|
* @param string|null $siteHandle |
|
|
|
|
144
|
|
|
* |
145
|
|
|
* @return Response |
146
|
|
|
* @throws \yii\web\ForbiddenHttpException |
147
|
|
|
* @throws \yii\web\NotFoundHttpException |
148
|
|
|
*/ |
149
|
|
|
public function actionImportCsv(string $siteHandle = null): Response |
150
|
|
|
{ |
151
|
|
|
$variables = []; |
152
|
|
|
PermissionHelper::controllerPermissionCheck('retour:redirects'); |
153
|
|
|
// If your CSV document was created or is read on a Macintosh computer, |
154
|
|
|
// add the following lines before using the library to help PHP detect line ending in Mac OS X |
155
|
|
|
if (!ini_get('auto_detect_line_endings')) { |
156
|
|
|
ini_set('auto_detect_line_endings', '1'); |
157
|
|
|
} |
158
|
|
|
// Get the site to edit |
159
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
160
|
|
|
$pluginName = Retour::$settings->pluginName; |
161
|
|
|
$templateTitle = Craft::t('retour', 'Import CSV File'); |
162
|
|
|
$view = Craft::$app->getView(); |
163
|
|
|
// Asset bundle |
164
|
|
|
try { |
165
|
|
|
$view->registerAssetBundle(RetourImportAsset::class); |
166
|
|
|
} catch (InvalidConfigException $e) { |
167
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
168
|
|
|
} |
169
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
170
|
|
|
'@nystudio107/retour/assetbundles/retour/dist', |
171
|
|
|
true |
172
|
|
|
); |
173
|
|
|
// Enabled sites |
174
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
175
|
|
|
$variables['controllerHandle'] = 'file'; |
176
|
|
|
|
177
|
|
|
// Basic variables |
178
|
|
|
$variables['fullPageForm'] = true; |
179
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
180
|
|
|
$variables['pluginName'] = $pluginName; |
181
|
|
|
$variables['title'] = $templateTitle; |
182
|
|
|
$siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : ''; |
183
|
|
|
$variables['crumbs'] = [ |
184
|
|
|
[ |
185
|
|
|
'label' => $pluginName, |
186
|
|
|
'url' => UrlHelper::cpUrl('retour'), |
187
|
|
|
], |
188
|
|
|
[ |
189
|
|
|
'label' => 'Redirects', |
190
|
|
|
'url' => UrlHelper::cpUrl('retour/redirects'.$siteHandleUri), |
191
|
|
|
], |
192
|
|
|
]; |
193
|
|
|
$variables['docTitle'] = "{$pluginName} - Redirects - {$templateTitle}"; |
194
|
|
|
$variables['selectedSubnavItem'] = 'redirects'; |
195
|
|
|
|
196
|
|
|
// The CSV file |
197
|
|
|
$file = UploadedFile::getInstanceByName('file'); |
198
|
|
|
if ($file !== null) { |
199
|
|
|
$filename = uniqid($file->name, true); |
200
|
|
|
$filePath = Craft::$app->getPath()->getTempPath().DIRECTORY_SEPARATOR.$filename; |
201
|
|
|
$file->saveAs($filePath, false); |
202
|
|
|
// Also save the file to the cache as a backup way to access it |
203
|
|
|
$cache = Craft::$app->getCache(); |
204
|
|
|
$fileHandle = fopen($filePath, 'r'); |
205
|
|
|
if ($fileHandle) { |
|
|
|
|
206
|
|
|
$fileContents = fgets($fileHandle); |
207
|
|
|
if ($fileContents) { |
208
|
|
|
$cache->set($filePath, $fileContents); |
209
|
|
|
} |
210
|
|
|
fclose($fileHandle); |
211
|
|
|
} |
212
|
|
|
// Read in the headers |
213
|
|
|
$csv = Reader::createFromPath($file->tempName); |
214
|
|
|
$headers = $csv->fetchOne(0); |
215
|
|
|
Craft::info(print_r($headers, true), __METHOD__); |
|
|
|
|
216
|
|
|
$variables['headers'] = $headers; |
217
|
|
|
$variables['filename'] = $filePath; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Render the template |
221
|
|
|
return $this->renderTemplate('retour/import/index', $variables); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Export the statistics table as a CSV file |
226
|
|
|
* |
227
|
|
|
* @throws \yii\web\ForbiddenHttpException |
228
|
|
|
*/ |
|
|
|
|
229
|
|
|
public function actionExportStatistics() |
230
|
|
|
{ |
231
|
|
|
PermissionHelper::controllerPermissionCheck('retour:redirects'); |
232
|
|
|
$this->exportCsvFile('retour-statistics', '{{%retour_stats}}', self::EXPORT_STATISTICS_CSV_FIELDS); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Export the redirects table as a CSV file |
237
|
|
|
* |
238
|
|
|
* @throws \yii\web\ForbiddenHttpException |
239
|
|
|
*/ |
|
|
|
|
240
|
|
|
public function actionExportRedirects() |
241
|
|
|
{ |
242
|
|
|
PermissionHelper::controllerPermissionCheck('retour:redirects'); |
243
|
|
|
$this->exportCsvFile('retour-redirects', '{{%retour_static_redirects}}', self::EXPORT_REDIRECTS_CSV_FIELDS); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// Public Methods |
247
|
|
|
// ========================================================================= |
248
|
|
|
|
249
|
|
|
/** |
|
|
|
|
250
|
|
|
* @param string $filename |
|
|
|
|
251
|
|
|
* @param string $table |
|
|
|
|
252
|
|
|
* @param array $columns |
|
|
|
|
253
|
|
|
*/ |
|
|
|
|
254
|
|
|
protected function exportCsvFile(string $filename, string $table, array $columns) |
255
|
|
|
{ |
256
|
|
|
// If your CSV document was created or is read on a Macintosh computer, |
257
|
|
|
// add the following lines before using the library to help PHP detect line ending in Mac OS X |
258
|
|
|
if (!ini_get('auto_detect_line_endings')) { |
259
|
|
|
ini_set('auto_detect_line_endings', '1'); |
260
|
|
|
} |
261
|
|
|
// Query the db table |
262
|
|
|
$data = (new Query()) |
263
|
|
|
->from([$table]) |
264
|
|
|
->select(array_keys($columns)) |
265
|
|
|
->orderBy('hitCount DESC') |
266
|
|
|
->all(); |
267
|
|
|
// Create our CSV file writer |
268
|
|
|
$csv = Writer::createFromFileObject(new \SplTempFileObject()); |
269
|
|
|
$csv->insertOne(array_values($columns)); |
270
|
|
|
$csv->insertAll($data); |
271
|
|
|
$csv->output($filename.'.csv'); |
272
|
|
|
exit(0); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
|
|
|
|
276
|
|
|
* @param AbstractCsv $csv |
|
|
|
|
277
|
|
|
* @param array $columns |
|
|
|
|
278
|
|
|
* @param array $headers |
|
|
|
|
279
|
|
|
*/ |
|
|
|
|
280
|
|
|
protected function importCsvApi8(AbstractCsv $csv, array $columns, array $headers) |
281
|
|
|
{ |
282
|
|
|
$csv->setOffset(1); |
283
|
|
|
$columns = ArrayHelper::filterEmptyStringsFromArray($columns); |
284
|
|
|
$csv->each(function ($row) use ($headers, $columns) { |
|
|
|
|
285
|
|
|
$redirectConfig = [ |
286
|
|
|
'id' => 0, |
287
|
|
|
]; |
288
|
|
|
$index = 0; |
289
|
|
|
foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { |
290
|
|
|
if (isset($columns[$index], $headers[$columns[$index]])) { |
291
|
|
|
$redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) |
292
|
|
|
? null |
293
|
|
|
: $row[$headers[$columns[$index]]]; |
294
|
|
|
} |
295
|
|
|
$index++; |
296
|
|
|
} |
297
|
|
|
Craft::debug('Importing row: ' . print_r($redirectConfig, true), __METHOD__); |
|
|
|
|
298
|
|
|
Retour::$plugin->redirects->saveRedirect($redirectConfig); |
299
|
|
|
|
300
|
|
|
return true; |
301
|
|
|
}); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
|
|
|
|
305
|
|
|
* @param AbstractCsv $csv |
|
|
|
|
306
|
|
|
* @param array $columns |
|
|
|
|
307
|
|
|
* @param array $headers |
|
|
|
|
308
|
|
|
* @throws \League\Csv\Exception |
|
|
|
|
309
|
|
|
*/ |
|
|
|
|
310
|
|
|
protected function importCsvApi9(AbstractCsv $csv, array $columns, array $headers) |
311
|
|
|
{ |
312
|
|
|
$stmt = (new Statement()) |
313
|
|
|
->offset(1) |
|
|
|
|
314
|
|
|
; |
315
|
|
|
$rows = $stmt->process($csv); |
316
|
|
|
$columns = ArrayHelper::filterEmptyStringsFromArray($columns); |
317
|
|
|
foreach ($rows as $row) { |
318
|
|
|
$redirectConfig = [ |
319
|
|
|
'id' => 0, |
320
|
|
|
]; |
321
|
|
|
$index = 0; |
322
|
|
|
foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { |
323
|
|
|
if (isset($columns[$index], $headers[$columns[$index]])) { |
324
|
|
|
$redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) |
325
|
|
|
? null |
326
|
|
|
: $row[$headers[$columns[$index]]]; |
327
|
|
|
} |
328
|
|
|
$index++; |
329
|
|
|
} |
330
|
|
|
Craft::debug('Importing row: ' . print_r($redirectConfig, true), __METHOD__); |
|
|
|
|
331
|
|
|
Retour::$plugin->redirects->saveRedirect($redirectConfig); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|