Browse Source

Upload files to 'classes'

Stortebeker 6 years ago
parent
commit
0fb8e56515

+ 110
- 93
classes/sitehistoryview.class.php View File

1
-<?
1
+<?php
2
 
2
 
3
-class SiteHistoryView {
4
-
5
-  public static function render_linkbox() {
6
-    if (check_perms('users_mod')
7
-      ) {
8
-?>
3
+class SiteHistoryView
4
+{
5
+    public static function render_linkbox()
6
+    {
7
+        if (check_perms('users_mod')
8
+        ) {
9
+            ?>
9
   <div class="linkbox">
10
   <div class="linkbox">
10
     <a href="sitehistory.php?action=edit" class="brackets">Create new event</a>
11
     <a href="sitehistory.php?action=edit" class="brackets">Create new event</a>
11
   </div>
12
   </div>
12
-<?
13
+            <?php
14
+        }
13
     }
15
     }
14
-  }
15
 
16
 
16
-  public static function render_events($Events) {
17
-    $Categories = SiteHistory::get_categories();
18
-    $SubCategories = SiteHistory::get_sub_categories();
19
-    $CanEdit = check_perms('users_mod') ;
20
-    foreach ($Events as $Event) {
21
-?>
17
+    public static function render_events($Events)
18
+    {
19
+        $Categories = SiteHistory::get_categories();
20
+        $SubCategories = SiteHistory::get_sub_categories();
21
+        $CanEdit = check_perms('users_mod');
22
+        foreach ($Events as $Event) {
23
+            ?>
22
       <div class="box">
24
       <div class="box">
23
         <div class="head colhead_dark">
25
         <div class="head colhead_dark">
24
           <div class="title">
26
           <div class="title">
25
-<?      if ($CanEdit) { ?>
27
+            <?php      if ($CanEdit) { ?>
26
             <a class="brackets" href="sitehistory.php?action=edit&amp;id=<?=$Event['ID']?>">Edit</a>
28
             <a class="brackets" href="sitehistory.php?action=edit&amp;id=<?=$Event['ID']?>">Edit</a>
27
-<?      } ?>
29
+            <?php      } ?>
28
 
30
 
29
-            <?=date('F d, Y', strtotime($Event['Date']));?>
31
+            <?=date('F d, Y', strtotime($Event['Date'])); ?>
30
               -
32
               -
31
             <a href="sitehistory.php?action=search&amp;category=<?=$Event['Category']?>" class="brackets"><?=$Categories[$Event['Category']]?></a>
33
             <a href="sitehistory.php?action=search&amp;category=<?=$Event['Category']?>" class="brackets"><?=$Categories[$Event['Category']]?></a>
32
             <a href="sitehistory.php?action=search&amp;subcategory=<?=$Event['SubCategory']?>" class="brackets"><?=$SubCategories[$Event['SubCategory']]?></a>
34
             <a href="sitehistory.php?action=search&amp;subcategory=<?=$Event['SubCategory']?>" class="brackets"><?=$SubCategories[$Event['SubCategory']]?></a>
33
 
35
 
34
-<?      if (!empty($Event['Url'])) { ?>
36
+            <?php      if (!empty($Event['Url'])) { ?>
35
             <a href="<?=$Event['Url']?>"><?=$Event['Title']?></a>
37
             <a href="<?=$Event['Url']?>"><?=$Event['Title']?></a>
36
-<?      } else { ?>
37
-            <?=$Event['Title']?>
38
-<?      } ?>
38
+            <?php      } else { ?>
39
+                <?=$Event['Title']?>
40
+            <?php      } ?>
39
           </div>
41
           </div>
40
           <div class="tags">
42
           <div class="tags">
41
-            <? self::render_tags($Event['Tags'])?>
43
+            <?php self::render_tags($Event['Tags'])?>
42
           </div>
44
           </div>
43
           </div>
45
           </div>
44
-<?      if (!empty($Event['Body'])) { ?>
46
+            <?php      if (!empty($Event['Body'])) { ?>
45
           <div class="body">
47
           <div class="body">
46
-            <?=Text::full_format($Event['Body'])?>
48
+                <?=Text::full_format($Event['Body'])?>
47
           </div>
49
           </div>
48
-<?      } ?>
50
+            <?php      } ?>
49
       </div>
51
       </div>
50
-<?
52
+            <?php
53
+        }
51
     }
54
     }
52
-  }
53
 
55
 
54
-  private static function render_tags($Tags) {
55
-    $Tags = explode(',', $Tags);
56
-    natcasesort($Tags);
57
-    $FormattedTags = '';
58
-    foreach ($Tags as $Tag) {
59
-      $FormattedTags .= "<a href=\"sitehistory.php?action=search&amp;tags=$Tag\">$Tag" . "</a>, ";
56
+    private static function render_tags($Tags)
57
+    {
58
+        $Tags = explode(',', $Tags);
59
+        natcasesort($Tags);
60
+        $FormattedTags = '';
61
+        foreach ($Tags as $Tag) {
62
+            $FormattedTags .= "<a href=\"sitehistory.php?action=search&amp;tags=$Tag\">$Tag" . "</a>, ";
63
+        }
64
+        echo rtrim($FormattedTags, ', ');
60
     }
65
     }
61
-    echo rtrim($FormattedTags, ', ');
62
-  }
63
 
66
 
64
-  public static function render_months($Months) { ?>
67
+    public static function render_months($Months)
68
+    {
69
+        ?>
65
     <div class="box">
70
     <div class="box">
66
       <div class="head">Calendar</div>
71
       <div class="head">Calendar</div>
67
       <div class="pad">
72
       <div class="pad">
68
-<?
69
-    $Year = "";
70
-    foreach ($Months as $Month) {
71
-      if ($Month['Year'] != $Year) {
72
-        $Year = $Month['Year'];
73
-        echo "<h2>$Year</h2>";
74
-      }
75
-?>
73
+        <?php
74
+        $Year = "";
75
+        foreach ($Months as $Month) {
76
+            if ($Month['Year'] != $Year) {
77
+                $Year = $Month['Year'];
78
+                echo "<h2>$Year</h2>";
79
+            } ?>
76
         <a style="margin-left: 5px;" href="sitehistory.php?month=<?=$Month['Month']?>&amp;year=<?=$Month['Year']?>"><?=$Month['MonthName']?></a>
80
         <a style="margin-left: 5px;" href="sitehistory.php?month=<?=$Month['Month']?>&amp;year=<?=$Month['Year']?>"><?=$Month['MonthName']?></a>
77
-<?    } ?>
81
+            <?php
82
+        } ?>
78
       </div>
83
       </div>
79
     </div>
84
     </div>
80
-<?
81
-  }
85
+        <?php
86
+    }
82
 
87
 
83
-  public static function render_search() { ?>
88
+    public static function render_search()
89
+    {
90
+        ?>
84
       <div class="box">
91
       <div class="box">
85
         <div class="head">Search</div>
92
         <div class="head">Search</div>
86
         <div class="pad">
93
         <div class="pad">
92
             <br /><br/>
99
             <br /><br/>
93
             <select name="category" id="category">
100
             <select name="category" id="category">
94
               <option value="0">Choose a category</option>
101
               <option value="0">Choose a category</option>
95
-<?
96
-      $Categories = SiteHistory::get_categories();
97
-      foreach ($Categories as $Key => $Value) {
98
-?>
102
+        <?php
103
+        $Categories = SiteHistory::get_categories();
104
+        foreach ($Categories as $Key => $Value) {
105
+            ?>
99
               <option<?=$Key == $Event['Category'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
106
               <option<?=$Key == $Event['Category'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
100
-<?      } ?>
107
+            <?php
108
+        } ?>
101
             </select>
109
             </select>
102
             <br /><br/>
110
             <br /><br/>
103
             <select name="subcategory">
111
             <select name="subcategory">
104
               <option value="0">Choose a subcategory</option>
112
               <option value="0">Choose a subcategory</option>
105
-<?
106
-      $SubCategories = SiteHistory::get_sub_categories();
107
-      foreach ($SubCategories as $Key => $Value) {
108
-?>
113
+        <?php
114
+        $SubCategories = SiteHistory::get_sub_categories();
115
+        foreach ($SubCategories as $Key => $Value) {
116
+            ?>
109
               <option<?=$Key == $Event['SubCategory'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
117
               <option<?=$Key == $Event['SubCategory'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
110
-<?      } ?>
118
+            <?php
119
+        } ?>
111
             </select>
120
             </select>
112
             <br /><br/>
121
             <br /><br/>
113
             <input value="Search" type="submit" />
122
             <input value="Search" type="submit" />
114
           </form>
123
           </form>
115
         </div>
124
         </div>
116
       </div>
125
       </div>
117
-<?  }
126
+        <?php
127
+    }
118
 
128
 
119
-  public static function render_edit_form($Event) { ?>
129
+    public static function render_edit_form($Event)
130
+    {
131
+        ?>
120
     <form id="event_form" method="post" action="">
132
     <form id="event_form" method="post" action="">
121
-<?    if ($Event) { ?>
133
+        <?php    if ($Event) { ?>
122
       <input type="hidden" name="action" value="take_edit" />
134
       <input type="hidden" name="action" value="take_edit" />
123
       <input type="hidden" name="id" value="<?=$Event['ID']?>" />
135
       <input type="hidden" name="id" value="<?=$Event['ID']?>" />
124
-<?    } else { ?>
136
+        <?php    } else { ?>
125
       <input type="hidden" name="action" value="take_create" />
137
       <input type="hidden" name="action" value="take_create" />
126
-<?    } ?>
138
+        <?php    } ?>
127
       <input type="hidden" name="auth" value="<?=G::$LoggedUser['AuthKey']?>" />
139
       <input type="hidden" name="auth" value="<?=G::$LoggedUser['AuthKey']?>" />
128
       <table cellpadding="6" cellspacing="1" border="0" class="layout border" width="100%">
140
       <table cellpadding="6" cellspacing="1" border="0" class="layout border" width="100%">
129
         <tr>
141
         <tr>
149
           <td>
161
           <td>
150
             <select id="category" name="category" class="required">
162
             <select id="category" name="category" class="required">
151
               <option value="0">Choose a category</option>
163
               <option value="0">Choose a category</option>
152
-<?
153
-    $Categories = SiteHistory::get_categories();
154
-    foreach ($Categories as $Key => $Value) {
155
-?>
164
+        <?php
165
+        $Categories = SiteHistory::get_categories();
166
+        foreach ($Categories as $Key => $Value) {
167
+            ?>
156
               <option<?=$Key == $Event['Category'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
168
               <option<?=$Key == $Event['Category'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
157
-<?    } ?>
169
+            <?php
170
+        } ?>
158
             </select>
171
             </select>
159
           </td>
172
           </td>
160
         </tr>
173
         </tr>
163
           <td>
176
           <td>
164
             <select id="category" name="sub_category" class="required">
177
             <select id="category" name="sub_category" class="required">
165
               <option value="0">Choose a subcategory</option>
178
               <option value="0">Choose a subcategory</option>
166
-<?    $SubCategories = SiteHistory::get_sub_categories();
167
-    foreach ($SubCategories as $Key => $Value) { ?>
179
+        <?php    $SubCategories = SiteHistory::get_sub_categories();
180
+        foreach ($SubCategories as $Key => $Value) { ?>
168
               <option<?=$Key == $Event['SubCategory'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
181
               <option<?=$Key == $Event['SubCategory'] ? ' selected="selected"' : ''?> value="<?=$Key?>"><?=$Value?></option>
169
-<?    } ?>
182
+        <?php    } ?>
170
             </select>
183
             </select>
171
           </td>
184
           </td>
172
         </tr>
185
         </tr>
176
             <input type="text" id="tags" name="tags" placeholder="Comma-separated tags; use periods/dots for spaces" size="50" value="<?=$Event['Tags']?>" />
189
             <input type="text" id="tags" name="tags" placeholder="Comma-separated tags; use periods/dots for spaces" size="50" value="<?=$Event['Tags']?>" />
177
             <select id="tag_list">
190
             <select id="tag_list">
178
               <option>Choose tags</option>
191
               <option>Choose tags</option>
179
-<?
180
-    $Tags = SiteHistory::get_tags();
181
-    foreach ($Tags as $Tag) {
182
-?>
192
+        <?php
193
+        $Tags = SiteHistory::get_tags();
194
+        foreach ($Tags as $Tag) {
195
+            ?>
183
               <option><?=$Tag?></option>
196
               <option><?=$Tag?></option>
184
-<?    } ?>
197
+            <?php
198
+        } ?>
185
             </select>
199
             </select>
186
           </td>
200
           </td>
187
         </tr>
201
         </tr>
193
         </tr>
207
         </tr>
194
       </table>
208
       </table>
195
       <input type="submit" name="submit" value="Submit" />
209
       <input type="submit" name="submit" value="Submit" />
196
-<?    if ($Event) { ?>
210
+        <?php    if ($Event) { ?>
197
       <input type="submit" name="delete" value="Delete" />
211
       <input type="submit" name="delete" value="Delete" />
198
-<?    } ?>
212
+        <?php    } ?>
199
     </form>
213
     </form>
200
-<?
201
-  }
214
+        <?php
215
+    }
202
 
216
 
203
-  public static function render_recent_sidebar($Events) { ?>
217
+    public static function render_recent_sidebar($Events)
218
+    {
219
+        ?>
204
     <div class="box">
220
     <div class="box">
205
       <div class="head colhead_dark">
221
       <div class="head colhead_dark">
206
         <strong><a href="sitehistory.php">Latest site history</a></strong>
222
         <strong><a href="sitehistory.php">Latest site history</a></strong>
207
       </div>
223
       </div>
208
       <ul class="stats nobullet">
224
       <ul class="stats nobullet">
209
-<?
210
-    $Categories = SiteHistory::get_categories();
211
-    foreach ($Events as $Event) {
212
-?>
225
+        <?php
226
+        $Categories = SiteHistory::get_categories();
227
+        foreach ($Events as $Event) {
228
+            ?>
213
         <li>
229
         <li>
214
           <a href="sitehistory.php?action=search&amp;category=<?=$Event['Category']?>" class="brackets"><?=$Categories[$Event['Category']]?></a>
230
           <a href="sitehistory.php?action=search&amp;category=<?=$Event['Category']?>" class="brackets"><?=$Categories[$Event['Category']]?></a>
215
-<?      if (!empty($Event['Url'])) { ?>
231
+            <?php      if (!empty($Event['Url'])) { ?>
216
           <a href="<?=$Event['Url']?>"><?=Format::cut_string($Event['Title'], 20)?></a>
232
           <a href="<?=$Event['Url']?>"><?=Format::cut_string($Event['Title'], 20)?></a>
217
-<?      } else { ?>
218
-          <?=Format::cut_string($Event['Title'], 20)?>
219
-<?      } ?>
233
+            <?php      } else { ?>
234
+                <?=Format::cut_string($Event['Title'], 20)?>
235
+            <?php      } ?>
220
         </li>
236
         </li>
221
-<?    } ?>
237
+            <?php
238
+        } ?>
222
       </ul>
239
       </ul>
223
     </div>
240
     </div>
224
-<?
225
-  }
241
+        <?php
242
+    }
226
 }
243
 }

+ 9
- 8
classes/slaves.class.php View File

1
-<?
2
-class Slaves {
3
-  public static function get_level($SlaveID) {
4
-    G::$DB->query("
1
+<?php
2
+class Slaves
3
+{
4
+    public static function get_level($SlaveID)
5
+    {
6
+        G::$DB->query("
5
       SELECT u.Uploaded, u.Downloaded, u.BonusPoints, COUNT(t.UserID)
7
       SELECT u.Uploaded, u.Downloaded, u.BonusPoints, COUNT(t.UserID)
6
       FROM users_main AS u
8
       FROM users_main AS u
7
       LEFT JOIN torrents AS t ON u.ID=t.UserID
9
       LEFT JOIN torrents AS t ON u.ID=t.UserID
8
       WHERE u.ID = $SlaveID");
10
       WHERE u.ID = $SlaveID");
9
-    list($Upload, $Download, $Points, $Uploads) = G::$DB->next_record();
10
-    return intval(((($Uploads**0.35)*1.5)+1) * max(($Upload+($Points*1000000)-$Download)/(1024**3),1));
11
-  }
11
+        list($Upload, $Download, $Points, $Uploads) = G::$DB->next_record();
12
+        return intval(((($Uploads**0.35)*1.5)+1) * max(($Upload+($Points*1000000)-$Download)/(1024**3), 1));
13
+    }
12
 };
14
 };
13
-?>

+ 138
- 131
classes/sphinxql.class.php View File

1
-<?
1
+<?php
2
 if (!extension_loaded('mysqli')) {
2
 if (!extension_loaded('mysqli')) {
3
-  error('Mysqli Extension not loaded.');
3
+    error('Mysqli Extension not loaded.');
4
 }
4
 }
5
 
5
 
6
-class Sphinxql extends mysqli {
7
-  private static $Connections = [];
8
-  private $Server;
9
-  private $Port;
10
-  private $Socket;
11
-  private $Ident;
12
-  private $Connected = false;
6
+class Sphinxql extends mysqli
7
+{
8
+    private static $Connections = [];
9
+    private $Server;
10
+    private $Port;
11
+    private $Socket;
12
+    private $Ident;
13
+    private $Connected = false;
13
 
14
 
14
-  public static $Queries = [];
15
-  public static $Time = 0.0;
15
+    public static $Queries = [];
16
+    public static $Time = 0.0;
16
 
17
 
17
 
18
 
18
-  /**
19
-   * Initialize Sphinxql object
20
-   *
21
-   * @param string $Server server address or hostname
22
-   * @param int $Port listening port
23
-   * @param string $Socket Unix socket address, overrides $Server:$Port
24
-   */
25
-  public function __construct($Server, $Port, $Socket) {
26
-    $this->Server = $Server;
27
-    $this->Port = $Port;
28
-    $this->Socket = $Socket;
29
-    $this->Ident = self::get_ident($Server, $Port, $Socket);
30
-  }
31
-
32
-  /**
33
-   * Create server ident based on connection information
34
-   *
35
-   * @param string $Server server address or hostname
36
-   * @param int $Port listening port
37
-   * @param string $Socket Unix socket address, overrides $Server:$Port
38
-   * @return identification string
39
-   */
40
-  private static function get_ident($Server, $Port, $Socket) {
41
-    if ($Socket) {
42
-      return $Socket;
43
-    } else {
44
-      return "$Server:$Port";
19
+    /**
20
+     * Initialize Sphinxql object
21
+     *
22
+     * @param string $Server server address or hostname
23
+     * @param int $Port listening port
24
+     * @param string $Socket Unix socket address, overrides $Server:$Port
25
+     */
26
+    public function __construct($Server, $Port, $Socket)
27
+    {
28
+        $this->Server = $Server;
29
+        $this->Port = $Port;
30
+        $this->Socket = $Socket;
31
+        $this->Ident = self::get_ident($Server, $Port, $Socket);
45
     }
32
     }
46
-  }
47
 
33
 
48
-  /**
49
-   * Create Sphinxql object or return existing one
50
-   *
51
-   * @param string $Server server address or hostname
52
-   * @param int $Port listening port
53
-   * @param string $Socket Unix socket address, overrides $Server:$Port
54
-   * @return Sphinxql object
55
-   */
56
-  public static function init_connection($Server, $Port, $Socket) {
57
-    $Ident = self::get_ident($Server, $Port, $Socket);
58
-    if (!isset(self::$Connections[$Ident])) {
59
-      self::$Connections[$Ident] = new Sphinxql($Server, $Port, $Socket);
34
+    /**
35
+     * Create server ident based on connection information
36
+     *
37
+     * @param string $Server server address or hostname
38
+     * @param int $Port listening port
39
+     * @param string $Socket Unix socket address, overrides $Server:$Port
40
+     * @return identification string
41
+     */
42
+    private static function get_ident($Server, $Port, $Socket)
43
+    {
44
+        if ($Socket) {
45
+            return $Socket;
46
+        } else {
47
+            return "$Server:$Port";
48
+        }
60
     }
49
     }
61
-    return self::$Connections[$Ident];
62
-  }
63
 
50
 
64
-  /**
65
-   * Connect the Sphinxql object to the Sphinx server
66
-   */
67
-  public function sph_connect() {
68
-    if ($this->Connected || $this->connect_errno) {
69
-      return;
70
-    }
71
-    global $Debug;
72
-    $Debug->set_flag("Connecting to Sphinx server $this->Ident");
73
-    for ($Attempt = 0; $Attempt < 3; $Attempt++) {
74
-      parent::__construct($this->Server, '', '', '', $this->Port, $this->Socket);
75
-      if (!$this->connect_errno) {
76
-        $this->Connected = true;
77
-        break;
78
-      }
79
-      sleep(1);
51
+    /**
52
+     * Create Sphinxql object or return existing one
53
+     *
54
+     * @param string $Server server address or hostname
55
+     * @param int $Port listening port
56
+     * @param string $Socket Unix socket address, overrides $Server:$Port
57
+     * @return Sphinxql object
58
+     */
59
+    public static function init_connection($Server, $Port, $Socket)
60
+    {
61
+        $Ident = self::get_ident($Server, $Port, $Socket);
62
+        if (!isset(self::$Connections[$Ident])) {
63
+            self::$Connections[$Ident] = new Sphinxql($Server, $Port, $Socket);
64
+        }
65
+        return self::$Connections[$Ident];
80
     }
66
     }
81
-    if ($this->connect_errno) {
82
-      $Errno = $this->connect_errno;
83
-      $Error = $this->connect_error;
84
-      $this->error("Connection failed. (".strval($Errno).": ".strval($Error).")");
85
-      $Debug->set_flag("Could not connect to Sphinx server $this->Ident. (".strval($Errno).": ".strval($Error).")");
86
-    } else {
87
-      $Debug->set_flag("Connected to Sphinx server $this->Ident");
67
+
68
+    /**
69
+     * Connect the Sphinxql object to the Sphinx server
70
+     */
71
+    public function sph_connect()
72
+    {
73
+        if ($this->Connected || $this->connect_errno) {
74
+            return;
75
+        }
76
+        global $Debug;
77
+        $Debug->set_flag("Connecting to Sphinx server $this->Ident");
78
+        for ($Attempt = 0; $Attempt < 3; $Attempt++) {
79
+            parent::__construct($this->Server, '', '', '', $this->Port, $this->Socket);
80
+            if (!$this->connect_errno) {
81
+                $this->Connected = true;
82
+                break;
83
+            }
84
+            sleep(1);
85
+        }
86
+        if ($this->connect_errno) {
87
+            $Errno = $this->connect_errno;
88
+            $Error = $this->connect_error;
89
+            $this->error("Connection failed. (".strval($Errno).": ".strval($Error).")");
90
+            $Debug->set_flag("Could not connect to Sphinx server $this->Ident. (".strval($Errno).": ".strval($Error).")");
91
+        } else {
92
+            $Debug->set_flag("Connected to Sphinx server $this->Ident");
93
+        }
88
     }
94
     }
89
-  }
90
 
95
 
91
-  /**
92
-   * Print a message to privileged users and optionally halt page processing
93
-   *
94
-   * @param string $Msg message to display
95
-   * @param bool $Halt halt page processing. Default is to continue processing the page
96
-   * @return Sphinxql object
97
-   */
98
-  public function error($Msg, $Halt = false) {
99
-    global $Debug;
100
-    $ErrorMsg = 'SphinxQL ('.$this->Ident.'): '.strval($Msg);
101
-    $Debug->analysis('SphinxQL Error', $ErrorMsg, 3600*24);
102
-    if ($Halt === true && (DEBUG_MODE || check_perms('site_debug'))) {
103
-      echo '<pre>'.display_str($ErrorMsg).'</pre>';
104
-      die();
105
-    } elseif ($Halt === true) {
106
-      error('-1');
96
+    /**
97
+     * Print a message to privileged users and optionally halt page processing
98
+     *
99
+     * @param string $Msg message to display
100
+     * @param bool $Halt halt page processing. Default is to continue processing the page
101
+     * @return Sphinxql object
102
+     */
103
+    public function error($Msg, $Halt = false)
104
+    {
105
+        global $Debug;
106
+        $ErrorMsg = 'SphinxQL ('.$this->Ident.'): '.strval($Msg);
107
+        $Debug->analysis('SphinxQL Error', $ErrorMsg, 3600*24);
108
+        if ($Halt === true && (DEBUG_MODE || check_perms('site_debug'))) {
109
+            echo '<pre>'.display_str($ErrorMsg).'</pre>';
110
+            die();
111
+        } elseif ($Halt === true) {
112
+            error('-1');
113
+        }
107
     }
114
     }
108
-  }
109
 
115
 
110
-  /**
111
-   * Escape special characters before sending them to the Sphinx server.
112
-   * Two escapes needed because the first one is eaten up by the mysql driver.
113
-   *
114
-   * @param string $String string to escape
115
-   * @return escaped string
116
-   */
117
-  public static function sph_escape_string($String) {
118
-    return strtr(strtolower($String), array(
119
-      '('=>'\\\\(',
120
-      ')'=>'\\\\)',
121
-      '|'=>'\\\\|',
122
-      '-'=>'\\\\-',
123
-      '@'=>'\\\\@',
124
-      '~'=>'\\\\~',
125
-      '&'=>'\\\\&',
126
-      '\''=>'\\\'',
127
-      '<'=>'\\\\<',
128
-      '!'=>'\\\\!',
129
-      '"'=>'\\\\"',
130
-      '/'=>'\\\\/',
131
-      '*'=>'\\\\*',
132
-      '$'=>'\\\\$',
133
-      '^'=>'\\\\^',
134
-      '\\'=>'\\\\\\\\')
135
-    );
136
-  }
116
+    /**
117
+     * Escape special characters before sending them to the Sphinx server.
118
+     * Two escapes needed because the first one is eaten up by the mysql driver.
119
+     *
120
+     * @param string $String string to escape
121
+     * @return escaped string
122
+     */
123
+    public static function sph_escape_string($String)
124
+    {
125
+        return strtr(strtolower($String), array(
126
+        '('=>'\\\\(',
127
+        ')'=>'\\\\)',
128
+        '|'=>'\\\\|',
129
+        '-'=>'\\\\-',
130
+        '@'=>'\\\\@',
131
+        '~'=>'\\\\~',
132
+        '&'=>'\\\\&',
133
+        '\''=>'\\\'',
134
+        '<'=>'\\\\<',
135
+        '!'=>'\\\\!',
136
+        '"'=>'\\\\"',
137
+        '/'=>'\\\\/',
138
+        '*'=>'\\\\*',
139
+        '$'=>'\\\\$',
140
+        '^'=>'\\\\^',
141
+        '\\'=>'\\\\\\\\'));
142
+    }
137
 
143
 
138
-  /**
139
-   * Register sent queries globally for later retrieval by debug functions
140
-   *
141
-   * @param string $QueryString query text
142
-   * @param param $QueryProcessTime time building and processing the query
143
-   */
144
-  public static function register_query($QueryString, $QueryProcessTime) {
145
-    self::$Queries[] = array($QueryString, $QueryProcessTime);
146
-    self::$Time += $QueryProcessTime;
147
-  }
144
+    /**
145
+     * Register sent queries globally for later retrieval by debug functions
146
+     *
147
+     * @param string $QueryString query text
148
+     * @param param $QueryProcessTime time building and processing the query
149
+     */
150
+    public static function register_query($QueryString, $QueryProcessTime)
151
+    {
152
+        self::$Queries[] = array($QueryString, $QueryProcessTime);
153
+        self::$Time += $QueryProcessTime;
154
+    }
148
 }
155
 }

+ 372
- 349
classes/sphinxqlquery.class.php View File

1
-<?
2
-class SphinxqlQuery {
3
-  private $Sphinxql;
1
+<?php
2
+class SphinxqlQuery
3
+{
4
+    private $Sphinxql;
4
 
5
 
5
-  private $Errors;
6
-  private $Expressions;
7
-  private $Filters;
8
-  private $GroupBy;
9
-  private $Indexes;
10
-  private $Limits;
11
-  private $Options;
12
-  private $QueryString;
13
-  private $Select;
14
-  private $SortBy;
15
-  private $SortGroupBy;
6
+    private $Errors;
7
+    private $Expressions;
8
+    private $Filters;
9
+    private $GroupBy;
10
+    private $Indexes;
11
+    private $Limits;
12
+    private $Options;
13
+    private $QueryString;
14
+    private $Select;
15
+    private $SortBy;
16
+    private $SortGroupBy;
16
 
17
 
17
-  /**
18
-   * Initialize Sphinxql object
19
-   *
20
-   * @param string $Server server address or hostname
21
-   * @param int $Port listening port
22
-   * @param string $Socket Unix socket address, overrides $Server:$Port
23
-   */
24
-  public function __construct($Server = SPHINXQL_HOST, $Port = SPHINXQL_PORT, $Socket = SPHINXQL_SOCK) {
25
-    $this->Sphinxql = Sphinxql::init_connection($Server, $Port, $Socket);
26
-    $this->reset();
27
-  }
28
-
29
-  /**
30
-   * Specify what data the Sphinx query is supposed to return
31
-   *
32
-   * @param string $Fields Attributes and expressions
33
-   * @return current Sphinxql query object
34
-   */
35
-  public function select($Fields) {
36
-    $this->Select = $Fields;
37
-    return $this;
38
-  }
18
+    /**
19
+     * Initialize Sphinxql object
20
+     *
21
+     * @param string $Server server address or hostname
22
+     * @param int $Port listening port
23
+     * @param string $Socket Unix socket address, overrides $Server:$Port
24
+     */
25
+    public function __construct($Server = SPHINXQL_HOST, $Port = SPHINXQL_PORT, $Socket = SPHINXQL_SOCK)
26
+    {
27
+        $this->Sphinxql = Sphinxql::init_connection($Server, $Port, $Socket);
28
+        $this->reset();
29
+    }
39
 
30
 
40
-  /**
41
-   * Specify the indexes to use in the search
42
-   *
43
-   * @param string $Indexes comma separated list of indexes
44
-   * @return current Sphinxql query object
45
-   */
46
-  public function from($Indexes) {
47
-    $this->Indexes = $Indexes;
48
-    return $this;
49
-  }
31
+    /**
32
+     * Specify what data the Sphinx query is supposed to return
33
+     *
34
+     * @param string $Fields Attributes and expressions
35
+     * @return current Sphinxql query object
36
+     */
37
+    public function select($Fields)
38
+    {
39
+        $this->Select = $Fields;
40
+        return $this;
41
+    }
50
 
42
 
51
-  /**
52
-   * Add attribute filter. Calling multiple filter functions results in boolean AND between each condition.
53
-   *
54
-   * @param string $Attribute attribute which the filter will apply to
55
-   * @param mixed $Values scalar or array of numerical values. Array uses boolean OR in query condition
56
-   * @param bool $Exclude whether to exclude or include matching documents. Default mode is to include matches
57
-   * @return current Sphinxql query object
58
-   */
59
-  public function where($Attribute, $Values, $Exclude = false) {
60
-    if (empty($Attribute) || !isset($Values)) {
61
-      $this->error("Attribute name and filter value are required.");
62
-      return $this;
43
+    /**
44
+     * Specify the indexes to use in the search
45
+     *
46
+     * @param string $Indexes comma separated list of indexes
47
+     * @return current Sphinxql query object
48
+     */
49
+    public function from($Indexes)
50
+    {
51
+        $this->Indexes = $Indexes;
52
+        return $this;
63
     }
53
     }
64
-    $Filters = [];
65
-    if (is_array($Values)) {
66
-      foreach ($Values as $Value) {
67
-        if (!is_number($Value)) {
68
-          $this->error("Filters only support numeric values.");
69
-          return $this;
54
+
55
+    /**
56
+     * Add attribute filter. Calling multiple filter functions results in boolean AND between each condition.
57
+     *
58
+     * @param string $Attribute attribute which the filter will apply to
59
+     * @param mixed $Values scalar or array of numerical values. Array uses boolean OR in query condition
60
+     * @param bool $Exclude whether to exclude or include matching documents. Default mode is to include matches
61
+     * @return current Sphinxql query object
62
+     */
63
+    public function where($Attribute, $Values, $Exclude = false)
64
+    {
65
+        if (empty($Attribute) || !isset($Values)) {
66
+            $this->error("Attribute name and filter value are required.");
67
+            return $this;
68
+        }
69
+        $Filters = [];
70
+        if (is_array($Values)) {
71
+            foreach ($Values as $Value) {
72
+                if (!is_number($Value)) {
73
+                    $this->error("Filters only support numeric values.");
74
+                    return $this;
75
+                }
76
+            }
77
+            if ($Exclude) {
78
+                $Filters[] = "$Attribute NOT IN (".implode(",", $Values).")";
79
+            } else {
80
+                $Filters[] = "$Attribute IN (".implode(",", $Values).")";
81
+            }
82
+        } else {
83
+            if (!is_number($Values)) {
84
+                $this->error("Filters only support numeric values.");
85
+                return $this;
86
+            }
87
+            if ($Exclude) {
88
+                $Filters[] = "$Attribute != $Values";
89
+            } else {
90
+                $Filters[] = "$Attribute = $Values";
91
+            }
70
         }
92
         }
71
-      }
72
-      if ($Exclude) {
73
-        $Filters[] = "$Attribute NOT IN (".implode(",", $Values).")";
74
-      } else {
75
-        $Filters[] = "$Attribute IN (".implode(",", $Values).")";
76
-      }
77
-    } else {
78
-      if (!is_number($Values)) {
79
-        $this->error("Filters only support numeric values.");
93
+        $this->Filters[] = implode(" AND ", $Filters);
80
         return $this;
94
         return $this;
81
-      }
82
-      if ($Exclude) {
83
-        $Filters[] = "$Attribute != $Values";
84
-      } else {
85
-        $Filters[] = "$Attribute = $Values";
86
-      }
87
     }
95
     }
88
-    $this->Filters[] = implode(" AND ", $Filters);
89
-    return $this;
90
-  }
91
 
96
 
92
-  /**
93
-   * Add attribute less-than filter. Calling multiple filter functions results in boolean AND between each condition.
94
-   *
95
-   * @param string $Attribute attribute which the filter will apply to
96
-   * @param array $Value upper limit for matches
97
-   * @param bool $Inclusive whether to use <= or <
98
-   * @return current Sphinxql query object
99
-   */
100
-  public function where_lt($Attribute, $Value, $Inclusive = false) {
101
-    if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
102
-      $this->error("Attribute name is required and only numeric filters are supported.");
103
-      return $this;
97
+    /**
98
+     * Add attribute less-than filter. Calling multiple filter functions results in boolean AND between each condition.
99
+     *
100
+     * @param string $Attribute attribute which the filter will apply to
101
+     * @param array $Value upper limit for matches
102
+     * @param bool $Inclusive whether to use <= or <
103
+     * @return current Sphinxql query object
104
+     */
105
+    public function where_lt($Attribute, $Value, $Inclusive = false)
106
+    {
107
+        if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
108
+            $this->error("Attribute name is required and only numeric filters are supported.");
109
+            return $this;
110
+        }
111
+        $this->Filters[] = $Inclusive ? "$Attribute <= $Value" : "$Attribute < $Value";
112
+        return $this;
104
     }
113
     }
105
-    $this->Filters[] = $Inclusive ? "$Attribute <= $Value" : "$Attribute < $Value";
106
-    return $this;
107
-  }
108
 
114
 
109
-  /**
110
-   * Add attribute greater-than filter. Calling multiple filter functions results in boolean AND between each condition.
111
-   *
112
-   * @param string $Attribute attribute which the filter will apply to
113
-   * @param array $Value lower limit for matches
114
-   * @param bool $Inclusive whether to use >= or >
115
-   * @return current Sphinxql query object
116
-   */
117
-  public function where_gt($Attribute, $Value, $Inclusive = false) {
118
-    if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
119
-      $this->error("Attribute name is required and only numeric filters are supported.");
120
-      return $this;
115
+    /**
116
+     * Add attribute greater-than filter. Calling multiple filter functions results in boolean AND between each condition.
117
+     *
118
+     * @param string $Attribute attribute which the filter will apply to
119
+     * @param array $Value lower limit for matches
120
+     * @param bool $Inclusive whether to use >= or >
121
+     * @return current Sphinxql query object
122
+     */
123
+    public function where_gt($Attribute, $Value, $Inclusive = false)
124
+    {
125
+        if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
126
+            $this->error("Attribute name is required and only numeric filters are supported.");
127
+            return $this;
128
+        }
129
+        $this->Filters[] = $Inclusive ? "$Attribute >= $Value" : "$Attribute > $Value";
130
+        return $this;
121
     }
131
     }
122
-    $this->Filters[] = $Inclusive ? "$Attribute >= $Value" : "$Attribute > $Value";
123
-    return $this;
124
-  }
125
 
132
 
126
-  /**
127
-   * Add attribute range filter. Calling multiple filter functions results in boolean AND between each condition.
128
-   *
129
-   * @param string $Attribute attribute which the filter will apply to
130
-   * @param array $Values pair of numerical values that defines the filter range
131
-   * @return current Sphinxql query object
132
-   */
133
-  public function where_between($Attribute, $Values) {
134
-    if (empty($Attribute) || empty($Values) || count($Values) != 2 || !is_number($Values[0]) || !is_number($Values[1])) {
135
-      $this->error("Filter range requires array of two numerical boundaries as values.");
136
-      return $this;
133
+    /**
134
+     * Add attribute range filter. Calling multiple filter functions results in boolean AND between each condition.
135
+     *
136
+     * @param string $Attribute attribute which the filter will apply to
137
+     * @param array $Values pair of numerical values that defines the filter range
138
+     * @return current Sphinxql query object
139
+     */
140
+    public function where_between($Attribute, $Values)
141
+    {
142
+        if (empty($Attribute) || empty($Values) || count($Values) != 2 || !is_number($Values[0]) || !is_number($Values[1])) {
143
+            $this->error("Filter range requires array of two numerical boundaries as values.");
144
+            return $this;
145
+        }
146
+        $this->Filters[] = "$Attribute BETWEEN $Values[0] AND $Values[1]";
147
+        return $this;
137
     }
148
     }
138
-    $this->Filters[] = "$Attribute BETWEEN $Values[0] AND $Values[1]";
139
-    return $this;
140
-  }
141
 
149
 
142
-  /**
143
-   * Add fulltext query expression. Calling multiple filter functions results in boolean AND between each condition.
144
-   * Query expression is escaped automatically
145
-   *
146
-   * @param string $Expr query expression
147
-   * @param string $Field field to match $Expr against. Default is *, which means all available fields
148
-   * @return current Sphinxql query object
149
-   */
150
-  public function where_match($Expr, $Field = '*', $Escape = true) {
151
-    if (empty($Expr)) {
152
-      return $this;
153
-    }
154
-    if ($Field !== false) {
155
-      $Field = "@$Field ";
156
-    }
157
-    if ($Escape === true) {
158
-      $this->Expressions[] = "$Field".Sphinxql::sph_escape_string($Expr);
159
-    } else {
160
-      $this->Expressions[] = $Field.$Expr;
150
+    /**
151
+     * Add fulltext query expression. Calling multiple filter functions results in boolean AND between each condition.
152
+     * Query expression is escaped automatically
153
+     *
154
+     * @param string $Expr query expression
155
+     * @param string $Field field to match $Expr against. Default is *, which means all available fields
156
+     * @return current Sphinxql query object
157
+     */
158
+    public function where_match($Expr, $Field = '*', $Escape = true)
159
+    {
160
+        if (empty($Expr)) {
161
+            return $this;
162
+        }
163
+        if ($Field !== false) {
164
+            $Field = "@$Field ";
165
+        }
166
+        if ($Escape === true) {
167
+            $this->Expressions[] = "$Field".Sphinxql::sph_escape_string($Expr);
168
+        } else {
169
+            $this->Expressions[] = $Field.$Expr;
170
+        }
171
+        return $this;
161
     }
172
     }
162
-    return $this;
163
-  }
164
 
173
 
165
-  /**
166
-   * Specify the order of the matches. Calling this function multiple times sets secondary priorities
167
-   *
168
-   * @param string $Attribute attribute to use for sorting.
169
-   *     Passing an empty attribute value will clear the current sort settings
170
-   * @param string $Mode sort method to apply to the selected attribute
171
-   * @return current Sphinxql query object
172
-   */
173
-  public function order_by($Attribute = false, $Mode = false) {
174
-    if (empty($Attribute)) {
175
-      $this->SortBy = [];
176
-    } else {
177
-      $this->SortBy[] = "$Attribute $Mode";
174
+    /**
175
+     * Specify the order of the matches. Calling this function multiple times sets secondary priorities
176
+     *
177
+     * @param string $Attribute attribute to use for sorting.
178
+     *     Passing an empty attribute value will clear the current sort settings
179
+     * @param string $Mode sort method to apply to the selected attribute
180
+     * @return current Sphinxql query object
181
+     */
182
+    public function order_by($Attribute = false, $Mode = false)
183
+    {
184
+        if (empty($Attribute)) {
185
+            $this->SortBy = [];
186
+        } else {
187
+            $this->SortBy[] = "$Attribute $Mode";
188
+        }
189
+        return $this;
178
     }
190
     }
179
-    return $this;
180
-  }
181
 
191
 
182
-  /**
183
-   * Specify how the results are grouped
184
-   *
185
-   * @param string $Attribute group matches with the same $Attribute value.
186
-   *     Passing an empty attribute value will clear the current group settings
187
-   * @return current Sphinxql query object
188
-   */
189
-  public function group_by($Attribute = false) {
190
-    if (empty($Attribute)) {
191
-      $this->GroupBy = '';
192
-    } else {
193
-      $this->GroupBy = $Attribute;
192
+    /**
193
+     * Specify how the results are grouped
194
+     *
195
+     * @param string $Attribute group matches with the same $Attribute value.
196
+     *     Passing an empty attribute value will clear the current group settings
197
+     * @return current Sphinxql query object
198
+     */
199
+    public function group_by($Attribute = false)
200
+    {
201
+        if (empty($Attribute)) {
202
+            $this->GroupBy = '';
203
+        } else {
204
+            $this->GroupBy = $Attribute;
205
+        }
206
+        return $this;
194
     }
207
     }
195
-    return $this;
196
-  }
197
 
208
 
198
-  /**
199
-   * Specify the order of the results within groups
200
-   *
201
-   * @param string $Attribute attribute to use for sorting.
202
-   *     Passing an empty attribute will clear the current group sort settings
203
-   * @param string $Mode sort method to apply to the selected attribute
204
-   * @return current Sphinxql query object
205
-   */
206
-  public function order_group_by($Attribute = false, $Mode = false) {
207
-    if (empty($Attribute)) {
208
-      $this->SortGroupBy = '';
209
-    } else {
210
-      $this->SortGroupBy = "$Attribute $Mode";
209
+    /**
210
+     * Specify the order of the results within groups
211
+     *
212
+     * @param string $Attribute attribute to use for sorting.
213
+     *     Passing an empty attribute will clear the current group sort settings
214
+     * @param string $Mode sort method to apply to the selected attribute
215
+     * @return current Sphinxql query object
216
+     */
217
+    public function order_group_by($Attribute = false, $Mode = false)
218
+    {
219
+        if (empty($Attribute)) {
220
+            $this->SortGroupBy = '';
221
+        } else {
222
+            $this->SortGroupBy = "$Attribute $Mode";
223
+        }
224
+        return $this;
211
     }
225
     }
212
-    return $this;
213
-  }
214
-
215
-  /**
216
-   * Specify the offset and amount of matches to return
217
-   *
218
-   * @param int $Offset number of matches to discard
219
-   * @param int $Limit number of matches to return
220
-   * @param int $MaxMatches number of results to store in the Sphinx server's memory. Must be >= ($Offset+$Limit)
221
-   * @return current Sphinxql query object
222
-   */
223
-  public function limit($Offset, $Limit, $MaxMatches = SPHINX_MAX_MATCHES) {
224
-    $this->Limits = "$Offset, $Limit";
225
-    $this->set('max_matches', $MaxMatches);
226
-    return $this;
227
-  }
228
-
229
-  /**
230
-   * Tweak the settings to use for the query. Sanity checking shouldn't be needed as Sphinx already does it
231
-   *
232
-   * @param string $Name setting name
233
-   * @param mixed $Value value
234
-   * @return current Sphinxql query object
235
-   */
236
-  public function set($Name, $Value) {
237
-    $this->Options[$Name] = $Value;
238
-    return $this;
239
-  }
240
 
226
 
241
-  /**
242
-   * Combine the query options into a valid Sphinx query segment
243
-   *
244
-   * @return string of options
245
-   */
246
-  private function build_options() {
247
-    $Options = [];
248
-    foreach ($this->Options as $Option => $Value) {
249
-      $Options[] = "$Option = $Value";
227
+    /**
228
+     * Specify the offset and amount of matches to return
229
+     *
230
+     * @param int $Offset number of matches to discard
231
+     * @param int $Limit number of matches to return
232
+     * @param int $MaxMatches number of results to store in the Sphinx server's memory. Must be >= ($Offset+$Limit)
233
+     * @return current Sphinxql query object
234
+     */
235
+    public function limit($Offset, $Limit, $MaxMatches = SPHINX_MAX_MATCHES)
236
+    {
237
+        $this->Limits = "$Offset, $Limit";
238
+        $this->set('max_matches', $MaxMatches);
239
+        return $this;
250
     }
240
     }
251
-    return implode(', ', $Options);
252
-  }
253
 
241
 
254
-  /**
255
-   * Combine the query conditions into a valid Sphinx query segment
256
-   */
257
-  private function build_query() {
258
-    if (!$this->Indexes) {
259
-      $this->error('Index name is required.');
260
-      return false;
261
-    }
262
-    $this->QueryString = "SELECT $this->Select\nFROM $this->Indexes";
263
-    if (!empty($this->Expressions)) {
264
-      $this->Filters['expr'] = "MATCH('".implode(' ', $this->Expressions)."')";
265
-    }
266
-    if (!empty($this->Filters)) {
267
-      $this->QueryString .= "\nWHERE ".implode("\n\tAND ", $this->Filters);
268
-    }
269
-    if (!empty($this->GroupBy)) {
270
-      $this->QueryString .= "\nGROUP BY $this->GroupBy";
271
-    }
272
-    if (!empty($this->SortGroupBy)) {
273
-      $this->QueryString .= "\nWITHIN GROUP ORDER BY $this->SortGroupBy";
274
-    }
275
-    if (!empty($this->SortBy)) {
276
-      $this->QueryString .= "\nORDER BY ".implode(", ", $this->SortBy);
277
-    }
278
-    if (!empty($this->Limits)) {
279
-      $this->QueryString .= "\nLIMIT $this->Limits";
242
+    /**
243
+     * Tweak the settings to use for the query. Sanity checking shouldn't be needed as Sphinx already does it
244
+     *
245
+     * @param string $Name setting name
246
+     * @param mixed $Value value
247
+     * @return current Sphinxql query object
248
+     */
249
+    public function set($Name, $Value)
250
+    {
251
+        $this->Options[$Name] = $Value;
252
+        return $this;
280
     }
253
     }
281
-    if (!empty($this->Options)) {
282
-      $Options = $this->build_options();
283
-      $this->QueryString .= "\nOPTION $Options";
254
+
255
+    /**
256
+     * Combine the query options into a valid Sphinx query segment
257
+     *
258
+     * @return string of options
259
+     */
260
+    private function build_options()
261
+    {
262
+        $Options = [];
263
+        foreach ($this->Options as $Option => $Value) {
264
+            $Options[] = "$Option = $Value";
265
+        }
266
+        return implode(', ', $Options);
284
     }
267
     }
285
-  }
286
 
268
 
287
-  /**
288
-   * Construct and send the query. Register the query in the global Sphinxql object
289
-   *
290
-   * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
291
-   * @return Sphinxql result object
292
-   */
293
-  public function query($GetMeta = true) {
294
-    $QueryStartTime = microtime(true);
295
-    $this->build_query();
296
-    if (count($this->Errors) > 0) {
297
-      $ErrorMsg = implode("\n", $this->Errors);
298
-      $this->Sphinxql->error("Query builder found errors:\n$ErrorMsg");
299
-      return new SphinxqlResult(null, null, 1, $ErrorMsg);
269
+    /**
270
+     * Combine the query conditions into a valid Sphinx query segment
271
+     */
272
+    private function build_query()
273
+    {
274
+        if (!$this->Indexes) {
275
+            $this->error('Index name is required.');
276
+            return false;
277
+        }
278
+        $this->QueryString = "SELECT $this->Select\nFROM $this->Indexes";
279
+        if (!empty($this->Expressions)) {
280
+            $this->Filters['expr'] = "MATCH('".implode(' ', $this->Expressions)."')";
281
+        }
282
+        if (!empty($this->Filters)) {
283
+            $this->QueryString .= "\nWHERE ".implode("\n\tAND ", $this->Filters);
284
+        }
285
+        if (!empty($this->GroupBy)) {
286
+            $this->QueryString .= "\nGROUP BY $this->GroupBy";
287
+        }
288
+        if (!empty($this->SortGroupBy)) {
289
+            $this->QueryString .= "\nWITHIN GROUP ORDER BY $this->SortGroupBy";
290
+        }
291
+        if (!empty($this->SortBy)) {
292
+            $this->QueryString .= "\nORDER BY ".implode(", ", $this->SortBy);
293
+        }
294
+        if (!empty($this->Limits)) {
295
+            $this->QueryString .= "\nLIMIT $this->Limits";
296
+        }
297
+        if (!empty($this->Options)) {
298
+            $Options = $this->build_options();
299
+            $this->QueryString .= "\nOPTION $Options";
300
+        }
300
     }
301
     }
301
-    $QueryString = $this->QueryString;
302
-    $Result = $this->send_query($GetMeta);
303
-    $QueryProcessTime = (microtime(true) - $QueryStartTime)*1000;
304
-    Sphinxql::register_query($QueryString, $QueryProcessTime);
305
-    return $Result;
306
-  }
307
 
302
 
308
-  /**
309
-   * Run a manually constructed query
310
-   *
311
-   * @param string Query query expression
312
-   * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
313
-   * @return Sphinxql result object
314
-   */
315
-  public function raw_query($Query, $GetMeta = true) {
316
-    $this->QueryString = $Query;
317
-    return $this->send_query($GetMeta);
318
-  }
303
+    /**
304
+     * Construct and send the query. Register the query in the global Sphinxql object
305
+     *
306
+     * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
307
+     * @return Sphinxql result object
308
+     */
309
+    public function query($GetMeta = true)
310
+    {
311
+        $QueryStartTime = microtime(true);
312
+        $this->build_query();
313
+        if (count($this->Errors) > 0) {
314
+            $ErrorMsg = implode("\n", $this->Errors);
315
+            $this->Sphinxql->error("Query builder found errors:\n$ErrorMsg");
316
+            return new SphinxqlResult(null, null, 1, $ErrorMsg);
317
+        }
318
+        $QueryString = $this->QueryString;
319
+        $Result = $this->send_query($GetMeta);
320
+        $QueryProcessTime = (microtime(true) - $QueryStartTime)*1000;
321
+        Sphinxql::register_query($QueryString, $QueryProcessTime);
322
+        return $Result;
323
+    }
319
 
324
 
320
-  /**
321
-   * Run a pre-processed query. Only used internally
322
-   *
323
-   * @param bool GetMeta whether to fetch meta data for the executed query
324
-   * @return Sphinxql result object
325
-   */
326
-  private function send_query($GetMeta) {
327
-    if (!$this->QueryString) {
328
-      return false;
325
+    /**
326
+     * Run a manually constructed query
327
+     *
328
+     * @param string Query query expression
329
+     * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
330
+     * @return Sphinxql result object
331
+     */
332
+    public function raw_query($Query, $GetMeta = true)
333
+    {
334
+        $this->QueryString = $Query;
335
+        return $this->send_query($GetMeta);
329
     }
336
     }
330
-    $this->Sphinxql->sph_connect();
331
-    $Result = $this->Sphinxql->query($this->QueryString);
332
-    if ($Result === false) {
333
-      $Errno = $this->Sphinxql->errno;
334
-      $Error = $this->Sphinxql->error;
335
-      $this->Sphinxql->error("Query returned error $Errno ($Error).\n$this->QueryString");
336
-      $Meta = null;
337
-    } else {
338
-      $Errno = 0;
339
-      $Error = '';
340
-      $Meta = $GetMeta ? $this->get_meta() : null;
337
+
338
+    /**
339
+     * Run a pre-processed query. Only used internally
340
+     *
341
+     * @param bool GetMeta whether to fetch meta data for the executed query
342
+     * @return Sphinxql result object
343
+     */
344
+    private function send_query($GetMeta)
345
+    {
346
+        if (!$this->QueryString) {
347
+            return false;
348
+        }
349
+        $this->Sphinxql->sph_connect();
350
+        $Result = $this->Sphinxql->query($this->QueryString);
351
+        if ($Result === false) {
352
+            $Errno = $this->Sphinxql->errno;
353
+            $Error = $this->Sphinxql->error;
354
+            $this->Sphinxql->error("Query returned error $Errno ($Error).\n$this->QueryString");
355
+            $Meta = null;
356
+        } else {
357
+            $Errno = 0;
358
+            $Error = '';
359
+            $Meta = $GetMeta ? $this->get_meta() : null;
360
+        }
361
+        return new SphinxqlResult($Result, $Meta, $Errno, $Error);
341
     }
362
     }
342
-    return new SphinxqlResult($Result, $Meta, $Errno, $Error);
343
-  }
344
 
363
 
345
-  /**
346
-   * Reset all query options and conditions
347
-   */
348
-  public function reset() {
349
-    $this->Errors = [];
350
-    $this->Expressions = [];
351
-    $this->Filters = [];
352
-    $this->GroupBy = '';
353
-    $this->Indexes = '';
354
-    $this->Limits = [];
355
-    $this->Options = array('ranker' => 'none');
356
-    $this->QueryString = '';
357
-    $this->Select = '*';
358
-    $this->SortBy = [];
359
-    $this->SortGroupBy = '';
360
-  }
364
+    /**
365
+     * Reset all query options and conditions
366
+     */
367
+    public function reset()
368
+    {
369
+        $this->Errors = [];
370
+        $this->Expressions = [];
371
+        $this->Filters = [];
372
+        $this->GroupBy = '';
373
+        $this->Indexes = '';
374
+        $this->Limits = [];
375
+        $this->Options = array('ranker' => 'none');
376
+        $this->QueryString = '';
377
+        $this->Select = '*';
378
+        $this->SortBy = [];
379
+        $this->SortGroupBy = '';
380
+    }
361
 
381
 
362
-  /**
363
-   * Fetch and store meta data for the last executed query
364
-   *
365
-   * @return meta data
366
-   */
367
-  private function get_meta() {
368
-    return $this->raw_query("SHOW META", false)->to_pair(0, 1);
369
-  }
382
+    /**
383
+     * Fetch and store meta data for the last executed query
384
+     *
385
+     * @return meta data
386
+     */
387
+    private function get_meta()
388
+    {
389
+        return $this->raw_query("SHOW META", false)->to_pair(0, 1);
390
+    }
370
 
391
 
371
-  /**
372
-   * Copy attribute filters from another SphinxqlQuery object
373
-   *
374
-   * @param SphinxqlQuery $SphQLSource object to copy the filters from
375
-   * @return current SphinxqlQuery object
376
-   */
377
-  public function copy_attributes_from($SphQLSource) {
378
-    $this->Filters = $SphQLSource->Filters;
379
-  }
392
+    /**
393
+     * Copy attribute filters from another SphinxqlQuery object
394
+     *
395
+     * @param SphinxqlQuery $SphQLSource object to copy the filters from
396
+     * @return current SphinxqlQuery object
397
+     */
398
+    public function copy_attributes_from($SphQLSource)
399
+    {
400
+        $this->Filters = $SphQLSource->Filters;
401
+    }
380
 
402
 
381
-  /**
382
-   * Store error messages
383
-   */
384
-  private function error($Msg) {
385
-    $this->Errors[] = $Msg;
386
-  }
403
+    /**
404
+     * Store error messages
405
+     */
406
+    private function error($Msg)
407
+    {
408
+        $this->Errors[] = $Msg;
409
+    }
387
 }
410
 }

+ 134
- 125
classes/sphinxqlresult.class.php View File

1
-<?
2
-class SphinxqlResult {
3
-  private $Result;
4
-  private $Meta;
5
-  public $Errno;
6
-  public $Error;
1
+<?php
2
+class SphinxqlResult
3
+{
4
+    private $Result;
5
+    private $Meta;
6
+    public $Errno;
7
+    public $Error;
7
 
8
 
8
-  /**
9
-   * Create Sphinxql result object
10
-   *
11
-   * @param mysqli_result $Result query results
12
-   * @param array $Meta meta data for the query
13
-   * @param int $Errno error code returned by the query upon failure
14
-   * @param string $Error error message returned by the query upon failure
15
-   */
16
-  public function __construct($Result, $Meta, $Errno, $Error) {
17
-    $this->Result = $Result;
18
-    $this->Meta = $Meta;
19
-    $this->Errno = $Errno;
20
-    $this->Error = $Error;
21
-  }
22
-
23
-  /**
24
-   * Redirect to the Mysqli result object if a nonexistent method is called
25
-   *
26
-   * @param string $Name method name
27
-   * @param array $Arguments arguments used in the function call
28
-   * @return whatever the parent function returns
29
-   */
30
-  public function __call($Name, $Arguments) {
31
-    return call_user_func_array(array($this->Result, $Name), $Arguments);
32
-  }
33
-
34
-  /**
35
-   * Did the query find anything?
36
-   *
37
-   * @return bool results were found
38
-   */
39
-  public function has_results() {
40
-    return $this->get_meta('total') > 0;
41
-  }
9
+    /**
10
+     * Create Sphinxql result object
11
+     *
12
+     * @param mysqli_result $Result query results
13
+     * @param array $Meta meta data for the query
14
+     * @param int $Errno error code returned by the query upon failure
15
+     * @param string $Error error message returned by the query upon failure
16
+     */
17
+    public function __construct($Result, $Meta, $Errno, $Error)
18
+    {
19
+        $this->Result = $Result;
20
+        $this->Meta = $Meta;
21
+        $this->Errno = $Errno;
22
+        $this->Error = $Error;
23
+    }
42
 
24
 
43
-  /**
44
-   * Collect and return the specified key of all results as a list
45
-   *
46
-   * @param string $Key key containing the desired data
47
-   * @return array with the $Key value of all results
48
-   */
49
-  public function collect($Key) {
50
-    $Return = [];
51
-    while ($Row = $this->fetch_array()) {
52
-      $Return[] = $Row[$Key];
25
+    /**
26
+     * Redirect to the Mysqli result object if a nonexistent method is called
27
+     *
28
+     * @param string $Name method name
29
+     * @param array $Arguments arguments used in the function call
30
+     * @return whatever the parent function returns
31
+     */
32
+    public function __call($Name, $Arguments)
33
+    {
34
+        return call_user_func_array(array($this->Result, $Name), $Arguments);
53
     }
35
     }
54
-    $this->data_seek(0);
55
-    return $Return;
56
-  }
57
 
36
 
58
-  /**
59
-   * Collect and return all available data for the matches optionally indexed by a specified key
60
-   *
61
-   * @param string $Key key to use as indexing value
62
-   * @param string $ResultType method to use when fetching data from the mysqli_result object. Default is MYSQLI_ASSOC
63
-   * @return array with all available data for the matches
64
-   */
65
-  public function to_array($Key, $ResultType = MYSQLI_ASSOC) {
66
-    $Return = [];
67
-    while ($Row = $this->fetch_array($ResultType)) {
68
-      if ($Key !== false) {
69
-        $Return[$Row[$Key]] = $Row;
70
-      } else {
71
-        $Return[] = $Row;
72
-      }
37
+    /**
38
+     * Did the query find anything?
39
+     *
40
+     * @return bool results were found
41
+     */
42
+    public function has_results()
43
+    {
44
+        return $this->get_meta('total') > 0;
73
     }
45
     }
74
-    $this->data_seek(0);
75
-    return $Return;
76
-  }
77
 
46
 
78
-  /**
79
-   * Collect pairs of keys for all matches
80
-   *
81
-   * @param string $Key1 key to use as indexing value
82
-   * @param string $Key2 key to use as value
83
-   * @return array with $Key1 => $Key2 pairs for matches
84
-   */
85
-  public function to_pair($Key1, $Key2) {
86
-    $Return = [];
87
-    while ($Row = $this->fetch_array()) {
88
-      $Return[$Row[$Key1]] = $Row[$Key2];
47
+    /**
48
+     * Collect and return the specified key of all results as a list
49
+     *
50
+     * @param string $Key key containing the desired data
51
+     * @return array with the $Key value of all results
52
+     */
53
+    public function collect($Key)
54
+    {
55
+        $Return = [];
56
+        while ($Row = $this->fetch_array()) {
57
+            $Return[] = $Row[$Key];
58
+        }
59
+        $this->data_seek(0);
60
+        return $Return;
89
     }
61
     }
90
-    $this->data_seek(0);
91
-    return $Return;
92
-  }
93
 
62
 
94
-  /**
95
-   * Return specified portions of the current Sphinxql result object's meta data
96
-   *
97
-   * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all meta data
98
-   * @return array with meta data
99
-   */
100
-  public function get_meta($Keys = false) {
101
-    if ($Keys !== false) {
102
-      if (is_array($Keys)) {
63
+    /**
64
+     * Collect and return all available data for the matches optionally indexed by a specified key
65
+     *
66
+     * @param string $Key key to use as indexing value
67
+     * @param string $ResultType method to use when fetching data from the mysqli_result object. Default is MYSQLI_ASSOC
68
+     * @return array with all available data for the matches
69
+     */
70
+    public function to_array($Key, $ResultType = MYSQLI_ASSOC)
71
+    {
103
         $Return = [];
72
         $Return = [];
104
-        foreach ($Keys as $Key) {
105
-          if (!isset($this->Meta[$Key])) {
106
-            continue;
107
-          }
108
-          $Return[$Key] = $this->Meta[$Key];
73
+        while ($Row = $this->fetch_array($ResultType)) {
74
+            if ($Key !== false) {
75
+                $Return[$Row[$Key]] = $Row;
76
+            } else {
77
+                $Return[] = $Row;
78
+            }
109
         }
79
         }
80
+        $this->data_seek(0);
110
         return $Return;
81
         return $Return;
111
-      } else {
112
-        return isset($this->Meta[$Keys]) ? $this->Meta[$Keys] : false;
113
-      }
114
-    } else {
115
-      return $this->Meta;
116
     }
82
     }
117
-  }
118
 
83
 
119
-  /**
120
-   * Return specified portions of the current Mysqli result object's information
121
-   *
122
-   * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all available information
123
-   * @return array with result information
124
-   */
125
-  public function get_result_info($Keys = false) {
126
-    if ($Keys !== false) {
127
-      if (is_array($Keys)) {
84
+    /**
85
+     * Collect pairs of keys for all matches
86
+     *
87
+     * @param string $Key1 key to use as indexing value
88
+     * @param string $Key2 key to use as value
89
+     * @return array with $Key1 => $Key2 pairs for matches
90
+     */
91
+    public function to_pair($Key1, $Key2)
92
+    {
128
         $Return = [];
93
         $Return = [];
129
-        foreach ($Keys as $Key) {
130
-          if (!isset($this->Result->$Key)) {
131
-            continue;
132
-          }
133
-          $Return[$Key] = $this->Result->$Key;
94
+        while ($Row = $this->fetch_array()) {
95
+            $Return[$Row[$Key1]] = $Row[$Key2];
134
         }
96
         }
97
+        $this->data_seek(0);
135
         return $Return;
98
         return $Return;
136
-      } else {
137
-        return isset($this->Result->$Keys) ? $this->Result->$Keys : false;
138
-      }
139
-    } else {
140
-      return $this->Result;
141
     }
99
     }
142
-  }
100
+
101
+    /**
102
+     * Return specified portions of the current Sphinxql result object's meta data
103
+     *
104
+     * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all meta data
105
+     * @return array with meta data
106
+     */
107
+    public function get_meta($Keys = false)
108
+    {
109
+        if ($Keys !== false) {
110
+            if (is_array($Keys)) {
111
+                $Return = [];
112
+                foreach ($Keys as $Key) {
113
+                    if (!isset($this->Meta[$Key])) {
114
+                        continue;
115
+                    }
116
+                    $Return[$Key] = $this->Meta[$Key];
117
+                }
118
+                return $Return;
119
+            } else {
120
+                return isset($this->Meta[$Keys]) ? $this->Meta[$Keys] : false;
121
+            }
122
+        } else {
123
+            return $this->Meta;
124
+        }
125
+    }
126
+
127
+    /**
128
+     * Return specified portions of the current Mysqli result object's information
129
+     *
130
+     * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all available information
131
+     * @return array with result information
132
+     */
133
+    public function get_result_info($Keys = false)
134
+    {
135
+        if ($Keys !== false) {
136
+            if (is_array($Keys)) {
137
+                $Return = [];
138
+                foreach ($Keys as $Key) {
139
+                    if (!isset($this->Result->$Key)) {
140
+                        continue;
141
+                    }
142
+                    $Return[$Key] = $this->Result->$Key;
143
+                }
144
+                return $Return;
145
+            } else {
146
+                return isset($this->Result->$Keys) ? $this->Result->$Keys : false;
147
+            }
148
+        } else {
149
+            return $this->Result;
150
+        }
151
+    }
143
 }
152
 }

Loading…
Cancel
Save