Back to index

d-push  2.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes
ImportChangesDiff Class Reference
Inheritance diagram for ImportChangesDiff:
Inheritance graph
[legend]
Collaboration diagram for ImportChangesDiff:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ImportChangesDiff ($backend, $folderid=false)
 Constructor.
 LoadConflicts ($contentparameters, $state)
 Would load objects which are expected to be exported with this state The DiffBackend implements conflict detection on the fly.
 ImportMessageChange ($id, $message)
 Imports a single message.
 ImportMessageDeletion ($id)
 Imports a deletion.
 ImportMessageReadFlag ($id, $flags)
 Imports a change in 'read' flag This can never conflict.
 ImportMessageMove ($id, $newfolder)
 Imports a move of a message.
 ImportFolderChange ($folder)
 Imports a change on a folder.
 ImportFolderDeletion ($id, $parent=false)
 Imports a folder deletion.
 Config ($state, $flags=0)
 Initializes the state.
 GetState ()
 Returns state.

Static Public Member Functions

static RowCmp ($a, $b)
 
DiffState specific stuff

Protected Member Functions

 getDiffTo ($new)
 Differential mechanism Compares the current syncstate to the sent $new.
 updateState ($type, $change)
 Update the state to reflect changes.
 isConflict ($type, $folderid, $id)
 Returns TRUE if the given ID conflicts with the given operation.

Protected Attributes

 $syncstate
 $backend
 $flags

Private Attributes

 $folderid

Detailed Description

Definition at line 45 of file importchangesdiff.php.


Member Function Documentation

DiffState::Config ( state,
flags = 0 
) [inherited]

Initializes the state.

Parameters:
string$state
int$flagspublic
Returns:
boolean status flag
Exceptions:
StatusException

Implements IChanges.

Definition at line 66 of file diffstate.php.

                                               {
        if ($state == "")
            $state = array();

        if (!is_array($state))
            throw new StatusException("Invalid state", SYNC_FSSTATUS_CODEUNKNOWN);

        $this->syncstate = $state;
        $this->flags = $flags;
        return true;
    }
DiffState::getDiffTo ( new) [protected, inherited]

Differential mechanism Compares the current syncstate to the sent $new.

Parameters:
array$newprotected
Returns:
array

Definition at line 120 of file diffstate.php.

                                       {
        $changes = array();

        // Sort both arrays in the same way by ID
        usort($this->syncstate, array("DiffState", "RowCmp"));
        usort($new, array("DiffState", "RowCmp"));

        $inew = 0;
        $iold = 0;

        // Get changes by comparing our list of messages with
        // our previous state
        while(1) {
            $change = array();

            if($iold >= count($this->syncstate) || $inew >= count($new))
                break;

            if($this->syncstate[$iold]["id"] == $new[$inew]["id"]) {
                // Both messages are still available, compare flags and mod
                if(isset($this->syncstate[$iold]["flags"]) && isset($new[$inew]["flags"]) && $this->syncstate[$iold]["flags"] != $new[$inew]["flags"]) {
                    // Flags changed
                    $change["type"] = "flags";
                    $change["id"] = $new[$inew]["id"];
                    $change["flags"] = $new[$inew]["flags"];
                    $changes[] = $change;
                }

                if($this->syncstate[$iold]["mod"] != $new[$inew]["mod"]) {
                    $change["type"] = "change";
                    $change["id"] = $new[$inew]["id"];
                    $changes[] = $change;
                }

                $inew++;
                $iold++;
            } else {
                if($this->syncstate[$iold]["id"] > $new[$inew]["id"]) {
                    // Message in state seems to have disappeared (delete)
                    $change["type"] = "delete";
                    $change["id"] = $this->syncstate[$iold]["id"];
                    $changes[] = $change;
                    $iold++;
                } else {
                    // Message in new seems to be new (add)
                    $change["type"] = "change";
                    $change["flags"] = SYNC_NEWMESSAGE;
                    $change["id"] = $new[$inew]["id"];
                    $changes[] = $change;
                    $inew++;
                }
            }
        }

        while($iold < count($this->syncstate)) {
            // All data left in 'syncstate' have been deleted
            $change["type"] = "delete";
            $change["id"] = $this->syncstate[$iold]["id"];
            $changes[] = $change;
            $iold++;
        }

        while($inew < count($new)) {
            // All data left in new have been added
            $change["type"] = "change";
            $change["flags"] = SYNC_NEWMESSAGE;
            $change["id"] = $new[$inew]["id"];
            $changes[] = $change;
            $inew++;
        }

        return $changes;
    }

Here is the caller graph for this function:

DiffState::GetState ( ) [inherited]

Returns state.

public

Returns:
string
Exceptions:
StatusException

Implements IChanges.

Definition at line 85 of file diffstate.php.

                               {
        if (!isset($this->syncstate) || !is_array($this->syncstate))
            throw new StatusException("DiffState->GetState(): Error, state not available", SYNC_FSSTATUS_CODEUNKNOWN, null, LOGLEVEL_WARN);

        return $this->syncstate;
    }
ImportChangesDiff::ImportChangesDiff ( backend,
folderid = false 
)

Constructor.

Parameters:
object$backend
string$folderidpublic
Exceptions:
StatusException

Definition at line 57 of file importchangesdiff.php.

                                                                   {
        $this->backend = $backend;
        $this->folderid = $folderid;
    }

Imports a change on a folder.

Parameters:
object$folderSyncFolder

public

Returns:
string id of the folder
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 215 of file importchangesdiff.php.

                                                {
        $id = $folder->serverid;
        $parent = $folder->parentid;
        $displayname = $folder->displayname;
        $type = $folder->type;

        //do nothing if it is a dummy folder
        if ($parent == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportFolderChange('%s'): can not be done on a dummy folder", $id), SYNC_FSSTATUS_SERVERERROR);

        if($id) {
            $change = array();
            $change["id"] = $id;
            $change["mod"] = $displayname;
            $change["parent"] = $parent;
            $change["flags"] = 0;
            $this->updateState("change", $change);
        }

        $stat = $this->backend->ChangeFolder($parent, $id, $displayname, $type);

        if($stat)
            $this->updateState("change", $stat);

        return $stat["id"];
    }

Here is the call graph for this function:

ImportChangesDiff::ImportFolderDeletion ( id,
parent = false 
)

Imports a folder deletion.

Parameters:
string$id
string$parentid

public

Returns:
int SYNC_FOLDERHIERARCHY_STATUS
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 252 of file importchangesdiff.php.

                                                               {
        //do nothing if it is a dummy folder
        if ($parent == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportFolderDeletion('%s','%s'): can not be done on a dummy folder", $id, $parent), SYNC_FSSTATUS_SERVERERROR);

        // check the foldertype
        $folder = $this->backend->GetFolder($id);
        if (isset($folder->type) && Utils::IsSystemFolder($folder->type))
            throw new StatusException(sprintf("ImportChangesDiff->ImportFolderDeletion('%s','%s'): Error deleting system/default folder", $id, $parent), SYNC_FSSTATUS_SYSTEMFOLDER);

        $ret = $this->backend->DeleteFolder($id, $parent);
        if (!$ret)
            throw new StatusException(sprintf("ImportChangesDiff->ImportFolderDeletion('%s','%s'): can not be done on a dummy folder", $id, $parent), SYNC_FSSTATUS_FOLDERDOESNOTEXIST);

        $change = array();
        $change["id"] = $id;

        $this->updateState("delete", $change);

        return true;
    }

Here is the call graph for this function:

ImportChangesDiff::ImportMessageChange ( id,
message 
)

Imports a single message.

Parameters:
string$id
SyncObject$messagepublic
Returns:
boolean/string - failure / id of message
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 88 of file importchangesdiff.php.

                                                       {
        //do nothing if it is in a dummy folder
        if ($this->folderid == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageChange('%s','%s'): can not be done on a dummy folder", $id, get_class($message)), SYNC_STATUS_SYNCCANNOTBECOMPLETED);

        if($id) {
            // See if there's a conflict
            $conflict = $this->isConflict("change", $this->folderid, $id);

            // Update client state if this is an update
            $change = array();
            $change["id"] = $id;
            $change["mod"] = 0; // dummy, will be updated later if the change succeeds
            $change["parent"] = $this->folderid;
            $change["flags"] = (isset($message->read)) ? $message->read : 0;
            $this->updateState("change", $change);

            if($conflict && $this->flags == SYNC_CONFLICT_OVERWRITE_PIM)
                // in these cases the status SYNC_STATUS_CONFLICTCLIENTSERVEROBJECT should be returned, so the mobile client can inform the end user
                throw new StatusException(sprintf("ImportChangesDiff->ImportMessageChange('%s','%s'): Conflict detected. Data from PIM will be dropped! Server overwrites PIM. User is informed.", $id, get_class($message)), SYNC_STATUS_CONFLICTCLIENTSERVEROBJECT, null, LOGLEVEL_INFO);
        }

        $stat = $this->backend->ChangeMessage($this->folderid, $id, $message);

        if(!is_array($stat))
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageChange('%s','%s'): unknown error in backend", $id, get_class($message)), SYNC_STATUS_SYNCCANNOTBECOMPLETED);

        // Record the state of the message
        $this->updateState("change", $stat);

        return $stat["id"];
    }

Here is the call graph for this function:

Imports a deletion.

This may conflict if the local object has been modified

Parameters:
string$id
SyncObject$messagepublic
Returns:
boolean
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 131 of file importchangesdiff.php.

                                               {
        //do nothing if it is in a dummy folder
        if ($this->folderid == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageDeletion('%s'): can not be done on a dummy folder", $id), SYNC_STATUS_SYNCCANNOTBECOMPLETED);

        // See if there's a conflict
        $conflict = $this->isConflict("delete", $this->folderid, $id);

        // Update client state
        $change = array();
        $change["id"] = $id;
        $this->updateState("delete", $change);

        // If there is a conflict, and the server 'wins', then return without performing the change
        // this will cause the exporter to 'see' the overriding item as a change, and send it back to the PIM
        if($conflict && $this->flags == SYNC_CONFLICT_OVERWRITE_PIM) {
            ZLog::Write(LOGLEVEL_INFO, sprintf("ImportChangesDiff->ImportMessageDeletion('%s'): Conflict detected. Data from PIM will be dropped! Object was deleted.", $id));
            return false;
        }

        $stat = $this->backend->DeleteMessage($this->folderid, $id);
        if(!$stat)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageDeletion('%s'): Unknown error in backend", $id), SYNC_STATUS_OBJECTNOTFOUND);

        return true;
    }

Here is the call graph for this function:

ImportChangesDiff::ImportMessageMove ( id,
newfolder 
)

Imports a move of a message.

This occurs when a user moves an item to another folder

Parameters:
string$id
int$flags- read/unread

public

Returns:
boolean
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 197 of file importchangesdiff.php.

                                                       {
        // don't move messages from or to a dummy folder (GetHierarchy compatibility)
        if ($this->folderid == SYNC_FOLDER_TYPE_DUMMY || $newfolder == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageMove('%s'): can not be done on a dummy folder", $id), SYNC_MOVEITEMSSTATUS_CANNOTMOVE);

        return $this->backend->MoveMessage($this->folderid, $id, $newfolder);
    }

Imports a change in 'read' flag This can never conflict.

Parameters:
string$id
int$flags- read/unread

public

Returns:
boolean
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 169 of file importchangesdiff.php.

                                                       {
        //do nothing if it is a dummy folder
        if ($this->folderid == SYNC_FOLDER_TYPE_DUMMY)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageReadFlag('%s','%s'): can not be done on a dummy folder", $id, $flags), SYNC_STATUS_SYNCCANNOTBECOMPLETED);

        // Update client state
        $change = array();
        $change["id"] = $id;
        $change["flags"] = $flags;
        $this->updateState("flags", $change);

        $stat = $this->backend->SetReadFlag($this->folderid, $id, $flags);
        if (!$stat)
            throw new StatusException(sprintf("ImportChangesDiff->ImportMessageReadFlag('%s','%s'): Error, unable retrieve message from backend", $id, $flags), SYNC_STATUS_OBJECTNOTFOUND);

        return true;
    }

Here is the call graph for this function:

DiffState::isConflict ( type,
folderid,
id 
) [protected, inherited]

Returns TRUE if the given ID conflicts with the given operation.

This is only true in the following situations:

  • Changed here and changed there
  • Changed here and deleted there
  • Deleted here and changed there Any other combination of operations can be done (e.g. change flags & move or move & delete)
Parameters:
string$typeof change
string$folderid
string$idprotected
Returns:

Definition at line 246 of file diffstate.php.

                                                         {
        $stat = $this->backend->StatMessage($folderid, $id);

        if(!$stat) {
            // Message is gone
            if($type == "change")
                return true; // deleted here, but changed there
            else
                return false; // all other remote changes still result in a delete (no conflict)
        }

        foreach($this->syncstate as $state) {
            if($state["id"] == $id) {
                $oldstat = $state;
                break;
            }
        }

        if(!isset($oldstat)) {
            // New message, can never conflict
            return false;
        }

        if($stat["mod"] != $oldstat["mod"]) {
            // Changed here
            if($type == "delete" || $type == "change")
                return true; // changed here, but deleted there -> conflict, or changed here and changed there -> conflict
            else
                return false; // changed here, and other remote changes (move or flags)
        }
    }

Here is the caller graph for this function:

ImportChangesDiff::LoadConflicts ( contentparameters,
state 
)

Would load objects which are expected to be exported with this state The DiffBackend implements conflict detection on the fly.

Parameters:
ContentParameters$contentparametersclass of objects
string$statepublic
Returns:
boolean
Exceptions:
StatusException

Implements IImportChanges.

Definition at line 73 of file importchangesdiff.php.

                                                              {
        // changes are detected on the fly
        return true;
    }
static DiffState::RowCmp ( a,
b 
) [static, inherited]


DiffState specific stuff

Comparing function used for sorting of the differential engine

Parameters:
array$a
array$bpublic
Returns:
boolean

Definition at line 106 of file diffstate.php.

                                          {
        // TODO implement different comparing functions
        return $a["id"] < $b["id"] ? 1 : -1;
    }
DiffState::updateState ( type,
change 
) [protected, inherited]

Update the state to reflect changes.

Parameters:
string$typeof change
array$changeprotected
Returns:

Definition at line 204 of file diffstate.php.

                                                   {
        // Change can be a change or an add
        if($type == "change") {
            for($i=0; $i < count($this->syncstate); $i++) {
                if($this->syncstate[$i]["id"] == $change["id"]) {
                    $this->syncstate[$i] = $change;
                    return;
                }
            }
            // Not found, add as new
            $this->syncstate[] = $change;
        } else {
            for($i=0; $i < count($this->syncstate); $i++) {
                // Search for the entry for this item
                if($this->syncstate[$i]["id"] == $change["id"]) {
                    if($type == "flags") {
                        // Update flags
                        $this->syncstate[$i]["flags"] = $change["flags"];
                    } else if($type == "delete") {
                        // Delete item
                        array_splice($this->syncstate, $i, 1);
                    }
                    return;
                }
            }
        }
    }

Here is the caller graph for this function:


Member Data Documentation

DiffState::$backend [protected, inherited]

Definition at line 53 of file diffstate.php.

DiffState::$flags [protected, inherited]

Definition at line 54 of file diffstate.php.

Definition at line 46 of file importchangesdiff.php.

DiffState::$syncstate [protected, inherited]

Definition at line 52 of file diffstate.php.


The documentation for this class was generated from the following file: