Merge branch 'nd/worktree-kill-parse-ref'
[git] / contrib / mw-to-git / t / test-gitmw-lib.sh
1 # Copyright (C) 2012
2 #     Charles Roussel <charles.roussel@ensimag.imag.fr>
3 #     Simon Cathebras <simon.cathebras@ensimag.imag.fr>
4 #     Julien Khayat <julien.khayat@ensimag.imag.fr>
5 #     Guillaume Sasdy <guillaume.sasdy@ensimag.imag.fr>
6 #     Simon Perrat <simon.perrat@ensimag.imag.fr>
7 # License: GPL v2 or later
8
9 #
10 # CONFIGURATION VARIABLES
11 # You might want to change these ones
12 #
13
14 . ./test.config
15
16 WIKI_URL=http://"$SERVER_ADDR:$PORT/$WIKI_DIR_NAME"
17 CURR_DIR=$(pwd)
18 TEST_OUTPUT_DIRECTORY=$(pwd)
19 TEST_DIRECTORY="$CURR_DIR"/../../../t
20
21 export TEST_OUTPUT_DIRECTORY TEST_DIRECTORY CURR_DIR
22
23 if test "$LIGHTTPD" = "false" ; then
24         PORT=80
25 else
26         WIKI_DIR_INST="$CURR_DIR/$WEB_WWW"
27 fi
28
29 wiki_upload_file () {
30         "$CURR_DIR"/test-gitmw.pl upload_file "$@"
31 }
32
33 wiki_getpage () {
34         "$CURR_DIR"/test-gitmw.pl get_page "$@"
35 }
36
37 wiki_delete_page () {
38         "$CURR_DIR"/test-gitmw.pl delete_page "$@"
39 }
40
41 wiki_editpage () {
42         "$CURR_DIR"/test-gitmw.pl edit_page "$@"
43 }
44
45 die () {
46         die_with_status 1 "$@"
47 }
48
49 die_with_status () {
50         status=$1
51         shift
52         echo >&2 "$*"
53         exit "$status"
54 }
55
56
57 # Check the preconditions to run git-remote-mediawiki's tests
58 test_check_precond () {
59         if ! test_have_prereq PERL
60         then
61                 skip_all='skipping gateway git-mw tests, perl not available'
62                 test_done
63         fi
64
65         GIT_EXEC_PATH=$(cd "$(dirname "$0")" && cd "../.." && pwd)
66         PATH="$GIT_EXEC_PATH"'/bin-wrapper:'"$PATH"
67
68         if [ ! -d "$WIKI_DIR_INST/$WIKI_DIR_NAME" ];
69         then
70                 skip_all='skipping gateway git-mw tests, no mediawiki found'
71                 test_done
72         fi
73 }
74
75 # test_diff_directories <dir_git> <dir_wiki>
76 #
77 # Compare the contents of directories <dir_git> and <dir_wiki> with diff
78 # and errors if they do not match. The program will
79 # not look into .git in the process.
80 # Warning: the first argument MUST be the directory containing the git data
81 test_diff_directories () {
82         rm -rf "$1_tmp"
83         mkdir -p "$1_tmp"
84         cp "$1"/*.mw "$1_tmp"
85         diff -r -b "$1_tmp" "$2"
86 }
87
88 # $1=<dir>
89 # $2=<N>
90 #
91 # Check that <dir> contains exactly <N> files
92 test_contains_N_files () {
93         if test $(ls -- "$1" | wc -l) -ne "$2"; then
94                 echo "directory $1 should contain $2 files"
95                 echo "it contains these files:"
96                 ls "$1"
97                 false
98         fi
99 }
100
101
102 # wiki_check_content <file_name> <page_name>
103 #
104 # Compares the contents of the file <file_name> and the wiki page
105 # <page_name> and exits with error 1 if they do not match.
106 wiki_check_content () {
107         mkdir -p wiki_tmp
108         wiki_getpage "$2" wiki_tmp
109         # replacement of forbidden character in file name
110         page_name=$(printf "%s\n" "$2" | sed -e "s/\//%2F/g")
111
112         diff -b "$1" wiki_tmp/"$page_name".mw
113         if test $? -ne 0
114         then
115                 rm -rf wiki_tmp
116                 error "ERROR: file $2 not found on wiki"
117         fi
118         rm -rf wiki_tmp
119 }
120
121 # wiki_page_exist <page_name>
122 #
123 # Check the existence of the page <page_name> on the wiki and exits
124 # with error if it is absent from it.
125 wiki_page_exist () {
126         mkdir -p wiki_tmp
127         wiki_getpage "$1" wiki_tmp
128         page_name=$(printf "%s\n" "$1" | sed "s/\//%2F/g")
129         if test -f wiki_tmp/"$page_name".mw ; then
130                 rm -rf wiki_tmp
131         else
132                 rm -rf wiki_tmp
133                 error "test failed: file $1 not found on wiki"
134         fi
135 }
136
137 # wiki_getallpagename
138 #
139 # Fetch the name of each page on the wiki.
140 wiki_getallpagename () {
141         "$CURR_DIR"/test-gitmw.pl getallpagename
142 }
143
144 # wiki_getallpagecategory <category>
145 #
146 # Fetch the name of each page belonging to <category> on the wiki.
147 wiki_getallpagecategory () {
148         "$CURR_DIR"/test-gitmw.pl getallpagename "$@"
149 }
150
151 # wiki_getallpage <dest_dir> [<category>]
152 #
153 # Fetch all the pages from the wiki and place them in the directory
154 # <dest_dir>.
155 # If <category> is define, then wiki_getallpage fetch the pages included
156 # in <category>.
157 wiki_getallpage () {
158         if test -z "$2";
159         then
160                 wiki_getallpagename
161         else
162                 wiki_getallpagecategory "$2"
163         fi
164         mkdir -p "$1"
165         while read -r line; do
166                 wiki_getpage "$line" $1;
167         done < all.txt
168 }
169
170 # ================= Install part =================
171
172 error () {
173         echo "$@" >&2
174         exit 1
175 }
176
177 # config_lighttpd
178 #
179 # Create the configuration files and the folders necessary to start lighttpd.
180 # Overwrite any existing file.
181 config_lighttpd () {
182         mkdir -p $WEB
183         mkdir -p $WEB_TMP
184         mkdir -p $WEB_WWW
185         cat > $WEB/lighttpd.conf <<EOF
186         server.document-root = "$CURR_DIR/$WEB_WWW"
187         server.port = $PORT
188         server.pid-file = "$CURR_DIR/$WEB_TMP/pid"
189
190         server.modules = (
191         "mod_rewrite",
192         "mod_redirect",
193         "mod_access",
194         "mod_accesslog",
195         "mod_fastcgi"
196         )
197
198         index-file.names = ("index.php" , "index.html")
199
200         mimetype.assign             = (
201         ".pdf"          =>      "application/pdf",
202         ".sig"          =>      "application/pgp-signature",
203         ".spl"          =>      "application/futuresplash",
204         ".class"        =>      "application/octet-stream",
205         ".ps"           =>      "application/postscript",
206         ".torrent"      =>      "application/x-bittorrent",
207         ".dvi"          =>      "application/x-dvi",
208         ".gz"           =>      "application/x-gzip",
209         ".pac"          =>      "application/x-ns-proxy-autoconfig",
210         ".swf"          =>      "application/x-shockwave-flash",
211         ".tar.gz"       =>      "application/x-tgz",
212         ".tgz"          =>      "application/x-tgz",
213         ".tar"          =>      "application/x-tar",
214         ".zip"          =>      "application/zip",
215         ".mp3"          =>      "audio/mpeg",
216         ".m3u"          =>      "audio/x-mpegurl",
217         ".wma"          =>      "audio/x-ms-wma",
218         ".wax"          =>      "audio/x-ms-wax",
219         ".ogg"          =>      "application/ogg",
220         ".wav"          =>      "audio/x-wav",
221         ".gif"          =>      "image/gif",
222         ".jpg"          =>      "image/jpeg",
223         ".jpeg"         =>      "image/jpeg",
224         ".png"          =>      "image/png",
225         ".xbm"          =>      "image/x-xbitmap",
226         ".xpm"          =>      "image/x-xpixmap",
227         ".xwd"          =>      "image/x-xwindowdump",
228         ".css"          =>      "text/css",
229         ".html"         =>      "text/html",
230         ".htm"          =>      "text/html",
231         ".js"           =>      "text/javascript",
232         ".asc"          =>      "text/plain",
233         ".c"            =>      "text/plain",
234         ".cpp"          =>      "text/plain",
235         ".log"          =>      "text/plain",
236         ".conf"         =>      "text/plain",
237         ".text"         =>      "text/plain",
238         ".txt"          =>      "text/plain",
239         ".dtd"          =>      "text/xml",
240         ".xml"          =>      "text/xml",
241         ".mpeg"         =>      "video/mpeg",
242         ".mpg"          =>      "video/mpeg",
243         ".mov"          =>      "video/quicktime",
244         ".qt"           =>      "video/quicktime",
245         ".avi"          =>      "video/x-msvideo",
246         ".asf"          =>      "video/x-ms-asf",
247         ".asx"          =>      "video/x-ms-asf",
248         ".wmv"          =>      "video/x-ms-wmv",
249         ".bz2"          =>      "application/x-bzip",
250         ".tbz"          =>      "application/x-bzip-compressed-tar",
251         ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
252         ""              =>      "text/plain"
253         )
254
255         fastcgi.server = ( ".php" =>
256         ("localhost" =>
257         ( "socket" => "$CURR_DIR/$WEB_TMP/php.socket",
258         "bin-path" => "$PHP_DIR/php-cgi -c $CURR_DIR/$WEB/php.ini"
259
260         )
261         )
262         )
263 EOF
264
265         cat > $WEB/php.ini <<EOF
266         session.save_path ='$CURR_DIR/$WEB_TMP'
267 EOF
268 }
269
270 # start_lighttpd
271 #
272 # Start or restart daemon lighttpd. If restart, rewrite configuration files.
273 start_lighttpd () {
274         if test -f "$WEB_TMP/pid"; then
275                 echo "Instance already running. Restarting..."
276                 stop_lighttpd
277         fi
278         config_lighttpd
279         "$LIGHTTPD_DIR"/lighttpd -f "$WEB"/lighttpd.conf
280
281         if test $? -ne 0 ; then
282                 echo "Could not execute http deamon lighttpd"
283                 exit 1
284         fi
285 }
286
287 # stop_lighttpd
288 #
289 # Kill daemon lighttpd and removes files and folders associated.
290 stop_lighttpd () {
291         test -f "$WEB_TMP/pid" && kill $(cat "$WEB_TMP/pid")
292 }
293
294 # Create the SQLite database of the MediaWiki. If the database file already
295 # exists, it will be deleted.
296 # This script should be runned from the directory where $FILES_FOLDER is
297 # located.
298 create_db () {
299         rm -f "$TMP/$DB_FILE"
300
301         echo "Generating the SQLite database file. It can take some time ..."
302         # Run the php script to generate the SQLite database file
303         # with cURL calls.
304         php "$FILES_FOLDER/$DB_INSTALL_SCRIPT" $(basename "$DB_FILE" .sqlite) \
305                 "$WIKI_ADMIN" "$WIKI_PASSW" "$TMP" "$PORT"
306
307         if [ ! -f "$TMP/$DB_FILE" ] ; then
308                 error "Can't create database file $TMP/$DB_FILE. Try to run ./install-wiki.sh delete first."
309         fi
310
311         # Copy the generated database file into the directory the
312         # user indicated.
313         cp "$TMP/$DB_FILE" "$FILES_FOLDER" ||
314                 error "Unable to copy $TMP/$DB_FILE to $FILES_FOLDER"
315 }
316
317 # Install a wiki in your web server directory.
318 wiki_install () {
319         if test $LIGHTTPD = "true" ; then
320                 start_lighttpd
321         fi
322
323         SERVER_ADDR=$SERVER_ADDR:$PORT
324         # In this part, we change directory to $TMP in order to download,
325         # unpack and copy the files of MediaWiki
326         (
327         mkdir -p "$WIKI_DIR_INST/$WIKI_DIR_NAME"
328         if [ ! -d "$WIKI_DIR_INST/$WIKI_DIR_NAME" ] ; then
329                 error "Folder $WIKI_DIR_INST/$WIKI_DIR_NAME doesn't exist.
330                 Please create it and launch the script again."
331         fi
332
333         # Fetch MediaWiki's archive if not already present in the TMP directory
334         MW_FILENAME="mediawiki-$MW_VERSION_MAJOR.$MW_VERSION_MINOR.tar.gz"
335         cd "$TMP"
336         if [ ! -f $MW_FILENAME ] ; then
337                 echo "Downloading $MW_VERSION_MAJOR.$MW_VERSION_MINOR sources ..."
338                 wget "http://download.wikimedia.org/mediawiki/$MW_VERSION_MAJOR/$MW_FILENAME" ||
339                         error "Unable to download "\
340                         "http://download.wikimedia.org/mediawiki/$MW_VERSION_MAJOR/"\
341                         "$MW_FILENAME. "\
342                         "Please fix your connection and launch the script again."
343                 echo "$MW_FILENAME downloaded in $(pwd). "\
344                         "You can delete it later if you want."
345         else
346                 echo "Reusing existing $MW_FILENAME downloaded in $(pwd)."
347         fi
348         archive_abs_path=$(pwd)/$MW_FILENAME
349         cd "$WIKI_DIR_INST/$WIKI_DIR_NAME/" ||
350                 error "can't cd to $WIKI_DIR_INST/$WIKI_DIR_NAME/"
351         tar xzf "$archive_abs_path" --strip-components=1 ||
352                 error "Unable to extract WikiMedia's files from $archive_abs_path to "\
353                         "$WIKI_DIR_INST/$WIKI_DIR_NAME"
354         ) || exit 1
355
356         create_db
357
358         # Copy the generic LocalSettings.php in the web server's directory
359         # And modify parameters according to the ones set at the top
360         # of this script.
361         # Note that LocalSettings.php is never modified.
362         if [ ! -f "$FILES_FOLDER/LocalSettings.php" ] ; then
363                 error "Can't find $FILES_FOLDER/LocalSettings.php " \
364                         "in the current folder. "\
365                 "Please run the script inside its folder."
366         fi
367         cp "$FILES_FOLDER/LocalSettings.php" \
368                 "$FILES_FOLDER/LocalSettings-tmp.php" ||
369                 error "Unable to copy $FILES_FOLDER/LocalSettings.php " \
370                 "to $FILES_FOLDER/LocalSettings-tmp.php"
371
372         # Parse and set the LocalSettings file of the user according to the
373         # CONFIGURATION VARIABLES section at the beginning of this script
374         file_swap="$FILES_FOLDER/LocalSettings-swap.php"
375         sed "s,@WG_SCRIPT_PATH@,/$WIKI_DIR_NAME," \
376                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
377         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
378         sed "s,@WG_SERVER@,http://$SERVER_ADDR," \
379                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
380         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
381         sed "s,@WG_SQLITE_DATADIR@,$TMP," \
382                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
383         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
384         sed "s,@WG_SQLITE_DATAFILE@,$( basename $DB_FILE .sqlite)," \
385                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
386         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
387
388         mv "$FILES_FOLDER/LocalSettings-tmp.php" \
389                 "$WIKI_DIR_INST/$WIKI_DIR_NAME/LocalSettings.php" ||
390                 error "Unable to move $FILES_FOLDER/LocalSettings-tmp.php" \
391                 "in $WIKI_DIR_INST/$WIKI_DIR_NAME"
392         echo "File $FILES_FOLDER/LocalSettings.php is set in" \
393                 " $WIKI_DIR_INST/$WIKI_DIR_NAME"
394
395         echo "Your wiki has been installed. You can check it at
396                 http://$SERVER_ADDR/$WIKI_DIR_NAME"
397 }
398
399 # Reset the database of the wiki and the password of the admin
400 #
401 # Warning: This function must be called only in a subdirectory of t/ directory
402 wiki_reset () {
403         # Copy initial database of the wiki
404         if [ ! -f "../$FILES_FOLDER/$DB_FILE" ] ; then
405                 error "Can't find ../$FILES_FOLDER/$DB_FILE in the current folder."
406         fi
407         cp "../$FILES_FOLDER/$DB_FILE" "$TMP" ||
408                 error "Can't copy ../$FILES_FOLDER/$DB_FILE in $TMP"
409         echo "File $FILES_FOLDER/$DB_FILE is set in $TMP"
410 }
411
412 # Delete the wiki created in the web server's directory and all its content
413 # saved in the database.
414 wiki_delete () {
415         if test $LIGHTTPD = "true"; then
416                 stop_lighttpd
417                 rm -fr "$WEB"
418         else
419                 # Delete the wiki's directory.
420                 rm -rf "$WIKI_DIR_INST/$WIKI_DIR_NAME" ||
421                         error "Wiki's directory $WIKI_DIR_INST/" \
422                         "$WIKI_DIR_NAME could not be deleted"
423                 # Delete the wiki's SQLite database.
424                 rm -f "$TMP/$DB_FILE" ||
425                         error "Database $TMP/$DB_FILE could not be deleted."
426         fi
427
428         # Delete the wiki's SQLite database
429         rm -f "$TMP/$DB_FILE" || error "Database $TMP/$DB_FILE could not be deleted."
430         rm -f "$FILES_FOLDER/$DB_FILE"
431         rm -rf "$TMP/mediawiki-$MW_VERSION_MAJOR.$MW_VERSION_MINOR.tar.gz"
432 }