Skip to content

Commit

Permalink
Cleanup code with phpcbf
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianZaremba committed Jul 23, 2021
1 parent 2a61649 commit 38ff51f
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 82 deletions.
33 changes: 18 additions & 15 deletions action_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public static function getWarningLevel($user, &$content = null)
{
$warning = 0;
$content = Api::$q->getpage('User talk:' . $user);
if (preg_match_all(
'/<!-- Template:(uw-[a-z]*(\d)(im)?|Blatantvandal \(serious warning\)) -->.*' .
'(\d{2}):(\d{2}), (\d+) ([a-zA-Z]+) (\d{4}) \(UTC\)/iU',
$content,
$match,
PREG_SET_ORDER
)) {
if (
preg_match_all(
'/<!-- Template:(uw-[a-z]*(\d)(im)?|Blatantvandal \(serious warning\)) -->.*' .
'(\d{2}):(\d{2}), (\d+) ([a-zA-Z]+) (\d{4}) \(UTC\)/iU',
$content,
$match,
PREG_SET_ORDER
)
) {
foreach ($match as $m) {
$month = array(
'January' => 1, 'February' => 2, 'March' => 3,
Expand Down Expand Up @@ -139,7 +141,6 @@ public static function doRevert($change)
$rbret = Api::$a->rollback(
$change['title'],
$change['user'],
/*'Edit by [[Special:Contribs/' . $change['user'] . '|' . $change['user'] . ']] has been reverted by [[WP:CBNG|' . Config::$user . ']] due to possible noncompliance with Wikipedia guidelines. [[WP:CBFP|Report False Positive?]] (' . $change['mysqlid'] .') (Bot)'*/
'Reverting possible vandalism by [[Special:Contribs/' . $change['user'] . '|' . $change['user'] . ']] ' .
'to ' . (($revid == 0) ? 'older version' : 'version by ' . $revdata['user']) . '. ' .
'[[WP:CBFP|Report False Positive?]] ' .
Expand Down Expand Up @@ -169,11 +170,12 @@ public static function shouldRevert($change)
return array(true, 'Angry-reverting in angry mode');
}
if ((time() - Globals::$tfas) >= 1800) {
if (preg_match(
'/\(\'\'\'\[\[([^|]*)\|more...\]\]\'\'\'\)/iU',
Api::$q->getpage('Wikipedia:Today\'s featured article/' . date('F j, Y')),
$tfam
)
if (
preg_match(
'/\(\'\'\'\[\[([^|]*)\|more...\]\]\'\'\'\)/iU',
Api::$q->getpage('Wikipedia:Today\'s featured article/' . date('F j, Y')),
$tfam
)
) {
Globals::$tfas = time();
Globals::$tfa = $tfam[1];
Expand Down Expand Up @@ -201,7 +203,8 @@ public static function shouldRevert($change)
return array(true, 'Angry-reverting on angry-optin');
}
$titles = unserialize(file_get_contents('titles.txt'));
if (!isset($titles[$change['title'] . $change['user']])
if (
!isset($titles[$change['title'] . $change['user']])
or ((time() - $titles[$change['title'] . $change['user']]) > (24 * 60 * 60))
) {
$titles[$change['title'] . $change['user']] = time();
Expand Down Expand Up @@ -235,7 +238,7 @@ public static function findAndParseBots($change)

public static function isWhitelisted($user)
{
foreach(Globals::$wl as $wl) {
foreach (Globals::$wl as $wl) {
if (preg_match('/^' . preg_quote($user, '/') . '$/', $wl)) {
return true;
}
Expand Down
20 changes: 12 additions & 8 deletions cbng.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ function namespace2name($nsid)

function parseFeed($feed)
{
if (preg_match(
'/^\[\[((Talk|User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal|Special|Book|Draft|TimedText|Module|Gadget|Gadget(?: |_)definition|Media)(( |_)talk)?:)?' .
'([^\x5d]*)\]\] (\S*) (https?:\/\/en\.wikipedia\.org\/w\/index\.php\?diff=(\d*)&oldid=(\d*).*|' .
'https?:\/\/en\.wikipedia\.org\/wiki\/\S+)? \* ([^*]*) \* (\(([^)]*)\))? (.*)$/S',
$feed,
$m
)) {
if (
preg_match(
'/^\[\[((Talk|User|Wikipedia|File|MediaWiki|Template|Help|Category' .
'|Portal|Special|Book|Draft|TimedText|Module|Gadget|Gadget(?: |_)definition|Media)(( |_)talk)?:)?' .
'([^\x5d]*)\]\] (\S*) (https?:\/\/en\.wikipedia\.org\/w\/index\.php\?diff=(\d*)&oldid=(\d*).*|' .
'https?:\/\/en\.wikipedia\.org\/wiki\/\S+)? \* ([^*]*) \* (\(([^)]*)\))? (.*)$/S',
$feed,
$m
)
) {
$change = array(
'namespace' => $m[1] ? $m[1] : 'Main:',
'namespaceid' => namespace2id($m[1] ? substr($m[1], 0, -1) : 'Main'),
Expand Down Expand Up @@ -187,7 +190,8 @@ function parseFeedData($feedData, $useOld = false)
list($api) = getUrlsInParallel($urls);
$api = current($api['query']['pages']);
$cb = getCbData($feedData['user'], $feedData['namespaceid'], $feedData['title'], $feedData['timestamp']);
if (!(isset($cb['user_edit_count'])
if (
!(isset($cb['user_edit_count'])
and isset($cb['user_distinct_pages'])
and isset($cb['user_warns'])
and isset($api['revisions'][1]['user'])
Expand Down
2 changes: 1 addition & 1 deletion cluebot-ng.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
doInit();
IRC::init();

for (; ;) {
for (;;) {
Feed::connectLoop();
}
3 changes: 2 additions & 1 deletion db_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static function vandalismReverted($edit_id)
checkMySQL();
mysqli_query(
Globals::$cb_mysql,
'UPDATE `vandalism` SET `reverted` = 1 WHERE `id` = \'' . mysqli_real_escape_string(Globals::$cb_mysql, $edit_id) . '\''
'UPDATE `vandalism` SET `reverted` = 1 WHERE `id` = \'' .
mysqli_real_escape_string(Globals::$cb_mysql, $edit_id) . '\''
);
}

Expand Down
23 changes: 13 additions & 10 deletions feed_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ private static function loop($line)
}
}
foreach (Globals::$edit as $key => $value) {
if (myfnmatch(
str_replace('_', ' ', $key),
str_replace(
'_',
' ',
($data['namespace'] == 'Main:' ? '' : $data['namespace']) .
$data['title']
if (
myfnmatch(
str_replace('_', ' ', $key),
str_replace(
'_',
' ',
($data['namespace'] == 'Main:' ? '' : $data['namespace']) .
$data['title']
)
)
)
) {
$stalkchannel = array_merge($stalkchannel, explode(',', $value));
}
Expand All @@ -120,7 +121,8 @@ private static function loop($line)
break;
}

if ($data['namespace'] != 'Main:' and
if (
$data['namespace'] != 'Main:' and
!preg_match(
'/\* \[\[(' . preg_quote($data['namespace'] . $data['title'], '/') .
')\]\] \- .*/i',
Expand Down Expand Up @@ -154,6 +156,7 @@ public static function bail($change, $why = '', $score = 'N/A', $reverted = fals
return;
}

IRC::spam($change['rawline'] . "\003 # " . $score . ' # ' . $why . ' # ' . ($reverted ? 'Reverted' : 'Not reverted'));
$logger->addInfo($change['rawline'] . " # " . $score .
' # ' . $why . ' # ' . ($reverted ? 'Reverted' : 'Not reverted'));
}
}
3 changes: 2 additions & 1 deletion http.classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function __construct()
curl_setopt($this->ch, CURLOPT_ENCODING, '');
curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($this->ch, CURLOPT_FRESH_CONNECT, 1);
if (isset(Config::$proxyhost) and isset(Config::$proxyport) and
if (
isset(Config::$proxyhost) and isset(Config::$proxyport) and
(Config::$proxyport != null) and (Config::$proxyhost != null)
) {
curl_setopt($this->ch, CURLOPT_PROXYTYPE, isset(Config::$proxytype) ? Config::$proxytype : CURLPROXY_HTTP);
Expand Down
11 changes: 10 additions & 1 deletion includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
date_default_timezone_set('Europe/London');
include 'vendor/autoload.php';
$logger = new \Monolog\Logger('cluebotng');
$logger->pushHandler(new \Monolog\Handler\RotatingFileHandler(getenv('HOME') . '/logs/cluebotng.log', 2, \Monolog\Logger::INFO, true, 0600, false));
$logger->pushHandler(
new \Monolog\Handler\RotatingFileHandler(
getenv('HOME') . '/logs/cluebotng.log',
2,
\Monolog\Logger::INFO,
true,
0600,
false
)
);

require_once 'cluebot-ng.config.php';
require_once 'action_functions.php';
Expand Down
4 changes: 2 additions & 2 deletions irc_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ private static function message($channel, $message)
{
global $logger;
$relay_node = Db::getCurrentRelayNode();
if(!isset($relay_node)) {
if (!isset($relay_node)) {
$logger->addError("Could not get relay node. Failed to send: " . $message);
return;
}
$logger->addInfo('Saying to ' . $channel . ': ' . $message);
$udp = fsockopen('udp://' . $relay_node, Config::$udpport);
if($udp !== false) {
if ($udp !== false) {
fwrite($udp, $channel . ':' . $message);
fclose($udp);
}
Expand Down
28 changes: 18 additions & 10 deletions mysql_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function checkRepMySQL()
Config::$mw_mysql_pass,
Config::$mw_mysql_db,
Config::$mw_mysql_port

);
if (!Globals::$mw_mysql) {
die('replica mysql error: ' . mysqli_connect_error());
Expand Down Expand Up @@ -90,7 +89,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
'" ORDER BY `rev_id` LIMIT 1'
);
if ($res === false) {
$logger->addWarning("page metadata query returned no data for " . $title . " (" . $nsid . "): ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("page metadata query returned no data for " . $title .
" (" . $nsid . "): " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['common']['page_made_time'] = $d['rev_timestamp'];
Expand All @@ -109,7 +109,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
mysqli_real_escape_string(Globals::$mw_mysql, $timestamp) . '"'
);
if ($res === false) {
$logger->addWarning("page recent edits query returned no data for " . $title . " (" . $nsid . ") > " . $timestamp . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("page recent edits query returned no data for " . $title .
" (" . $nsid . ") > " . $timestamp . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['common']['num_recent_edits'] = $d['count'];
Expand All @@ -128,12 +129,14 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
"' AND `comment_text` LIKE 'Revert%'"
);
if ($res === false) {
$logger->addWarning("page recent reverts query returned no data for " . $title . " (" . $nsid . ") > " . $timestamp . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("page recent reverts query returned no data for " . $title .
" (" . $nsid . ") > " . $timestamp . ": " . mysqli_error(Globals::$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) ||
if (
filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ||
filter_var($user, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
) {
$data['user_reg_time'] = time();
Expand All @@ -145,7 +148,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
mysqli_real_escape_string(Globals::$mw_mysql, $user) . '"'
);
if ($res === false) {
$logger->addWarning("user edit count query returned no data for (invalid ip) " . $user . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("user edit count query returned no data for (invalid ip) " .
$user . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['user_edit_count'] = $d['user_editcount'];
Expand All @@ -158,7 +162,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
);
$d = mysqli_fetch_assoc($res);
if ($res === false) {
$logger->addWarning("user registration query returned no data for " . $user . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("user registration query returned no data for " .
$user . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$data['user_reg_time'] = $d['user_registration'];
}
Expand All @@ -171,7 +176,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
mysqli_real_escape_string(Globals::$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(Globals::$mw_mysql));
$logger->addWarning("user registration via revision query returned no data for " .
$user . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['user_reg_time'] = $d['rev_timestamp'];
Expand All @@ -183,7 +189,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
mysqli_real_escape_string(Globals::$mw_mysql, $user) . '"'
);
if ($res === false) {
$logger->addWarning("user edit count query returned no data for " . $user . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("user edit count query returned no data for " .
$user . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['user_edit_count'] = $d['user_editcount'];
Expand All @@ -200,7 +207,8 @@ function getCbData($user = '', $nsid = '', $title = '', $timestamp = '')
" LIKE 'General note: Nonconstructive%')"
);
if ($res === false) {
$logger->addWarning("user warnings query returned no data for " . $userPage . ": ". mysqli_error(Globals::$mw_mysql));
$logger->addWarning("user warnings query returned no data for " .
$userPage . ": " . mysqli_error(Globals::$mw_mysql));
} else {
$d = mysqli_fetch_assoc($res);
$data['user_warns'] = $d['count'];
Expand Down
3 changes: 2 additions & 1 deletion process_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Process
public static function processEdit($change)
{
global $logger;
if ((time() - Globals::$tfas) >= 1800 and
if (
(time() - Globals::$tfas) >= 1800 and
preg_match(
'/\(\'\'\'\[\[([^|]*)\|more...\]\]\'\'\'\)/iU',
Api::$q->getpage('Wikipedia:Today\'s featured article/' . date('F j, Y')),
Expand Down
12 changes: 5 additions & 7 deletions wikipedia_api.classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public function logs(
$start = null,
$end = null,
$dir = 'older'
)
{
) {
$append = '';
if ($user != null) {
$append .= '&leuser=' . urlencode($user);
Expand Down Expand Up @@ -398,8 +397,7 @@ public function edit(
$wpStarttime = null,
$wpEdittime = null,
$checkrun = true
)
{
) {
global $logger;
$wpq = new WikipediaQuery();
$wpq->queryurl = str_replace('api.php', 'query.php', $this->apiurl);
Expand Down Expand Up @@ -643,8 +641,7 @@ public function revisions(
$getrbtok = false,
$dieonerror = true,
$redirects = false
)
{
) {
$x = $this->http->get(
$this->apiurl . '?action=query&rawcontinue=1&prop=revisions&titles=' .
urlencode($page) . '&rvlimit=' . urlencode($count) . '&rvprop=timestamp|ids|user|comment' .
Expand Down Expand Up @@ -697,7 +694,8 @@ public function revisions(
$data['revisions']['ns'] = $data['ns'];
$data['revisions']['title'] = $data['title'];
$data['revisions']['currentuser'] = $x['query']['userinfo']['name'];
if (array_key_exists('query-continue', $x) &&
if (
array_key_exists('query-continue', $x) &&
array_key_exists('revisions', $x['query-continue']) &&
array_key_exists('rvstartid', $x['query-continue']['revisions'])
) {
Expand Down
Loading

0 comments on commit 38ff51f

Please sign in to comment.