jenkins-bot has submitted this change and it was merged.

Change subject: Expose ores scores in js
......................................................................


Expose ores scores in js

Bug: T143611
Change-Id: I42300f9bad536e8dcb2fa2f4025051f70ee1f805
---
M includes/Hooks.php
M tests/phpunit/includes/HooksTest.php
2 files changed, 81 insertions(+), 8 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Hooks.php b/includes/Hooks.php
index 48c52a6..f930b5a 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -11,6 +11,7 @@
 use FormOptions;
 use JobQueueGroup;
 use Html;
+use IContextSource;
 use MediaWiki\Logger\LoggerFactory;
 use OutputPage;
 use RCCacheEntry;
@@ -191,7 +192,7 @@
                        return true;
                }
 
-               self::processRecentChangesList( $rcObj, $data, $classes );
+               self::processRecentChangesList( $rcObj, $data, $classes, 
$ecl->getContext() );
 
                return true;
        }
@@ -213,7 +214,8 @@
                        return true;
                }
 
-               self::processRecentChangesList( $rcObj, $data );
+               $classes = [];
+               self::processRecentChangesList( $rcObj, $data, $classes, 
$ecl->getContext() );
 
                return true;
        }
@@ -239,7 +241,7 @@
                        return true;
                }
 
-               $damaging = self::getScoreRecentChangesList( $rc );
+               $damaging = self::getScoreRecentChangesList( $rc, 
$changesList->getContext() );
                if ( $damaging ) {
                        $separator = ' <span class="mw-changeslist-separator">. 
.</span> ';
                        if ( strpos( $s, $separator ) === false ) {
@@ -319,6 +321,8 @@
                        return true;
                }
 
+               self::addRowData( $context, $row->rev_id, 
(float)$row->ores_damaging_score, 'damaging' );
+
                if ( $row->ores_damaging_score > $row->ores_damaging_threshold 
) {
                        // Prepend the "r" flag
                        array_unshift( $flags, ChangesList::flag( 'damaging' ) 
);
@@ -388,9 +392,10 @@
        protected static function processRecentChangesList(
                RCCacheEntry $rcObj,
                array &$data,
-               array &$classes = []
+               array &$classes = [],
+               IContextSource $context
        ) {
-               $damaging = self::getScoreRecentChangesList( $rcObj );
+               $damaging = self::getScoreRecentChangesList( $rcObj, $context );
                if ( $damaging ) {
                        $classes[] = 'damaging';
                        $data['recentChangesFlags']['damaging'] = true;
@@ -400,9 +405,10 @@
        /**
         * Check if we should flag a row
         * @param RecentChange $rcObj
+        * @param IContextSource $context
         * @return bool
         */
-       public static function getScoreRecentChangesList( $rcObj ) {
+       public static function getScoreRecentChangesList( $rcObj, 
IContextSource $context ) {
                global $wgUser;
                $threshold = $rcObj->getAttribute( 'ores_damaging_threshold' );
                if ( $threshold === null ) {
@@ -410,6 +416,18 @@
                }
                $score = $rcObj->getAttribute( 'ores_damaging_score' );
                $patrolled = $rcObj->getAttribute( 'rc_patrolled' );
+
+               if ( !$score ) {
+                       // Shorten out
+                       return false;
+               }
+
+               self::addRowData(
+                       $context,
+                       $rcObj->getAttribute( 'rc_this_oldid' ),
+                       (float)$score,
+                       'damaging'
+               );
 
                return $score && $score >= $threshold && !$patrolled;
        }
@@ -481,6 +499,13 @@
                if ( !self::oresEnabled( $out->getUser() ) ) {
                        return true;
                }
+
+               $oresData = $out->getProperty( 'oresData' );
+
+               if ( $oresData !== null ) {
+                       $out->addJsConfigVars( 'oresData', $oresData );
+               }
+
                $out->addModuleStyles( 'ext.ores.styles' );
                return true;
        }
@@ -525,4 +550,22 @@
                global $wgOresModels;
                return isset( $wgOresModels[$model] ) && $wgOresModels[$model];
        }
+
+       /**
+        * @param IContextSource $context
+        * @param int $revisionId
+        * @param float $score
+        * @param string $model
+        */
+       private static function addRowData( IContextSource $context, 
$revisionId, $score, $model ) {
+               $out = $context->getOutput();
+               $data = $out->getProperty( 'oresData' );
+               if ( !isset( $data[$revisionId] ) ) {
+                       $data[$revisionId] = [];
+               }
+               $data[$revisionId][$model] = $score;
+               $out->setProperty( 'oresData', $data );
+       }
+
 }
+
diff --git a/tests/phpunit/includes/HooksTest.php 
b/tests/phpunit/includes/HooksTest.php
index 77353fa..e8bb7b6 100644
--- a/tests/phpunit/includes/HooksTest.php
+++ b/tests/phpunit/includes/HooksTest.php
@@ -4,8 +4,10 @@
 use ChangesListSpecialPage;
 use EnhancedChangesList;
 use FormOptions;
+use IContextSource;
 use RCCacheEntry;
 use RecentChange;
+use RequestContext;
 use ORES;
 
 /**
@@ -15,6 +17,8 @@
 class OresHooksTest extends \MediaWikiTestCase {
        protected $user;
 
+       protected $context;
+
        protected function setUp() {
                parent::setUp();
 
@@ -22,6 +26,8 @@
                $this->user->setOption( 'ores-enabled', "1" );
                $this->user->setOption( 'oresDamagingPref', 'soft' );
                $this->user->saveSettings();
+
+               $this->context = self::getContext();
        }
 
        public function testOresEnabled() {
@@ -48,11 +54,11 @@
                $row->rc_deleted = 0;
 
                $rc = RecentChange::newFromRow( $row );
-               $this->assertTrue( ORES\Hooks::getScoreRecentChangesList( $rc ) 
);
+               $this->assertTrue( ORES\Hooks::getScoreRecentChangesList( $rc, 
$this->context ) );
 
                $row->ores_damaging_threshold = 0.4;
                $rc = RecentChange::newFromRow( $row );
-               $this->assertFalse( ORES\Hooks::getScoreRecentChangesList( $rc 
) );
+               $this->assertFalse( ORES\Hooks::getScoreRecentChangesList( $rc, 
$this->context ) );
        }
 
        public function testOnChangesListSpecialPageFilters() {
@@ -62,6 +68,10 @@
                $clsp->expects( $this->any() )
                        ->method( 'getUser' )
                        ->will( $this->returnValue( $this->user ) );
+
+               $clsp->expects( $this->any() )
+                       ->method( 'getContext' )
+                       ->will( $this->returnValue( $this->context ) );
 
                ORES\Hooks::onChangesListSpecialPageFilters( $clsp, $filters );
                $expected = [
@@ -146,6 +156,10 @@
                        ->method( 'getUser' )
                        ->will( $this->returnValue( $this->user ) );
 
+               $ecl->expects( $this->any() )
+                       ->method( 'getContext' )
+                       ->will( $this->returnValue( $this->context ) );
+
                $data = [];
                $block = [];
                $classes = [];
@@ -175,6 +189,10 @@
                        ->method( 'getUser' )
                        ->will( $this->returnValue( $this->user ) );
 
+               $ecl->expects( $this->any() )
+                       ->method( 'getContext' )
+                       ->will( $this->returnValue( $this->context ) );
+
                $data = [];
                $block = [];
                $classes = [];
@@ -185,4 +203,16 @@
                $this->assertSame( [], $block );
                $this->assertSame( [], $classes );
        }
+
+       /**
+        * @return IContextSource
+        */
+       private static function getContext() {
+
+               $context = new RequestContext();
+
+               $context->setLanguage( 'en' );
+
+               return $context;
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I42300f9bad536e8dcb2fa2f4025051f70ee1f805
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Catrope <r...@wikimedia.org>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Thiemo M├Ąttig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to