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

Change subject: Added the update class
......................................................................


Added the update class

Also, added the helper class for fetching the annotations which fixes bug 52072.

Fixed spelling in ApiAnnotatorCreate

Bug: 52072
Change-Id: Ia2b75ff666dc6e0fe2299c1611a29a7a4b855dcf
---
A AnnotationRepository.php
M Annotator.php
M api/ApiAnnotatorCreate.php
M api/ApiAnnotatorRead.php
M api/ApiAnnotatorSearch.php
A api/ApiAnnotatorUpdate.php
M modules/Annotator.js
7 files changed, 153 insertions(+), 50 deletions(-)

Approvals:
  Mattflaschen: Looks good to me, but someone else must approve
  Parent5446: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/AnnotationRepository.php b/AnnotationRepository.php
new file mode 100644
index 0000000..68b87ac
--- /dev/null
+++ b/AnnotationRepository.php
@@ -0,0 +1,53 @@
+<?php
+class AnnotationRepository {
+       public function get( $annotation_id ) {
+
+               $dbr = wfGetDB( DB_SLAVE );
+               //select the annotation object from the database
+               $annotation_json = $dbr->selectField(
+                       'annotator',
+                       'annotation_json',
+                       array(
+                               'annotation_id' => $annotation_id
+                               )
+                       );
+
+               if( $annotation_json === false ) {
+                       return null;
+               }
+
+               $annotation = AnnotationRepository::populateAnnotation( 
$annotation_json, $annotation_id );
+               return $annotation;
+       }
+
+       public function getAllByRevid( $revid ) {
+               //selects annotations of a particular revision ID
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select(
+                       'annotator',
+                       array(
+                               'annotation' => 'annotation_json',
+                               'id' => 'annotation_id'
+                               ),
+                       array(
+                               'rev_id' => $revid
+                               )
+                       );
+
+               $annotations = array();
+               $annotations['rows'] = array();
+               $total = 0;
+               foreach($res as $result) {
+                       $annotations['rows'][] = 
AnnotationRepository::populateAnnotation( $result->annotation, $result->id );
+                       $total = $total + 1;
+               }
+               $annotations['total'] = $total;
+               return $annotations;
+       }
+
+       protected function populateAnnotation( $annotation_json, $annotation_id 
) {
+               $annotation = json_decode($annotation_json);
+               $annotation->id = $annotation_id; //update the annotation 
object with the ID
+               return $annotation;
+       }
+}
\ No newline at end of file
diff --git a/Annotator.php b/Annotator.php
index 7b019fb..94711c0 100755
--- a/Annotator.php
+++ b/Annotator.php
@@ -37,6 +37,8 @@
 $wgAutoloadClasses['ApiAnnotatorRead'] = $dir . 'api/ApiAnnotatorRead.php';
 $wgAutoloadClasses['ApiAnnotatorSearch'] = $dir . 'api/ApiAnnotatorSearch.php';
 $wgAutoloadClasses['ApiAnnotatorDestroy'] = $dir . 
'api/ApiAnnotatorDestroy.php';
+$wgAutoloadClasses['ApiAnnotatorUpdate'] = $dir . 'api/ApiAnnotatorUpdate.php';
+$wgAutoloadClasses['AnnotationRepository'] = $dir . 'AnnotationRepository.php';
 
 //Hooks
 $wgHooks['BeforePageDisplay'][] = 'AnnotatorHooks::onBeforePageDisplay';
@@ -46,5 +48,6 @@
 $wgAPIModules['annotator-read'] = 'ApiAnnotatorRead';
 $wgAPIModules['annotator-search'] = 'ApiAnnotatorSearch';
 $wgAPIModules['annotator-destroy'] = 'ApiAnnotatorDestroy';
+$wgAPIModules['annotator-update'] = 'ApiAnnotatorUpdate';
 
 unset( $dir );
diff --git a/api/ApiAnnotatorCreate.php b/api/ApiAnnotatorCreate.php
index 257796e..e4847d8 100755
--- a/api/ApiAnnotatorCreate.php
+++ b/api/ApiAnnotatorCreate.php
@@ -6,7 +6,7 @@
 
                //If the annotation object is null, then die
                if( $annotation == null ) {
-                       $this->dieUsage( "No annotation object received", 
'annotation_not_recieved', 500 );
+                       $this->dieUsage( "No annotation object received", 
'annotation_not_received', 500 );
                }
 
                //get the user object
diff --git a/api/ApiAnnotatorRead.php b/api/ApiAnnotatorRead.php
index c206858..9176057 100644
--- a/api/ApiAnnotatorRead.php
+++ b/api/ApiAnnotatorRead.php
@@ -4,34 +4,17 @@
                $params = $this->extractRequestParams();
                $annotation_id = $params['id']; //get the annotation ID
 
-               $dbr = wfGetDB( DB_SLAVE );
-               //select the annotation object from the database
-               $res = $dbr->select(
-                               'annotator',
-                               array('annotation_json'),
-                               array(
-                                       'annotation_id' => $annotation_id
-                                       )
-                               );
+               $repository = new AnnotationRepository();
+               $annotation = $repository->get( $annotation_id );
 
-               $row = $dbr->fetchObject( $res );
-
-               //return 404 if annotation is not found
-               if( !$row ) {
+               if( $annotation === null ) {
                        $this->dieUsage( "No annotation found", 
'annotation_not_found', 404 );
                }
-
-               $annotation = json_decode($row->annotation_json);
-
                $result = $this->getResult();
-
-               $result->addValue( null, 'id', $annotation_id );
-               $result->addValue( null, 'text' , $annotation->text );
-               $result->addValue( null, 'quote', $annotation->quote );
-               $result->addValue( 'ranges', 'start', 
$annotation->ranges[0]->start );
-               $result->addValue( 'ranges', 'startOffset', 
$annotation->ranges[0]->startOffset );
-               $result->addValue( 'ranges', 'end', $annotation->ranges[0]->end 
);
-               $result->addValue( 'ranges', 'endOffset', 
$annotation->ranges[0]->endOffset );
+               
+               foreach( $annotation as $k => &$v ) {
+                       $result->addValue( null, $k, $v );
+               }
                return true;
        }
 
diff --git a/api/ApiAnnotatorSearch.php b/api/ApiAnnotatorSearch.php
index 8408d65..2b50869 100644
--- a/api/ApiAnnotatorSearch.php
+++ b/api/ApiAnnotatorSearch.php
@@ -10,32 +10,14 @@
                        $this->dieUsage( "The revision ID is not valid", 
'invalid_revision_id', 404 );
                }
 
-               //selects annotations of a particular revision ID
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select(
-                       'annotator',
-                       array(
-                               'annotation' => 'annotation_json',
-                               'id' => 'annotation_id'
-                               ),
-                       array(
-                               'rev_id' => $revid
-                               )
-                       );
+               $repository = new AnnotationRepository();
+               $annotations = $repository->getAllByRevid( $revid );
 
-               $annotations = array();
-               $annotations['rows'] = array();
-               $total = 0;
-               foreach($res as $result) {
-                       $annotation = json_decode($result->annotation);
-                       $annotation->id = $result->id; //update the annotation 
object with the annotation ID
-                       $annotations['rows'][] = $annotation;
-                       $total = $total + 1;
-               }
-               $annotations['total'] = $total;
                $result = $this->getResult();
-               $result->addValue( null, 'rows' , $annotations['rows'] );
-               $result->addValue( null, 'total', $total );
+
+               foreach( $annotations as $k => &$v ) {
+                       $result->addValue( null, $k, $v );
+               }
                return true;
        }
 
diff --git a/api/ApiAnnotatorUpdate.php b/api/ApiAnnotatorUpdate.php
new file mode 100644
index 0000000..743c676
--- /dev/null
+++ b/api/ApiAnnotatorUpdate.php
@@ -0,0 +1,82 @@
+<?php
+class ApiAnnotatorUpdate extends ApiBase {
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $id = $params['id'];
+
+               //get the annotation object
+               $annotation_json = $this->getRawPostString();
+
+               //If the annotation object is null, then die
+               if( $annotation_json == null ) {
+                       $this->dieUsage( "No annotation object received", 
'annotation_not_received', 500 );
+               }
+
+               //get the user object
+               $user = $this->getUser();
+
+               //checks user log in
+               if( !$user->isLoggedIn() ) {
+                       $this->dieUsage( "Log in to update annotation", 
'user_not_logged_in', 401 );
+               }
+               else {
+                       $userId = $user->getId();
+               }
+
+               $annotation_json = json_decode($annotation_json);
+               unset($annotation_json->id); //strip out the id element
+               $annotation_json = json_encode($annotation_json);
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin(); //lock the annotation in the db
+               $user_id = $dbw->selectField(
+                       'annotator',
+                       'user_id',
+                       array(
+                               'annotation_id' => $id
+                               ),
+                       __METHOD__,
+                       array( 'FOR UPDATE' )
+                       );
+
+               //return 404 if the id is not valid
+               if( $user_id === false ) {
+                       $dbw->rollback();
+                       $this->dieUsage( "No annotation found", 
'annotation_not_found', 404 );
+               }
+
+               //checks if the user_id is of the same user who created the 
annotation
+               if( $userId !== (int) $user_id ) {
+                       $dbw->rollback();
+                       $this->dieUsage( "You don't have permissions to update 
this annotation", 'user_not_authorized', 401 );
+               }
+               
+               $dbw->update(
+                       'annotator',
+                       array(
+                               'annotation_json' => $annotation_json
+                               ),
+                       array(
+                               'annotation_id' => $id
+                               )
+                       );
+               $dbw->commit(); //release the lock
+               $url = wfExpandUrl( wfScript( 'api' ) . 
'?action=annotator-read&format=json&id=' . $id, PROTO_CURRENT );
+               $response = $this->getRequest()->response();
+               $response->header( "Location: {$url}", true, 303 );
+               return true;
+       }
+
+       public function getRawPostString() {
+               return file_get_contents('php://input');
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'id' =>array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_REQUIRED => true
+                               )
+                       );
+       }
+}
\ No newline at end of file
diff --git a/modules/Annotator.js b/modules/Annotator.js
index 7733e99..5bce970 100755
--- a/modules/Annotator.js
+++ b/modules/Annotator.js
@@ -14,7 +14,7 @@
                        prefix: mw.util.wikiScript( 'api' ),
                        urls: {
                                create: 
'?action=annotator-create&format=json&revid=' + revid,
-                               update: '',
+                               update: 
'?action=annotator-update&format=json&id=:id',
                                read: 
'?action=annotator-read&format=json&id=:id',
                                destroy: 
'?action=annotator-destroy&format=json&id=:id',
                                search: '?action=annotator-search&format=json'

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia2b75ff666dc6e0fe2299c1611a29a7a4b855dcf
Gerrit-PatchSet: 15
Gerrit-Project: mediawiki/extensions/Annotator
Gerrit-Branch: master
Gerrit-Owner: Rjain <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: Rjain <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to