git-remote-mediawiki: scripts to install, delete and clear a MediaWiki
[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 CURR_DIR=$(pwd)
17 TEST_OUTPUT_DIRECTORY=$(pwd)
18 TEST_DIRECTORY="$CURR_DIR"/../../../t
19
20 export TEST_OUTPUT_DIRECTORY TEST_DIRECTORY CURR_DIR
21
22 if test "$LIGHTTPD" = "false" ; then
23         PORT=80
24 else
25         WIKI_DIR_INST="$CURR_DIR/$WEB_WWW"
26 fi
27
28 # ================= Install part =================
29
30 error () {
31         echo "$@" >&2
32         exit 1
33 }
34
35 # config_lighttpd
36 #
37 # Create the configuration files and the folders necessary to start lighttpd.
38 # Overwrite any existing file.
39 config_lighttpd () {
40         mkdir -p $WEB
41         mkdir -p $WEB_TMP
42         mkdir -p $WEB_WWW
43         cat > $WEB/lighttpd.conf <<EOF
44         server.document-root = "$CURR_DIR/$WEB_WWW"
45         server.port = $PORT
46         server.pid-file = "$CURR_DIR/$WEB_TMP/pid"
47
48         server.modules = (
49         "mod_rewrite",
50         "mod_redirect",
51         "mod_access",
52         "mod_accesslog",
53         "mod_fastcgi"
54         )
55
56         index-file.names = ("index.php" , "index.html")
57
58         mimetype.assign             = (
59         ".pdf"          =>      "application/pdf",
60         ".sig"          =>      "application/pgp-signature",
61         ".spl"          =>      "application/futuresplash",
62         ".class"        =>      "application/octet-stream",
63         ".ps"           =>      "application/postscript",
64         ".torrent"      =>      "application/x-bittorrent",
65         ".dvi"          =>      "application/x-dvi",
66         ".gz"           =>      "application/x-gzip",
67         ".pac"          =>      "application/x-ns-proxy-autoconfig",
68         ".swf"          =>      "application/x-shockwave-flash",
69         ".tar.gz"       =>      "application/x-tgz",
70         ".tgz"          =>      "application/x-tgz",
71         ".tar"          =>      "application/x-tar",
72         ".zip"          =>      "application/zip",
73         ".mp3"          =>      "audio/mpeg",
74         ".m3u"          =>      "audio/x-mpegurl",
75         ".wma"          =>      "audio/x-ms-wma",
76         ".wax"          =>      "audio/x-ms-wax",
77         ".ogg"          =>      "application/ogg",
78         ".wav"          =>      "audio/x-wav",
79         ".gif"          =>      "image/gif",
80         ".jpg"          =>      "image/jpeg",
81         ".jpeg"         =>      "image/jpeg",
82         ".png"          =>      "image/png",
83         ".xbm"          =>      "image/x-xbitmap",
84         ".xpm"          =>      "image/x-xpixmap",
85         ".xwd"          =>      "image/x-xwindowdump",
86         ".css"          =>      "text/css",
87         ".html"         =>      "text/html",
88         ".htm"          =>      "text/html",
89         ".js"           =>      "text/javascript",
90         ".asc"          =>      "text/plain",
91         ".c"            =>      "text/plain",
92         ".cpp"          =>      "text/plain",
93         ".log"          =>      "text/plain",
94         ".conf"         =>      "text/plain",
95         ".text"         =>      "text/plain",
96         ".txt"          =>      "text/plain",
97         ".dtd"          =>      "text/xml",
98         ".xml"          =>      "text/xml",
99         ".mpeg"         =>      "video/mpeg",
100         ".mpg"          =>      "video/mpeg",
101         ".mov"          =>      "video/quicktime",
102         ".qt"           =>      "video/quicktime",
103         ".avi"          =>      "video/x-msvideo",
104         ".asf"          =>      "video/x-ms-asf",
105         ".asx"          =>      "video/x-ms-asf",
106         ".wmv"          =>      "video/x-ms-wmv",
107         ".bz2"          =>      "application/x-bzip",
108         ".tbz"          =>      "application/x-bzip-compressed-tar",
109         ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
110         ""              =>      "text/plain"
111         )
112
113         fastcgi.server = ( ".php" =>
114         ("localhost" =>
115         ( "socket" => "$CURR_DIR/$WEB_TMP/php.socket",
116         "bin-path" => "$PHP_DIR/php-cgi -c $CURR_DIR/$WEB/php.ini"
117
118         )
119         )
120         )
121 EOF
122
123         cat > $WEB/php.ini <<EOF
124         session.save_path ='$CURR_DIR/$WEB_TMP'
125 EOF
126 }
127
128 # start_lighttpd
129 #
130 # Start or restart daemon lighttpd. If restart, rewrite configuration files.
131 start_lighttpd () {
132         if test -f "$WEB_TMP/pid"; then
133                 echo "Instance already running. Restarting..."
134                 stop_lighttpd
135         fi
136         config_lighttpd
137         "$LIGHTTPD_DIR"/lighttpd -f "$WEB"/lighttpd.conf
138
139         if test $? -ne 0 ; then
140                 echo "Could not execute http deamon lighttpd"
141                 exit 1
142         fi
143 }
144
145 # stop_lighttpd
146 #
147 # Kill daemon lighttpd and removes files and folders associated.
148 stop_lighttpd () {
149         test -f "$WEB_TMP/pid" && kill $(cat "$WEB_TMP/pid")
150         rm -rf "$WEB"
151 }
152
153 # Create the SQLite database of the MediaWiki. If the database file already
154 # exists, it will be deleted.
155 # This script should be runned from the directory where $FILES_FOLDER is
156 # located.
157 create_db () {
158         rm -f "$TMP/$DB_FILE"
159
160         echo "Generating the SQLite database file. It can take some time ..."
161         # Run the php script to generate the SQLite database file
162         # with cURL calls.
163         php "$FILES_FOLDER/$DB_INSTALL_SCRIPT" $(basename "$DB_FILE" .sqlite) \
164                 "$WIKI_ADMIN" "$WIKI_PASSW" "$TMP" "$PORT"
165
166         if [ ! -f "$TMP/$DB_FILE" ] ; then
167                 error "Can't create database file $TMP/$DB_FILE. Try to run ./install-wiki.sh delete first."
168         fi
169
170         # Copy the generated database file into the directory the
171         # user indicated.
172         cp "$TMP/$DB_FILE" "$FILES_FOLDER" ||
173                 error "Unable to copy $TMP/$DB_FILE to $FILES_FOLDER"
174 }
175
176 # Install a wiki in your web server directory.
177 wiki_install () {
178         if test $LIGHTTPD = "true" ; then
179                 start_lighttpd
180         fi
181
182         SERVER_ADDR=$SERVER_ADDR:$PORT
183         # In this part, we change directory to $TMP in order to download,
184         # unpack and copy the files of MediaWiki
185         (
186         mkdir -p "$WIKI_DIR_INST/$WIKI_DIR_NAME"
187         if [ ! -d "$WIKI_DIR_INST/$WIKI_DIR_NAME" ] ; then
188                 error "Folder $WIKI_DIR_INST/$WIKI_DIR_NAME doesn't exist.
189                 Please create it and launch the script again."
190         fi
191
192         # Fetch MediaWiki's archive if not already present in the TMP directory
193         cd "$TMP"
194         if [ ! -f "$MW_VERSION.tar.gz" ] ; then
195                 echo "Downloading $MW_VERSION sources ..."
196                 wget "http://download.wikimedia.org/mediawiki/1.19/mediawiki-1.19.0.tar.gz" ||
197                         error "Unable to download "\
198                         "http://download.wikimedia.org/mediawiki/1.19/"\
199                         "mediawiki-1.19.0.tar.gz. "\
200                         "Please fix your connection and launch the script again."
201                 echo "$MW_VERSION.tar.gz downloaded in `pwd`. "\
202                         "You can delete it later if you want."
203         else
204                 echo "Reusing existing $MW_VERSION.tar.gz downloaded in `pwd`."
205         fi
206         archive_abs_path=$(pwd)/"$MW_VERSION.tar.gz"
207         cd "$WIKI_DIR_INST/$WIKI_DIR_NAME/" ||
208                 error "can't cd to $WIKI_DIR_INST/$WIKI_DIR_NAME/"
209         tar xzf "$archive_abs_path" --strip-components=1 ||
210                 error "Unable to extract WikiMedia's files from $archive_abs_path to "\
211                         "$WIKI_DIR_INST/$WIKI_DIR_NAME"
212         ) || exit 1
213
214         create_db
215
216         # Copy the generic LocalSettings.php in the web server's directory
217         # And modify parameters according to the ones set at the top
218         # of this script.
219         # Note that LocalSettings.php is never modified.
220         if [ ! -f "$FILES_FOLDER/LocalSettings.php" ] ; then
221                 error "Can't find $FILES_FOLDER/LocalSettings.php " \
222                         "in the current folder. "\
223                 "Please run the script inside its folder."
224         fi
225         cp "$FILES_FOLDER/LocalSettings.php" \
226                 "$FILES_FOLDER/LocalSettings-tmp.php" ||
227                 error "Unable to copy $FILES_FOLDER/LocalSettings.php " \
228                 "to $FILES_FOLDER/LocalSettings-tmp.php"
229
230         # Parse and set the LocalSettings file of the user according to the
231         # CONFIGURATION VARIABLES section at the beginning of this script
232         file_swap="$FILES_FOLDER/LocalSettings-swap.php"
233         sed "s,@WG_SCRIPT_PATH@,/$WIKI_DIR_NAME," \
234                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
235         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
236         sed "s,@WG_SERVER@,http://$SERVER_ADDR," \
237                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
238         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
239         sed "s,@WG_SQLITE_DATADIR@,$TMP," \
240                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
241         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
242         sed "s,@WG_SQLITE_DATAFILE@,$( basename $DB_FILE .sqlite)," \
243                 "$FILES_FOLDER/LocalSettings-tmp.php" > "$file_swap"
244         mv "$file_swap" "$FILES_FOLDER/LocalSettings-tmp.php"
245
246         mv "$FILES_FOLDER/LocalSettings-tmp.php" \
247                 "$WIKI_DIR_INST/$WIKI_DIR_NAME/LocalSettings.php" ||
248                 error "Unable to move $FILES_FOLDER/LocalSettings-tmp.php" \
249                 "in $WIKI_DIR_INST/$WIKI_DIR_NAME"
250         echo "File $FILES_FOLDER/LocalSettings.php is set in" \
251                 " $WIKI_DIR_INST/$WIKI_DIR_NAME"
252
253         echo "Your wiki has been installed. You can check it at
254                 http://$SERVER_ADDR/$WIKI_DIR_NAME"
255 }
256
257 # Reset the database of the wiki and the password of the admin
258 #
259 # Warning: This function must be called only in a subdirectory of t/ directory
260 wiki_reset () {
261         # Copy initial database of the wiki
262         if [ ! -f "../$FILES_FOLDER/$DB_FILE" ] ; then
263                 error "Can't find ../$FILES_FOLDER/$DB_FILE in the current folder."
264         fi
265         cp "../$FILES_FOLDER/$DB_FILE" "$TMP" ||
266                 error "Can't copy ../$FILES_FOLDER/$DB_FILE in $TMP"
267         echo "File $FILES_FOLDER/$DB_FILE is set in $TMP"
268 }
269
270 # Delete the wiki created in the web server's directory and all its content
271 # saved in the database.
272 wiki_delete () {
273         if test $LIGHTTPD = "true"; then
274                 stop_lighttpd
275         else
276                 # Delete the wiki's directory.
277                 rm -rf "$WIKI_DIR_INST/$WIKI_DIR_NAME" ||
278                         error "Wiki's directory $WIKI_DIR_INST/" \
279                         "$WIKI_DIR_NAME could not be deleted"
280                 # Delete the wiki's SQLite database.
281                 rm -f "$TMP/$DB_FILE" ||
282                         error "Database $TMP/$DB_FILE could not be deleted."
283         fi
284
285         # Delete the wiki's SQLite database
286         rm -f "$TMP/$DB_FILE" || error "Database $TMP/$DB_FILE could not be deleted."
287         rm -f "$FILES_FOLDER/$DB_FILE"
288         rm -rf "$TMP/$MW_VERSION"
289 }