Bsitu has uploaded a new change for review.

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

Change subject: [WIP]Add TargetPage model and mapper to notifications
......................................................................

[WIP]Add TargetPage model and mapper to notifications

This will be used for marking a notificaiton as read when
a user visits a target page

Change-Id: I605cbc79adfc12d22bd889c5bb513d05c479fe6e
---
M Echo.php
M Hooks.php
A db_patches/echo_notification_target_page.sql
M echo.sql
A includes/mapper/TargetPageMapper.php
A model/TargetPage.php
A tests/includes/mapper/TargetPageMapperTest.php
A tests/model/TargetPageTest.php
8 files changed, 289 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/92/151992/1

diff --git a/Echo.php b/Echo.php
index 02c92af..8aa3194 100644
--- a/Echo.php
+++ b/Echo.php
@@ -51,6 +51,7 @@
 $wgAutoloadClasses['EchoHooks'] = $dir . 'Hooks.php';
 $wgAutoloadClasses['EchoEvent'] = $dir . 'model/Event.php';
 $wgAutoloadClasses['EchoNotification'] = $dir . 'model/Notification.php';
+$wgAutoloadClasses['EchoTargetPage'] = $dir . 'model/TargetPage.php';
 $wgAutoloadClasses['MWEchoEmailBatch'] = $dir . 'includes/EmailBatch.php';
 $wgAutoloadClasses['MWDbEchoEmailBatch'] = $dir . 'includes/DbEmailBatch.php';
 $wgAutoloadClasses['MWEchoEmailBundler'] = $dir . 'includes/EmailBundler.php';
@@ -61,6 +62,7 @@
 // Database mappers && gateways
 $wgAutoloadClasses['EchoEventMapper'] = $dir . 
'includes/mapper/EventMapper.php';
 $wgAutoloadClasses['EchoNotificationMapper'] = $dir . 
'includes/mapper/NotificationMapper.php';
+$wgAutoloadClasses['EchoTargetPageMapper'] = $dir . 
'includes/mapper/EchoTargetPageMapper.php';
 $wgAutoloadClasses['EchoUserNotificationGateway'] = $dir . 
'includes/gateway/UserNotificationGateway.php';
 
 // Output formatters
diff --git a/Hooks.php b/Hooks.php
index 8f98c58..5112c56 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -63,6 +63,7 @@
                $baseSQLFile = "$dir/echo.sql";
                $updater->addExtensionTable( 'echo_event', $baseSQLFile );
                $updater->addExtensionTable( 'echo_email_batch', 
"$dir/db_patches/echo_email_batch.sql" );
+               $updater->addExtensionTable( 'echo_target_page', 
"$dir/db_patches/echo_target_page.sql" );
 
                if ( $updater->getDB()->getType() === 'sqlite' ) {
                        $updater->modifyExtensionField( 'echo_event', 
'event_agent', "$dir/db_patches/patch-event_agent-split.sqlite.sql" );
diff --git a/db_patches/echo_notification_target_page.sql 
b/db_patches/echo_notification_target_page.sql
new file mode 100644
index 0000000..f68af4b
--- /dev/null
+++ b/db_patches/echo_notification_target_page.sql
@@ -0,0 +1,8 @@
+CREATE TABLE /*_*/echo_target_page (
+       etp_id int unsigned not null primary key auto_increment,
+       etp_user int unsigned not null default 0,
+       etp_page int unsigned not null default 0,
+       etp_event int unsigned not null default 0
+) /*$wgDBTableOptions*/;
+
+CREATE UNIQUE INDEX /*i*/echo_target_page_user_page_event ON 
/*_*/echo_target_page (etp_user, etp_page, etp_event);
diff --git a/echo.sql b/echo.sql
index 7b3f388..7fd7067 100644
--- a/echo.sql
+++ b/echo.sql
@@ -41,3 +41,12 @@
 
 CREATE UNIQUE INDEX /*i*/echo_email_batch_user_event ON /*_*/echo_email_batch 
(eeb_user_id,eeb_event_id);
 CREATE INDEX /*i*/echo_email_batch_user_hash_priority ON /*_*/echo_email_batch 
(eeb_user_id, eeb_event_hash, eeb_event_priority);
+
+CREATE TABLE /*_*/echo_target_page (
+       etp_id int unsigned not null primary key auto_increment,
+       etp_user int unsigned not null default 0,
+       etp_page int unsigned not null default 0,
+       etp_event int unsigned not null default 0
+) /*$wgDBTableOptions*/;
+
+CREATE UNIQUE INDEX /*i*/echo_target_page_user_page_event ON 
/*_*/echo_target_page (etp_user, etp_page, etp_event);
diff --git a/includes/mapper/TargetPageMapper.php 
b/includes/mapper/TargetPageMapper.php
new file mode 100644
index 0000000..3ca6d77
--- /dev/null
+++ b/includes/mapper/TargetPageMapper.php
@@ -0,0 +1,101 @@
+<?php
+
+/**
+ * Database mapper for TargetPage model
+ */
+class EchoTargetPageMapper {
+
+       /**
+        * Echo database factory
+        * @param MWEchoDbFactory
+        */
+       protected $dbFactory;
+
+       /**
+        * @param MWEchoDbFactory
+        */
+       public function __construct( MWEchoDbFactory $dbFactory ) {
+               $this->dbFactory = $dbFactory;
+       }
+
+       /**
+        * Fetch an EchoTargetPage instance by User & page id
+        *
+        * @param User $user
+        * @param int $pageId
+        * @return EchoTargetPage[]|boolean
+        */
+       public function fetchByUserPageId( User $user, $pageId ) {
+               $dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
+
+               $res = $dbr->select(
+                       array( 'echo_target_page' ),
+                       array( '*' ),
+                       array(
+                               'etp_user' => $user->getId(),
+                               'etp_page' => $pageId
+                       ),
+                       __METHOD__
+               );
+               if ( $res ) {
+                       $targetPage = array();
+                       foreach ( $res as $row ) {
+                               $targetPage[] = EchoTargetPage::newFromRow( 
$row );
+                       }
+                       return $targetPage;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * Insert an EchoTargetPage instance into the database
+        *
+        * @param EchoTargetPage $targetPage
+        * @return int|boolean
+        */
+       public function insert( EchoTargetPage $targetPage ) {
+               $dbw = $this->dbFactory->getEchoDb( DB_MASTER );
+
+               $id = $dbw->nextSequenceValue( 'etp_id' );
+
+               $row = $event->toDbArray();
+               if ( $id ) {
+                       $row['etp_id'] = $id;
+               }
+
+               $res = $dbw->insert( 'echo_target_page', $row, __METHOD__ );
+
+               if ( $res ) {
+                       if ( !$id ) {
+                               $id = $dbw->insertId();
+                       }
+                       return $id;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * Delete an EchoTargetPage instance from the database
+        *
+        * @param EchoTargetPage
+        * @return boolean
+        */
+       public function delete( EchoTargetPage $targetPage ) {
+               $dbw = $this->dbFactory->getEchoDb( DB_MASTER );
+
+               $res = $dbw->delete(
+                       'echo_target_page',
+                       array(
+                               'etp_user' => $targetPage->getUserId(),
+                               'etp_page' => $targetPage->getPageId(),
+                               'etp_event' => $targetPage->getEventId()
+                       ),
+                       __METHOD__
+               );
+               return $res;
+       }
+}
+
+
diff --git a/model/TargetPage.php b/model/TargetPage.php
new file mode 100644
index 0000000..ed50696
--- /dev/null
+++ b/model/TargetPage.php
@@ -0,0 +1,114 @@
+<?php
+
+/**
+ * Map a title to an echo notification so that we can mark a notification as 
read
+ * when visiting the page. This only supports titles with ids because majority
+ * of notifications have pageIds and searching by namespace and title is slow
+ */
+class EchoTargetPage {
+
+       /**
+        * @var User
+        */
+       protected $user;
+
+       /**
+        * @var Title|null
+        */
+       protected $title;
+
+       /**
+        * @var int
+        */
+       protected $pageId;
+
+       /**
+        * @var EchoEvent|null
+        */
+       protected $event;
+
+       /**
+        * @var int
+        */
+       protected $eventId;
+
+       /**
+        * Only allow creating instance internally
+        */
+       protected function __construct() {}
+
+       /**
+        * Create an instance from stdClass object
+        * @param stdClass $row
+        * @return EchoTargetPage
+        */
+       public static function newFromRow( $row ) {
+               $requiredFields = array (
+                       'etp_user',
+                       'etp_page',
+                       'etp_event'
+               );
+               foreach ( $requiredFields as $field ) {
+                       if ( !isset( $row->$field ) || !$row->$field ) {
+                               throw new MWException( $field . ' is not set in 
the row!' );
+                       }
+               }
+               $obj = new self();
+               $obj->user = User::newFromId( $row->etp_user );
+               $obj->pageId = $row->etp_page;
+               $obj->eventId = $row->etp_event;
+               return $obj;
+       }
+
+       /**
+        * @return User
+        */
+       public function getUser() {
+               return $this->user;     
+       }
+
+       /**
+        * @return Title|null
+        */
+       public function getTitle() {
+               if ( !$this->title ) {
+                       $this->title = Title::newFromId( $this->pageId );
+               }
+               return $this->title;
+       }
+
+       /**
+        * @return int
+        */
+       public function getPageId() {
+               return $this->pageId;
+       }
+
+       /**
+        * @return EchoEvent
+        */
+       public function getEvent() {
+               if ( !$this->event ) {
+                       $this->event = EchoEvent::newFromID( $this->eventId );
+               }
+               return $this->event;
+       }
+
+       /**
+        * @return id
+        */
+       public function getEventId() {
+               return $this->eventId;
+       }
+
+       /**
+        * Convert the properties to a database row
+        */
+       public function toDbArray() {
+               return array (
+                       'etp_user' => $this->user->getId(),
+                       'etp_page' => $this->pageId,
+                       'etp_event' => $this->eventId
+               );
+       }
+}
diff --git a/tests/includes/mapper/TargetPageMapperTest.php 
b/tests/includes/mapper/TargetPageMapperTest.php
new file mode 100644
index 0000000..d4b30da
--- /dev/null
+++ b/tests/includes/mapper/TargetPageMapperTest.php
@@ -0,0 +1,17 @@
+<?php
+
+class EchoTargetPageMapperTest extends MediaWikiTestCase {
+
+       public function testFetchByUserPageId() {
+
+       }
+
+       public function testInsert() {
+
+       }
+
+       public function testDelete() {
+               
+       }
+
+}
diff --git a/tests/model/TargetPageTest.php b/tests/model/TargetPageTest.php
new file mode 100644
index 0000000..3994791
--- /dev/null
+++ b/tests/model/TargetPageTest.php
@@ -0,0 +1,37 @@
+<?php
+
+class EchoTargetPageTest extends MediaWikiTestCase {
+
+       public function testNewFromRow() {
+               $row = (object) array (
+                       'etp_user' => 1,
+                       'etp_page' => 2,
+                       'etp_event' => 3
+               );
+               $obj = EchoTargetPage::newFromRow( $row );
+               $this->assertInstanceOf( 'EchoTargetPage', $obj );
+               return $obj;
+       }
+
+       /**
+        * @expectedException MWException
+        */
+       public function testNewFromRowWithException() {
+               $row = (object) array (
+                       'etp_page' => 2,
+                       'etp_event' => 3
+               );
+               $this->assertInstanceOf( 'EchoTargetPage', 
EchoTargetPage::newFromRow( $row ) );        
+       }
+
+       /**
+        * @depends testNewFromRow
+        */
+       public function testToDbArray( $obj ) {
+               $row = $obj->toDbArray();
+               $this->assertTrue( is_array( $row ) );
+               $this->assertArrayHasKey( 'etp_user', $row );
+               $this->assertArrayHasKey( 'etp_page', $row );
+               $this->assertArrayHasKey( 'etp_event', $row );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I605cbc79adfc12d22bd889c5bb513d05c479fe6e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>

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

Reply via email to