Description

services_content_lock adds Services support to the community module called content_lock that will prevent two users from editing the same node concurrently. This module exposes the main operations of content_lock through Services as a resource, so that content locking can be done over a web service API such as REST. The main operations it currently implements are retrieve, create, and delete. This page will document how to use the operations in this resource.

Operations

The operations this resource implements for content_lock are:

  • retrieve - Find out if a node is locked and get info about a lock e.g. who owns the lock and when it was created.
  • create - Create a new content lock on a node. Locks a node preventing other users from editing it.
  • delete - Deletes an existing lock on a node. Only the lock owner may delete the lock.

Below each operation is documented in detail, including example CURL commands.

Retrieve

Definition

Description

Retrieves a lock object from a node if one exists.

Access

Based on "http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_access/7">
user_access. Users who have access to view this node will have access to
perform this operation.

Method

GET

URL

{drupal_host}/{endpoint}/content_lock/{nid}

Params

nid - The node id to retrieve lock info from.

Returns

An lock object if one exists. Empty result if no lock exists.

Success

200

Errors

404 if the nid in the request is not found.

Caveats

None.

Example CURL Bash script for Retrieve operation

#!/bin/bash
# Drupal Session cookie of an authenticated user.
COOKIE=SESS49960de5880e8c687434170f6476605b=xo1yKeavUmvACa7_vDYSiXTu2-xDCF0Vq5SMG8BHE8g

# Drupal hostname where services is running
DRUPAL_HOST=localhost 

# ENDPOINT corresponds to the path of the Services endpoint, that has 
# the content_lock resource enabled that you configured under: 
#   admin/structure/services
# You can also import this endpoint by clicking the "Import" link at the page
# above and then copy and pasting the contents of the this file:  
#   services_content_lock/examples/endpoint_import.txt
ENDPOINT=content_lock_api

# NID you want to get retrieve content_lock info from
NID=1

curl -vkL -X GET -b $COOKIE -H "Accept: application/xml" "http://$DRUPAL_HOST/?q=$ENDPOINT/content_lock/$NID"

Example Response Data

<?xml version="1.0" encoding="utf-8"?>
<result>
  <nid>1</nid>
  <uid>1</uid>
  <timestamp>1355340407</timestamp>
  <ajax_key>2000157999</ajax_key>
  <name>admin</name>
</result>

Response Fields Definition

nid

The node id that the locked node.

uid

The drupal user id of the user who owns the lock

timestamp

The timestamp that the lock was created. This can be used to derive the
expiration of the lock, if content locks are configued to auto expire after a
certain time.

ajax_key

Random number used by the UI. Can be ignored.

name

The username of the user who owns the lock.

Create

Definition

Description

Creates a lock on a node

Access

Based on "http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_access/7">
user_access. Users who have access to edit this node will have access to
perform this operation.

Method

POST

URL

{drupal_host}/{endpoint}/content_lock/

Params

nid - the node id to lock

name (optional) - The username who owns the lock. Default value is the
current authenticated user.

Returns

Message that the node was locked successfully.

Success

200 + message stating that the lock was created successfully

Errors

404 + message - if the nid in the request is not found.

406 + message - If required nid is not present in the post data.

406 + message - If name is passed in and that username does not exist.

409 + message - If the node is already locked by another user, lock owner username is returned.

Caveats

If you create a lock on a node that is already locked by you, the lock
timestamp is updated, so it's a way of refreshing your expiration.  If you
try to create a lock on a node that is locked by a different owner you get a
409 conflict error and a message with the username of the current lock owner
and the datetime that the lock was created.

Example CURL Bash script for Create Operation

#!/bin/bash
# Drupal Session cookie of an authenticated user.
COOKIE=SESS49960de5880e8c687434170f6476605b=xo1yKeavUmvACa7_vDYSiXTu2-xDCF0Vq5SMG8BHE8g

# Drupal hostname where services is running
DRUPAL_HOST=localhost 

# ENDPOINT corresponds to the path of the Services endpoint, that has 
# the content_lock resource enabled that you configured under: 
#   admin/structure/services
# You can also import this endpoint by clicking the "Import" link at the page
# above and then copy and pasting the contents of the this file:  
#   services_content_lock/examples/endpoint_import.txt
ENDPOINT=content_lock_api

curl -vkL -X POST -b $COOKIE -H "Content-Type: application/xml" --data "`cat ./lock.xml`" "http://$DRUPAL_HOST/?q=$ENDPOINT/content_lock/"

Example POST data

In the above example “lock.xml” would look like this:

<?xml version="1.0" encoding="utf-8"?>
<lock>
  <nid>1</nid>
</lock>

Example Response

<?xml version="1.0" encoding="utf-8"?>
<result>Lock successfully created on node: 2</result>

Delete

Definition

Description

Deletes a lock on a node.

Access

Based on "http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_access/7">
user_access. Users who have access to edit this node will have access to
perform this operation.

Method

DELETE

URL

{drupal_host}/{endpoint}/content_lock/{nid}

Params

nid - The node id to delete the lock on.

Returns

A message that the lock was deleted successfully.

Success

200

Errors

404 + message - If the nid in the request is not found.

406 + message - If there is no lock to delete on this nid.

409 + message - If attempting to delete a lock owned by a different
user.

406 + message - If after attempting to delete the lock and it still exists
for an unknown reason.

Caveats

You can't delete a lock owned by another user. If you have to forcefully
take the lock from another user than you need to login as an administrator
through the web UI.  Content Lock can be configured to automatically
delete locks after a certain time.

Example CURL Bash script for Delete operation

#!/bin/bash
# Drupal Session cookie of an authenticated user.
COOKIE=SESS49960de5880e8c687434170f6476605b=xo1yKeavUmvACa7_vDYSiXTu2-xDCF0Vq5SMG8BHE8g

# Drupal hostname where services is running
DRUPAL_HOST=localhost 

# ENDPOINT corresponds to the path of the Services endpoint, that has 
# the content_lock resource enabled that you configured under: 
#   admin/structure/services
# You can also import this endpoint by clicking the "Import" link at the page
# above and then copy and pasting the contents of the this file:  
#   services_content_lock/examples/endpoint_import.txt
ENDPOINT=content_lock_api

# NID you want to get retrieve content_lock info from
NID=1

curl -vkL -X DELETE -b $COOKIE -H "Accept: application/xml" "http://$DRUPAL_HOST/?q=$ENDPOINT/content_lock/$NID"

Example Response

<?xml version="1.0" encoding="utf-8"?>
<result>Lock successfully deleted on node: 1</result>