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