-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API CRUD support, bearer token permissions, and OpenAPI docs #78
Conversation
…roundwork for good api crud support
app/Artists.php
Outdated
$Results = Artists::get_artists(array($GroupID)); | ||
return $Results[$GroupID]; | ||
} | ||
|
||
/** | ||
* Format an array of artists for display. | ||
* todo: Revisit the logic of this, see if we can helper-function the copypasta. | ||
* | ||
* @param array Artists an array of the form output by get_artists | ||
* @param boolean $MakeLink if true, the artists will be links, if false, they will be text. | ||
* @param boolean $IncludeHyphen FEATURE REMOVED, ARGUMENT KEPT FOR COMPATIBILITY | ||
* @param $Escape if true, output will be escaped. Think carefully before setting it false. | ||
*/ | ||
public static function display_artists($creators, $MakeLink = true, $IncludeHyphen = true, $Escape = true) | ||
{ | ||
if (!empty($creators)) { | ||
$ampersand = ($Escape) ? ' & ' : ' & '; | ||
$link = ''; | ||
|
||
switch (count($creators)) { | ||
case 0: | ||
break; | ||
|
||
case 4: | ||
$link .= Artists::display_artist($creators[2], $MakeLink, $Escape). ", "; | ||
// no break | ||
|
||
case 3: | ||
$link .= Artists::display_artist($creators[2], $MakeLink, $Escape). ", "; | ||
// no break | ||
|
||
case 2: | ||
$link .= Artists::display_artist($creators[1], $MakeLink, $Escape). ", "; | ||
// no break | ||
|
||
case 1: | ||
$link .= Artists::display_artist($creators[0], $MakeLink, $Escape); | ||
break; | ||
|
||
default: | ||
$link = Artists::display_artist($creators[0], $MakeLink, $Escape).' et al.'; | ||
} | ||
|
||
return $link; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* Formats a single artist name. | ||
* | ||
* @param array $creator an array of the form ('id' => ID, 'name' => Name) | ||
* @param boolean $MakeLink If true, links to the artist page. | ||
* @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name. | ||
* @return string Formatted artist name. | ||
*/ | ||
public static function display_artist($creator, $MakeLink = true, $Escape = true) | ||
{ | ||
if ($MakeLink && !$Escape) { | ||
error('Invalid parameters to Artists::display_artist()'); | ||
} elseif ($MakeLink) { | ||
return '<a href="/artist.php?id='.$creator['id'].'">'.\Gazelle\Text::esc($creator['name']).'</a>'; | ||
} elseif ($Escape) { | ||
return \Gazelle\Text::esc($creator['name']); | ||
} else { | ||
return $creator['name']; | ||
} | ||
} | ||
|
||
/** | ||
* Deletes an artist and their requests, wiki, and tags. | ||
* Does NOT delete their torrents. | ||
* | ||
* @param int $creatorID | ||
*/ | ||
public static function delete_artist($creatorID) | ||
{ | ||
$app = \Gazelle\App::go(); | ||
while (list($GroupID, $creatorID, $creatorName) = $app->dbOld->next_record(MYSQLI_BOTH, false)) { | ||
$Results[$GroupID][] = array('id' => $creatorID, 'name' => $creatorName); | ||
$New[$GroupID][] = array('id' => $creatorID, 'name' => $creatorName); | ||
} | ||
|
||
$app->dbOld->set_query_id($QueryID); | ||
foreach ($dbs as $GroupID) { | ||
if (isset($New[$GroupID])) { | ||
$app->cache->set("groups_artists_$GroupID", $New[$GroupID]); | ||
} else { | ||
$app->cache->set("groups_artists_$GroupID", []); | ||
} | ||
} | ||
|
||
$Missing = array_diff($GroupIDs, array_keys($Results)); | ||
if (!empty($Missing)) { | ||
$Results += array_fill_keys($Missing, []); | ||
} | ||
} | ||
return $Results; | ||
} |
Check warning
Code scanning / PHPMD
Code Size Rules: CyclomaticComplexity Warning
app/Artists.php
Outdated
`RequestID` = '$RequestID' | ||
"); | ||
|
||
$app->dbOld->prepared_query(" | ||
$app->dbOld->prepared_query(" | ||
DELETE | ||
FROM | ||
`requests_tags` | ||
WHERE | ||
`RequestID` = '$RequestID' | ||
"); | ||
|
||
$app->dbOld->prepared_query(" | ||
$app->dbOld->prepared_query(" | ||
DELETE | ||
FROM | ||
`requests_artists` | ||
WHERE | ||
`RequestID` = '$RequestID' | ||
"); | ||
} | ||
} | ||
|
||
// Delete artist | ||
$app->dbOld->prepared_query(" | ||
// Delete artist | ||
$app->dbOld->prepared_query(" | ||
DELETE | ||
FROM | ||
`artists_group` | ||
WHERE | ||
`ArtistID` = '$creatorID' | ||
"); | ||
$app->cache->decrement('stats_artist_count'); | ||
$app->cache->decrement('stats_artist_count'); | ||
|
||
// Delete wiki revisions | ||
$app->dbOld->prepared_query(" | ||
// Delete wiki revisions | ||
$app->dbOld->prepared_query(" | ||
DELETE | ||
FROM | ||
`wiki_artists` | ||
WHERE | ||
`PageID` = '$creatorID' | ||
"); | ||
|
||
// Delete tags | ||
$app->dbOld->prepared_query(" | ||
// Delete tags | ||
$app->dbOld->prepared_query(" | ||
DELETE | ||
FROM | ||
`artists_tags` | ||
WHERE | ||
`ArtistID` = '$creatorID' | ||
"); | ||
|
||
// Delete artist comments, subscriptions and quote notifications | ||
Comments::delete_page('artist', $creatorID); | ||
$app->cache->delete("artist_$creatorID"); | ||
$app->cache->delete("artist_groups_$creatorID"); | ||
|
||
// Record in log | ||
if (!empty($app->user->core['username'])) { | ||
$Username = $app->user->core['username']; | ||
} else { | ||
$Username = 'System'; | ||
} | ||
|
||
Misc::write_log("Artist $creatorID ($Name) was deleted by $Username"); | ||
$app->dbOld->set_query_id($QueryID); | ||
} | ||
|
||
/** | ||
* Remove LRM (left-right-marker) and trims, because people copypaste carelessly. | ||
* If we don't do this, we get seemingly duplicate artist names. | ||
* todo: make stricter, e.g., on all whitespace characters or Unicode normalisation | ||
* | ||
* @param string $creatorName | ||
*/ | ||
public static function normalise_artist_name($creatorName) | ||
{ | ||
# \u200e is ‎ | ||
$creatorName = trim($creatorName); | ||
// Delete artist comments, subscriptions and quote notifications | ||
Comments::delete_page('artist', $creatorID); | ||
$app->cache->delete("artist_$creatorID"); | ||
$app->cache->delete("artist_groups_$creatorID"); | ||
|
||
// Record in log | ||
if (!empty($app->user->core['username'])) { | ||
$Username = $app->user->core['username']; | ||
} else { | ||
$Username = 'System'; | ||
} | ||
|
||
Misc::write_log("Artist $creatorID ($Name) was deleted by $Username"); | ||
$app->dbOld->set_query_id($QueryID); | ||
} |
Check warning
Code scanning / PHPMD
Code Size Rules: ExcessiveMethodLength Warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PHPMD found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
Docs are being fleshed out at Stoplight. As usual, it's a bit of wishful thinking, but there's a good scaffold in place to get at least the simpler objects in place. Bearer token permissions are all set.