832f9d9b86b8b2fb0f1c403f0a5aab0a61d2180e
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010 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.Mapper;
44 import net.pterodactylus.util.collection.Mappers;
45 import net.pterodactylus.util.collection.Pagination;
46 import net.pterodactylus.util.collection.TimedMap;
47 import net.pterodactylus.util.filter.Filter;
48 import net.pterodactylus.util.filter.Filters;
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                 @SuppressWarnings("synthetic-access")
71                 public CacheItem<Set<Hit<Post>>> retrieve(List<Phrase> phrases) throws CacheException {
72                         Set<Post> posts = new HashSet<Post>();
73                         for (Sone sone : webInterface.getCore().getSones()) {
74                                 posts.addAll(sone.getPosts());
75                         }
76                         return new DefaultCacheItem<Set<Hit<Post>>>(getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()));
77                 }
78
79         }, new TimedMap<List<Phrase>, CacheItem<Set<Hit<Post>>>>(300000));
80
81         /**
82          * Creates a new search page.
83          *
84          * @param template
85          *            The template to render
86          * @param webInterface
87          *            The Sone web interface
88          */
89         public SearchPage(Template template, WebInterface webInterface) {
90                 super("search.html", template, "Page.Search.Title", webInterface);
91         }
92
93         //
94         // SONETEMPLATEPAGE METHODS
95         //
96
97         /**
98          * {@inheritDoc}
99          */
100         @Override
101         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
102                 super.processTemplate(request, templateContext);
103                 String query = request.getHttpRequest().getParam("query").trim();
104                 if (query.length() == 0) {
105                         throw new RedirectException("index.html");
106                 }
107
108                 List<Phrase> phrases = parseSearchPhrases(query);
109                 if (phrases.isEmpty()) {
110                         throw new RedirectException("index.html");
111                 }
112
113                 Set<Sone> sones = webInterface.getCore().getSones();
114                 Set<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
115
116                 Set<Hit<Post>> postHits;
117                 try {
118                         postHits = hitCache.get(phrases);
119                 } catch (CacheException ce1) {
120                         /* should never happen. */
121                         logger.log(Level.SEVERE, "Could not get search results from cache!", ce1);
122                         postHits = Collections.emptySet();
123                 }
124
125                 /* now filter. */
126                 soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
127                 postHits = Filters.filteredSet(postHits, Hit.POSITIVE_FILTER);
128
129                 /* now sort. */
130                 List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
131                 Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
132                 List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
133                 Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
134
135                 /* extract Sones and posts. */
136                 List<Sone> resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper<Sone>());
137                 List<Post> resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper<Post>());
138
139                 /* pagination. */
140                 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
141                 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
142
143                 templateContext.set("sonePagination", sonePagination);
144                 templateContext.set("soneHits", sonePagination.getItems());
145                 templateContext.set("postPagination", postPagination);
146                 templateContext.set("postHits", postPagination.getItems());
147         }
148
149         //
150         // PRIVATE METHODS
151         //
152
153         /**
154          * Collects hit information for the given objects. The objects are converted
155          * to a {@link String} using the given {@link StringGenerator}, and the
156          * {@link #calculateScore(List, String) calculated score} is stored together
157          * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
158          * returned.
159          *
160          * @param <T>
161          *            The type of the objects
162          * @param objects
163          *            The objects to search over
164          * @param phrases
165          *            The phrases to search for
166          * @param stringGenerator
167          *            The string generator for the objects
168          * @return The hits for the given phrases
169          */
170         private <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
171                 Set<Hit<T>> hits = new HashSet<Hit<T>>();
172                 for (T object : objects) {
173                         String objectString = stringGenerator.generateString(object);
174                         double score = calculateScore(phrases, objectString);
175                         hits.add(new Hit<T>(object, score));
176                 }
177                 return hits;
178         }
179
180         /**
181          * Parses the given query into search phrases. The query is split on
182          * whitespace while allowing to group words using single or double quotes.
183          * Isolated phrases starting with a “+” are
184          * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
185          * {@link Phrase.Optionality#FORBIDDEN}.
186          *
187          * @param query
188          *            The query to parse
189          * @return The parsed phrases
190          */
191         private List<Phrase> parseSearchPhrases(String query) {
192                 List<String> parsedPhrases = null;
193                 try {
194                         parsedPhrases = StringEscaper.parseLine(query);
195                 } catch (TextException te1) {
196                         /* invalid query. */
197                         return Collections.emptyList();
198                 }
199
200                 List<Phrase> phrases = new ArrayList<Phrase>();
201                 for (String phrase : parsedPhrases) {
202                         if (phrase.startsWith("+")) {
203                                 if (phrase.length() > 1) {
204                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
205                                 } else {
206                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
207                                 }
208                         } else if (phrase.startsWith("-")) {
209                                 if (phrase.length() > 1) {
210                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
211                                 } else {
212                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
213                                 }
214                         } else {
215                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
216                         }
217                 }
218                 return phrases;
219         }
220
221         /**
222          * Calculates the score for the given expression when using the given
223          * phrases.
224          *
225          * @param phrases
226          *            The phrases to search for
227          * @param expression
228          *            The expression to search
229          * @return The score of the expression
230          */
231         private double calculateScore(List<Phrase> phrases, String expression) {
232                 logger.log(Level.FINEST, "Calculating Score for “%s”…", expression);
233                 double optionalHits = 0;
234                 double requiredHits = 0;
235                 int forbiddenHits = 0;
236                 int requiredPhrases = 0;
237                 for (Phrase phrase : phrases) {
238                         String phraseString = phrase.getPhrase().toLowerCase();
239                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
240                                 ++requiredPhrases;
241                         }
242                         int matches = 0;
243                         int index = 0;
244                         double score = 0;
245                         while (index < expression.length()) {
246                                 int position = expression.toLowerCase().indexOf(phraseString, index);
247                                 if (position == -1) {
248                                         break;
249                                 }
250                                 score += Math.pow(1 - position / (double) expression.length(), 2);
251                                 index = position + phraseString.length();
252                                 logger.log(Level.FINEST, "Got hit at position %d.", position);
253                                 ++matches;
254                         }
255                         logger.log(Level.FINEST, "Score: %f", score);
256                         if (matches == 0) {
257                                 continue;
258                         }
259                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
260                                 requiredHits += score;
261                         }
262                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
263                                 optionalHits += score;
264                         }
265                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
266                                 forbiddenHits += matches;
267                         }
268                 }
269                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
270         }
271
272         /**
273          * Converts a given object into a {@link String}.
274          *
275          * @param <T>
276          *            The type of the objects
277          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
278          */
279         private static interface StringGenerator<T> {
280
281                 /**
282                  * Generates a {@link String} for the given object.
283                  *
284                  * @param object
285                  *            The object to generate the {@link String} for
286                  * @return The generated {@link String}
287                  */
288                 public String generateString(T object);
289
290         }
291
292         /**
293          * Generates a {@link String} from a {@link Sone}, concatenating the name of
294          * the Sone and all {@link Profile} {@link Field} values.
295          *
296          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
297          */
298         private static class SoneStringGenerator implements StringGenerator<Sone> {
299
300                 /** A static instance of a complete Sone string generator. */
301                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
302
303                 /**
304                  * A static instance of a Sone string generator that will only use the
305                  * name of the Sone.
306                  */
307                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
308
309                 /** Whether to generate a string from all data of a Sone. */
310                 private final boolean complete;
311
312                 /**
313                  * Creates a new Sone string generator.
314                  *
315                  * @param complete
316                  *            {@code true} to use the profile’s fields, {@code false} to
317                  *            not to use the profile‘s fields
318                  */
319                 private SoneStringGenerator(boolean complete) {
320                         this.complete = complete;
321                 }
322
323                 /**
324                  * {@inheritDoc}
325                  */
326                 @Override
327                 public String generateString(Sone sone) {
328                         StringBuilder soneString = new StringBuilder();
329                         soneString.append(sone.getName());
330                         Profile soneProfile = sone.getProfile();
331                         if (soneProfile.getFirstName() != null) {
332                                 soneString.append(' ').append(soneProfile.getFirstName());
333                         }
334                         if (soneProfile.getMiddleName() != null) {
335                                 soneString.append(' ').append(soneProfile.getMiddleName());
336                         }
337                         if (soneProfile.getLastName() != null) {
338                                 soneString.append(' ').append(soneProfile.getLastName());
339                         }
340                         if (complete) {
341                                 for (Field field : soneProfile.getFields()) {
342                                         soneString.append(' ').append(field.getValue());
343                                 }
344                         }
345                         return soneString.toString();
346                 }
347
348         }
349
350         /**
351          * Generates a {@link String} from a {@link Post}, concatenating the text of
352          * the post, the text of all {@link Reply}s, and the name of all
353          * {@link Sone}s that have replied.
354          *
355          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
356          */
357         private class PostStringGenerator implements StringGenerator<Post> {
358
359                 /**
360                  * {@inheritDoc}
361                  */
362                 @Override
363                 public String generateString(Post post) {
364                         StringBuilder postString = new StringBuilder();
365                         postString.append(post.getText());
366                         if (post.getRecipient() != null) {
367                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
368                         }
369                         for (PostReply reply : Filters.filteredList(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) {
370                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
371                                 postString.append(' ').append(reply.getText());
372                         }
373                         return postString.toString();
374                 }
375
376         }
377
378         /**
379          * A search phrase.
380          *
381          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
382          */
383         private static class Phrase {
384
385                 /**
386                  * The optionality of a search phrase.
387                  *
388                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
389                  *         Roden</a>
390                  */
391                 public enum Optionality {
392
393                         /** The phrase is optional. */
394                         OPTIONAL,
395
396                         /** The phrase is required. */
397                         REQUIRED,
398
399                         /** The phrase is forbidden. */
400                         FORBIDDEN
401
402                 }
403
404                 /** The phrase to search for. */
405                 private final String phrase;
406
407                 /** The optionality of the phrase. */
408                 private final Optionality optionality;
409
410                 /**
411                  * Creates a new phrase.
412                  *
413                  * @param phrase
414                  *            The phrase to search for
415                  * @param optionality
416                  *            The optionality of the phrase
417                  */
418                 public Phrase(String phrase, Optionality optionality) {
419                         this.optionality = optionality;
420                         this.phrase = phrase;
421                 }
422
423                 /**
424                  * Returns the phrase to search for.
425                  *
426                  * @return The phrase to search for
427                  */
428                 public String getPhrase() {
429                         return phrase;
430                 }
431
432                 /**
433                  * Returns the optionality of the phrase.
434                  *
435                  * @return The optionality of the phrase
436                  */
437                 public Optionality getOptionality() {
438                         return optionality;
439                 }
440
441                 //
442                 // OBJECT METHODS
443                 //
444
445                 /**
446                  * {@inheritDoc}
447                  */
448                 @Override
449                 public int hashCode() {
450                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
451                 }
452
453                 /**
454                  * {@inheritDoc}
455                  */
456                 @Override
457                 public boolean equals(Object object) {
458                         if (!(object instanceof Phrase)) {
459                                 return false;
460                         }
461                         Phrase phrase = (Phrase) object;
462                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
463                 }
464
465         }
466
467         /**
468          * A hit consists of a searched object and the score it got for the phrases
469          * of the search.
470          *
471          * @see SearchPage#calculateScore(List, String)
472          * @param <T>
473          *            The type of the searched object
474          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
475          */
476         private static class Hit<T> {
477
478                 /** Filter for {@link Hit}s with a score of more than 0. */
479                 public static final Filter<Hit<?>> POSITIVE_FILTER = new Filter<Hit<?>>() {
480
481                         @Override
482                         public boolean filterObject(Hit<?> hit) {
483                                 return hit.getScore() > 0;
484                         }
485
486                 };
487
488                 /** Comparator that sorts {@link Hit}s descending by score. */
489                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
490
491                         @Override
492                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
493                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
494                         }
495
496                 };
497
498                 /** The object that was searched. */
499                 private final T object;
500
501                 /** The score of the object. */
502                 private final double score;
503
504                 /**
505                  * Creates a new hit.
506                  *
507                  * @param object
508                  *            The object that was searched
509                  * @param score
510                  *            The score of the object
511                  */
512                 public Hit(T object, double score) {
513                         this.object = object;
514                         this.score = score;
515                 }
516
517                 /**
518                  * Returns the object that was searched.
519                  *
520                  * @return The object that was searched
521                  */
522                 public T getObject() {
523                         return object;
524                 }
525
526                 /**
527                  * Returns the score of the object.
528                  *
529                  * @return The score of the object
530                  */
531                 public double getScore() {
532                         return score;
533                 }
534
535         }
536
537         /**
538          * Extracts the object from a {@link Hit}.
539          *
540          * @param <T>
541          *            The type of the object to extract
542          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
543          */
544         public static class HitMapper<T> implements Mapper<Hit<T>, T> {
545
546                 /**
547                  * {@inheritDoc}
548                  */
549                 @Override
550                 public T map(Hit<T> input) {
551                         return input.getObject();
552                 }
553
554         }
555
556 }