Skip to content

Commit

Permalink
Adds a Scripto base class and moves non-document specific methods to …
Browse files Browse the repository at this point in the history
…it. Changes Scripto_Document signature.
  • Loading branch information
Jim Safley committed Mar 1, 2011
1 parent 6f6a401 commit 8f95829
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 122 deletions.
4 changes: 2 additions & 2 deletions examples/SideBySide/index.php
Expand Up @@ -15,9 +15,9 @@
// Set the Document object.
require_once 'Scripto/Document.php';
$doc = new Scripto_Document($documentId,
$adapter,
MEDIAWIKI_API_URL,
MEDIAWIKI_DB_NAME,
$adapter);
MEDIAWIKI_DB_NAME);

// Must set the current page first.
$doc->setPage($pageId);
Expand Down
4 changes: 2 additions & 2 deletions examples/Simple/index.php
Expand Up @@ -15,9 +15,9 @@
// Set the Document object.
require_once 'Scripto/Document.php';
$doc = new Scripto_Document($documentId,
$adapter,
MEDIAWIKI_API_URL,
MEDIAWIKI_DB_NAME,
$adapter);
MEDIAWIKI_DB_NAME);

// Must set the current page first.
$doc->setPage($pageId);
Expand Down
4 changes: 2 additions & 2 deletions examples/TopAndBottom/index.php
Expand Up @@ -15,9 +15,9 @@
// Set the Document object.
require_once 'Scripto/Document.php';
$doc = new Scripto_Document($documentId,
$adapter,
MEDIAWIKI_API_URL,
MEDIAWIKI_DB_NAME,
$adapter);
MEDIAWIKI_DB_NAME);

// Must set the current page first.
$doc->setPage($pageId);
Expand Down
4 changes: 2 additions & 2 deletions examples/shared/ajax.php
Expand Up @@ -13,9 +13,9 @@
// Set the Document object.
require_once 'Scripto/Document.php';
$doc = new Scripto_Document($documentId,
$adapter,
MEDIAWIKI_API_URL,
MEDIAWIKI_DB_NAME,
$adapter);
MEDIAWIKI_DB_NAME);

// Must set the current page first.
$doc->setPage($pageId);
Expand Down
129 changes: 129 additions & 0 deletions lib/Scripto.php
@@ -0,0 +1,129 @@
<?php
/**
* @copyright © 2011, Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

require_once 'Scripto/Exception.php';

/**
* Base Scripto class. Serves as a connector object between the external system
* API and MediaWiki API.
*/
class Scripto
{
/**
* @var Scripto_Adapter_Interface The adapter object for the external
* system.
*/
protected $_adapter;

/**
* @var Scripto_Service_MediaWiki The MediaWiki service object.
*/
protected $_mediawiki;

/**
* @var array The cached user info of the current user.
*/
protected $_userInfo;

/**
* Construct the Scripto object.
*
* @param string $mediaWikiApiUrl The MediaWiki API URL.
* @param string $mediaWikiDbName The MediaWiki database name.
* @param Scripto_Adapter_Interface $adapter The adapter object.
* @param bool $passCookies Pass cookies to the web browser via API client.
*/
public function __construct(Scripto_Adapter_Interface $adapter,
$mediaWikiApiUrl,
$mediaWikiDbName,
$passCookies = true)
{
$this->_adapter = $adapter;

require_once 'Scripto/Service/MediaWiki.php';
$this->_mediawiki = new Scripto_Service_MediaWiki($mediaWikiApiUrl,
$mediaWikiDbName,
(bool) $passCookies);
}

/**
* Login via the MediaWiki service.
*
* @param string $username
* @param string $password
*/
public function login($username, $password)
{
$this->_mediawiki->login($username, $password);
}

/**
* Logout via the MediaWiki service.
*/
public function logout()
{
$this->_mediawiki->logout();
}

/**
* Return information about the currently logged-in user.
*
* @return stdClass
*/
public function getUserInfo()
{
// If not already cached, set the current user info.
if (null === $this->_userInfo) {
$userInfo = $this->_mediawiki->getUserInfo()->query->userinfo;
$this->_userInfo = array('id' => $userInfo->id,
'name' => $userInfo->name,
'rights' => $userInfo->rights,
'edit_count' => $userInfo->editcount,
'email' => $userInfo->email);
}
return $this->_userInfo;
}

/**
* Return the specified user's contributions.
*
* @param null|string $username
* @param int $limit
* @return array
*/
public function getUserContributions($username = null, $limit = 10)
{
// If no username was specified, set it to the current user.
if (null === $username) {
$userInfo = $this->getUserInfo();
$username = $userInfo['name'];
}

$userContribs = $this->_mediawiki->getUserContributions($username, $limit)
->query
->usercontribs;
$userContributions = array();
foreach ($userContribs as $value) {

// Filter out contributions that are not document pages.
if (self::BASE_TITLE_PREFIX != $value->title[0]) {
continue;
}

$document = self::decodeBaseTitle($value->title);

$userContributions[] = array('page_id' => $value->pageid,
'revision_id' => $value->revid,
'title' => $value->title,
'document_id' => $document[0],
'document_page_id' => $document[1],
'timestamp' => $value->timestamp,
'comment' => $value->comment,
'size' => $value->size);
}
return $userContributions;
}
}
130 changes: 17 additions & 113 deletions lib/Scripto/Document.php
Expand Up @@ -4,13 +4,13 @@
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

require_once 'Scripto.php';
require_once 'Scripto/Exception.php';

/**
* Base class for Scripto documents. Serves as a connector object between the
* external system API and MediaWiki API.
* Represents a Scripto document and its pages.
*/
class Scripto_Document
class Scripto_Document extends Scripto
{
/**
* The prefix used in the base title to keep MediaWiki from capitalizing the
Expand All @@ -32,33 +32,17 @@ class Scripto_Document
/**
* @var string The document ID provided by the external system.
*/
private $_id;
protected $_id;

/**
* @var string The document page ID provided by the external system.
*/
private $_pageId;
protected $_pageId;

/**
* @var string The base title of the corresponding MediaWiki page.
*/
private $_baseTitle;

/**
* @var Scripto_Adapter_Interface The adapter object for the external
* system.
*/
private $_adapter;

/**
* @var Scripto_Service_MediaWiki The MediaWiki service object.
*/
private $_mediawiki;

/**
* @var array The cached user info of the current user.
*/
private $_userInfo;
protected $_baseTitle;

/**
* Construct the Scripto document object.
Expand All @@ -70,49 +54,28 @@ class Scripto_Document
* @param bool $passCookies Pass cookies to the web browser via API client.
*/
public function __construct($id,
Scripto_Adapter_Interface $adapter,
$mediaWikiApiUrl,
$mediaWikiDbName,
Scripto_Adapter_Interface $adapter,
$passCookies = true)
{
$this->_id = $id;

// Send the parameters to the parent constructor.
parent::__construct($adapter,
$mediaWikiApiUrl,
$mediaWikiDbName,
$passCookies);

// Document IDs must not be empty strings, null, or false.
if (!strlen($id)
|| is_null($id)
|| false === $id) {
if (!strlen($id) || is_null($id) || false === $id) {
throw new Scripto_Exception('The document ID is invalid.');
}

// Query the adapter whether the document exists.
if (!$adapter->documentExists($id)) {
if (!$this->_adapter->documentExists($id)) {
throw new Scripto_Exception("The specified document does not exist: {$this->_id}");
}

$this->_id = $id;
$this->_adapter = $adapter;

require_once 'Scripto/Service/MediaWiki.php';
$this->_mediawiki = new Scripto_Service_MediaWiki($mediaWikiApiUrl,
$mediaWikiDbName,
(bool) $passCookies);
}

/**
* Login via the MediaWiki service.
*
* @param string $username
* @param string $password
*/
public function login($username, $password)
{
$this->_mediawiki->login($username, $password);
}

/**
* Logout via the MediaWiki service.
*/
public function logout()
{
$this->_mediawiki->logout();
}

/**
Expand Down Expand Up @@ -297,65 +260,6 @@ public function getPreview($wikitext)
return $this->_mediawiki->getPreview($wikitext);
}

/**
* Return information about the currently logged-in user.
*
* @return stdClass
*/
public function getUserInfo()
{
// If not already cached, set the current user info.
if (null === $this->_userInfo) {
$userInfo = $this->_mediawiki->getUserInfo()->query->userinfo;
$this->_userInfo = array('id' => $userInfo->id,
'name' => $userInfo->name,
'rights' => $userInfo->rights,
'edit_count' => $userInfo->editcount,
'email' => $userInfo->email);
}
return $this->_userInfo;
}

/**
* Return the specified user's contributions.
*
* @param null|string $username
* @param int $limit
* @return array
*/
public function getUserContributions($username = null, $limit = 10)
{
// If no username was specified, set it to the current user.
if (null === $username) {
$userInfo = $this->getUserInfo();
$username = $userInfo['name'];
}

$userContribs = $this->_mediawiki->getUserContributions($username, $limit)
->query
->usercontribs;
$userContributions = array();
foreach ($userContribs as $value) {

// Filter out contributions that are not document pages.
if (self::BASE_TITLE_PREFIX != $value->title[0]) {
continue;
}

$document = self::decodeBaseTitle($value->title);

$userContributions[] = array('page_id' => $value->pageid,
'revision_id' => $value->revid,
'title' => $value->title,
'document_id' => $document[0],
'document_page_id' => $document[1],
'timestamp' => $value->timestamp,
'comment' => $value->comment,
'size' => $value->size);
}
return $userContributions;
}

/**
* Determine if the current user can edit the MediaWiki page.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/document_test.php
Expand Up @@ -25,9 +25,9 @@ public function __construct()
// Instantiate the Scripto_Document object and set it.
require 'Scripto/Document.php';
$this->_testDocument = new Scripto_Document(TEST_DOCUMENT_ID,
new $testAdapterClassName,
TEST_MEDIAWIKI_API_URL,
TEST_MEDIAWIKI_DB_NAME,
new $testAdapterClassName,
false);
}

Expand Down

0 comments on commit 8f95829

Please sign in to comment.