Browse Source

Rewrite screenshot adding in new torrents to use nicer SQL

spaghetti 7 years ago
parent
commit
c9b6bcf0be
2 changed files with 24 additions and 20 deletions
  1. 9
    10
      classes/mysql.class.php
  2. 15
    10
      sections/upload/upload_handle.php

+ 9
- 10
classes/mysql.class.php View File

@@ -149,6 +149,7 @@ class DB_MYSQL {
149 149
   public $LinkID = false;
150 150
   protected $QueryID = false;
151 151
   protected $StatementID = false;
152
+  protected $PreparedQuery = false;
152 153
   protected $Record = [];
153 154
   protected $Row;
154 155
   protected $Errno = 0;
@@ -203,8 +204,7 @@ class DB_MYSQL {
203 204
     mysqli_set_charset($this->LinkID, "utf8");
204 205
   }
205 206
 
206
-  function query_p($Query, &...$BindVars) {
207
-    $QueryStartTime = microtime(true);
207
+  function prepare_query($Query, &...$BindVars) {
208 208
     $this->connect();
209 209
 
210 210
     $this->StatementID = mysqli_prepare($this->LinkID, $Query);
@@ -216,13 +216,17 @@ class DB_MYSQL {
216 216
       }
217 217
       mysqli_stmt_bind_param($this->StatementID, $Types, ...$BindVars);
218 218
     }
219
+    $this->PreparedQuery = $Query;
220
+    return $this->StatementID;
221
+  }
222
+
223
+  function exec_prepared_query() {
224
+    $QueryStartTime = microtime(true);
219 225
     mysqli_stmt_execute($this->StatementID);
220 226
     $this->QueryID = mysqli_stmt_get_result($this->StatementID);
221 227
     $QueryRunTime = (microtime(true) - $QueryStartTime) * 1000;
222
-    $this->Queries[] = [$Query, $QueryRunTime, null];
228
+    $this->Queries[] = [$this->PreppedQuery, $QueryRunTime, null];
223 229
     $this->Time += $QueryRunTime;
224
-
225
-    return $this->StatementID;
226 230
   }
227 231
 
228 232
   function query($Query, &...$BindVars) {
@@ -291,11 +295,6 @@ class DB_MYSQL {
291 295
     return $this->QueryID;
292 296
   }
293 297
 
294
-  function reexec_query() {
295
-    mysqli_stmt_execute($this->StatementID);
296
-    $this->QueryID = mysqli_stmt_get_result($this->StatementID);
297
-  }
298
-
299 298
   function query_unb($Query) {
300 299
     $this->connect();
301 300
     mysqli_real_query($this->LinkID, $Query);

+ 15
- 10
sections/upload/upload_handle.php View File

@@ -395,18 +395,23 @@ if (!isset($GroupID) || !$GroupID) {
395 395
   $Cache->increment('stats_group_count');
396 396
 
397 397
   // Add screenshots
398
-  $Screenshots = array_slice(array_filter(array_map("db_string", array_map("trim", array_unique(explode("\n", $T['Screenshots'])))), function ($s) { return preg_match('/^'.IMAGE_REGEX.'$/i', $s); }), 0, 10);
399
-
400
-  $values = [];
401
-  foreach ($Screenshots as $s) {
402
-    $values[] = "(" . $GroupID . ", " . $LoggedUser['ID'] . ", NOW(), '" . $s . "')";
403
-  }
404
-
405
-  if (!empty($values)) {
406
-    $DB->query("
398
+  $Screenshots = explode("\n", $T['Screenshots']);
399
+  $Screenshots = array_map("trim", $Screenshots);
400
+  $Screenshots = array_filter($Screenshots, function($s) {
401
+    return preg_match('/^'.IMAGE_REGEX.'$/i', $s);
402
+  });
403
+  $Screenshots = array_unique($Screenshots);
404
+  $Screenshots = array_slice($Screenshots, 0, 10);
405
+
406
+  if (!empty($Screenshots)) {
407
+    $Screenshot = '';
408
+    $DB->prepare_query("
407 409
       INSERT INTO torrents_screenshots
408 410
         (GroupID, UserID, Time, Image)
409
-      VALUES " . implode(", ", $values));
411
+      VALUES (?, ?, NOW(), ?)", $GroupID, $LoggedUser['ID'], $Screenshot);
412
+    foreach ($Screenshots as $Screenshot) {
413
+      $DB->exec_prepared_query();
414
+    }
410 415
   }
411 416
 
412 417
 } else {

Loading…
Cancel
Save