Browse Source

Add search by size

spaghetti 8 years ago
parent
commit
b6b3343db1
2 changed files with 106 additions and 91 deletions
  1. 85
    85
      classes/torrentsearch.class.php
  2. 21
    6
      sections/torrents/browse.php

+ 85
- 85
classes/torrentsearch.class.php View File

@@ -6,7 +6,7 @@ class TorrentSearch {
6 6
   const SPH_BOOL_OR = ' | ';
7 7
 
8 8
   // Map of sort mode => attribute name for ungrouped torrent page
9
-  public static $SortOrders = array(
9
+  public static $SortOrders = [
10 10
     'year' => 'year',
11 11
     'time' => 'id',
12 12
     'size' => 'size',
@@ -14,10 +14,11 @@ class TorrentSearch {
14 14
     'leechers' => 'leechers',
15 15
     'snatched' => 'snatched',
16 16
     'cataloguenumber' => 'cataloguenumber',
17
-    'random' => 1);
17
+    'random' => 1
18
+  ];
18 19
 
19 20
   // Map of sort mode => attribute name for grouped torrent page
20
-  private static $SortOrdersGrouped = array(
21
+  private static $SortOrdersGrouped = [
21 22
     'year' => 'year',
22 23
     'time' => 'id',
23 24
     'size' => 'maxsize',
@@ -25,25 +26,29 @@ class TorrentSearch {
25 26
     'leechers' => 'sumleechers',
26 27
     'snatched' => 'sumsnatched',
27 28
     'cataloguenumber' => 'cataloguenumber',
28
-    'random' => 1);
29
+    'random' => 1
30
+  ];
29 31
 
30 32
   // Map of sort mode => aggregate expression required for some grouped sort orders
31
-  private static $AggregateExp = array(
33
+  private static $AggregateExp = [
32 34
     'size' => 'MAX(size) AS maxsize',
33 35
     'seeders' => 'SUM(seeders) AS sumseeders',
34 36
     'leechers' => 'SUM(leechers) AS sumleechers',
35
-    'snatched' => 'SUM(snatched) AS sumsnatched');
37
+    'snatched' => 'SUM(snatched) AS sumsnatched'
38
+  ];
36 39
 
37 40
   // Map of attribute name => global variable name with list of values that can be used for filtering
38
-  private static $Attributes = array(
41
+  private static $Attributes = [
39 42
     'filter_cat' => false,
40 43
     'releasetype' => 'ReleaseTypes',
41 44
     'freetorrent' => false,
42 45
     'censored' => false,
43
-    'year' => false);
46
+    'size_unit' => false,
47
+    'year' => false
48
+  ];
44 49
 
45 50
   // List of fields that can be used for fulltext searches
46
-  private static $Fields = array(
51
+  private static $Fields = [
47 52
     'artistname' => 1,
48 53
     'audioformat' => 1,
49 54
     'cataloguenumber' => 1,
@@ -64,37 +69,42 @@ class TorrentSearch {
64 69
     'studio' => 1,
65 70
     'subber' => 1,
66 71
     'subbing' => 1,
67
-    'taglist' => 1);
72
+    'taglist' => 1
73
+  ];
68 74
 
69 75
   // List of torrent-specific fields that can be used for filtering
70
-  private static $TorrentFields = array(
76
+  private static $TorrentFields = [
71 77
     'description' => 1,
72 78
     'encoding' => 1,
73 79
     'censored' => 1,
74 80
     'language' => 1,
75 81
     'filelist' => 1,
76 82
     'format' => 1,
77
-    'media' => 1);
83
+    'media' => 1
84
+  ];
78 85
 
79 86
   // Some form field names don't match the ones in the index
80
-  private static $FormsToFields = array(
87
+  private static $FormsToFields = [
81 88
     'searchstr' => '(groupname,groupnamerj,groupnamejp,artistname,studio,series,dlsiteid,cataloguenumber,yearfulltext)',
82
-    'advgroupname' => '(groupname,groupnamerj,groupnamejp)');
89
+    'advgroupname' => '(groupname,groupnamerj,groupnamejp)'
90
+  ];
83 91
 
84 92
   // Specify the operator type to use for fields. Empty key sets the default
85
-  private static $FieldOperators = array(
93
+  private static $FieldOperators = [
86 94
     '' => self::SPH_BOOL_AND,
87 95
     'encoding' => self::SPH_BOOL_OR,
88 96
     'format' => self::SPH_BOOL_OR,
89
-    'media' => self::SPH_BOOL_OR);
97
+    'media' => self::SPH_BOOL_OR
98
+  ];
90 99
 
91 100
   // Specify the separator character to use for fields. Empty key sets the default
92
-  private static $FieldSeparators = array(
101
+  private static $FieldSeparators = [
93 102
     '' => ' ',
94 103
     'encoding' => '|',
95 104
     'format' => '|',
96 105
     'media' => '|',
97
-    'taglist' => ',');
106
+    'taglist' => ','
107
+  ];
98 108
 
99 109
   // Primary SphinxqlQuery object used to get group IDs or torrent IDs for ungrouped searches
100 110
   private $SphQL;
@@ -109,7 +119,7 @@ class TorrentSearch {
109 119
   // Number of results
110 120
   private $NumResults = 0;
111 121
   // Array with info from all matching torrent groups
112
-  private $Groups = array();
122
+  private $Groups = [];
113 123
   // Whether any filters were used
114 124
   private $Filtered = false;
115 125
   // Whether the random sort order is selected
@@ -122,15 +132,15 @@ class TorrentSearch {
122 132
    *     'operator' => self::SPH_BOOL_AND | self::SPH_BOOL_OR
123 133
    * ]], ...
124 134
    */
125
-  private $Terms = array();
135
+  private $Terms = [];
126 136
   // Unprocessed search terms for retrieval
127
-  private $RawTerms = array();
137
+  private $RawTerms = [];
128 138
   // Storage for used torrent-specific attribute filters
129 139
   // ['Field name' => 'Search expression', ...]
130
-  private $UsedTorrentAttrs = array();
140
+  private $UsedTorrentAttrs = [];
131 141
   // Storage for used torrent-specific fulltext fields
132 142
   // ['Field name' => 'Search expression', ...]
133
-  private $UsedTorrentFields = array();
143
+  private $UsedTorrentFields = [];
134 144
 
135 145
   /**
136 146
    * Initialize and configure a TorrentSearch object
@@ -144,7 +154,7 @@ class TorrentSearch {
144 154
   public function __construct($GroupResults, $OrderBy, $OrderWay, $Page, $PageSize) {
145 155
     if ($GroupResults && !isset(self::$SortOrdersGrouped[$OrderBy])
146 156
         || !$GroupResults && !isset(self::$SortOrders[$OrderBy])
147
-        || !in_array($OrderWay, array('asc', 'desc'))
157
+        || !in_array($OrderWay, ['asc', 'desc'])
148 158
     ) {
149 159
       global $Debug;
150 160
       $ErrMsg = "TorrentSearch constructor arguments:\n" . print_r(func_get_args(), true);
@@ -199,7 +209,7 @@ class TorrentSearch {
199 209
    * @param array $Terms Array containing all search terms (e.g. $_GET)
200 210
    * @return array List of matching group IDs with torrent ID as key for ungrouped results
201 211
    */
202
-  public function query($Terms = array()) {
212
+  public function query($Terms = []) {
203 213
     $this->process_search_terms($Terms);
204 214
     $this->build_query();
205 215
     $this->run_query();
@@ -260,7 +270,7 @@ class TorrentSearch {
260 270
       if (isset(self::$FormsToFields[$Field])) {
261 271
         $Field = self::$FormsToFields[$Field];
262 272
       }
263
-      $QueryParts = array('include' => array(), 'exclude' => array());
273
+      $QueryParts = ['include' => [], 'exclude' => []];
264 274
       if (!empty($Words['include'])) {
265 275
         foreach ($Words['include'] as $Word) {
266 276
           $QueryParts['include'][] = Sphinxql::sph_escape_string($Word);
@@ -316,7 +326,7 @@ class TorrentSearch {
316 326
       }
317 327
       $this->RawTerms[$Key] = $Term;
318 328
     }
319
-    $this->post_process_fields();
329
+    $this->post_process();
320 330
   }
321 331
 
322 332
   /**
@@ -326,65 +336,55 @@ class TorrentSearch {
326 336
    * @param mixed $Value The filter's condition for a match
327 337
    */
328 338
   private function process_attribute($Attribute, $Value) {
329
-    if ($Value === '') {
330
-      return;
331
-    }
332
-    switch ($Attribute) {
333
-      case 'year':
334
-        if (!$this->search_year($Value)) {
335
-          return;
336
-        }
337
-        break;
338
-
339
-      case 'freetorrent':
340
-        if ($Value == 3) {
341
-          $this->SphQL->where('freetorrent', 0, true);
342
-          $this->UsedTorrentAttrs['freetorrent'] = 3;
343
-        } elseif ($Value >= 0 && $Value < 3) {
344
-          $this->SphQL->where('freetorrent', $Value);
345
-          $this->UsedTorrentAttrs[$Attribute] = $Value;
339
+    if ($Value === '') { return; }
340
+
341
+    if ($Attribute === 'year') {
342
+      $this->search_year($Value);
343
+    } elseif ($Attribute === 'size_unit') {
344
+      // for the record, size_unit must appear in the GET parameters after size_min and size_max for this to work. Sorry.
345
+      if (is_numeric($this->RawTerms['size_min']) || is_numeric($this->RawTerms['size_max']))  {
346
+        $this->SphQL->where_between('size', [intval(($this->RawTerms['size_min'] ?? 0)*(1024**$Value)), intval(min(PHP_INT_MAX, ($this->RawTerms['size_max'] ?? INF)*(1024**$Value)))]);
347
+      }
348
+    } elseif ($Attribute === 'freetorrent') {
349
+      if ($Value == 3) {
350
+        $this->SphQL->where('freetorrent', 0, true);
351
+        $this->UsedTorrentAttrs['freetorrent'] = 3;
352
+      } elseif ($Value >= 0 && $Value < 3) {
353
+        $this->SphQL->where('freetorrent', $Value);
354
+        $this->UsedTorrentAttrs[$Attribute] = $Value;
355
+      }
356
+    } elseif ($Attribute === 'filter_cat') {
357
+      if (!is_array($Value)) {
358
+        $Value = array_fill_keys(explode('|', $Value), 1);
359
+      }
360
+      $CategoryFilter = [];
361
+      foreach (array_keys($Value) as $Category) {
362
+        if (is_number($Category)) {
363
+          $CategoryFilter[] = $Category;
346 364
         } else {
347
-          return;
348
-        }
349
-        break;
350
-
351
-      case 'filter_cat':
352
-        if (!is_array($Value)) {
353
-          $Value = array_fill_keys(explode('|', $Value), 1);
354
-        }
355
-        $CategoryFilter = array();
356
-        foreach (array_keys($Value) as $Category) {
357
-          if (is_number($Category)) {
358
-            $CategoryFilter[] = $Category;
359
-          } else {
360
-            global $Categories;
361
-            $ValidValues = array_map('strtolower', $Categories);
362
-            if (($CategoryID = array_search(strtolower($Category), $ValidValues)) !== false) {
363
-              $CategoryFilter[] = $CategoryID + 1;
364
-            }
365
+          global $Categories;
366
+          $ValidValues = array_map('strtolower', $Categories);
367
+          if (($CategoryID = array_search(strtolower($Category), $ValidValues)) !== false) {
368
+            $CategoryFilter[] = $CategoryID + 1;
365 369
           }
366 370
         }
367
-        if (empty($CategoryFilter)) {
368
-          $CategoryFilter = 0;
369
-        }
370
-        $this->SphQL->where('categoryid', $CategoryFilter);
371
-        break;
372
-
373
-      default:
374
-        if (!is_number($Value) && self::$Attributes[$Attribute] !== false) {
375
-          // Check if the submitted value can be converted to a valid one
376
-          $ValidValuesVarname = self::$Attributes[$Attribute];
377
-          global $$ValidValuesVarname;
378
-          $ValidValues = array_map('strtolower', $$ValidValuesVarname);
379
-          if (($Value = array_search(strtolower($Value), $ValidValues)) === false) {
380
-            // Force the query to return 0 results if value is still invalid
381
-            $Value = max(array_keys($ValidValues)) + 1;
382
-          }
371
+      }
372
+      $this->SphQL->where('categoryid', ($CategoryFilter ?? 0));
373
+    } else {
374
+      if (!is_number($Value) && self::$Attributes[$Attribute] !== false) {
375
+        // Check if the submitted value can be converted to a valid one
376
+        $ValidValuesVarname = self::$Attributes[$Attribute];
377
+        global $$ValidValuesVarname;
378
+        $ValidValues = array_map('strtolower', $$ValidValuesVarname);
379
+        if (($Value = array_search(strtolower($Value), $ValidValues)) === false) {
380
+          // Force the query to return 0 results if value is still invalid
381
+          $Value = max(array_keys($ValidValues)) + 1;
383 382
         }
384
-        $this->SphQL->where($Attribute, $Value);
385
-        $this->UsedTorrentAttrs[$Attribute] = $Value;
386
-        break;
383
+      }
384
+      $this->SphQL->where($Attribute, $Value);
385
+      $this->UsedTorrentAttrs[$Attribute] = $Value;
387 386
     }
387
+
388 388
     $this->Filtered = true;
389 389
   }
390 390
 
@@ -413,7 +413,7 @@ class TorrentSearch {
413 413
   /**
414 414
    * Some fields may require post-processing
415 415
    */
416
-  private function post_process_fields() {
416
+  private function post_process() {
417 417
     if (isset($this->Terms['taglist'])) {
418 418
       // Replace bad tags with tag aliases
419 419
       $this->Terms['taglist'] = Tags::remove_aliases($this->Terms['taglist']);
@@ -424,7 +424,7 @@ class TorrentSearch {
424 424
       if (isset($this->Terms['taglist']['include'])) {
425 425
         $AllTags = $this->Terms['taglist']['include'];
426 426
       } else {
427
-        $AllTags = array();
427
+        $AllTags = [];
428 428
       }
429 429
       if (isset($this->Terms['taglist']['exclude'])) {
430 430
         $AllTags = array_merge($AllTags, $this->Terms['taglist']['exclude']);
@@ -501,7 +501,7 @@ class TorrentSearch {
501 501
         $this->SphQL->where_gt('year', $Years[0], true);
502 502
       } elseif (is_number($Years[0]) && is_number($Years[1])) {
503 503
         // Range: 2005 - 2009
504
-        $this->SphQL->where_between('year', array(min($Years), max($Years)));
504
+        $this->SphQL->where_between('year', [min($Years), max($Years)]);
505 505
       } else {
506 506
         // Invalid input
507 507
         return false;
@@ -609,7 +609,7 @@ class TorrentSearch {
609 609
    * were used to get primary results and they are grouped
610 610
    */
611 611
   private function filter_torrents_sph() {
612
-    $AllTorrents = array();
612
+    $AllTorrents = [];
613 613
     foreach ($this->Groups as $GroupID => $Group) {
614 614
       if (!empty($Group['Torrents'])) {
615 615
         $AllTorrents += array_fill_keys(array_keys($Group['Torrents']), $GroupID);

+ 21
- 6
sections/torrents/browse.php View File

@@ -13,7 +13,7 @@ function header_link($SortKey, $DefaultWay = 'desc') {
13 13
   } else {
14 14
     $NewWay = $DefaultWay;
15 15
   }
16
-  return "torrents.php?order_way=$NewWay&amp;order_by=$SortKey&amp;".Format::get_url(array('order_way', 'order_by'));
16
+  return "torrents.php?order_way=$NewWay&amp;order_by=$SortKey&amp;".Format::get_url(['order_way', 'order_by']);
17 17
 }
18 18
 
19 19
 if (!empty($_GET['searchstr']) || !empty($_GET['groupname'])) {
@@ -41,7 +41,7 @@ if (!empty($_GET['searchstr']) || !empty($_GET['groupname'])) {
41 41
 
42 42
 // Setting default search options
43 43
 if (!empty($_GET['setdefault'])) {
44
-  $UnsetList = array('page', 'setdefault');
44
+  $UnsetList = ['page', 'setdefault'];
45 45
   $UnsetRegexp = '/(&|^)('.implode('|', $UnsetList).')=.*?(&|$)/i';
46 46
 
47 47
   $DB->query("
@@ -52,7 +52,7 @@ if (!empty($_GET['setdefault'])) {
52 52
   if (!empty($SiteOptions)) {
53 53
     $SiteOptions = unserialize($SiteOptions);
54 54
   } else {
55
-    $SiteOptions = array();
55
+    $SiteOptions = [];
56 56
   }
57 57
   $SiteOptions['DefaultSearch'] = preg_replace($UnsetRegexp, '', $_SERVER['QUERY_STRING']);
58 58
   $DB->query("
@@ -60,7 +60,7 @@ if (!empty($_GET['setdefault'])) {
60 60
     SET SiteOptions = '".db_string(serialize($SiteOptions))."'
61 61
     WHERE UserID = '".db_string($LoggedUser['ID'])."'");
62 62
   $Cache->begin_transaction("user_info_heavy_$UserID");
63
-  $Cache->update_row(false, array('DefaultSearch' => $SiteOptions['DefaultSearch']));
63
+  $Cache->update_row(false, ['DefaultSearch' => $SiteOptions['DefaultSearch']]);
64 64
   $Cache->commit_transaction(0);
65 65
 
66 66
 // Clearing default search options
@@ -77,7 +77,7 @@ if (!empty($_GET['setdefault'])) {
77 77
     SET SiteOptions = '".db_string(serialize($SiteOptions))."'
78 78
     WHERE UserID = '".db_string($LoggedUser['ID'])."'");
79 79
   $Cache->begin_transaction("user_info_heavy_$UserID");
80
-  $Cache->update_row(false, array('DefaultSearch' => ''));
80
+  $Cache->update_row(false, ['DefaultSearch' => '']);
81 81
   $Cache->commit_transaction(0);
82 82
 
83 83
 // Use default search options
@@ -268,6 +268,21 @@ View::show_header('Browse Torrents', 'browse');
268 268
             <input type="search" spellcheck="false" size="65" name="subber" class="inputtext smaller fti_advanced" placeholder="Translation Group" value="<?Format::form('subber')?>" />
269 269
           </td>
270 270
         </tr>
271
+        <tr id="size" class="ftr_advanced<?=$HideAdvanced?>">
272
+          <td class="label">Size:</td>
273
+          <td class="ft_size">
274
+            <input type="size_min" spellcheck="false" size="6" name="size_min" class="inputtext smaller fti_advanced" placeholder="Min" value="<?Format::form('size_min')?>" /> -
275
+            <input type="size_max" spellcheck="false" size="6" name="size_max" class="inputtext smaller fti_advanced" placeholder="Max" value="<?Format::form('size_max')?>" />
276
+            <select name="size_unit" class="ft_size fti_advanced">
277
+              <option value="">Unit</option>
278
+              <option value="0"<?Format::selected('size_unit', 0)?>>B</option>
279
+              <option value="1"<?Format::selected('size_unit', 1)?>>KiB</option>
280
+              <option value="2"<?Format::selected('size_unit', 2)?>>MiB</option>
281
+              <option value="3"<?Format::selected('size_unit', 3)?>>GiB</option>
282
+              <option value="4"<?Format::selected('size_unit', 4)?>>TiB</option>
283
+            </select>
284
+          </td>
285
+        </tr>
271 286
         <tr id="misc" class="ftr_advanced<?=$HideAdvanced?>">
272 287
           <td class="label">Misc:</td>
273 288
           <td class="nobr ft_misc">
@@ -495,7 +510,7 @@ View::show_header('Browse Torrents', 'browse');
495 510
     }
496 511
   } else {
497 512
     $TorrentID = $Key;
498
-    $Torrents = array($TorrentID => $GroupInfo['Torrents'][$TorrentID]);
513
+    $Torrents = [$TorrentID => $GroupInfo['Torrents'][$TorrentID]];
499 514
   }
500 515
 
501 516
   $TorrentTags = new Tags($GroupInfo['TagList']);

Loading…
Cancel
Save