slitaz-forge view pkgs/search.sh @ rev 360

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