Browse Source

Upload files to 'classes'

Stortebeker 6 years ago
parent
commit
61c91a283e
2 changed files with 183 additions and 189 deletions
  1. 2
    1
      classes/artist.class.php
  2. 181
    188
      classes/artists.class.php

+ 2
- 1
classes/artist.class.php View File

1
-<?php ;
1
+<?
2
 // Placeholder for if we ever decide to actaully have a model for an artist.
2
 // Placeholder for if we ever decide to actaully have a model for an artist.
3
+?>

+ 181
- 188
classes/artists.class.php View File

1
-<?php
2
-class Artists
3
-{
4
-    /**
5
-     * Given an array of GroupIDs, return their associated artists.
6
-     *
7
-     * @param array $GroupIDs
8
-     * @return an array of the following form:
9
-     *  GroupID => {
10
-     *    [ArtistType] => {
11
-     *      id, name, aliasid
12
-     *    }
13
-     *  }
14
-     * ArtistType is an int. It can be:
15
-     * 1 => Main artist
16
-     * 2 => Guest artist
17
-     * 4 => Composer
18
-     * 5 => Conductor
19
-     * 6 => DJ
20
-     */
21
-    public static function get_artists($GroupIDs)
22
-    {
23
-        $Results = [];
24
-        $DBs = [];
25
-        foreach ($GroupIDs as $GroupID) {
26
-            if (!is_number($GroupID)) {
27
-                continue;
28
-            }
29
-            $Artists = G::$Cache->get_value('groups_artists_'.$GroupID);
30
-            if (is_array($Artists)) {
31
-                $Results[$GroupID] = $Artists;
32
-            } else {
33
-                $DBs[] = $GroupID;
34
-            }
35
-        }
36
-        if (count($DBs) > 0) {
37
-            $IDs = implode(',', $DBs);
38
-            if (empty($IDs)) {
39
-                $IDs = "null";
40
-            }
41
-            $QueryID = G::$DB->get_query_id();
42
-            G::$DB->query("
1
+<?
2
+class Artists {
3
+  /**
4
+   * Given an array of GroupIDs, return their associated artists.
5
+   *
6
+   * @param array $GroupIDs
7
+   * @return an array of the following form:
8
+   *  GroupID => {
9
+   *    [ArtistType] => {
10
+   *      id, name, aliasid
11
+   *    }
12
+   *  }
13
+   * ArtistType is an int. It can be:
14
+   * 1 => Main artist
15
+   * 2 => Guest artist
16
+   * 4 => Composer
17
+   * 5 => Conductor
18
+   * 6 => DJ
19
+   */
20
+  public static function get_artists($GroupIDs) {
21
+    $Results = [];
22
+    $DBs = [];
23
+    foreach ($GroupIDs as $GroupID) {
24
+      if (!is_number($GroupID)) {
25
+        continue;
26
+      }
27
+      $Artists = G::$Cache->get_value('groups_artists_'.$GroupID);
28
+      if (is_array($Artists)) {
29
+        $Results[$GroupID] = $Artists;
30
+      } else {
31
+        $DBs[] = $GroupID;
32
+      }
33
+    }
34
+    if (count($DBs) > 0) {
35
+      $IDs = implode(',', $DBs);
36
+      if (empty($IDs)) {
37
+        $IDs = "null";
38
+      }
39
+      $QueryID = G::$DB->get_query_id();
40
+      G::$DB->query("
43
         SELECT ta.GroupID,
41
         SELECT ta.GroupID,
44
           ta.ArtistID,
42
           ta.ArtistID,
45
           ag.Name
43
           ag.Name
48
         WHERE ta.GroupID IN ($IDs)
46
         WHERE ta.GroupID IN ($IDs)
49
         ORDER BY ta.GroupID ASC,
47
         ORDER BY ta.GroupID ASC,
50
           ag.Name ASC;");
48
           ag.Name ASC;");
51
-            while (list($GroupID, $ArtistID, $ArtistName) = G::$DB->next_record(MYSQLI_BOTH, false)) {
52
-                $Results[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
53
-                $New[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
54
-            }
55
-            G::$DB->set_query_id($QueryID);
56
-            foreach ($DBs as $GroupID) {
57
-                if (isset($New[$GroupID])) {
58
-                    G::$Cache->cache_value('groups_artists_'.$GroupID, $New[$GroupID]);
59
-                } else {
60
-                    G::$Cache->cache_value('groups_artists_'.$GroupID, []);
61
-                }
62
-            }
63
-            $Missing = array_diff($GroupIDs, array_keys($Results));
64
-            if (!empty($Missing)) {
65
-                $Results += array_fill_keys($Missing, []);
66
-            }
49
+      while (list($GroupID, $ArtistID, $ArtistName) = G::$DB->next_record(MYSQLI_BOTH, false)) {
50
+        $Results[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
51
+        $New[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
52
+      }
53
+      G::$DB->set_query_id($QueryID);
54
+      foreach ($DBs as $GroupID) {
55
+        if (isset($New[$GroupID])) {
56
+          G::$Cache->cache_value('groups_artists_'.$GroupID, $New[$GroupID]);
67
         }
57
         }
68
-        return $Results;
69
-    }
70
-
71
-
72
-    /**
73
-     * Convenience function for get_artists, when you just need one group.
74
-     *
75
-     * @param int $GroupID
76
-     * @return array - see get_artists
77
-     */
78
-    public static function get_artist($GroupID)
79
-    {
80
-        $Results = Artists::get_artists(array($GroupID));
81
-        return $Results[$GroupID];
82
-    }
83
-
84
-
85
-    /**
86
-     * Format an array of artists for display.
87
-     * TODO: Revisit the logic of this, see if we can helper-function the copypasta.
88
-     *
89
-     * @param array Artists an array of the form output by get_artists
90
-     * @param boolean $MakeLink if true, the artists will be links, if false, they will be text.
91
-     * @param boolean $IncludeHyphen if true, appends " - " to the end.
92
-     * @param $Escape if true, output will be escaped. Think carefully before setting it false.
93
-     */
94
-    public static function display_artists($Artists, $MakeLink = true, $IncludeHyphen = true, $Escape = true)
95
-    {
96
-        if (!empty($Artists)) {
97
-            $ampersand = ($Escape) ? ' &amp; ' : ' & ';
98
-            $link = '';
99
-
100
-            switch (count($Artists)) {
101
-                case 0:
102
-                    break;
103
-                case 3:
104
-                    $link .= Artists::display_artist($Artists[2], $MakeLink, $Escape). ", ";
105
-                    // no break
106
-                case 2:
107
-                    $link .= Artists::display_artist($Artists[1], $MakeLink, $Escape). ", ";
108
-                    // no break
109
-                case 1:
110
-                    $link .= Artists::display_artist($Artists[0], $MakeLink, $Escape).($IncludeHyphen?' – ':'');
111
-                    break;
112
-                default:
113
-                    $link = "Various".($IncludeHyphen?' – ':'');
114
-            }
115
-
116
-            return $link;
117
-        } else {
118
-            return '';
58
+        else {
59
+          G::$Cache->cache_value('groups_artists_'.$GroupID, []);
119
         }
60
         }
61
+      }
62
+      $Missing = array_diff($GroupIDs, array_keys($Results));
63
+      if (!empty($Missing)) {
64
+        $Results += array_fill_keys($Missing, []);
65
+      }
120
     }
66
     }
121
-
122
-
123
-    /**
124
-     * Formats a single artist name.
125
-     *
126
-     * @param array $Artist an array of the form ('id'=>ID, 'name'=>Name)
127
-     * @param boolean $MakeLink If true, links to the artist page.
128
-     * @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name.
129
-     * @return string Formatted artist name.
130
-     */
131
-    public static function display_artist($Artist, $MakeLink = true, $Escape = true)
132
-    {
133
-        if ($MakeLink && !$Escape) {
134
-            error('Invalid parameters to Artists::display_artist()');
135
-        } elseif ($MakeLink) {
136
-            return '<a href="artist.php?id='.$Artist['id'].'" dir="ltr">'.display_str($Artist['name']).'</a>';
137
-        } elseif ($Escape) {
138
-            return display_str($Artist['name']);
139
-        } else {
140
-            return $Artist['name'];
141
-        }
67
+    return $Results;
68
+  }
69
+
70
+
71
+  /**
72
+   * Convenience function for get_artists, when you just need one group.
73
+   *
74
+   * @param int $GroupID
75
+   * @return array - see get_artists
76
+   */
77
+  public static function get_artist($GroupID) {
78
+    $Results = Artists::get_artists(array($GroupID));
79
+    return $Results[$GroupID];
80
+  }
81
+
82
+
83
+  /**
84
+   * Format an array of artists for display.
85
+   * TODO: Revisit the logic of this, see if we can helper-function the copypasta.
86
+   *
87
+   * @param array Artists an array of the form output by get_artists
88
+   * @param boolean $MakeLink if true, the artists will be links, if false, they will be text.
89
+   * @param boolean $IncludeHyphen if true, appends " - " to the end.
90
+   * @param $Escape if true, output will be escaped. Think carefully before setting it false.
91
+   */
92
+  public static function display_artists($Artists, $MakeLink = true, $IncludeHyphen = true, $Escape = true) {
93
+    if (!empty($Artists)) {
94
+      $ampersand = ($Escape) ? ' &amp; ' : ' & ';
95
+      $link = '';
96
+
97
+      switch(count($Artists)) {
98
+        case 0:
99
+          break;
100
+        case 3:
101
+          $link .= Artists::display_artist($Artists[2], $MakeLink, $Escape). ", ";
102
+        case 2:
103
+          $link .= Artists::display_artist($Artists[1], $MakeLink, $Escape). ", ";
104
+        case 1:
105
+          $link .= Artists::display_artist($Artists[0], $MakeLink, $Escape).($IncludeHyphen?' – ':'');
106
+          break;
107
+        default:
108
+          $link = "Various".($IncludeHyphen?' – ':'');
109
+      }
110
+
111
+      return $link;
112
+    } else {
113
+      return '';
142
     }
114
     }
143
-
144
-    /**
145
-     * Deletes an artist and their requests, wiki, and tags.
146
-     * Does NOT delete their torrents.
147
-     *
148
-     * @param int $ArtistID
149
-     */
150
-    public static function delete_artist($ArtistID)
151
-    {
152
-        $QueryID = G::$DB->get_query_id();
153
-        G::$DB->query("
115
+  }
116
+
117
+
118
+  /**
119
+   * Formats a single artist name.
120
+   *
121
+   * @param array $Artist an array of the form ('id'=>ID, 'name'=>Name)
122
+   * @param boolean $MakeLink If true, links to the artist page.
123
+   * @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name.
124
+   * @return string Formatted artist name.
125
+   */
126
+  public static function display_artist($Artist, $MakeLink = true, $Escape = true) {
127
+    if ($MakeLink && !$Escape) {
128
+      error('Invalid parameters to Artists::display_artist()');
129
+    } elseif ($MakeLink) {
130
+      return '<a href="artist.php?id='.$Artist['id'].'" dir="ltr">'.display_str($Artist['name']).'</a>';
131
+    } elseif ($Escape) {
132
+      return display_str($Artist['name']);
133
+    } else {
134
+      return $Artist['name'];
135
+    }
136
+  }
137
+
138
+  /**
139
+   * Deletes an artist and their requests, wiki, and tags.
140
+   * Does NOT delete their torrents.
141
+   *
142
+   * @param int $ArtistID
143
+   */
144
+  public static function delete_artist($ArtistID) {
145
+    $QueryID = G::$DB->get_query_id();
146
+    G::$DB->query("
154
       SELECT Name
147
       SELECT Name
155
       FROM artists_group
148
       FROM artists_group
156
       WHERE ArtistID = ".$ArtistID);
149
       WHERE ArtistID = ".$ArtistID);
157
-        list($Name) = G::$DB->next_record(MYSQLI_NUM, false);
150
+    list($Name) = G::$DB->next_record(MYSQLI_NUM, false);
158
 
151
 
159
-        // Delete requests
160
-        G::$DB->query("
152
+    // Delete requests
153
+    G::$DB->query("
161
       SELECT RequestID
154
       SELECT RequestID
162
       FROM requests_artists
155
       FROM requests_artists
163
       WHERE ArtistID = $ArtistID
156
       WHERE ArtistID = $ArtistID
164
         AND ArtistID != 0");
157
         AND ArtistID != 0");
165
-        $Requests = G::$DB->to_array();
166
-        foreach ($Requests as $Request) {
167
-            list($RequestID) = $Request;
168
-            G::$DB->query('DELETE FROM requests WHERE ID='.$RequestID);
169
-            G::$DB->query('DELETE FROM requests_votes WHERE RequestID='.$RequestID);
170
-            G::$DB->query('DELETE FROM requests_tags WHERE RequestID='.$RequestID);
171
-            G::$DB->query('DELETE FROM requests_artists WHERE RequestID='.$RequestID);
172
-        }
173
-
174
-        // Delete artist
175
-        G::$DB->query('DELETE FROM artists_group WHERE ArtistID='.$ArtistID);
176
-        G::$Cache->decrement('stats_artist_count');
177
-
178
-        // Delete wiki revisions
179
-        G::$DB->query('DELETE FROM wiki_artists WHERE PageID='.$ArtistID);
158
+    $Requests = G::$DB->to_array();
159
+    foreach ($Requests AS $Request) {
160
+      list($RequestID) = $Request;
161
+      G::$DB->query('DELETE FROM requests WHERE ID='.$RequestID);
162
+      G::$DB->query('DELETE FROM requests_votes WHERE RequestID='.$RequestID);
163
+      G::$DB->query('DELETE FROM requests_tags WHERE RequestID='.$RequestID);
164
+      G::$DB->query('DELETE FROM requests_artists WHERE RequestID='.$RequestID);
165
+    }
180
 
166
 
181
-        // Delete tags
182
-        G::$DB->query('DELETE FROM artists_tags WHERE ArtistID='.$ArtistID);
167
+    // Delete artist
168
+    G::$DB->query('DELETE FROM artists_group WHERE ArtistID='.$ArtistID);
169
+    G::$Cache->decrement('stats_artist_count');
183
 
170
 
184
-        // Delete artist comments, subscriptions and quote notifications
185
-        Comments::delete_page('artist', $ArtistID);
171
+    // Delete wiki revisions
172
+    G::$DB->query('DELETE FROM wiki_artists WHERE PageID='.$ArtistID);
186
 
173
 
187
-        G::$Cache->delete_value('artist_'.$ArtistID);
188
-        G::$Cache->delete_value('artist_groups_'.$ArtistID);
189
-        // Record in log
174
+    // Delete tags
175
+    G::$DB->query('DELETE FROM artists_tags WHERE ArtistID='.$ArtistID);
190
 
176
 
191
-        if (!empty(G::$LoggedUser['Username'])) {
192
-            $Username = G::$LoggedUser['Username'];
193
-        } else {
194
-            $Username = 'System';
195
-        }
196
-        Misc::write_log("Artist $ArtistID ($Name) was deleted by $Username");
197
-        G::$DB->set_query_id($QueryID);
198
-    }
177
+    // Delete artist comments, subscriptions and quote notifications
178
+    Comments::delete_page('artist', $ArtistID);
199
 
179
 
180
+    G::$Cache->delete_value('artist_'.$ArtistID);
181
+    G::$Cache->delete_value('artist_groups_'.$ArtistID);
182
+    // Record in log
200
 
183
 
201
-    /**
202
-     * Remove LRM (left-right-marker) and trims, because people copypaste carelessly.
203
-     * If we don't do this, we get seemingly duplicate artist names.
204
-     * TODO: make stricter, e.g. on all whitespace characters or Unicode normalisation
205
-     *
206
-     * @param string $ArtistName
207
-     */
208
-    public static function normalise_artist_name($ArtistName)
209
-    {
210
-        // \u200e is &lrm;
211
-        $ArtistName = trim($ArtistName);
212
-        $ArtistName = preg_replace('/^(\xE2\x80\x8E)+/', '', $ArtistName);
213
-        $ArtistName = preg_replace('/(\xE2\x80\x8E)+$/', '', $ArtistName);
214
-        return trim(preg_replace('/ +/', ' ', $ArtistName));
184
+    if (!empty(G::$LoggedUser['Username'])) {
185
+      $Username = G::$LoggedUser['Username'];
186
+    } else {
187
+      $Username = 'System';
215
     }
188
     }
189
+    Misc::write_log("Artist $ArtistID ($Name) was deleted by $Username");
190
+    G::$DB->set_query_id($QueryID);
191
+  }
192
+
193
+
194
+  /**
195
+   * Remove LRM (left-right-marker) and trims, because people copypaste carelessly.
196
+   * If we don't do this, we get seemingly duplicate artist names.
197
+   * TODO: make stricter, e.g. on all whitespace characters or Unicode normalisation
198
+   *
199
+   * @param string $ArtistName
200
+   */
201
+  public static function normalise_artist_name($ArtistName) {
202
+    // \u200e is &lrm;
203
+    $ArtistName = trim($ArtistName);
204
+    $ArtistName = preg_replace('/^(\xE2\x80\x8E)+/', '', $ArtistName);
205
+    $ArtistName = preg_replace('/(\xE2\x80\x8E)+$/', '', $ArtistName);
206
+    return trim(preg_replace('/ +/', ' ', $ArtistName));
207
+  }
216
 }
208
 }
209
+?>

Loading…
Cancel
Save