Skip to content

Commit

Permalink
Adds page revision details and the ability to revert to previous revi…
Browse files Browse the repository at this point in the history
…sions.
  • Loading branch information
jimsafley committed Jun 23, 2011
1 parent aaaa989 commit d6c815e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
35 changes: 35 additions & 0 deletions controllers/IndexController.php
Expand Up @@ -209,6 +209,41 @@ public function historyAction()
$this->view->namespaceIndex = $this->_getParam('namespace-index');
}

/**
* View a page revision.
*/
public function revisionAction()
{
try {
$scripto = ScriptoPlugin::getScripto();
$doc = $scripto->getDocument($this->_getParam('item-id'));
$doc->setPage($this->_getParam('file-id'));
$revision = $scripto->getRevision($this->_getParam('revision-id'));

// Handle a revert.
if ($this->_getParam('scripto-page-revert')) {
if (1 == $this->_getParam('namespace-index')) {
$doc->editTalkPage($revision['wikitext']);
} else {
$doc->editTranscriptionPage($revision['wikitext']);
}
$this->flashSuccess('Successfully reverted the page to a previous revision.');
$this->_helper->redirector->gotoRoute(array('item-id' => $doc->getId(),
'file-id' => $doc->getPageId(),
'namespace-index' => $this->_getParam('namespace-index')),
'scripto_history');
}

} catch (Scripto_Exception $e) {
$this->flashError($e->getMessage());
}

$this->view->scripto = $scripto;
$this->view->doc = $doc;
$this->view->revision = $revision;
$this->view->namespaceIndex = $this->_getParam('namespace-index');
}

/**
* View diff between page revisions.
*/
Expand Down
10 changes: 10 additions & 0 deletions libraries/Scripto.php
Expand Up @@ -651,6 +651,15 @@ public function getRevision($revisionId)
);
$page = current($response['query']['pages']);

// Set the action.
$actions = array('Replaced content', 'Unprotected', 'Protected', 'Created page');
$actionPattern = '/^(' . implode('|', $actions) . ').+$/s';
if (preg_match($actionPattern, $page['revisions'][0]['comment'])) {
$action = preg_replace($actionPattern, '$1', $page['revisions'][0]['comment']);
} else {
$action = '';
}

// Parse the wikitext into HTML.
$response = $this->_mediawiki->parse(
array('text' => '__NOEDITSECTION__' . $page['revisions'][0]['*'])
Expand All @@ -661,6 +670,7 @@ public function getRevision($revisionId)
'user' => $page['revisions'][0]['user'],
'timestamp' => $page['revisions'][0]['timestamp'],
'comment' => $page['revisions'][0]['comment'],
'action' => $action,
'size' => $page['revisions'][0]['size'],
'wikitext' => $page['revisions'][0]['*'],
'html' => $response['parse']['text']['*']);
Expand Down
9 changes: 3 additions & 6 deletions libraries/Scripto/Document.php
Expand Up @@ -823,10 +823,9 @@ protected function _getPageInfo($title)
*/
protected function _getPageHistory($title, $limit = 10, $startRevisionId = null)
{
$actions = array('Replaced content',
'Unprotected',
'Protected',
'Created page');
$actions = array('Replaced content', 'Unprotected', 'Protected', 'Created page');
$actionPattern = '/^(' . implode('|', $actions) . ').+$/s';

$revisions = array();
do {
$response = $this->_mediawiki->getRevisions(
Expand All @@ -843,8 +842,6 @@ protected function _getPageHistory($title, $limit = 10, $startRevisionId = null)
}

foreach ($page['revisions'] as $revision) {

$actionPattern = '/^(' . implode('|', $actions) . ').+$/';
if (preg_match($actionPattern, $revision['comment'])) {
$action = preg_replace($actionPattern, '$1', $revision['comment']);
} else {
Expand Down
9 changes: 9 additions & 0 deletions routes.ini
Expand Up @@ -14,6 +14,15 @@ scripto_action_item_file.defaults.controller = "index"
scripto_action_item_file.reqs.item-id = "\d+"
scripto_action_item_file.reqs.file-id = "\d+"

scripto_revision.route = "scripto/revision/:item-id/:file-id/:namespace-index/:revision-id"
scripto_revision.defaults.module = "scripto"
scripto_revision.defaults.controller = "index"
scripto_revision.defaults.action = "revision"
scripto_revision.reqs.item-id = "\d+"
scripto_revision.reqs.file-id = "\d+"
scripto_revision.reqs.namespace-index = "0|1"
scripto_revision.reqs.revision-id = "\d+"

scripto_history.route = "scripto/history/:item-id/:file-id/:namespace-index"
scripto_history.defaults.module = "scripto"
scripto_history.defaults.controller = "index"
Expand Down
10 changes: 2 additions & 8 deletions views/shared/index/diff.php
Expand Up @@ -36,19 +36,13 @@
<h3><?php echo $this->doc->getPageName(); ?></h3>

<!-- difference -->
<?php
$actions = array('Protected', 'Unprotected', 'Created');
$pattern = '/^(' . implode('|', $actions) . ').+$/';
$actionOldRevision = preg_replace($pattern, '$1', $this->oldRevision['comment']);
$actionRevision = preg_replace($pattern, '$1', $this->revision['comment']);
?>
<table>
<thead>
<tr>
<th colspan="2">Revision as of <?php echo date('H:i:s, M d, Y', strtotime($this->oldRevision['timestamp'])); ?><br />
<?php echo $actionOldRevision; ?> by <?php echo $this->oldRevision['user']; ?></th>
<?php echo $this->oldRevision['action']; ?> by <?php echo $this->oldRevision['user']; ?></th>
<th colspan="2">Revision as of <?php echo date('H:i:s, M d, Y', strtotime($this->revision['timestamp'])); ?><br />
<?php echo $actionRevision; ?> by <?php echo $this->revision['user']; ?></th>
<?php echo $this->revision['action']; ?> by <?php echo $this->revision['user']; ?></th>
</tr>
</thead>
<tbody>
Expand Down
7 changes: 6 additions & 1 deletion views/shared/index/history.php
Expand Up @@ -54,10 +54,15 @@
'old-revision-id' => $revision['parent_id'],
'revision-id' => $revision['revision_id']),
'scripto_diff');
$urlRevert = uri(array('item-id' => $this->doc->getId(),
'file-id' => $this->doc->getPageId(),
'namespace-index' => $this->namespaceIndex,
'revision-id' => $revision['revision_id']),
'scripto_revision');
?>
<tr>
<td>(<?php if ($revision['revision_id'] != $this->info['last_revision_id']): ?><a href="<?php echo html_escape($urlCurrent); ?>">current</a><?php else: ?>current<?php endif; ?> | <?php if (0 != $revision['parent_id']): ?><a href="<?php echo html_escape($urlPrevious); ?>">previous</a><?php else: ?>previous<?php endif; ?>)</td>
<td><?php echo date('H:i:s M d, Y', strtotime($revision['timestamp'])); ?></td>
<td><a href="<?php echo html_escape($urlRevert); ?>"><?php echo date('H:i:s M d, Y', strtotime($revision['timestamp'])); ?></a></td>
<td><?php echo $revision['user']; ?></td>
<td><?php echo $revision['size']; ?></td>
<td><?php echo $revision['action']; ?></td>
Expand Down
48 changes: 48 additions & 0 deletions views/shared/index/revision.php
@@ -0,0 +1,48 @@
<?php
$head = array('title' => html_escape('Scripto'));
head($head);
?>
<h1><?php echo $head['title']; ?></h1>
<div id="primary">
<?php echo flash(); ?>

<div id="scripto-history" class="scripto">
<!-- navigation -->
<p>
<?php if ($this->scripto->isLoggedIn()): ?>
Logged in as <a href="<?php echo html_escape(uri('scripto')); ?>"><?php echo $this->scripto->getUserName(); ?></a>
(<a href="<?php echo html_escape(uri('scripto/logout')); ?>">logout</a>)
| <a href="<?php echo html_escape(uri('scripto/watchlist')); ?>">Your watchlist</a>
<?php else: ?>
<a href="<?php echo html_escape(uri('scripto/login')); ?>">Log in to Scripto</a>
<?php endif; ?>
| <a href="<?php echo html_escape(uri('scripto/recent-changes')); ?>">Recent changes</a>
| <a href="<?php echo html_escape(uri(array('controller' => 'items', 'action' => 'show', 'id' => $this->doc->getId()), 'id')); ?>">View item</a>
| <a href="<?php echo html_escape(uri(array('controller' => 'files', 'action' => 'show', 'id' => $this->doc->getPageId()), 'id')); ?>">View file</a>
| <a href="<?php echo html_escape(uri(array('action' => 'transcribe', 'item-id' => $this->doc->getId(), 'file-id' => $this->doc->getPageId()), 'scripto_action_item_file')); ?>">Transcribe page</a>
| <a href="<?php echo html_escape(uri(array('item-id' => $this->doc->getId(), 'file-id' => $this->doc->getPageId(), 'namespace-index' => $this->namespaceIndex), 'scripto_history')); ?>">View history</a>
</p>

<h2><?php if (1 == $this->namespaceIndex): ?>Discussion<?php else: ?>Transcription<?php endif; ?> Page Revision</h2>
<h3><?php echo $this->doc->getPageName(); ?></h3>

<!-- revert -->
<?php if (1 == $this->namespaceIndex && $this->doc->canEditTalkPage()): ?>
<div><?php echo $this->formTextarea('scripto-page-wikitext', $this->revision['wikitext'], array('cols' => '76', 'rows' => '16', 'disabled' => 'disabled')); ?></div>
<form method="post" action="<?php echo html_escape(uri(array('item-id' => $this->doc->getId(), 'file-id' => $this->doc->getPageId(), 'namespace-index' => $this->namespaceIndex, 'revision-id' => $this->revision['revision_id']), 'scripto_revision')); ?>">
<?php echo $this->formSubmit('scripto-page-revert', 'Revert to this revision', array('style' => 'display:inline; float:none;')); ?>
</form>
<?php elseif ($this->doc->canEditTranscriptionPage()): ?>
<div><?php echo $this->formTextarea('scripto-page-wikitext', $this->revision['wikitext'], array('cols' => '76', 'rows' => '16', 'disabled' => 'disabled')); ?></div>
<form method="post" action="<?php echo html_escape(uri(array('item-id' => $this->doc->getId(), 'file-id' => $this->doc->getPageId(), 'namespace-index' => $this->namespaceIndex, 'revision-id' => $this->revision['revision_id']), 'scripto_revision')); ?>">
<?php echo $this->formSubmit('scripto-page-revert', 'Revert to this revision', array('style' => 'display:inline; float:none;')); ?>
</form>
<?php endif; ?>

<!-- revision -->
<h2>Revision as of <?php echo date('H:i:s, M d, Y', strtotime($this->revision['timestamp'])); ?>, <?php echo $this->revision['action']; ?> by <?php echo $this->revision['user']; ?></h2>
<div><?php echo $this->revision['html']; ?></div>

</div><!-- #scripto-history -->
</div>
<?php foot(); ?>

0 comments on commit d6c815e

Please sign in to comment.