This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
extUpdate.sh
executable file
·443 lines (396 loc) · 13.9 KB
/
extUpdate.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
# TYPO3 Extension Update Script
# written by Oliver Salzburg
set -o nounset
set -o errexit
SELF=$(basename "$0")
# Show the help for this script
function showHelp() {
cat << EOF
Usage: $0 [OPTIONS]
Core:
--help Display this help and exit.
--verbose Display more detailed messages.
--quiet Do not display anything.
--force Perform actions that would otherwise abort the script.
--update Tries to update the script to the latest version.
--update-check Checks if a newer version of the script is available.
--export-config Prints the default configuration of this script.
--extract-config Extracts configuration parameters from TYPO3.
--base=PATH The name of the base path where TYPO3 is
installed. If no base is supplied, "typo3" is used.
Options:
--extension=EXTKEY The extension key of the extension that should be
operated on.
--changelog Display the upload comments for updated extensions.
Database:
--hostname=HOST The name of the host where the TYPO3 database is running.
--username=USER The username to use when connecting to the TYPO3
database.
--password=PASSWORD The password to use when connecting to the TYPO3
database.
--database=DB The name of the database in which TYPO3 is stored.
EOF
}
# Print the default configuration to ease creation of a config file.
function exportConfig() {
# Spaces are escaped here to avoid sed matching this line when exporting the
# configuration
sed -n "/#\ Script\ Configuration\ start/,/# Script Configuration end/p" "$0"
}
# Extract all known (database related) parameters from the TYPO3 configuration.
function extractConfig() {
LOCALCONF="$BASE/typo3conf/localconf.php"
LOCALCONFIGURATION="$BASE/typo3conf/LocalConfiguration.php"
if [[ -r $LOCALCONF ]]; then
echo HOST=$(tac $LOCALCONF | grep --perl-regexp --only-matching "(?<=typo_db_host = ')[^']*(?=';)")
echo USER=$(tac $LOCALCONF | grep --perl-regexp --only-matching "(?<=typo_db_username = ')[^']*(?=';)")
echo PASS=$(tac $LOCALCONF | grep --perl-regexp --only-matching "(?<=typo_db_password = ')[^']*(?=';)")
echo DB=$(tac $LOCALCONF | grep --perl-regexp --only-matching "(?<=typo_db = ')[^']*(?=';)")
elif [[ -r $LOCALCONFIGURATION ]]; then
if [[ ! -e "./configurationProxy.php" ]]; then
echo "Required 'configurationProxy.php' is missing.";
exit 1
fi
echo HOST=$(./configurationProxy.php --get=TYPO3_CONF_VARS.DB.host)
echo USER=$(./configurationProxy.php --get=TYPO3_CONF_VARS.DB.username)
echo PASS=$(./configurationProxy.php --get=TYPO3_CONF_VARS.DB.password)
echo DB=$(./configurationProxy.php --get=TYPO3_CONF_VARS.DB.database)
else
echo "Unable to find readable configuration file." >&2
fi
}
# Check on minimal command line argument count
REQUIRED_ARGUMENT_COUNT=0
if [[ $# -lt $REQUIRED_ARGUMENT_COUNT ]]; then
echo "Insufficient command line arguments!" >&2
echo "Use $0 --help to get additional information." >&2
exit 1
fi
# Script Configuration start
# Should the script give more detailed feedback?
VERBOSE=false
# Should the script surpress all feedback?
QUIET=false
# Should the script ignore reasons that would otherwise cause it to abort?
FORCE=false
# The base directory where Typo3 is installed
BASE=typo3
# The hostname of the MySQL server that Typo3 uses
HOST=localhost
# The username used to connect to that MySQL server
USER=*username*
# The password for that user
PASS=*password*
# The name of the database in which Typo3 is stored
DB=typo3
# The extension key for which to retrieve the changelog
EXTENSION=
# Should the upload comments be displayed for extensions that have updates available?
DISPLAY_CHANGELOG=0
# Script Configuration end
function consoleWrite() {
[ "false" == "$QUIET" ] && echo -n $* >&2
return 0
}
function consoleWriteLine() {
[ "false" == "$QUIET" ] && echo $* >&2
return 0
}
function consoleWriteVerbose() {
$VERBOSE && consoleWrite $*
return 0
}
function consoleWriteLineVerbose() {
$VERBOSE && consoleWriteLine $*
return 0
}
# The base location from where to retrieve new versions of this script
UPDATE_BASE=https://raw.github.com/oliversalzburg/typo3scripts/master
# Update check
function updateCheck() {
if ! hash curl 2>&-; then
consoleWriteLine "Update checking requires curl. Check skipped."
return 2
fi
SUM_LATEST=$(curl $UPDATE_BASE/versions 2>&1 | grep $SELF | awk '{print $2}')
SUM_SELF=$(tail --lines=+2 "$0" | md5sum | awk '{print $1}')
consoleWriteLineVerbose "Remote hash source: '$UPDATE_BASE/versions'"
consoleWriteLineVerbose "Own hash: '$SUM_SELF' Remote hash: '$SUM_LATEST'"
if [[ "" == $SUM_LATEST ]]; then
consoleWriteLine "No update information is available for '$SELF'"
consoleWriteLine "Please check the project home page 'https://github.com/oliversalzburg/typo3scripts'."
return 2
elif [[ "$SUM_LATEST" != "$SUM_SELF" ]]; then
consoleWriteLine "NOTE: New version available!"
return 1
fi
return 0
}
# Self-update
function runSelfUpdate() {
echo "Performing self-update..."
_tempFileName="$0.tmp"
_payloadName="$0.payload"
# Download new version
echo -n "Downloading latest version..."
if ! wget --quiet --output-document="$_payloadName" $UPDATE_BASE/$SELF ; then
echo "Failed: Error while trying to wget new version!"
echo "File requested: $UPDATE_BASE/$SELF"
exit 1
fi
echo "Done."
# Restore shebang
_interpreter=$(head --lines=1 "$0")
echo $_interpreter > "$_tempFileName"
tail --lines=+2 "$_payloadName" >> "$_tempFileName"
rm "$_payloadName"
# Copy over modes from old version
OCTAL_MODE=$(stat -c '%a' $SELF)
if ! chmod $OCTAL_MODE "$_tempFileName" ; then
echo "Failed: Error while trying to set mode on $_tempFileName."
exit 1
fi
# Spawn update script
cat > updateScript.sh << EOF
#!/bin/bash
# Overwrite old file with new
if mv "$_tempFileName" "$0"; then
echo "Done."
echo "Update complete."
rm -- \$0
else
echo "Failed!"
fi
EOF
echo -n "Inserting update process..."
exec /bin/bash updateScript.sh
}
# Make a quick run through the command line arguments to see if the user wants
# to print the help. This saves us a lot of headache with respecting the order
# in which configuration parameters have to be overwritten.
for option in $*; do
case "$option" in
--help|-h)
showHelp
exit 0
;;
esac
done
# Read external configuration - Stage 1 - typo3scripts.conf (overwrites default, hard-coded configuration)
BASE_CONFIG_FILENAME="typo3scripts.conf"
if [[ -e "$BASE_CONFIG_FILENAME" ]]; then
if [[ ! -r $BASE_CONFIG_FILENAME ]]; then
consoleWriteLine "Unable to read '$BASE_CONFIG_FILENAME'. Check permissions."
exit 1
fi
consoleWriteVerbose "Sourcing script configuration from $BASE_CONFIG_FILENAME..."
source $BASE_CONFIG_FILENAME
consoleWriteLineVerbose "Done."
fi
# Read external configuration - Stage 2 - script-specific (overwrites default, hard-coded configuration)
CONFIG_FILENAME=${SELF:0:${#SELF}-3}.conf
if [[ -e "$CONFIG_FILENAME" ]]; then
if [[ ! -r $CONFIG_FILENAME ]]; then
consoleWriteLine "Unable to read '$CONFIG_FILENAME'. Check permissions."
exit 1
fi
consoleWriteVerbose "Sourcing script configuration from $CONFIG_FILENAME..."
source $CONFIG_FILENAME
consoleWriteLineVerbose "Done."
fi
# Read command line arguments (overwrites config file)
for option in $*; do
case "$option" in
--verbose)
VERBOSE=true
;;
--quiet)
QUIET=true
;;
--force)
FORCE=true
;;
--update)
runSelfUpdate
;;
--update-check)
updateCheck
exit $?
;;
--export-config)
exportConfig
exit 0
;;
--base=*)
BASE=$(echo $option | cut -d'=' -f2)
;;
--hostname=*)
HOST=$(echo $option | cut -d'=' -f2)
;;
--username=*)
USER=$(echo $option | cut -d'=' -f2)
;;
--password=*)
PASS=$(echo $option | cut -d'=' -f2)
;;
--database=*)
DB=$(echo $option | cut -d'=' -f2)
;;
--extension=*)
EXTENSION=$(echo $option | cut -d'=' -f2)
;;
--changelog)
DISPLAY_CHANGELOG=1
;;
*)
EXTENSION=$option
;;
esac
done
# Check for dependencies
function checkDependency() {
consoleWriteVerbose "Checking dependency '$1' => "
if ! hash $1 2>&-; then
consoleWriteLine "Failed!"
consoleWriteLine "This script requires '$1' but it can not be found. Aborting."
exit 1
fi
consoleWriteLineVerbose $(which $1)
return 0
}
consoleWrite "Checking dependencies..."
consoleWriteLineVerbose
checkDependency mysql
checkDependency sed
consoleWriteLine "Succeeded."
# Begin main operation
# Check default argument validity
if [[ $EXTENSION == --* ]]; then
consoleWriteLine "The given extension key '$EXTENSION' looks like a command line parameter."
consoleWriteLine "Please use --help to see a list of available command line parameters."
exit 1
fi
# Does the base directory exist?
if [[ ! -d $BASE ]]; then
consoleWriteLine "The base directory '$BASE' does not seem to exist!"
exit 1
fi
# Is the base directory readable?
if [[ ! -r $BASE ]]; then
consoleWriteLine "The base directory '$BASE' is not readable!"
exit 1
fi
# Check if extChangelog.sh is required and available
if [[ $DISPLAY_CHANGELOG == 1 && ! -e extChangelog.sh ]]; then
consoleWriteLine "Upload comments will NOT be displayed! To enable this feature, download extChangelog.sh from the typo3scripts project and place it in the same folder as $SELF."
fi
# Version number compare helper function
# Created by Dennis Williamson (http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format)
function compareVersions() {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# Check if extension cache has been updated recently
_extensionCacheFile="$BASE/typo3temp/1.extensions.xml.gz"
if [[ ! -e $_extensionCacheFile ]]; then
consoleWriteLine "Unable to find extension cache '$_extensionCacheFile'. Either you are using a repository with a non-standard ID (not TYPO3.org) or you have never loaded an extension list."
else
_lastUpdateTime=$(date --reference=$_extensionCacheFile +%s)
_currentTime=$(date +%s)
_lastUpdatePeriod=$(( $_currentTime - $_lastUpdateTime ))
# Longest period the cache can be left not update without extUpdate.sh complaining
# By default, 172800 seconds = 48 hours
MAX_CACHE_UPDATE_DELAY=172800
if [[ "$_lastUpdatePeriod" -ge "$MAX_CACHE_UPDATE_DELAY" ]]; then
consoleWriteLine "WARNING: Did you forget to update your extension cache? Last update: $(date --date @$_lastUpdateTime "+%Y-%m-%d %T")"
fi
fi
# Check versions on all installed extensions
_updatesAvailable=0
for _extDirectory in "$BASE/typo3conf/ext/"*; do
# Skip non-directories
if [[ ! -d $_extDirectory ]]; then
continue
fi
_extKey=$(basename "$_extDirectory")
if [[ "" != $EXTENSION && $_extKey != $EXTENSION ]]; then
continue
fi
set +e errexit
# Determine installed version from ext_emconf.php
_installedVersion=$(grep --perl-regexp "'version'\s*=>\s*'\d{1,3}\.\d{1,3}\.\d{1,3}'" "$_extDirectory/ext_emconf.php" | grep --perl-regexp --only-matching "\d{1,3}\.\d{1,3}\.\d{1,3}")
if [[ "" == $_installedVersion ]]; then
consoleWriteLine "Warning: Could not determine the installed version of extension '$_extKey'!"
consoleWriteLineVerbose " ext_emconf.php does not exist or the contained version is invalid."
continue
fi
# Get the latest known version from the cache in the database
_query="SELECT \`version\` FROM \`cache_extensions\` WHERE (\`extkey\` = '$_extKey') ORDER BY \`intversion\` DESC LIMIT 1;"
_errorMessage=$(echo $_query | mysql --host=$HOST --user=$USER --pass=$PASS --database=$DB --batch --skip-column-names 2>&1 > extVersion.out)
_status=$?
_latestVersion=$(cat extVersion.out)
rm -f extVersion.out
if [[ "" == $_latestVersion ]]; then
# Try again with the 6.2 table. This should be the default in due time.
_query="SELECT \`version\` FROM \`tx_extensionmanager_domain_model_extension\` WHERE (\`extension_key\` = '$_extKey') ORDER BY \`integer_version\` DESC LIMIT 1;"
_errorMessage=$(echo $_query | mysql --host=$HOST --user=$USER --pass=$PASS --database=$DB --batch --skip-column-names 2>&1 > extVersion.out)
_status=$?
_latestVersion=$(cat extVersion.out)
rm -f extVersion.out
if [[ "" == $_latestVersion ]]; then
consoleWriteLine "Warning: Could not determine the latest version of extension '$_extKey'!"
consoleWriteLineVerbose " No entry for the extension could be found in the extension cache."
continue
fi
fi
set -e errexit
if [[ 0 < $_status ]]; then
consoleWriteLine "Failed!"
consoleWriteLine "Error: $_errorMessage"
exit 1
fi
# Compare versions
set +e errexit
compareVersions $_installedVersion $_latestVersion
_versionsEqual=$?
set -e errexit
if [[ $_versionsEqual != 0 ]]; then
(( ++_updatesAvailable ))
consoleWriteLine "New version of '$_extKey' available. Installed: $_installedVersion Latest: $_latestVersion"
if [[ $DISPLAY_CHANGELOG == 1 && -e extChangelog.sh ]]; then
./extChangelog.sh --extension=$_extKey --first=$_installedVersion --skip-first 2>/dev/null
consoleWriteLine
fi
fi
done
if [[ 0 -eq $_updatesAvailable ]]; then
consoleWriteLine "No updates available."
fi
# vim:ts=2:sw=2:expandtab: