Use named thread factory to create downloader threads.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010–2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Profile;
33 import net.pterodactylus.sone.data.Profile.Field;
34 import net.pterodactylus.sone.data.Reply;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.web.page.FreenetRequest;
37 import net.pterodactylus.util.cache.Cache;
38 import net.pterodactylus.util.cache.CacheException;
39 import net.pterodactylus.util.cache.CacheItem;
40 import net.pterodactylus.util.cache.DefaultCacheItem;
41 import net.pterodactylus.util.cache.MemoryCache;
42 import net.pterodactylus.util.cache.ValueRetriever;
43 import net.pterodactylus.util.collection.Pagination;
44 import net.pterodactylus.util.collection.TimedMap;
45 import net.pterodactylus.util.collection.filter.Filter;
46 import net.pterodactylus.util.collection.filter.Filters;
47 import net.pterodactylus.util.collection.mapper.Mapper;
48 import net.pterodactylus.util.collection.mapper.Mappers;
49 import net.pterodactylus.util.logging.Logging;
50 import net.pterodactylus.util.number.Numbers;
51 import net.pterodactylus.util.template.Template;
52 import net.pterodactylus.util.template.TemplateContext;
53 import net.pterodactylus.util.text.StringEscaper;
54 import net.pterodactylus.util.text.TextException;
55
56 /**
57  * This page lets the user search for posts and replies that contain certain
58  * words.
59  *
60  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61  */
62 public class SearchPage extends SoneTemplatePage {
63
64         /** The logger. */
65         private static final Logger logger = Logging.getLogger(SearchPage.class);
66
67         /** Short-term cache. */
68         private final Cache<List<Phrase>, Set<Hit<Post>>> hitCache = new MemoryCache<List<Phrase>, Set<Hit<Post>>>(new ValueRetriever<List<Phrase>, Set<Hit<Post>>>() {
69
70                 @Override
71                 @SuppressWarnings("synthetic-access")
72                 public CacheItem<Set<Hit<Post>>> retrieve(List<Phrase> phrases) throws CacheException {
73                         Set<Post> posts = new HashSet<Post>();
74                         for (Sone sone : webInterface.getCore().getSones()) {
75                                 posts.addAll(sone.getPosts());
76                         }
77                         return new DefaultCacheItem<Set<Hit<Post>>>(getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()));
78                 }
79
80         }, new TimedMap<List<Phrase>, CacheItem<Set<Hit<Post>>>>(300000));
81
82         /**
83          * Creates a new search page.
84          *
85          * @param template
86          *            The template to render
87          * @param webInterface
88          *            The Sone web interface
89          */
90         public SearchPage(Template template, WebInterface webInterface) {
91                 super("search.html", template, "Page.Search.Title", webInterface);
92         }
93
94         //
95         // SONETEMPLATEPAGE METHODS
96         //
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
103                 super.processTemplate(request, templateContext);
104                 String query = request.getHttpRequest().getParam("query").trim();
105                 if (query.length() == 0) {
106                         throw new RedirectException("index.html");
107                 }
108
109                 List<Phrase> phrases = parseSearchPhrases(query);
110                 if (phrases.isEmpty()) {
111                         throw new RedirectException("index.html");
112                 }
113
114                 /* check for a couple of shortcuts. */
115                 if (phrases.size() == 1) {
116                         String phrase = phrases.get(0).getPhrase();
117
118                         /* is it a Sone ID? */
119                         redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
120
121                         /* is it a post ID? */
122                         redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
123
124                         /* is it a reply ID? show the post. */
125                         redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
126
127                         /* is it an album ID? */
128                         redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
129
130                         /* is it an image ID? */
131                         redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
132                 }
133
134                 Set<Sone> sones = webInterface.getCore().getSones();
135                 Set<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
136
137                 Set<Hit<Post>> postHits;
138                 try {
139                         postHits = hitCache.get(phrases);
140                 } catch (CacheException ce1) {
141                         /* should never happen. */
142                         logger.log(Level.SEVERE, "Could not get search results from cache!", ce1);
143                         postHits = Collections.emptySet();
144                 }
145
146                 /* now filter. */
147                 soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
148                 postHits = Filters.filteredSet(postHits, Hit.POSITIVE_FILTER);
149
150                 /* now sort. */
151                 List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
152                 Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
153                 List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
154                 Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
155
156                 /* extract Sones and posts. */
157                 List<Sone> resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper<Sone>());
158                 List<Post> resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper<Post>());
159
160                 /* pagination. */
161                 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
162                 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
163
164                 templateContext.set("sonePagination", sonePagination);
165                 templateContext.set("soneHits", sonePagination.getItems());
166                 templateContext.set("postPagination", postPagination);
167                 templateContext.set("postHits", postPagination.getItems());
168         }
169
170         //
171         // PRIVATE METHODS
172         //
173
174         /**
175          * Collects hit information for the given objects. The objects are converted
176          * to a {@link String} using the given {@link StringGenerator}, and the
177          * {@link #calculateScore(List, String) calculated score} is stored together
178          * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
179          * returned.
180          *
181          * @param <T>
182          *            The type of the objects
183          * @param objects
184          *            The objects to search over
185          * @param phrases
186          *            The phrases to search for
187          * @param stringGenerator
188          *            The string generator for the objects
189          * @return The hits for the given phrases
190          */
191         private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
192                 Set<Hit<T>> hits = new HashSet<Hit<T>>();
193                 for (T object : objects) {
194                         String objectString = stringGenerator.generateString(object);
195                         double score = calculateScore(phrases, objectString);
196                         hits.add(new Hit<T>(object, score));
197                 }
198                 return hits;
199         }
200
201         /**
202          * Parses the given query into search phrases. The query is split on
203          * whitespace while allowing to group words using single or double quotes.
204          * Isolated phrases starting with a “+” are
205          * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
206          * {@link Phrase.Optionality#FORBIDDEN}.
207          *
208          * @param query
209          *            The query to parse
210          * @return The parsed phrases
211          */
212         private static List<Phrase> parseSearchPhrases(String query) {
213                 List<String> parsedPhrases = null;
214                 try {
215                         parsedPhrases = StringEscaper.parseLine(query);
216                 } catch (TextException te1) {
217                         /* invalid query. */
218                         return Collections.emptyList();
219                 }
220
221                 List<Phrase> phrases = new ArrayList<Phrase>();
222                 for (String phrase : parsedPhrases) {
223                         if (phrase.startsWith("+")) {
224                                 if (phrase.length() > 1) {
225                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
226                                 } else {
227                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
228                                 }
229                         } else if (phrase.startsWith("-")) {
230                                 if (phrase.length() > 1) {
231                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
232                                 } else {
233                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
234                                 }
235                         } else {
236                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
237                         }
238                 }
239                 return phrases;
240         }
241
242         /**
243          * Calculates the score for the given expression when using the given
244          * phrases.
245          *
246          * @param phrases
247          *            The phrases to search for
248          * @param expression
249          *            The expression to search
250          * @return The score of the expression
251          */
252         private static double calculateScore(List<Phrase> phrases, String expression) {
253                 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
254                 double optionalHits = 0;
255                 double requiredHits = 0;
256                 int forbiddenHits = 0;
257                 int requiredPhrases = 0;
258                 for (Phrase phrase : phrases) {
259                         String phraseString = phrase.getPhrase().toLowerCase();
260                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
261                                 ++requiredPhrases;
262                         }
263                         int matches = 0;
264                         int index = 0;
265                         double score = 0;
266                         while (index < expression.length()) {
267                                 int position = expression.toLowerCase().indexOf(phraseString, index);
268                                 if (position == -1) {
269                                         break;
270                                 }
271                                 score += Math.pow(1 - position / (double) expression.length(), 2);
272                                 index = position + phraseString.length();
273                                 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
274                                 ++matches;
275                         }
276                         logger.log(Level.FINEST, String.format("Score: %f", score));
277                         if (matches == 0) {
278                                 continue;
279                         }
280                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
281                                 requiredHits += score;
282                         }
283                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
284                                 optionalHits += score;
285                         }
286                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
287                                 forbiddenHits += matches;
288                         }
289                 }
290                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
291         }
292
293         /**
294          * Throws a
295          * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
296          * if the given object is not {@code null}, appending the object to the
297          * given target URL.
298          *
299          * @param object
300          *            The object on which to redirect
301          * @param target
302          *            The target of the redirect
303          * @throws RedirectException
304          *             if {@code object} is not {@code null}
305          */
306         private static void redirectIfNotNull(String object, String target) throws RedirectException {
307                 if (object != null) {
308                         throw new RedirectException(target + object);
309                 }
310         }
311
312         /**
313          * If the given phrase contains a Sone ID (optionally prefixed by
314          * “sone://”), returns said Sone ID, otherwise return {@code null}.
315          *
316          * @param phrase
317          *            The phrase that maybe is a Sone ID
318          * @return The Sone ID, or {@code null}
319          */
320         private String getSoneId(String phrase) {
321                 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
322                 return (webInterface.getCore().getSone(soneId, false) != null) ? soneId : null;
323         }
324
325         /**
326          * If the given phrase contains a post ID (optionally prefixed by
327          * “post://”), returns said post ID, otherwise return {@code null}.
328          *
329          * @param phrase
330          *            The phrase that maybe is a post ID
331          * @return The post ID, or {@code null}
332          */
333         private String getPostId(String phrase) {
334                 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
335                 return (webInterface.getCore().getPost(postId, false) != null) ? postId : null;
336         }
337
338         /**
339          * If the given phrase contains a reply ID (optionally prefixed by
340          * “reply://”), returns the ID of the post the reply belongs to, otherwise
341          * return {@code null}.
342          *
343          * @param phrase
344          *            The phrase that maybe is a reply ID
345          * @return The reply’s post ID, or {@code null}
346          */
347         private String getReplyPostId(String phrase) {
348                 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
349                 return (webInterface.getCore().getReply(replyId, false) != null) ? webInterface.getCore().getReply(replyId, false).getPost().getId() : null;
350         }
351
352         /**
353          * If the given phrase contains an album ID (optionally prefixed by
354          * “album://”), returns said album ID, otherwise return {@code null}.
355          *
356          * @param phrase
357          *            The phrase that maybe is an album ID
358          * @return The album ID, or {@code null}
359          */
360         private String getAlbumId(String phrase) {
361                 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
362                 return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null;
363         }
364
365         /**
366          * If the given phrase contains an image ID (optionally prefixed by
367          * “image://”), returns said image ID, otherwise return {@code null}.
368          *
369          * @param phrase
370          *            The phrase that maybe is an image ID
371          * @return The image ID, or {@code null}
372          */
373         private String getImageId(String phrase) {
374                 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
375                 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
376         }
377
378         /**
379          * Converts a given object into a {@link String}.
380          *
381          * @param <T>
382          *            The type of the objects
383          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
384          */
385         private static interface StringGenerator<T> {
386
387                 /**
388                  * Generates a {@link String} for the given object.
389                  *
390                  * @param object
391                  *            The object to generate the {@link String} for
392                  * @return The generated {@link String}
393                  */
394                 public String generateString(T object);
395
396         }
397
398         /**
399          * Generates a {@link String} from a {@link Sone}, concatenating the name of
400          * the Sone and all {@link Profile} {@link Field} values.
401          *
402          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
403          */
404         private static class SoneStringGenerator implements StringGenerator<Sone> {
405
406                 /** A static instance of a complete Sone string generator. */
407                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
408
409                 /**
410                  * A static instance of a Sone string generator that will only use the
411                  * name of the Sone.
412                  */
413                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
414
415                 /** Whether to generate a string from all data of a Sone. */
416                 private final boolean complete;
417
418                 /**
419                  * Creates a new Sone string generator.
420                  *
421                  * @param complete
422                  *            {@code true} to use the profile’s fields, {@code false} to
423                  *            not to use the profile‘s fields
424                  */
425                 private SoneStringGenerator(boolean complete) {
426                         this.complete = complete;
427                 }
428
429                 /**
430                  * {@inheritDoc}
431                  */
432                 @Override
433                 public String generateString(Sone sone) {
434                         StringBuilder soneString = new StringBuilder();
435                         soneString.append(sone.getName());
436                         Profile soneProfile = sone.getProfile();
437                         if (soneProfile.getFirstName() != null) {
438                                 soneString.append(' ').append(soneProfile.getFirstName());
439                         }
440                         if (soneProfile.getMiddleName() != null) {
441                                 soneString.append(' ').append(soneProfile.getMiddleName());
442                         }
443                         if (soneProfile.getLastName() != null) {
444                                 soneString.append(' ').append(soneProfile.getLastName());
445                         }
446                         if (complete) {
447                                 for (Field field : soneProfile.getFields()) {
448                                         soneString.append(' ').append(field.getValue());
449                                 }
450                         }
451                         return soneString.toString();
452                 }
453
454         }
455
456         /**
457          * Generates a {@link String} from a {@link Post}, concatenating the text of
458          * the post, the text of all {@link Reply}s, and the name of all
459          * {@link Sone}s that have replied.
460          *
461          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
462          */
463         private class PostStringGenerator implements StringGenerator<Post> {
464
465                 /**
466                  * {@inheritDoc}
467                  */
468                 @Override
469                 public String generateString(Post post) {
470                         StringBuilder postString = new StringBuilder();
471                         postString.append(post.getText());
472                         if (post.getRecipient() != null) {
473                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
474                         }
475                         for (PostReply reply : Filters.filteredList(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) {
476                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
477                                 postString.append(' ').append(reply.getText());
478                         }
479                         return postString.toString();
480                 }
481
482         }
483
484         /**
485          * A search phrase.
486          *
487          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
488          */
489         private static class Phrase {
490
491                 /**
492                  * The optionality of a search phrase.
493                  *
494                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
495                  *         Roden</a>
496                  */
497                 public enum Optionality {
498
499                         /** The phrase is optional. */
500                         OPTIONAL,
501
502                         /** The phrase is required. */
503                         REQUIRED,
504
505                         /** The phrase is forbidden. */
506                         FORBIDDEN
507
508                 }
509
510                 /** The phrase to search for. */
511                 private final String phrase;
512
513                 /** The optionality of the phrase. */
514                 private final Optionality optionality;
515
516                 /**
517                  * Creates a new phrase.
518                  *
519                  * @param phrase
520                  *            The phrase to search for
521                  * @param optionality
522                  *            The optionality of the phrase
523                  */
524                 public Phrase(String phrase, Optionality optionality) {
525                         this.optionality = optionality;
526                         this.phrase = phrase;
527                 }
528
529                 /**
530                  * Returns the phrase to search for.
531                  *
532                  * @return The phrase to search for
533                  */
534                 public String getPhrase() {
535                         return phrase;
536                 }
537
538                 /**
539                  * Returns the optionality of the phrase.
540                  *
541                  * @return The optionality of the phrase
542                  */
543                 public Optionality getOptionality() {
544                         return optionality;
545                 }
546
547                 //
548                 // OBJECT METHODS
549                 //
550
551                 /**
552                  * {@inheritDoc}
553                  */
554                 @Override
555                 public int hashCode() {
556                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
557                 }
558
559                 /**
560                  * {@inheritDoc}
561                  */
562                 @Override
563                 public boolean equals(Object object) {
564                         if (!(object instanceof Phrase)) {
565                                 return false;
566                         }
567                         Phrase phrase = (Phrase) object;
568                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
569                 }
570
571         }
572
573         /**
574          * A hit consists of a searched object and the score it got for the phrases
575          * of the search.
576          *
577          * @see SearchPage#calculateScore(List, String)
578          * @param <T>
579          *            The type of the searched object
580          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
581          */
582         private static class Hit<T> {
583
584                 /** Filter for {@link Hit}s with a score of more than 0. */
585                 public static final Filter<Hit<?>> POSITIVE_FILTER = new Filter<Hit<?>>() {
586
587                         @Override
588                         public boolean filterObject(Hit<?> hit) {
589                                 return hit.getScore() > 0;
590                         }
591
592                 };
593
594                 /** Comparator that sorts {@link Hit}s descending by score. */
595                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
596
597                         @Override
598                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
599                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
600                         }
601
602                 };
603
604                 /** The object that was searched. */
605                 private final T object;
606
607                 /** The score of the object. */
608                 private final double score;
609
610                 /**
611                  * Creates a new hit.
612                  *
613                  * @param object
614                  *            The object that was searched
615                  * @param score
616                  *            The score of the object
617                  */
618                 public Hit(T object, double score) {
619                         this.object = object;
620                         this.score = score;
621                 }
622
623                 /**
624                  * Returns the object that was searched.
625                  *
626                  * @return The object that was searched
627                  */
628                 public T getObject() {
629                         return object;
630                 }
631
632                 /**
633                  * Returns the score of the object.
634                  *
635                  * @return The score of the object
636                  */
637                 public double getScore() {
638                         return score;
639                 }
640
641         }
642
643         /**
644          * Extracts the object from a {@link Hit}.
645          *
646          * @param <T>
647          *            The type of the object to extract
648          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
649          */
650         public static class HitMapper<T> implements Mapper<Hit<T>, T> {
651
652                 /**
653                  * {@inheritDoc}
654                  */
655                 @Override
656                 public T map(Hit<T> input) {
657                         return input.getObject();
658                 }
659
660         }
661
662 }