-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecialInvalidateCache.php
105 lines (92 loc) · 3.04 KB
/
SpecialInvalidateCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Copyright (C) 2013 Orain, Kudu and contributors
*
* This file is part of InvalidateCache.
*
* InvalidateCache is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* InvalidateCache is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with InvalidateCache. If not, see <http://www.gnu.org/licenses/>.
*/
require 'vendor/autoload.php';
use Aws\CloudFront\CloudFrontClient;
class SpecialInvalidateCache extends SpecialPage {
function __construct() {
parent::__construct( 'InvalidateCache', 'invalidatecache' );
}
function execute( $par ) {
$this->checkPermissions();
$this->setHeaders();
$form = new HTMLForm( array(
'paths' => array(
'cols' => 120,
'label-message' => 'invalidatecache-label-paths',
'required' => true,
'rows' => 4,
'type' => 'textarea',
),
'comment' => array(
'label-message' => 'invalidatecache-label-comment',
'maxlength' => 79,
'size' => 79,
'type' => 'text',
),
)
);
$form->setSubmitTextMsg( 'invalidatecache-label-invalidate' );
$form->setTitle( $this->getTitle() );
$form->setSubmitCallback( array( 'SpecialInvalidateCache', 'processInput' ) );
$form->show();
}
static function processInput( $formData, $form ) {
global $wmgHostname, $wgInvalidateCacheAccessKey, $wgInvalidateCacheSecretKey, $wgInvalidateCacheDistributionID;
$pathText = $formData['paths'];
$paths = explode( PHP_EOL, $pathText );
$paths = array_filter( array_map( "trim", $paths ) );
$notprefixed = false;
$prefix = '/' . $wmgHostname . '/';
foreach ( $paths as $path ) {
if ( substr( $path, 0, strlen( $prefix ) ) !== $prefix ) {
$notprefixed = true;
break;
}
}
if ( $notprefixed ) {
return wfMessage( 'invalidatecache-error-notprefixed' )->plain();
}
$pathLine = implode( ',', $paths );
$logEntry = new ManualLogEntry( 'invalidatecache', 'invalidate' );
$logEntry->setPerformer( $form->getUser() );
$logEntry->setTarget( $form->getTitle() );
$logEntry->setComment( $formData['comment'] );
$logEntry->setParameters( array(
'4::paths' => $pathLine,
)
);
$logID = $logEntry->insert();
$logEntry->publish( $logID );
$client = CloudFrontClient::factory( array(
'key' => $wgInvalidateCacheAccessKey,
'secret' => $wgInvalidateCacheSecretKey,
) );
// Something could be done with the result
$client->createInvalidation( array(
'DistributionId' => $wgInvalidateCacheDistributionID,
'Paths' => array(
'Quantity' => count( $paths ),
'Items' => $paths,
),
// Invalidation request ID
'CallerReference' => md5( $pathLine . time() ),
) );
}
}