Siebrand has uploaded a new change for review.

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

Change subject: Add tests
......................................................................

Add tests

Fix all issues so tests pass.

Change-Id: I17975bc28b4a56bf0dd834be68964a6b00e190f5
---
M .gitignore
M Akismet.class.php
D AkismetKlik.i18n.php
M AkismetKlik.php
A Gruntfile.js
A composer.json
M i18n/en.json
A package.json
A phpcs.xml
9 files changed, 136 insertions(+), 86 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AkismetKlik 
refs/changes/40/242340/1

diff --git a/.gitignore b/.gitignore
index 98b092a..854a2d2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
-.svn
 *~
 *.kate-swp
 .*.swp
+node_modules/
+/composer.lock
+/vendor/
diff --git a/Akismet.class.php b/Akismet.class.php
index a0efdb1..d599b30 100644
--- a/Akismet.class.php
+++ b/Akismet.class.php
@@ -3,15 +3,21 @@
 /**
  * Akismet anti-comment spam service
  *
- * The class in this package allows use of the {@link http://akismet.com 
Akismet} anti-comment spam service in any PHP5 application.
+ * The class in this package allows use of the {@link http://akismet.com 
Akismet}
+ * anti-comment spam service in any PHP5 application.
  *
- * This service performs a number of checks on submitted data and returns 
whether or not the data is likely to be spam.
+ * This service performs a number of checks on submitted data and returns
+ * whether or not the data is likely to be spam.
  *
- * Please note that in order to use this class, you must have a vaild {@link 
http://wordpress.com/api-keys/ WordPress API key}.  They are free for 
non/small-profit types and getting one will only take a couple of minutes.
+ * Please note that in order to use this class, you must have a vaild
+ * {@link http://wordpress.com/api-keys/ WordPress API key}.  They are free for
+ * non/small-profit types and getting one will only take a couple of minutes.
  *
- * For commercial use, please {@link http://akismet.com/commercial/ visit the 
Akismet commercial licensing page}.
+ * For commercial use, please {@link http://akismet.com/commercial/ visit the
+ * Akismet commercial licensing page}.
  *
- * Please be aware that this class is PHP5 only.  Attempts to run it under 
PHP4 will most likely fail.
+ * Please be aware that this class is PHP5 only.  Attempts to run it under PHP4
+ * will most likely fail.
  *
  * See the Akismet class documentation page linked to below for usage 
information.
  *
@@ -25,7 +31,9 @@
 /**
  * The Akismet PHP5 Class
  *
- * This class takes the functionality from the Akismet WordPress plugin 
written by {@link http://photomatt.net/ Matt Mullenweg} and allows it to be 
integrated into any PHP5 application or website.
+ * This class takes the functionality from the Akismet WordPress plugin written
+ * by {@link http://photomatt.net/ Matt Mullenweg} and allows it to be 
integrated
+ * into any PHP5 application or website.
  *
  * The original plugin is {@link http://akismet.com/download/ available on the 
Akismet website}.
  *
@@ -86,8 +94,8 @@
                'PHP_SELF' );
 
        /**
-        * @param    string    $blogURL            The URL of your blog.
-        * @param    string    $wordPressAPIKey    WordPress API key.
+        * @param    string $blogURL The URL of your blog.
+        * @param    string $wordPressAPIKey WordPress API key.
         */
        public function __construct( $blogURL, $wordPressAPIKey ) {
                $this->blogURL = $blogURL;
@@ -134,7 +142,9 @@
         */
        public function isKeyValid() {
                // Check to see if the key is valid
-               $response = $this->sendRequest( 'key=' . $this->wordPressAPIKey 
. '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion 
. '/verify-key' );
+               $response = $this->sendRequest( 'key=' . $this->wordPressAPIKey 
.
+                       '&blog=' . $this->blogURL, $this->akismetServer, '/' . 
$this->akismetVersion . '/verify-key' );
+
                return $response[1] == 'valid';
        }
 
@@ -189,16 +199,22 @@
        /**
         * Tests for spam.
         *
-        * Uses the web service provided by {@link http://www.akismet.com 
Akismet} to see whether or not the submitted comment is spam.  Returns a 
boolean value.
+        * Uses the web service provided by {@link http://www.akismet.com 
Akismet}
+        * to see whether or not the submitted comment is spam.  Returns a 
boolean value.
         *
-        * @return    bool    True if the comment is spam, false if not
-        * @throws    MWException Will throw an exception if the API key passed 
to the constructor is invalid.
+        * @return bool True if the comment is spam, false if not
+        * @throws MWException Will throw an exception if the API key passed to 
the
+        *   constructor is invalid.
         */
        public function isCommentSpam() {
-               $response = $this->sendRequest( $this->getQueryString(), 
$this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . 
'/comment-check' );
+               $response = $this->sendRequest(
+                       $this->getQueryString(),
+                       $this->wordPressAPIKey . '.rest.akismet.com', '/' . 
$this->akismetVersion . '/comment-check'
+               );
 
                if ( $response[1] == 'invalid' && !$this->isKeyValid() ) {
-                       throw new MWException( 'The Wordpress API key passed to 
the Akismet constructor is invalid.  Please obtain a valid one from 
http://wordpress.com/api-keys/' );
+                       throw new MWException( 'The Wordpress API key passed to 
the Akismet ' .
+                               'constructor is invalid. Please obtain a valid 
one from http://wordpress.com/api-keys/' );
                }
 
                return ( $response[1] == 'true' );
@@ -207,25 +223,31 @@
        /**
         * Submit spam that is incorrectly tagged as ham.
         *
-        * Using this function will make you a good citizen as it helps Akismet 
to learn from its mistakes.  This will improve the service for everybody.
+        * Using this function will make you a good citizen as it helps Akismet 
to
+        * learn from its mistakes.  This will improve the service for 
everybody.
         */
        public function submitSpam() {
-               $this->sendRequest( $this->getQueryString(), 
$this->wordPressAPIKey . '.' . $this->akismetServer, '/' . 
$this->akismetVersion . '/submit-spam' );
+               $this->sendRequest(
+                       $this->getQueryString(),
+                       $this->wordPressAPIKey . '.' . $this->akismetServer, 
'/' . $this->akismetVersion . '/submit-spam'
+               );
        }
 
        /**
         * Submit ham that is incorrectly tagged as spam.
         *
-        * Using this function will make you a good citizen as it helps Akismet 
to learn from its mistakes.  This will improve the service for everybody.
+        * Using this function will make you a good citizen as it helps Akismet
+        * to learn from its mistakes.  This will improve the service for 
everybody.
         */
        public function submitHam() {
-               $this->sendRequest( $this->getQueryString(), 
$this->wordPressAPIKey . '.' . $this->akismetServer, '/' . 
$this->akismetVersion . '/submit-ham' );
+               $this->sendRequest( $this->getQueryString(), 
$this->wordPressAPIKey .
+                       '.' . $this->akismetServer, '/' . $this->akismetVersion 
. '/submit-ham' );
        }
 
        /**
         * To override the user IP address when submitting spam/ham later on
         *
-        * @param string $userip    An IP address.  Optional.
+        * @param string $userip An IP address.  Optional.
         */
        public function setUserIP( $userip ) {
                $this->comment['user_ip'] = $userip;
@@ -234,7 +256,7 @@
        /**
         * To override the referring page when submitting spam/ham later on
         *
-        * @param string $referrer    The referring page.  Optional.
+        * @param string $referrer The referring page.  Optional.
         */
        public function setReferrer( $referrer ) {
                $this->comment['referrer'] = $referrer;
@@ -243,7 +265,7 @@
        /**
         * A permanent URL referencing the blog post the comment was submitted 
to.
         *
-        * @param string $permalink    The URL.  Optional.
+        * @param string $permalink The URL.  Optional.
         */
        public function setPermalink( $permalink ) {
                $this->comment['permalink'] = $permalink;
@@ -341,9 +363,12 @@
 /**
  * Used internally by Akismet
  *
- * This class is used by Akismet to do the actual sending and receiving of 
data.  It opens a connection to a remote host, sends some data and the reads 
the response and makes it available to the calling program.
+ * This class is used by Akismet to do the actual sending and receiving of 
data.
+ * It opens a connection to a remote host, sends some data and the reads the
+ * response and makes it available to the calling program.
  *
- * The code that makes up this class originates in the Akismet WordPress 
plugin, which is {@link http://akismet.com/download/ available on the Akismet 
website}.
+ * The code that makes up this class originates in the Akismet WordPress 
plugin,
+ * which is {@link http://akismet.com/download/ available on the Akismet 
website}.
  *
  * N.B. It is not necessary to call this class directly to use the Akismet 
class.
  *
@@ -366,10 +391,10 @@
        /**
         *  Sends the data to the remote host.
         *
-        * @param    string    $host            The host to send/receive data.
-        * @param    int        $port            The port on the remote host.
-        * @param    string    $request        The data to send.
-        * @param    int        $responseLength    The amount of data to read.  
Defaults to 1160 bytes.
+        * @param    string $host The host to send/receive data.
+        * @param    int $port The port on the remote host.
+        * @param    string $request The data to send.
+        * @param    int $responseLength The amount of data to read.  Defaults 
to 1160 bytes.
         * @throws    MWException An exception is thrown if a connection cannot 
be made to the remote host.
         * @return    string The server response
         */
@@ -379,11 +404,14 @@
                $fs = fsockopen( $host, $port, $this->errorNumber, 
$this->errorString, 3 );
 
                if ( $this->errorNumber != 0 ) {
-                       throw new MWException( 'Error connecting to host: ' . 
$host . ' Error number: ' . $this->errorNumber . ' Error message: ' . 
$this->errorString );
+                       throw new MWException( 'Error connecting to host: ' . 
$host .
+                               ' Error number: ' . $this->errorNumber . ' 
Error message: ' . $this->errorString );
                }
 
                if ( $fs !== false ) {
-                       @fwrite( $fs, $request );
+                       MediaWiki\suppressWarnings();
+                       fwrite( $fs, $request );
+                       MediaWiki\restoreWarnings();
 
                        while ( !feof( $fs ) ) {
                                $response .= fgets( $fs, $responseLength );
@@ -466,10 +494,10 @@
        /**
         *  Sends the data to the remote host.
         *
-        * @param    string    $host            The host to send/receive data.
-        * @param    int        $port            The port on the remote host.
-        * @param    string    $request        The data to send.
-        * @param    int        $responseLength    The amount of data to read.  
Defaults to 1160 bytes.
+        * @param    string $host The host to send/receive data.
+        * @param    int $port The port on the remote host.
+        * @param    string $request The data to send.
+        * @param    int $responseLength The amount of data to read.  Defaults 
to 1160 bytes.
         * @throws    MWException An exception is thrown if a connection cannot 
be made to the remote host.
         * @return string    The server response
         */
diff --git a/AkismetKlik.i18n.php b/AkismetKlik.i18n.php
deleted file mode 100644
index 7ca570a..0000000
--- a/AkismetKlik.i18n.php
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php
-/**
- * This is a backwards-compatibility shim, generated by:
- * 
https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
- *
- * Beginning with MediaWiki 1.23, translation strings are stored in json files,
- * and the EXTENSION.i18n.php file only exists to provide compatibility with
- * older releases of MediaWiki. For more information about this migration, see:
- * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
- *
- * This shim maintains compatibility back to MediaWiki 1.17.
- */
-$messages = array();
-if ( !function_exists( 'wfJsonI18nShimd6761848c746425e' ) ) {
-       function wfJsonI18nShimd6761848c746425e( $cache, $code, &$cachedData ) {
-               $codeSequence = array_merge( array( $code ), 
$cachedData['fallbackSequence'] );
-               foreach ( $codeSequence as $csCode ) {
-                       $fileName = dirname( __FILE__ ) . "/i18n/$csCode.json";
-                       if ( is_readable( $fileName ) ) {
-                               $data = FormatJson::decode( file_get_contents( 
$fileName ), true );
-                               foreach ( array_keys( $data ) as $key ) {
-                                       if ( $key === '' || $key[0] === '@' ) {
-                                               unset( $data[$key] );
-                                       }
-                               }
-                               $cachedData['messages'] = array_merge( $data, 
$cachedData['messages'] );
-                       }
-
-                       $cachedData['deps'][] = new FileDependency( $fileName );
-               }
-               return true;
-       }
-
-       $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = 
'wfJsonI18nShimd6761848c746425e';
-}
diff --git a/AkismetKlik.php b/AkismetKlik.php
index cf1000e..ae5ee03 100644
--- a/AkismetKlik.php
+++ b/AkismetKlik.php
@@ -4,9 +4,9 @@
 }
 
 # Include PHP5 Akismet class from http://www.achingbrain.net/stuff/akismet 
(GPL)
-require_once( 'Akismet.class.php' );
+require_once 'Akismet.class.php';
 
-#Extension credits
+# Extension credits
 $wgExtensionCredits['other'][] = array(
        'path' => __FILE__,
        'name' => 'AkismetKlik',
@@ -15,17 +15,14 @@
        'descriptionmsg' => 'akismetklik-desc',
 );
 
-$dir = dirname( __FILE__ ) . '/';
 $wgMessagesDirs['AkismetKlik'] = __DIR__ . '/i18n';
-$wgExtensionMessagesFiles['AkismetKlik'] = $dir . 'AkismetKlik.i18n.php';
 
 # Set site-specific configuration values
 $wgAKkey = '';
 $wgAKSiteUrl = '';
 
-#
 # MediaWiki hooks
-#
+
 # Loader for spam blacklist feature
 # Include this from LocalSettings.php
 $wgHooks['EditFilterMerged'][] = 'wfAkismetFilterMerged';
@@ -42,6 +39,7 @@
                $spamObj = new AkismetKlik( $wgSpamBlacklistSettings );
                $spamObj->previousFilter = $wgPreSpamFilterCallback;
        }
+
        return $spamObj;
 }
 
@@ -54,6 +52,7 @@
 function wfAkismetFilterMerged( $editPage, $text ) {
        $spamObj = new AkismetKlik();
        $ret = $spamObj->filter( $editPage->getArticle()->getTitle(), $text, 
'', $editPage );
+
        return !$ret;
 }
 
@@ -61,7 +60,6 @@
  * This class provides the interface to the filters
  */
 class AkismetKlik {
-
        public $previousFilter;
 
        /**
@@ -83,7 +81,10 @@
         * If the return value is true, an error will have been sent to $wgOut
         */
        function filter( $title, $text, $section, $editPage ) {
-               global $wgParser, $wgUser, $wgAKSiteUrl, $wgAKkey, $IP;
+               global $wgUser, $wgAKSiteUrl, $wgAKkey;
+               // @codingStandardsIgnoreStart
+               global $IP;
+               // @codingStandardsIgnoreEnd
 
                if ( strlen( $wgAKkey ) == 0 ) {
                        throw new MWException( 'Set $wgAKkey in 
LocalSettings.php or relevant configuration file.' );
@@ -100,7 +101,7 @@
                $links = implode( "\n", array_keys( $out->getExternalLinks() ) 
);
 
                # Do the match
-               if ( $wgUser->mName == "" ) {
+               if ( $wgUser->mName == '' ) {
                        $user = $IP;
                } else {
                        $user = $wgUser->mName;
@@ -110,15 +111,17 @@
                $akismet->setCommentAuthorEmail( $wgUser->getEmail() );
                $akismet->setCommentAuthorURL( $links );
                $akismet->setCommentContent( $text );
-               $akismet->setCommentType( "wiki" );
+               $akismet->setCommentType( 'wiki' );
                $akismet->setPermalink( $wgAKSiteUrl . '/wiki/' . $pgtitle );
                if ( $akismet->isCommentSpam() && !$wgUser->isAllowed( 
'bypassakismet' ) ) {
                        wfDebugLog( 'AkismetKlik', "Match!\n" );
-                       $editPage->spamPageWithContent( "http://akismet.com 
blacklist error" );
+                       $editPage->spamPageWithContent( 'http://akismet.com 
blacklist error' );
                        wfProfileOut( __METHOD__ );
+
                        return true;
                }
                wfProfileOut( __METHOD__ );
+
                return false;
        }
 }
diff --git a/Gruntfile.js b/Gruntfile.js
new file mode 100644
index 0000000..9c56558
--- /dev/null
+++ b/Gruntfile.js
@@ -0,0 +1,20 @@
+/*jshint node:true */
+module.exports = function ( grunt ) {
+       grunt.loadNpmTasks( 'grunt-banana-checker' );
+       grunt.loadNpmTasks( 'grunt-jsonlint' );
+
+       grunt.initConfig( {
+               banana: {
+                       all: 'i18n/'
+               },
+               jsonlint: {
+                       all: [
+                               '**/*.json',
+                               '!node_modules/**'
+                       ]
+               }
+       } );
+
+       grunt.registerTask( 'test', [ 'jsonlint', 'banana' ] );
+       grunt.registerTask( 'default', 'test' );
+};
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..4365e8a
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,12 @@
+{
+       "require-dev": {
+               "jakub-onderka/php-parallel-lint": "0.9",
+               "mediawiki/mediawiki-codesniffer": "0.4.0"
+       },
+       "scripts": {
+               "test": [
+                       "parallel-lint . --exclude vendor",
+                       "phpcs -p -s"
+               ]
+       }
+}
diff --git a/i18n/en.json b/i18n/en.json
index 7af8cee..42e290e 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -1,8 +1,8 @@
 {
-    "@metadata": {
-        "authors": [
-            "Carl Austin Bennett"
-        ]
-    },
-    "akismetklik-desc": "Rejects edits from suspected comment spammers on 
Akismet's blacklist"
+       "@metadata": {
+               "authors": [
+                       "Carl Austin Bennett"
+               ]
+       },
+       "akismetklik-desc": "Rejects edits from suspected comment spammers on 
Akismet's blacklist"
 }
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..76e8a82
--- /dev/null
+++ b/package.json
@@ -0,0 +1,12 @@
+{
+  "private": true,
+  "scripts": {
+    "test": "grunt test"
+  },
+  "devDependencies": {
+    "grunt": "0.4.5",
+    "grunt-cli": "0.1.13",
+    "grunt-banana-checker": "0.2.2",
+    "grunt-jsonlint": "1.0.4"
+  }
+}
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
index 0000000..d81a292
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<ruleset>
+       <rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/>
+       <file>.</file>
+       <arg name="extensions" value="php,php5,inc"/>
+       <arg name="encoding" value="utf8"/>
+       <exclude-pattern>vendor</exclude-pattern>
+</ruleset>

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I17975bc28b4a56bf0dd834be68964a6b00e190f5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/AkismetKlik
Gerrit-Branch: master
Gerrit-Owner: Siebrand <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to