diff --git a/.gitignore b/.gitignore index dbef442..5e65c2f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ cluebot-ng.config.php # Temp stuff vendor titles.txt +.phplint-cache diff --git a/feed_functions.php b/feed_functions.php index ac01183..31a52de 100644 --- a/feed_functions.php +++ b/feed_functions.php @@ -104,7 +104,7 @@ private static function loop($line) Globals::$optin ) ) { - self::bail($data, 'Outside of valid namespaces'); + $logger->addInfo('Skipping due to namespace: ' . $message); return; } $logger->addInfo('Processing: ' . $message); diff --git a/includes.php b/includes.php index abe2a43..23bada8 100644 --- a/includes.php +++ b/includes.php @@ -35,9 +35,6 @@ ) ); -// Stick with warnings in php 8 (at least for now) -mysqli_report(MYSQLI_REPORT_OFF); - require_once 'cluebot-ng.config.php'; require_once 'action_functions.php'; require_once 'cbng.php'; diff --git a/mysql_functions.php b/mysql_functions.php index 743aa30..ba149dc 100644 --- a/mysql_functions.php +++ b/mysql_functions.php @@ -2,6 +2,8 @@ namespace CluebotNG; +use mysqli_sql_exception; + /* * Copyright (C) 2015 Jacobi Carter and Chris Breneman * @@ -22,7 +24,11 @@ */ function is_mysql_alive($con) { - return @mysqli_query($con, 'SELECT LAST_INSERT_ID()'); + try { + return @mysqli_query($con, 'SELECT LAST_INSERT_ID()'); + } catch (mysqli_sql_exception $e) { + return false; + } } function checkMySQL() @@ -86,159 +92,258 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '') 'user_edit_count' => false, 'user_distinct_pages' => false, ); - $res = mysqli_query( - $mw_mysql, - 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT `rev_timestamp`, `actor_name` FROM `page`' . - ' JOIN `revision` ON `rev_page` = `page_id`' . - ' JOIN `actor` ON `actor_id` = `rev_actor`' . - ' WHERE `page_namespace` = "' . - mysqli_real_escape_string($mw_mysql, $nsid) . - '" AND `page_title` = "' . - mysqli_real_escape_string($mw_mysql, $title) . - '" ORDER BY `rev_id` LIMIT 1' - ); - if ($res === false) { - $logger->addWarning("page metadata query returned no data for " . $title . - " (" . $nsid . "): " . mysqli_error($mw_mysql)); - } else { - $d = mysqli_fetch_assoc($res); - $data['common']['page_made_time'] = $d['rev_timestamp']; - $data['common']['creator'] = $d['actor_name']; - } - $res = mysqli_query( - $mw_mysql, - 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT COUNT(*) as count FROM `page`' . - ' JOIN `revision` ON `rev_page` = `page_id`' . - ' WHERE `page_namespace` = "' . - mysqli_real_escape_string($mw_mysql, $nsid) . - '" AND `page_title` = "' . - mysqli_real_escape_string($mw_mysql, $title) . - '" AND `rev_timestamp` > "' . - mysqli_real_escape_string($mw_mysql, $timestamp) . '"' - ); - if ($res === false) { - $logger->addWarning("page recent edits query returned no data for " . $title . - " (" . $nsid . ") > " . $timestamp . ": " . mysqli_error($mw_mysql)); - } else { - $d = mysqli_fetch_assoc($res); - $data['common']['num_recent_edits'] = $d['count']; - } - $res = mysqli_query( - $mw_mysql, - 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT COUNT(*) as count FROM `page`' . - ' JOIN `revision` ON `rev_page` = `page_id`' . - ' JOIN `comment` ON `rev_comment_id` = `comment_id`' . - " WHERE `page_namespace` = '" . - mysqli_real_escape_string($mw_mysql, $nsid) . - "' AND `page_title` = '" . - mysqli_real_escape_string($mw_mysql, $title) . - "' AND `rev_timestamp` > '" . - mysqli_real_escape_string($mw_mysql, $timestamp) . - "' AND `comment_text` LIKE 'Revert%'" - ); - if ($res === false) { - $logger->addWarning("page recent reverts query returned no data for " . $title . - " (" . $nsid . ") > " . $timestamp . ": " . mysqli_error($mw_mysql)); - } else { - $d = mysqli_fetch_assoc($res); - $data['common']['num_recent_reversions'] = $d['count']; - } - if ( - filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || - filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) - ) { - $data['user_reg_time'] = time(); + try { $res = mysqli_query( $mw_mysql, 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT COUNT(*) AS `user_editcount` FROM `revision_userindex` ' . + 'SELECT `rev_timestamp`, `actor_name` FROM `page`' . + ' JOIN `revision` ON `rev_page` = `page_id`' . ' JOIN `actor` ON `actor_id` = `rev_actor`' . - ' WHERE `actor_name` = "' . - mysqli_real_escape_string($mw_mysql, $user) . '"' + ' WHERE `page_namespace` = "' . + mysqli_real_escape_string($mw_mysql, $nsid) . + '" AND `page_title` = "' . + mysqli_real_escape_string($mw_mysql, $title) . + '" ORDER BY `rev_id` LIMIT 1' ); if ($res === false) { - $logger->addWarning("user edit count query returned no data for (invalid ip) " . - $user . ": " . mysqli_error($mw_mysql)); + $logger->addWarning("page metadata query returned no data for " . $title . + " (" . $nsid . "): " . mysqli_error($mw_mysql)); } else { $d = mysqli_fetch_assoc($res); - $data['user_edit_count'] = $d['user_editcount']; + $data['common']['page_made_time'] = $d['rev_timestamp']; + $data['common']['creator'] = $d['actor_name']; } - } else { + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("page metadata query timed out for " . $title . " (" . $nsid . ")"); + } else { + $logger->addError("page metadata query returned an error for " . $title . + " (" . $nsid . "): " . $e->getMessage()); + } + } + + try { $res = mysqli_query( $mw_mysql, 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT `user_registration` FROM `user` WHERE `user_name` = "' . - mysqli_real_escape_string($mw_mysql, $user) . '"' + 'SELECT COUNT(*) as count FROM `page`' . + ' JOIN `revision` ON `rev_page` = `page_id`' . + ' WHERE `page_namespace` = "' . + mysqli_real_escape_string($mw_mysql, $nsid) . + '" AND `page_title` = "' . + mysqli_real_escape_string($mw_mysql, $title) . + '" AND `rev_timestamp` > "' . + mysqli_real_escape_string($mw_mysql, $timestamp) . '"' ); - $d = mysqli_fetch_assoc($res); + if ($res === false) { - $logger->addWarning("user registration query returned no data for " . - $user . ": " . mysqli_error($mw_mysql)); + $logger->addWarning("page recent edits query returned no data for " . $title . + " (" . $nsid . ") > " . $timestamp . ": " . mysqli_error($mw_mysql)); + } else { + $d = mysqli_fetch_assoc($res); + $data['common']['num_recent_edits'] = $d['count']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("page recent edits query timed out for " . $title . " (" . $nsid . ") > " . + $timestamp); } else { - $data['user_reg_time'] = $d['user_registration']; + $logger->addError("page recent edits query returned an error for " . $title . + " (" . $nsid . ") > " . $timestamp . ": " . $e->getMessage()); } - if (!$data['user_reg_time']) { + } + + try { + $res = mysqli_query( + $mw_mysql, + 'SET STATEMENT max_statement_time=10 FOR ' . + 'SELECT COUNT(*) as count FROM `page`' . + ' JOIN `revision` ON `rev_page` = `page_id`' . + ' JOIN `comment` ON `rev_comment_id` = `comment_id`' . + " WHERE `page_namespace` = '" . + mysqli_real_escape_string($mw_mysql, $nsid) . + "' AND `page_title` = '" . + mysqli_real_escape_string($mw_mysql, $title) . + "' AND `rev_timestamp` > '" . + mysqli_real_escape_string($mw_mysql, $timestamp) . + "' AND `comment_text` LIKE 'Revert%'" + ); + + if ($res === false) { + $logger->addWarning("page recent reverts query returned no data for " . $title . + " (" . $nsid . ") > " . $timestamp . ": " . mysqli_error($mw_mysql)); + } else { + $d = mysqli_fetch_assoc($res); + $data['common']['num_recent_reversions'] = $d['count']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("page recent reverts query timed out for " . $title . + " (" . $nsid . ") > " . $timestamp); + } else { + $logger->addError("page recent reverts query returned an error for " . $title . + " (" . $nsid . ") > " . $timestamp . ": " . $e->getMessage()); + } + } + + if ( + filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || + filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) + ) { + $data['user_reg_time'] = time(); + + try { $res = mysqli_query( $mw_mysql, 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT `rev_timestamp` FROM `revision_userindex` ' . + 'SELECT COUNT(*) AS `user_editcount` FROM `revision_userindex` ' . ' JOIN `actor` ON `actor_id` = `rev_actor`' . ' WHERE `actor_name` = "' . - mysqli_real_escape_string($mw_mysql, $user) . '" ORDER BY `rev_timestamp` LIMIT 0,1' + mysqli_real_escape_string($mw_mysql, $user) . '"' + ); + + if ($res === false) { + $logger->addWarning("user edit count query returned no data for (invalid ip) " . + $user . ": " . mysqli_error($mw_mysql)); + } else { + $d = mysqli_fetch_assoc($res); + $data['user_edit_count'] = $d['user_editcount']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user edit count query timed out for " . $user); + } else { + $logger->addError("user edit count query returned an error for " . $user . ": " . + $e->getMessage()); + } + } + } else { + try { + $res = mysqli_query( + $mw_mysql, + 'SET STATEMENT max_statement_time=10 FOR ' . + 'SELECT `user_registration` FROM `user` WHERE `user_name` = "' . + mysqli_real_escape_string($mw_mysql, $user) . '"' ); + + $d = mysqli_fetch_assoc($res); if ($res === false) { - $logger->addWarning("user registration via revision query returned no data for " . + $logger->addWarning("user registration query returned no data for " . + $user . ": " . mysqli_error($mw_mysql)); + } else { + $data['user_reg_time'] = $d['user_registration']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user registration query timed out for " . $user); + } else { + $logger->addError("user registration query returned an error for " . $user . ": " . + $e->getMessage()); + } + } + + if (!$data['user_reg_time']) { + try { + $res = mysqli_query( + $mw_mysql, + 'SET STATEMENT max_statement_time=10 FOR ' . + 'SELECT `rev_timestamp` FROM `revision_userindex` ' . + ' JOIN `actor` ON `actor_id` = `rev_actor`' . + ' WHERE `actor_name` = "' . + mysqli_real_escape_string($mw_mysql, $user) . '" ORDER BY `rev_timestamp` LIMIT 0,1' + ); + + if ($res === false) { + $logger->addWarning("user registration via revision query returned no data for " . + $user . ": " . mysqli_error($mw_mysql)); + } else { + $d = mysqli_fetch_assoc($res); + $data['user_reg_time'] = $d['rev_timestamp']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user registration via revision query timed out for " . $user); + } else { + $logger->addError("user registration via revision query returned an error for " . $user . + ": " . $e->getMessage()); + } + } + } + + try { + $res = mysqli_query( + $mw_mysql, + 'SET STATEMENT max_statement_time=10 FOR ' . + 'SELECT `user_editcount` FROM `user` WHERE `user_name` = "' . + mysqli_real_escape_string($mw_mysql, $user) . '"' + ); + + if ($res === false) { + $logger->addWarning("user edit count query returned no data for " . $user . ": " . mysqli_error($mw_mysql)); } else { $d = mysqli_fetch_assoc($res); - $data['user_reg_time'] = $d['rev_timestamp']; + $data['user_edit_count'] = $d['user_editcount']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user edit count query timed out for " . $user); + } else { + $logger->addError("user edit count query returned an error for " . $user . ": " . + $e->getMessage()); } } + } + + try { $res = mysqli_query( $mw_mysql, 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT `user_editcount` FROM `user` WHERE `user_name` = "' . - mysqli_real_escape_string($mw_mysql, $user) . '"' + 'SELECT COUNT(*) as count FROM `page`' . + ' JOIN `revision` ON `rev_page` = `page_id`' . + ' JOIN `comment` ON `rev_comment_id` = `comment_id`' . + " WHERE `page_namespace` = 3 AND `page_title` = '" . + mysqli_real_escape_string($mw_mysql, $userPage) . + "' AND (`comment_text` LIKE '%warning%' OR `comment_text`" . + " LIKE 'General note: Nonconstructive%')" ); + if ($res === false) { - $logger->addWarning("user edit count query returned no data for " . - $user . ": " . mysqli_error($mw_mysql)); + $logger->addWarning("user warnings count query returned no data for " . + $userPage . ": " . mysqli_error($mw_mysql)); } else { $d = mysqli_fetch_assoc($res); - $data['user_edit_count'] = $d['user_editcount']; + $data['user_warns'] = $d['count']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user warnings count query timed out for " . $user); + } else { + $logger->addError("user warnings count query returned an error for " . $user . ": " . + $e->getMessage()); } } - $res = mysqli_query( - $mw_mysql, - 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT COUNT(*) as count FROM `page`' . - ' JOIN `revision` ON `rev_page` = `page_id`' . - ' JOIN `comment` ON `rev_comment_id` = `comment_id`' . - " WHERE `page_namespace` = 3 AND `page_title` = '" . - mysqli_real_escape_string($mw_mysql, $userPage) . - "' AND (`comment_text` LIKE '%warning%' OR `comment_text`" . - " LIKE 'General note: Nonconstructive%')" - ); - if ($res === false) { - $logger->addWarning("user warnings query returned no data for " . - $userPage . ": " . mysqli_error($mw_mysql)); - } else { - $d = mysqli_fetch_assoc($res); - $data['user_warns'] = $d['count']; - } - $res = mysqli_query( - $mw_mysql, - 'SET STATEMENT max_statement_time=10 FOR ' . - 'SELECT count(distinct rev_page) AS count FROM' . - ' `revision_userindex` JOIN `actor` ON `actor_id` = `rev_actor`' . - " WHERE `actor_name` = '" . mysqli_real_escape_string($mw_mysql, $userPage) . "'" - ); - if ($res !== false) { - $d = mysqli_fetch_assoc($res); - $data['user_distinct_pages'] = $d['count']; + + try { + $res = mysqli_query( + $mw_mysql, + 'SET STATEMENT max_statement_time=10 FOR ' . + 'SELECT count(distinct rev_page) AS count FROM' . + ' `revision_userindex` JOIN `actor` ON `actor_id` = `rev_actor`' . + " WHERE `actor_name` = '" . mysqli_real_escape_string($mw_mysql, $userPage) . "'" + ); + + if ($res !== false) { + $d = mysqli_fetch_assoc($res); + $data['user_distinct_pages'] = $d['count']; + } + } catch (mysqli_sql_exception $e) { + if ($e->getCode() == 1969) { + $logger->addWarning("user distinct page edits query timed out for " . $userPage); + } else { + $logger->addError("user distinct page edits query returned an error for " . $userPage . + ": " . $e->getMessage()); + } } if ($data['common']['page_made_time']) { $data['common']['page_made_time'] = gmmktime( diff --git a/process_functions.php b/process_functions.php index 043ea14..e8beedd 100644 --- a/process_functions.php +++ b/process_functions.php @@ -69,12 +69,8 @@ public static function processEditThread($change) { global $logger; $change['edit_status'] = 'not_reverted'; - if (!isset($s)) { - $change['edit_score'] = 'N/A'; - $s = null; - } else { - $change['edit_score'] = $s; - } + $change['edit_score'] = 'N/A'; + $s = null; if (!array_key_exists('all', $change)) { Feed::bail($change, 'Missing edit data', $s); return;