Moved log infos to context, fix #4
This commit is contained in:
parent
769b9b4ccd
commit
70776ac21f
20
phpcs.xml
Normal file
20
phpcs.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<ruleset name="PHP_CodeSniffer">
|
||||
|
||||
<description>PHPCS configuration file.</description>
|
||||
<!-- check all files in the src directory, feel free to add more files with:
|
||||
-->
|
||||
<file>.</file>
|
||||
<file>src</file>
|
||||
|
||||
<!-- exclude our migrations directory from the violation check-->
|
||||
<exclude-pattern>scripts/*</exclude-pattern>
|
||||
|
||||
<!-- Our base rule: set to PSR12-->
|
||||
<rule ref="PSR12"/>
|
||||
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired">
|
||||
<severity>1</severity>
|
||||
</rule>
|
||||
<rule ref="Zend.Files.ClosingTag"/>
|
||||
</ruleset>
|
@ -5,4 +5,4 @@ require __DIR__ . '/vendor/autoload.php';
|
||||
$config = include __DIR__ . '/config.php';
|
||||
$internal_pics_dir = realpath(__DIR__ . $config['pics_dir']);
|
||||
$app = new Phlaym\Roastmonday\Roastmonday($config, $internal_pics_dir);
|
||||
$app->resetOriginalAvatars();
|
||||
$app->resetOriginalAvatars();
|
||||
|
@ -77,7 +77,7 @@ class Roastmonday extends APnutI
|
||||
$this->logger->info("Downloading picture", ['url' => $link]);
|
||||
$img_header = get_headers($link, 1);
|
||||
if (strpos($img_header[0], "200") === false && strpos($img_header[0], "OK")) {
|
||||
$this->logger->error("Error fetching avatar header: " . json_encode($img_header));
|
||||
$this->logger->error("Error fetching avatar header", ['header' => $img_header]);
|
||||
return null;
|
||||
}
|
||||
$ext = $this->getExtension($img_header['Content-Type']);
|
||||
@ -131,7 +131,7 @@ class Roastmonday extends APnutI
|
||||
try {
|
||||
$past_themes_list = $past_themes_element->parentNode->nextSibling->nextSibling;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error parsing wiki: ' . $e->getMessage());
|
||||
$this->logger->error('Error parsing wiki', ['exception' => $e->getMessage()]);
|
||||
return [];
|
||||
}
|
||||
foreach ($past_themes_list->childNodes as $child) {
|
||||
@ -151,15 +151,14 @@ class Roastmonday extends APnutI
|
||||
}
|
||||
}
|
||||
if ($tag !== null && $date !== null) {
|
||||
$tag = trim($tag);
|
||||
$this->logger->info(
|
||||
'Found ThemeMonday in wiki: '
|
||||
. trim($tag)
|
||||
. ' on '
|
||||
. $date->format('Y-m-d')
|
||||
'Found ThemeMonday in wiki',
|
||||
['tag' => $tag, 'date' => $date]
|
||||
);
|
||||
$m[] = [
|
||||
'date' => new \DateTime($date->format('Y-m-d')),
|
||||
'tag' => trim($tag)
|
||||
'tag' => $tag
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -171,7 +170,6 @@ class Roastmonday extends APnutI
|
||||
public function getThemeMonday($id)
|
||||
{
|
||||
$t = $this->db->getTheme($id);
|
||||
$this->logger->debug('Theme ' . $id . ': ' . json_encode($t));
|
||||
return $t;
|
||||
}
|
||||
|
||||
@ -183,33 +181,32 @@ class Roastmonday extends APnutI
|
||||
try {
|
||||
$p = $this->getPollsFromUser(616);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error reading @ThemeMonday polls: ' . $e->getMessage());
|
||||
$this->logger->error('Error reading @ThemeMonday polls', ['exception' => $e->getMessage()]);
|
||||
return [];
|
||||
}
|
||||
|
||||
$p = array_filter($p, function ($e) {
|
||||
return stripos($e->prompt, '#thememonday') !== false;
|
||||
});
|
||||
$this->logger->info('Found ' . count($p) . ' polls');
|
||||
$this->logger->info('Found polls count', ['count' => count($p)]);
|
||||
if (count($p) === 0) {
|
||||
return [];
|
||||
}
|
||||
foreach ($p as $poll) {
|
||||
$tag = $poll->getMostVotedOption();
|
||||
if (count($tag) !== 1) {
|
||||
$this->logger->info('Skipping ' . implode(', ', $tag) . ', because it was a tie');
|
||||
$this->logger->info('Skipping tag, because it was a tie', ['tag' => $tag]);
|
||||
continue;
|
||||
}
|
||||
$date = Roastmonday::getMondayAfterPoll($poll);
|
||||
$tagtext = $tag[0]->text;
|
||||
$this->logger->info(
|
||||
'Found ThemeMonday: '
|
||||
. $tag[0]->text
|
||||
. ' on '
|
||||
. $date->format('Y-m-d')
|
||||
'Found ThemeMonday',
|
||||
['tag' => $tagtext, 'date' => $date]
|
||||
);
|
||||
$m[] = [
|
||||
'date' => new \DateTime($date->format('Y-m-d')),
|
||||
'tag' => $tag[0]->text
|
||||
'tag' => $tagtext
|
||||
];
|
||||
}
|
||||
return $m;
|
||||
@ -224,13 +221,11 @@ class Roastmonday extends APnutI
|
||||
|
||||
protected function savePictureForPost($post, $theme_id)
|
||||
{
|
||||
$this->logger->info('Checking post ' . $post->id . ' for theme ' . $theme_id);
|
||||
$this->logger->info('Checking post for theme', ['post' => $post->id, 'theme' => $theme_id]);
|
||||
if (!empty($post->user) && !empty($post->user->avatar_image)) {
|
||||
$this->logger->info(
|
||||
'Found new avatar on post '
|
||||
. $post->id
|
||||
. ' by @'
|
||||
. $post->user->username
|
||||
'Found new avatar on post',
|
||||
['post' => $post->id, 'user' => $post->user->username]
|
||||
);
|
||||
$filename = $this->downloadPicture($post->user->avatar_image->link, $theme_id);
|
||||
if ($filename !== null) {
|
||||
@ -247,11 +242,8 @@ class Roastmonday extends APnutI
|
||||
}
|
||||
}
|
||||
$this->logger->info(
|
||||
'Saving avatar by '
|
||||
. $post->user->username
|
||||
. ' for '
|
||||
. $theme_id
|
||||
. ' to database'
|
||||
'Saving avatar to database',
|
||||
['theme' => $theme_id, 'user' => $post->user->username]
|
||||
);
|
||||
try {
|
||||
$status = $this->db->saveAvatar(
|
||||
@ -274,16 +266,20 @@ class Roastmonday extends APnutI
|
||||
$this->logger->info('Successfully inserted avatar');
|
||||
break;
|
||||
default:
|
||||
$this->logger->info('Unknown return code ' . $status);
|
||||
$this->logger->info('Unknown return code', ['status' => $status]);
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error inserting avatar into database: ' . $e->getMessage());
|
||||
$this->logger->error(
|
||||
'Error inserting avatar into database',
|
||||
['exception' => $e->getMessage()]
|
||||
);
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
$this->logger->warning(
|
||||
"Cannot save picture for post {$post->id}: Doesn't have a user or user doesn't have an avatar"
|
||||
"Cannot save picture for post. Doesn't have a user or user doesn't have an avatar",
|
||||
['post' => $post->id]
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -293,7 +289,7 @@ class Roastmonday extends APnutI
|
||||
$post = parent::getPost($post_id, $args);
|
||||
if (array_key_exists('avatarWidth', $args)) {
|
||||
$a = parent::getAvatar($post->user->id, ['w' => $args['avatarWidth']]);
|
||||
$this->logger->debug("Resized avatar: {$a}");
|
||||
$this->logger->debug('Resized avatar', ['avatar' => $a]);
|
||||
$post->user->avatar_image->link = $a;
|
||||
}
|
||||
return $post;
|
||||
@ -322,20 +318,19 @@ class Roastmonday extends APnutI
|
||||
public function getThemeMondays()
|
||||
{
|
||||
$themes = $this->db->listThemes();
|
||||
$this->logger->debug('Themes: ' . json_encode($themes));
|
||||
$this->logger->debug('Themes', ['themes' => $themes]);
|
||||
return $themes;
|
||||
}
|
||||
|
||||
public function getPicturesForTheme($id)
|
||||
{
|
||||
$this->logger->debug("Avatars for theme {$id}: ");
|
||||
$this->logger->debug('Avatars for theme', ['theme' => $id]);
|
||||
$pics_folder = $this->pics_root . $id . '/';
|
||||
$pics = [];
|
||||
if (!is_dir($pics_folder)) {
|
||||
$this->logger->warning($pics_folder . ' ist not a directory');
|
||||
$this->logger->warning('Pics folder ist not a directory', ['path' => $pics_folder]);
|
||||
return [];
|
||||
}
|
||||
$this->logger->debug("DB: " . json_encode($this->db));
|
||||
$avatars = $this->db->getAvatarsForTheme($id);
|
||||
foreach ($avatars as $avatar) {
|
||||
$avatar['file'] = $pics_folder . $avatar['file'];
|
||||
@ -360,7 +355,7 @@ class Roastmonday extends APnutI
|
||||
}
|
||||
return $a['date'] > $b['date'] ? -1 : 1;
|
||||
});
|
||||
$this->logger->info('Found theme mondays', $m);
|
||||
$this->logger->info('Found theme mondays', ['mondays' => $m]);
|
||||
return $m;
|
||||
}
|
||||
|
||||
@ -369,7 +364,7 @@ class Roastmonday extends APnutI
|
||||
$tag = preg_replace('/^#/', '', $theme['tag']);
|
||||
$tag = preg_replace('/ .*$/', '', $tag);
|
||||
$posts = [];
|
||||
$this->logger->info('Searching pictures for: ' . $tag);
|
||||
$this->logger->info('Searching pictures for', ['tag' => $tag]);
|
||||
foreach (Roastmonday::$new_avatar_keywords as $keyword) {
|
||||
$query = [
|
||||
'tags' => $tag,
|
||||
@ -380,7 +375,7 @@ class Roastmonday extends APnutI
|
||||
'include_html' => false,
|
||||
];
|
||||
$p = $this->searchPosts($query);
|
||||
$this->logger->info('Found ' . count($p) . ' posts');
|
||||
$this->logger->info('Found posts', ['count' => count($p)]);
|
||||
foreach ($p as $post) {
|
||||
$this->savePictureForPost($post, $theme['id']);
|
||||
if (!in_array($post, $posts)) {
|
||||
@ -394,7 +389,7 @@ class Roastmonday extends APnutI
|
||||
{
|
||||
$tag = preg_replace('/^#/', '', $tag);
|
||||
$tag = preg_replace('/ .*$/', '', $tag);
|
||||
$this->logger->info('Adding: ' . $tag . ' to database');
|
||||
$this->logger->info('Adding: tag to database', ['tag' => $tag]);
|
||||
$id = $this->db->addTheme($tag, $date->format('Y-m-d'));
|
||||
if ($id !== -1) {
|
||||
$pics_folder = $this->pics_root . $id . '/';
|
||||
@ -413,7 +408,7 @@ class Roastmonday extends APnutI
|
||||
$posttext = null,
|
||||
$overwrite_if_exist = false
|
||||
) {
|
||||
$this->logger->info("Adding temporary avatar to theme {$theme} for {$avatar_duration} days");
|
||||
$this->logger->info('Adding temporary avatar to theme', ['theme' => $theme, 'duration' => $avatar_duration]);
|
||||
|
||||
switch ($avatar['error']) {
|
||||
case UPLOAD_ERR_OK:
|
||||
@ -429,9 +424,7 @@ class Roastmonday extends APnutI
|
||||
|
||||
#1. Save current user avatar
|
||||
$a = self::getAvatar('me');
|
||||
$this->logger->info("Current avatar: {$a}");
|
||||
#$a = explode('/', $a);
|
||||
#$filename = explode('?', end($a))[0];
|
||||
$this->logger->info('Current avatar', ['avatar' => $a]);
|
||||
$original_avatar = $this->downloadPicture($a, $this->temp_pics_root);
|
||||
if (empty($original_avatar)) {
|
||||
return ['error' => ['message' => 'Could not download original avatar']];
|
||||
@ -444,7 +437,7 @@ class Roastmonday extends APnutI
|
||||
if (empty($current_user)) {
|
||||
return ['error' => ['message' => 'Could not fetch the authorized user']];
|
||||
}
|
||||
$this->logger->info("Current user: @{$current_user->username} ({$current_user->id})");
|
||||
$this->logger->info('Current user', ['username' => $current_user->username, 'id' => $current_user->id]);
|
||||
$avatar_duration = min($avatar_duration, 1);
|
||||
$d = new \DateTime();
|
||||
$d->add(new \DateInterval("P{$avatar_duration}D"));
|
||||
@ -510,8 +503,7 @@ class Roastmonday extends APnutI
|
||||
try {
|
||||
$post = $this->createPost($posttext, is_nsfw: false, auto_crop: true);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
$this->logger->error($e->getTraceAsString());
|
||||
$this->logger->error('Could not create post', ['exception' => $e]);
|
||||
return [
|
||||
'success' => true,
|
||||
'warn' => 'Your avatar has been updated, but an error occured creating the post:<br />'
|
||||
@ -526,8 +518,7 @@ class Roastmonday extends APnutI
|
||||
$response = $this->manuallyAddAvatar($theme, $post->id, null, overwrite_if_exist: $overwrite_if_exist);
|
||||
$this->logger->info('Successfully added to gallery');
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
$this->logger->error($e->getTraceAsString());
|
||||
$this->logger->error('Could not authenticateServerToken', ['exception' => $e]);
|
||||
return [
|
||||
'success' => true,
|
||||
'postID' => $post->id,
|
||||
@ -554,7 +545,10 @@ class Roastmonday extends APnutI
|
||||
$user = $post->user;
|
||||
$date = $post->created_at;
|
||||
if (empty($user)) {
|
||||
$this->logger->error("Error fetching avatar from post #{$post_id}. Post has no creator.");
|
||||
$this->logger->error(
|
||||
'Error fetching avatar from post. Post has no creator.',
|
||||
['post_id' => $post_id]
|
||||
);
|
||||
$resp['error'] = [
|
||||
'code' => static::$ERROR_FETCHING_CREATOR_FIELD,
|
||||
'message' => 'Error fetching avatar. Post has no creator.'
|
||||
@ -563,7 +557,10 @@ class Roastmonday extends APnutI
|
||||
}
|
||||
}
|
||||
if (empty($user->avatar_image) || empty($user->avatar_image->link)) {
|
||||
$this->logger->error("Error fetching avatar from post #{$post_id}. Post creator has no avatar.");
|
||||
$this->logger->error(
|
||||
'Error fetching avatar from post. Post creator has no avatar.',
|
||||
['post_id' => $post_id]
|
||||
);
|
||||
$resp['error'] = [
|
||||
'code' => static::$ERROR_FETCHING_CREATOR_FIELD,
|
||||
'message' => 'Error fetching avatar. Post creator has no avatar.'
|
||||
@ -571,9 +568,9 @@ class Roastmonday extends APnutI
|
||||
return $resp;
|
||||
}
|
||||
$astr = print_r($avatar, true);
|
||||
$this->logger->debug("Manually adding avatar: {$astr}");
|
||||
$this->logger->debug('Manually adding avatar', ['avatar' => $astr]);
|
||||
if (!empty($avatar) && (empty($avatar['error']) || $avatar['error'] === 0)) {
|
||||
$this->logger->info("Manually added post #{$post_id} has an avatar attached.");
|
||||
$this->logger->info('Manually added post has an avatar attached', ['post_id' => $post_id]);
|
||||
$ext = $this->getExtension($avatar['type']);
|
||||
$target_file_name_without_theme = time() . '_' . $post_id . '.' . $ext;
|
||||
$target_file_name = $theme . '/' . $target_file_name_without_theme;
|
||||
@ -581,14 +578,17 @@ class Roastmonday extends APnutI
|
||||
if (move_uploaded_file($avatar["tmp_name"], $target_file)) {
|
||||
$resp['img'] = 'pics/' . $target_file_name;
|
||||
} else {
|
||||
$this->logger->error("Error saving uploaded avatar from post #{$post_id}. Post has no creator.");
|
||||
$this->logger->error(
|
||||
'Error saving uploaded avatar from post. Post has no creator',
|
||||
['post_id' => $post_id]
|
||||
);
|
||||
$resp['error'] = [
|
||||
'code' => static::$ERROR_UNKNOWN_SERVER_ERROR,
|
||||
'message' => 'Uploaded file could not be moved'
|
||||
];
|
||||
return $resp;
|
||||
}
|
||||
$this->logger->info("Saved manually uploaded file to " . $target_file . ". Adding to database");
|
||||
$this->logger->info('Saved manually uploaded file. Adding to database', ['path' => $target_file]);
|
||||
$realname = null;
|
||||
$username = '';
|
||||
if (isset($user)) {
|
||||
@ -615,7 +615,8 @@ class Roastmonday extends APnutI
|
||||
);
|
||||
} else {
|
||||
$this->logger->info(
|
||||
"Manually added post #{$post_id} doesn't have an avatar attached. fetching from user object."
|
||||
"Manually added post doesn't have an avatar attached. fetching from user object",
|
||||
['post_id' => $post_id]
|
||||
);
|
||||
$filename = $this->savePictureForPost($post, $theme);
|
||||
if (empty($filename)) {
|
||||
@ -627,8 +628,6 @@ class Roastmonday extends APnutI
|
||||
return $resp;
|
||||
}
|
||||
$resp['img'] = 'pics/' . $theme . '/' . $filename;
|
||||
//protected function savePicture($avatar, $theme_id, $filename) {
|
||||
//$avatar = file_get_contents($this->pics_root.$theme.'/'.$filename);
|
||||
$this->logger->info("Saved downloaded avatar to {$resp['img']}");
|
||||
}
|
||||
$resp['presence'] = -1;
|
||||
@ -644,15 +643,6 @@ class Roastmonday extends APnutI
|
||||
$resp['user'] = '@' . $username;
|
||||
|
||||
$text = "";
|
||||
/*
|
||||
if (isset($post->content)) {
|
||||
if (isset($post->content->html)) {
|
||||
$text = $post->content->html;
|
||||
} elseif (isset($post->content->text)) {
|
||||
$text = $post->content->text;
|
||||
}
|
||||
}
|
||||
*/
|
||||
$text = empty($post) ? '' : $post->getText();
|
||||
$resp['success'] = true;
|
||||
$resp['realname'] = $realname;
|
||||
@ -660,19 +650,22 @@ class Roastmonday extends APnutI
|
||||
$resp['postid'] = $post_id;
|
||||
$resp['timestamp'] = $date->getTimestamp();
|
||||
$this->logger->info(
|
||||
"Successfully added manually uploaded avatar for user to theme",
|
||||
'Successfully added manually uploaded avatar for user to theme',
|
||||
['user' => $resp['user'], 'theme' => $theme]
|
||||
);
|
||||
return $resp;
|
||||
} catch (NotFoundException $nfe) {
|
||||
$this->logger->error(
|
||||
"Error adding manually uploaded avatar for theme. Post could not be found",
|
||||
'Error adding manually uploaded avatar for theme. Post could not be found',
|
||||
['post_id' => $post_id, 'theme' => $theme, 'exception' => $nfe]
|
||||
);
|
||||
$resp['error'] = ['code' => static::$ERROR_NOT_FOUND, 'message' => 'Post could not be found'];
|
||||
return $resp;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error("Error adding manually uploaded avatar for theme {$theme}: " . $e->getMessage());
|
||||
$this->logger->error(
|
||||
'Error adding manually uploaded avatar for theme',
|
||||
['theme' => $theme, 'exception' => $e]
|
||||
);
|
||||
$resp['error'] = ['code' => static::$ERROR_UNKNOWN_SERVER_ERROR, 'message' => $e->getMessage()];
|
||||
return $resp;
|
||||
}
|
||||
@ -682,38 +675,52 @@ class Roastmonday extends APnutI
|
||||
{
|
||||
$this->logger->info('Get outdated avatars');
|
||||
$to_reset = $this->db->getOutdatedThemeAvatars();
|
||||
$this->logger->info('Found ' . count($to_reset) . ' outdated avatars');
|
||||
$this->logger->info('Found outdated avatars', ['count' => count($to_reset)]);
|
||||
foreach ($to_reset as $value) {
|
||||
$user_id = $value['user_id'];
|
||||
$p = $this->pics_root . $this->temp_pics_root . $value['original_avatar'];
|
||||
$avatar_path = realpath($p);
|
||||
if ($avatar_path === false) {
|
||||
$this->logger->error("Avatar path '{$p}' for user {$user_id} does not exist!");
|
||||
$this->logger->error(
|
||||
'Avatar path for user does not exist!',
|
||||
['path' => $p, 'user_id' => $user_id]
|
||||
);
|
||||
}
|
||||
$this->logger->info('Resetting avatar for user ' . $user_id . ' to original at: ' . $avatar_path);
|
||||
$this->logger->info(
|
||||
'Resetting avatar for user to original',
|
||||
['path' => $avatar_path, 'user_id' => $user_id]
|
||||
);
|
||||
$this->access_token = $value['auth_token'];
|
||||
try {
|
||||
$this->updateAvatar($avatar_path);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error resetting user avatar: ' . $e->getMessage());
|
||||
$this->logger->error($e->getTraceAsString());
|
||||
$this->logger->error('Error resetting user avatar', ['exception' => $e]);
|
||||
return;
|
||||
}
|
||||
$this->logger->info('Resetted avatar for user ' . $user_id . ' to original at: ' . $avatar_path);
|
||||
$this->logger->info('Deleting avatar for user ' . $user_id . ' at: ' . $avatar_path);
|
||||
$this->logger->info(
|
||||
'Resetted avatar for user to original',
|
||||
['path' => $avatar_path, 'user_id' => $user_id]
|
||||
);
|
||||
$this->logger->info(
|
||||
'Deleting original avatar for user',
|
||||
['path' => $avatar_path, 'user_id' => $user_id]
|
||||
);
|
||||
$res = unlink($avatar_path);
|
||||
if (!$res) {
|
||||
$this->logger->error('Failed to delete avatar at: ' . $avatar_path);
|
||||
$this->logger->error('Failed to delete avatar at', ['path' => $avatar_path]);
|
||||
return;
|
||||
}
|
||||
$this->logger->info('Deleted avatar for user ' . $user_id . ' at: ' . $avatar_path);
|
||||
$this->logger->info(
|
||||
'Deleted original avatar for user',
|
||||
['path' => $avatar_path, 'user_id' => $user_id]
|
||||
);
|
||||
try {
|
||||
$this->db->removeOutdatedThemeAvatars($user_id);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error resetting user avatar', ['exception' => $e]);
|
||||
return;
|
||||
}
|
||||
$this->logger->info('Avatar for user ' . $user_id . ' is back to its original value!');
|
||||
$this->logger->info('Avatar for user is back to its original value!', ['user_id' => $user_id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user