Comments/includes/CommentFunctions.php

300 lines
7.3 KiB
PHP
Raw Permalink Normal View History

2017-09-27 17:53:26 +02:00
<?php
class CommentFunctions {
/**
* The following four functions are borrowed
* from includes/wikia/GlobalFunctionsNY.php
*/
static function dateDiff( $date1, $date2 ) {
$dtDiff = $date1 - $date2;
$totalDays = intval( $dtDiff / ( 24 * 60 * 60 ) );
$totalSecs = $dtDiff - ( $totalDays * 24 * 60 * 60 );
$dif['mo'] = intval( $totalDays / 30 );
$dif['d'] = $totalDays;
$dif['h'] = $h = intval( $totalSecs / ( 60 * 60 ) );
$dif['m'] = $m = intval( ( $totalSecs - ( $h * 60 * 60 ) ) / 60 );
$dif['s'] = $totalSecs - ( $h * 60 * 60 ) - ( $m * 60 );
return $dif;
}
static function getTimeOffset( $time, $timeabrv, $timename ) {
$timeStr = ''; // misza: initialize variables, DUMB FUCKS!
if ( $time[$timeabrv] > 0 ) {
2017-09-27 17:53:26 +02:00
// Give grep a chance to find the usages:
// comments-time-days, comments-time-hours, comments-time-minutes, comments-time-seconds, comments-time-months
$timeStr = wfMessage( "comments-time-{$timename}", $time[$timeabrv] )->parse();
}
if ( $timeStr ) {
2017-09-27 17:53:26 +02:00
$timeStr .= ' ';
}
return $timeStr;
}
static function getTimeAgo( $time ) {
$timeArray = self::dateDiff( time(), $time );
$timeStr = '';
$timeStrMo = self::getTimeOffset( $timeArray, 'mo', 'months' );
$timeStrD = self::getTimeOffset( $timeArray, 'd', 'days' );
$timeStrH = self::getTimeOffset( $timeArray, 'h', 'hours' );
$timeStrM = self::getTimeOffset( $timeArray, 'm', 'minutes' );
$timeStrS = self::getTimeOffset( $timeArray, 's', 'seconds' );
if ( $timeStrMo ) {
$timeStr = $timeStrMo;
} else {
$timeStr = $timeStrD;
if ( $timeStr < 2 ) {
2017-09-27 17:53:26 +02:00
$timeStr .= $timeStrH;
$timeStr .= $timeStrM;
if ( !$timeStr ) {
2017-09-27 17:53:26 +02:00
$timeStr .= $timeStrS;
}
}
}
if ( !$timeStr ) {
2017-09-27 17:53:26 +02:00
$timeStr = wfMessage( 'comments-time-seconds', 1 )->parse();
}
return $timeStr;
}
/**
* Makes sure that link text is not too long by changing too long links to
* <a href=#>http://www.abc....xyz.html</a>
*
* @param $matches Array
* @return String shortened URL
2017-09-27 17:53:26 +02:00
*/
public static function cutCommentLinkText( $matches ) {
$tagOpen = $matches[1];
$linkText = $matches[2];
$tagClose = $matches[3];
$image = preg_match( "/<img src=/i", $linkText );
$isURL = ( preg_match( '%^(?:http|https|ftp)://(?:www\.)?.*$%i', $linkText ) ? true : false );
if ( $isURL && !$image && strlen( $linkText ) > 30 ) {
2017-09-27 17:53:26 +02:00
$start = substr( $linkText, 0, ( 30 / 2 ) - 3 );
$end = substr( $linkText, strlen( $linkText ) - ( 30 / 2 ) + 3, ( 30 / 2 ) - 3 );
$linkText = trim( $start ) . wfMessage( 'ellipsis' )->escaped() . trim( $end );
}
return $tagOpen . $linkText . $tagClose;
}
/**
* Simple spam check -- checks the supplied text against MediaWiki's
* built-in regex-based spam filters
*
* @param $text String: text to check for spam patterns
* @return Boolean true if it contains spam, otherwise false
2017-09-27 17:53:26 +02:00
*/
public static function isSpam( $text ) {
global $wgSpamRegex, $wgSummarySpamRegex;
$retVal = false;
// Allow to hook other anti-spam extensions so that sites that use,
// for example, AbuseFilter, Phalanx or SpamBlacklist can add additional
// checks
Hooks::run( 'Comments::isSpam', [ &$text, &$retVal ] );
2017-09-27 17:53:26 +02:00
if ( $retVal ) {
// Should only be true here...
return $retVal;
}
// Run text through $wgSpamRegex (and $wgSummarySpamRegex if it has been specified)
if ( $wgSpamRegex && preg_match( $wgSpamRegex, $text ) ) {
return true;
}
if ( $wgSummarySpamRegex && is_array( $wgSummarySpamRegex ) ) {
foreach ( $wgSummarySpamRegex as $spamRegex ) {
if ( preg_match( $spamRegex, $text ) ) {
return true;
}
}
}
return $retVal;
}
/**
* Checks the supplied text for links
*
* @param $text String: text to check
* @return Boolean true if it contains links, otherwise false
2017-09-27 17:53:26 +02:00
*/
public static function haveLinks( $text ) {
$linkPatterns = [
2017-09-27 17:53:26 +02:00
'/(https?)|(ftp):\/\//',
'/=\\s*[\'"]?\\s*mailto:/',
];
2017-09-27 17:53:26 +02:00
foreach ( $linkPatterns as $linkPattern ) {
if ( preg_match( $linkPattern, $text ) ) {
return true;
}
}
return false;
}
/**
* Blocks comments from a user
*
* @param User $blocker The user who is blocking someone else's comments
* @param int $userId User ID of the guy whose comments we want to block
* @param mixed $userName User name of the same guy
*/
public static function blockUser( $blocker, $userId, $userName ) {
$dbw = wfGetDB( DB_MASTER );
Wikimedia\suppressWarnings(); // E_STRICT bitching
2017-09-27 17:53:26 +02:00
$date = date( 'Y-m-d H:i:s' );
Wikimedia\restoreWarnings();
2017-09-27 17:53:26 +02:00
$dbw->insert(
'Comments_block',
[
2017-09-27 17:53:26 +02:00
'cb_user_id' => $blocker->getId(),
'cb_user_name' => $blocker->getName(),
'cb_user_id_blocked' => $userId,
'cb_user_name_blocked' => $userName,
'cb_date' => $date
],
2017-09-27 17:53:26 +02:00
__METHOD__
);
}
/**
* Fetches the list of blocked users from the database
*
* @param int $userId User ID for whom we're getting the blocks(?)
* @return array List of comment-blocked users
*/
static function getBlockList( $userId ) {
$blockList = [];
$dbr = wfGetDB( DB_REPLICA );
2017-09-27 17:53:26 +02:00
$res = $dbr->select(
'Comments_block',
'cb_user_name_blocked',
[ 'cb_user_id' => $userId ],
2017-09-27 17:53:26 +02:00
__METHOD__
);
foreach ( $res as $row ) {
$blockList[] = $row->cb_user_name_blocked;
}
return $blockList;
}
static function isUserCommentBlocked( $userId, $userIdBlocked ) {
$dbr = wfGetDB( DB_REPLICA );
2017-09-27 17:53:26 +02:00
$s = $dbr->selectRow(
'Comments_block',
[ 'cb_id' ],
[
2017-09-27 17:53:26 +02:00
'cb_user_id' => $userId,
'cb_user_id_blocked' => $userIdBlocked
],
2017-09-27 17:53:26 +02:00
__METHOD__
);
if ( $s !== false ) {
return true;
} else {
return false;
}
}
/**
* Deletes a user from your personal comment-block list.
*
* @param int $userId Your user ID
* @param int $userIdBlocked User ID of the blocked user
*/
public static function deleteBlock( $userId, $userIdBlocked ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
'Comments_block',
[
2017-09-27 17:53:26 +02:00
'cb_user_id' => $userId,
'cb_user_id_blocked' => $userIdBlocked
],
2017-09-27 17:53:26 +02:00
__METHOD__
);
}
/**
* Sort threads ascending
*
* @param $x
* @param $y
* @return int
*/
public static function sortAsc( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->timestamp < $y[0]->timestamp ) {
return -1;
} else {
return 1;
}
}
/**
* Sort threads descending
*
* @param $x
* @param $y
* @return int
*/
public static function sortDesc( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->timestamp > $y[0]->timestamp ) {
return -1;
} else {
return 1;
}
}
/**
* Sort threads by score
*
* @param $x
* @param $y
*/
public static function sortScore( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->currentScore > $y[0]->currentScore ) {
return -1;
} else {
return 1;
}
}
/**
* Sort the comments purely by the time, from earliest to latest
*
* @param $x
* @param $y
* @return int
*/
public static function sortTime( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x->timestamp == $y->timestamp ) {
return 0;
} elseif ( $x->timestamp < $y->timestamp ) {
return -1;
} else {
return 1;
}
}
}