UltrasonicNXT has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/182361

Change subject: Move static functions into their own file
......................................................................

Move static functions into their own file

As part of larger rewrite

Change-Id: I544ba274dd77da5b89a2c7a14b01528a30670844
---
M CommentClass.php
A CommentFunctions.php
M Comments.php
M Comments_AjaxFunctions.php
4 files changed, 145 insertions(+), 140 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Comments 
refs/changes/61/182361/1

diff --git a/CommentClass.php b/CommentClass.php
index 7664bab..04d8e47 100644
--- a/CommentClass.php
+++ b/CommentClass.php
@@ -77,86 +77,6 @@
 
        public $PAGE_QUERY = 'cpage';
 
-       /**
-        * 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 ) {
-                       // 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 ) {
-                       $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 ) {
-                               $timeStr .= $timeStrH;
-                               $timeStr .= $timeStrM;
-                               if ( !$timeStr ) {
-                                       $timeStr .= $timeStrS;
-                               }
-                       }
-               }
-               if ( !$timeStr ) {
-                       $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 array $matches
-        * @return string Shortened URL
-        */
-       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 ) {
-                       $start = substr( $linkText, 0, ( 30 / 2 ) - 3 );
-                       $end = substr( $linkText, strlen( $linkText ) - ( 30 / 
2 ) + 3, ( 30 / 2 ) - 3 );
-                       $linkText = trim( $start ) . wfMsg( 'ellipsis' ) . 
trim( $end );
-               }
-               return $tagOpen . $linkText . $tagClose;
-       }
 
        /**
         * Constructor - set the page ID
@@ -207,7 +127,7 @@
                // this function changes too long links to <a 
href=#>http://www.abc....xyz.html</a>
                $comment_text = preg_replace_callback(
                        "/(<a[^>]*>)(.*?)(<\/a>)/i",
-                       array( 'Comment', 'cutCommentLinkText' ),
+                       array( 'CommentFunctions', 'cutCommentLinkText' ),
                        $comment_text
                );
 
@@ -318,62 +238,6 @@
                        $count = $s->CommentCount;
                }
                return $count;
-       }
-
-       /**
-        * Simple spam check -- checks the supplied text against MediaWiki's
-        * built-in regex-based spam filters
-        *
-        * @param string $text Text to check for spam patterns
-        * @return bool True if it contains spam, otherwise false
-        */
-       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
-               wfRunHooks( 'Comments::isSpam', array( &$text, &$retVal ) );
-               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 string $text Text to check
-        * @return bool True if it contains links, otherwise false
-        */
-       public static function haveLinks( $text ) {
-               $linkPatterns = array(
-                       '/(https?)|(ftp):\/\//',
-                       '/=\\s*[\'"]?\\s*mailto:/',
-               );
-               foreach ( $linkPatterns as $linkPattern ) {
-                       if ( preg_match( $linkPattern, $text ) ) {
-                               return true;
-                       }
-               }
-
-               return false;
        }
 
        /**
@@ -1194,7 +1058,7 @@
                                $output .= '<div class="c-time">' .
                                        wfMessage(
                                                'comments-time-ago',
-                                               self::getTimeAgo( strtotime( 
$comment['Comment_Date'] ) )
+                                               CommentFunctions::getTimeAgo( 
strtotime( $comment['Comment_Date'] ) )
                                        )->parse() . '</div>' . "\n";
                                wfRestoreWarnings();
 
diff --git a/CommentFunctions.php b/CommentFunctions.php
new file mode 100644
index 0000000..612fab2
--- /dev/null
+++ b/CommentFunctions.php
@@ -0,0 +1,140 @@
+<?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 ) {
+            // 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 ) {
+            $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 ) {
+                $timeStr .= $timeStrH;
+                $timeStr .= $timeStrM;
+                if( !$timeStr ) {
+                    $timeStr .= $timeStrS;
+                }
+            }
+        }
+        if( !$timeStr ) {
+            $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
+     */
+    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 ) {
+            $start = substr( $linkText, 0, ( 30 / 2 ) - 3 );
+            $end = substr( $linkText, strlen( $linkText ) - ( 30 / 2 ) + 3, ( 
30 / 2 ) - 3 );
+            $linkText = trim( $start ) . wfMsg( 'ellipsis' ) . 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
+     */
+    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
+        wfRunHooks( 'Comments::isSpam', array( &$text, &$retVal ) );
+        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
+     */
+    public static function haveLinks( $text ) {
+        $linkPatterns = array(
+            '/(https?)|(ftp):\/\//',
+            '/=\\s*[\'"]?\\s*mailto:/',
+        );
+        foreach ( $linkPatterns as $linkPattern ) {
+            if ( preg_match( $linkPattern, $text ) ) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+}
\ No newline at end of file
diff --git a/Comments.php b/Comments.php
index d53ed44..d31a537 100644
--- a/Comments.php
+++ b/Comments.php
@@ -72,6 +72,7 @@
 $wgMessagesDirs['Comments'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['CommentsMagic'] = __DIR__ . 
'/Comments.i18n.magic.php';
 $wgAutoloadClasses['Comment'] = __DIR__ . '/CommentClass.php';
+$wgAutoloadClasses['CommentFunctions'] = __DIR__ . '/CommentFunctions.php';
 $wgAutoloadClasses['CommentIgnoreList'] = __DIR__ . 
'/SpecialCommentIgnoreList.php';
 $wgAutoloadClasses['CommentListGet'] = __DIR__ . '/CommentAction.php';
 $wgSpecialPages['CommentIgnoreList'] = 'CommentIgnoreList';
diff --git a/Comments_AjaxFunctions.php b/Comments_AjaxFunctions.php
index 17172f6..349043b 100644
--- a/Comments_AjaxFunctions.php
+++ b/Comments_AjaxFunctions.php
@@ -24,13 +24,13 @@
                // To protect against spam, it's necessary to check the 
supplied text
                // against spam filters (but comment admins are allowed to 
bypass the
                // spam filters)
-               if ( !$wgUser->isAllowed( 'commentadmin' ) && Comment::isSpam( 
$comment_text ) ) {
+               if ( !$wgUser->isAllowed( 'commentadmin' ) && 
CommentFunctions::isSpam( $comment_text ) ) {
                        return wfMessage( 'comments-is-spam' )->plain();
                }
 
                // If the comment contains links but the user isn't allowed to 
post
                // links, reject the submission
-               if ( !$wgUser->isAllowed( 'commentlinks' ) && 
Comment::haveLinks( $comment_text ) ) {
+               if ( !$wgUser->isAllowed( 'commentlinks' ) && 
CommentFunctions::haveLinks( $comment_text ) ) {
                        return wfMessage( 'comments-links-are-forbidden' 
)->plain();
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/182361
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I544ba274dd77da5b89a2c7a14b01528a30670844
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Comments
Gerrit-Branch: master
Gerrit-Owner: UltrasonicNXT <adamr_car...@btinternet.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to