slitaz-forge view pkgs/mirror-search.sh @ rev 370

mirror-search.sh: add license cloud
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jun 04 17:02:27 2013 +0200 (2013-06-04)
parents b4c0f3e369f9
children eb03961adce3
line source
1 #!/bin/sh
3 # Tiny CGI search engine for SliTaz packages on http://pkgs.slitaz.org/
4 # Christophe Lincoln <pankso@slitaz.org>
5 # Pascal Bellard <pascal.bellard@slitaz.org>
6 # Aleksej Bobylev <al.bobylev@gmail.com>
7 #
9 renice -n 19 $$
11 # Parse query string
12 . /usr/lib/slitaz/httphelper.sh
14 case "$HTTP_USER_AGENT" in
15 */[Bb]ot*|*[Bb]ot/*|*spider/*) exec $PWD/Travaux.sh ;;
16 esac
18 # User preferred language
19 # parameter $1 have priority; without parameter $1 - browser language only
20 # if we don't support any of user languages (or only en), then return C locale
21 user_lang() {
22 LANG="C"
23 IFS=","
24 for lang in $1 $HTTP_ACCEPT_LANGUAGE
25 do
26 lang=${lang%;*} lang=${lang# } lang=${lang%-*} lang=${lang%_*}
27 case "$lang" in
28 de) LANG="de_DE" ;;
29 es) LANG="es_ES" ;;
30 fr) LANG="fr_FR" ;;
31 it) LANG="it_IT" ;;
32 pt) LANG="pt_BR" ;;
33 ru) LANG="ru_RU" ;;
34 zh) LANG="zh_TW" ;;
35 esac
36 if echo "de en fr pt ru zh" | fgrep -q "$lang"; then
37 break
38 fi
39 done
40 unset IFS
41 echo "$LANG"
42 }
44 # Short 2-letter lang code from ll_CC
45 ll_lang() {
46 ll_CC="$1"
47 echo ${ll_CC%_*}
48 }
50 # Nice URL replacer - to copy url from address bar
51 # TODO: deal with POST method of form submitting
52 nice_url() {
53 # if user submitted a form
54 if [ ! -z $(GET submit) ]; then
55 OBJECT="$(GET object)"
56 SEARCH="$(GET query)"
57 case $OBJECT in
58 Package) NICE="package=$SEARCH";;
59 Desc) NICE="desc=$SEARCH";;
60 Tags) NICE="tags=$SEARCH";;
61 Arch) NICE="arch=$SEARCH";;
62 Bugs) NICE="bugs=$SEARCH";;
63 Receipt) NICE="receipt=$SEARCH";;
64 Depends) NICE="depends=$SEARCH";;
65 BuildDepends) NICE="builddepends=$SEARCH";;
66 File) NICE="file=$SEARCH";;
67 File_list) NICE="filelist=$SEARCH";;
68 FileOverlap) NICE="fileoverlap=$SEARCH";;
69 esac
70 # version, if needed
71 version="$(GET version)"
72 if [ ! -z "$version" -a "$version" != "cooking" ]; then
73 NICE="${NICE}&version=${version:0:1}"
74 fi
75 # lang, if needed
76 query_lang="$(GET lang)"
77 pref_lang="$(user_lang)"
78 browser_lang="$(ll_lang $pref_lang)"
79 if [ ! -z "$query_lang" -a "$query_lang" != "$browser_lang" ]; then
80 NICE="${NICE}&lang=$query_lang"
81 fi
82 # verbose, if needed
83 verboseq="$(GET verbose)"
84 if [ ! -z "$verboseq" -a "$verboseq" != "0" ]; then
85 NICE="${NICE}&verbose=1"
86 fi
87 # redirect
88 # TODO: implement HTTP 301 Redirect
89 cat << EOT
90 Content-type: text/html
92 <!DOCTYPE html>
93 <html><head><meta http-equiv="refresh" content="0;url=$SCRIPT_NAME?$NICE" />
94 <title>Redirect</title></head></html>
95 EOT
96 # echo "Location: $SCRIPT_NAME?$NICE"
97 # echo
98 exit 0
99 fi
100 }
102 nice_url
106 OBJECT="$(GET object)"
107 SEARCH="$(GET query)"
108 SLITAZ_VERSION="$(GET version)"
109 VERBOSE="$(GET verbose)"
111 # Internal variables
112 #DATE=$(date +%Y-%m-%d\ %H:%M:%S)
114 # Internationalization
115 . /usr/bin/gettext.sh
116 export TEXTDOMAIN='tazpkg-web'
118 SEARCH=""
119 VERBOSE=0
120 for i in $(echo $QUERY_STRING | sed 's/[?&]/ /g'); do
121 # SLITAZ_VERSION=cooking
122 case "$(echo $i | tr [A-Z] [a-z])" in
123 query=*|search=*) [ ${i#*=} == Search ] || SEARCH=${i#*=};;
124 object=*) OBJECT=${i#*=};;
125 verbose=*) VERBOSE=${i#*=};;
126 lang=*) LANG=${i#*=};;
127 file=*) SEARCH=${i#*=}; OBJECT=File;;
128 desc=*) SEARCH=${i#*=}; OBJECT=Desc;;
129 tags=*) SEARCH=${i#*=}; OBJECT=Tags;;
130 arch=*) SEARCH=${i#*=}; OBJECT=Arch;;
131 bugs=*) SEARCH=${i#*=}; OBJECT=Bugs;;
132 receipt=*) SEARCH=${i#*=}; OBJECT=Receipt;;
133 filelist=*) SEARCH=${i#*=}; OBJECT=File_list;;
134 package=*) SEARCH=${i#*=}; OBJECT=Package;;
135 depends=*) SEARCH=${i#*=}; OBJECT=Depends;;
136 builddepends=*) SEARCH=${i#*=}; OBJECT=BuildDepends;;
137 fileoverlap=*) SEARCH=${i#*=}; OBJECT=FileOverlap;;
138 category=*) SEARCH=${i#*=}; OBJECT=Category;;
139 maintainer=*) SEARCH=${i#*=}; OBJECT=Maintainer;;
140 license=*) SEARCH=${i#*=}; OBJECT=License;;
141 version=[1-9]*) i=${i%%.*}; SLITAZ_VERSION=${i#*=}.0;;
142 version=s*|version=4*) SLITAZ_VERSION=stable;;
143 version=u*) SLITAZ_VERSION=undigest;;
144 version=t*) SLITAZ_VERSION=tiny;;
145 esac
146 done
147 [ -z "$SLITAZ_VERSION" ] && SLITAZ_VERSION=cooking
148 #[ -n "$SEARCH" ] && REQUEST_METHOD="POST"
149 #[ "$SEARCH" == "." ] && SEARCH=
152 # Content negotiation for Gettext
153 LANG=$(user_lang $(GET lang))
154 lang="$(ll_lang $LANG)"
155 export LANG LC_ALL=$LANG
158 case "$OBJECT" in
159 File) selected_file="selected";;
160 Desc) selected_desc="selected";;
161 Tags) selected_tags="selected";;
162 Arch) selected_arch="selected";;
163 Bugs) selected_bugs="selected";;
164 Receipt) selected_receipt="selected";;
165 File_list) selected_file_list="selected";;
166 Depends) selected_depends="selected";;
167 BuildDepends) selected_build_depends="selected";;
168 FileOverlap) selected_overlap="selected";;
169 esac
171 case "$SLITAZ_VERSION" in
172 tiny) selected_tiny="selected";;
173 1.0) selected_1="selected";;
174 2.0) selected_2="selected";;
175 3.0) selected_3="selected";;
176 stable) selected_stable="selected";;
177 undigest) selected_undigest="selected";;
178 esac
180 # unescape query
181 SEARCH="$(echo $SEARCH | sed 's/%2B/+/g; s/%3A/:/g; s|%2F|/|g')"
183 WOK=/home/slitaz/$SLITAZ_VERSION/wok
184 PACKAGES_REPOSITORY=/home/slitaz/$SLITAZ_VERSION/packages
187 # TODO: header function from httphelper
188 echo "Content-type: text/html"
189 echo
191 # Search form
192 # TODO: implement POST method
193 # ... method="post" enctype="multipart/form-data" ...
194 # TODO: add hint 'You are can search for depends loop, if textfield is empty'...
195 # add progress ticker while page is not complete
196 search_form()
197 {
198 cat << _EOT_
200 <form id="s_form" method="get" action="$SCRIPT_NAME">
201 <input type="hidden" name="lang" value="$lang" />
202 <select name="object">
203 <option value="Package">$(gettext "Package")</option>
204 <option $selected_desc value="Desc">$(gettext "Description")</option>
205 <option $selected_tags value="Tags">$(gettext "Tags")</option>
206 <!-- option $selected_arch value="Tags">$(gettext "Arch")</option -->
207 <!-- option $selected_bugs value="Bugs">$(gettext "Bugs")</option -->
208 <option $selected_receipt value="Receipt">$(gettext "Receipt")</option>
209 <option $selected_depends value="Depends">$(gettext "Depends")</option>
210 <option $selected_build_depends value="BuildDepends">$(gettext "Build depends")</option>
211 <option $selected_file value="File">$(gettext "File")</option>
212 <option $selected_file_list value="File_list">$(gettext "File list")</option>
213 <option $selected_overlap value="FileOverlap">$(gettext "common files")</option>
214 </select>
215 <input type="text" name="query" id="query" size="20" value="$SEARCH" />
216 <select name="version">
217 <option value="cooking">$(gettext "cooking")</option>
218 <option $selected_stable value="stable">4.0</option>
219 <option $selected_3 value="3.0">3.0</option>
220 <option $selected_2 value="2.0">2.0</option>
221 <option $selected_1 value="1.0">1.0</option>
222 <option $selected_tiny value="tiny">$(gettext "tiny")</option>
223 <option $selected_undigest value="undigest">$(gettext "undigest")</option>
224 </select>
225 <input type="submit" name="submit" value="$(gettext 'Search')" />
226 </form>
227 _EOT_
228 }
230 # xHTML5 Header.
231 xhtml_header() {
232 . lib/header.sh
233 }
235 cat_files_list()
236 {
237 local tmp=/tmp/files.list.$(basename ${1%/packages})
238 if [ ! -s $tmp -o $1/files.list.lzma -nt $tmp ]; then
239 unlzma -c $1/files.list.lzma > $tmp.$$ && mv $tmp.$$ $tmp
240 fi
241 case "$2" in
242 lines) if [ ! -s $tmp.lines -o $tmp -nt $tmp.lines ]; then
243 cat $tmp | wc -l > $tmp.lines.$$ &&
244 mv $tmp.lines.$$ $tmp.lines
245 fi
246 cat $tmp.lines ;;
247 *) cat $tmp ;;
248 esac
249 }
251 # xHTML Footer.
252 # TODO: caching the summary for 5 minutes
253 xhtml_footer() {
254 file=/tmp/footer-$SLITAZ_VERSION
255 [ ! -e $file -o $PACKAGES_REPOSITORY/packages.txt -nt $file ] &&
256 cat > $file.$$ <<EOT && mv -f $file.$$ $file
257 PKGS=$(ls $WOK/ | wc -l)
258 FILES=$(cat_files_list $PACKAGES_REPOSITORY lines)
259 EOT
260 PKGS=$(sed '/PKGS=/!d;s/PKGS=//' $file)
261 FILES=$(sed '/FILES=/!d;s/FILES=//' $file)
262 . lib/footer.sh
263 }
265 installed_size()
266 {
267 if [ $VERBOSE -gt 0 ]; then
268 inst=$(grep -A 3 "^$1\$" $PACKAGES_REPOSITORY/packages.txt | grep installed)