Render a resource preview view with more information about the

resource, such as its summary, its mime type, a preview (available
for some image/* and text/* mime types) and a list of pages where
the specific resource revision is used.
This commit is contained in:
Thomas Keller 2011-11-26 23:25:46 +01:00
parent ff2b19d587
commit 58ccb93f2d
5 changed files with 178 additions and 1 deletions

View File

@ -275,7 +275,7 @@ class IDF_Views_Wiki
} }
return Pluf_Shortcuts_RenderToResponse('idf/wiki/createResource.html', return Pluf_Shortcuts_RenderToResponse('idf/wiki/createResource.html',
array( array(
'resource_title' => $title, 'page_title' => $title,
'form' => $form, 'form' => $form,
), ),
$request); $request);
@ -327,6 +327,64 @@ class IDF_Views_Wiki
$request); $request);
} }
/**
* View a documentation resource.
*/
public $viewResource_precond = array('IDF_Precondition::accessWiki');
public function viewResource($request, $match)
{
$prj = $request->project;
$sql = new Pluf_SQL('project=%s AND title=%s',
array($prj->id, $match[2]));
$resources = Pluf::factory('IDF_Wiki_Resource')->getList(array('filter'=>$sql->gen()));
if ($resources->count() != 1) {
return new Pluf_HTTP_Response_NotFound($request);
}
$resource = $resources[0];
$revision = $resource->get_current_revision();
// grab the old revision if requested.
if (isset($request->GET['rev']) and preg_match('/^[0-9]+$/', $request->GET['rev'])) {
$revision = Pluf_Shortcuts_GetObjectOr404('IDF_Wiki_ResourceRevision',
$request->GET['rev']);
if ($oldrev->wikiresource != $resource->id or $oldrev->is_head == true) {
return new Pluf_HTTP_Response_NotFound($request);
}
}
$pagerevs = $revision->getPageRevisions();
$title = $resource->title;
$false = Pluf_DB_BooleanToDb(false, $resource->getDbConnection());
$revs = $resource->get_revisions_list(array('order' => 'creation_dtime DESC',
'filter' => 'is_head='.$false));
return Pluf_Shortcuts_RenderToResponse('idf/wiki/viewResource.html',
array(
'page_title' => $title,
'resource' => $resource,
'rev' => $revision,
'revs' => $revs,
'pagerevs' => $pagerevs,
),
$request);
}
/**
* Returns a bytestream to the given raw resource revision
*/
public $rawResource_precond = array('IDF_Precondition::accessWiki');
public function rawResource($request, $match)
{
$prj = $request->project;
$rev = Pluf_Shortcuts_GetObjectOr404('IDF_Wiki_ResourceRevision',
$match[2]);
$res = $rev->get_wikiresource();
if ($res->get_project()->id != $prj->id) {
return new Pluf_HTTP_Response_NotFound($request);
}
return new Pluf_HTTP_Response_File($rev->getFilePath(), $res->mime_type);
}
/** /**
* Remove a revision of a page. * Remove a revision of a page.
*/ */

View File

@ -138,8 +138,55 @@ class IDF_Wiki_ResourceRevision extends Pluf_Model
$this->get_wikiresource()->id, $this->id, $this->get_wikiresource()->orig_file_ext); $this->get_wikiresource()->id, $this->id, $this->get_wikiresource()->orig_file_ext);
} }
function getFileURL()
{
$prj = $this->get_wikiresource()->get_project();
return Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::rawResource',
array($prj->shortname, $this->id));
}
function preDelete() function preDelete()
{ {
@unlink($this->getFilePath()); @unlink($this->getFilePath());
} }
/**
* Returns the page revisions which contain references to this resource revision
*/
function getPageRevisions()
{
$db =& Pluf::db();
$sql_results = $db->select(
'SELECT idf_wiki_pagerevision_id as id '.
'FROM '.Pluf::f('db_table_prefix', '').'idf_wiki_pagerevision_idf_wiki_resourcerevision_assoc '.
'WHERE idf_wiki_resourcerevision_id='.$this->id
);
$ids = array(0);
foreach ($sql_results as $id) {
$ids[] = $id['id'];
}
$ids = implode (',', $ids);
$sql = new Pluf_SQL('id IN ('.$ids.')');
return Pluf::factory('IDF_Wiki_PageRevision')
->getList(array('filter' => $sql->gen()));
}
/**
* Renders the resource
*/
function render()
{
$url = $this->getFileURL();
$resource = $this->get_wikiresource();
if (preg_match('#^image/(gif|jpeg|png|tiff)$#', $resource->mime_type)) {
return sprintf('<a href="%s"><img src="%s" alt="%s" /></a>', $url, $url, $resource->title);
}
if (preg_match('#^text/(xml|html|sgml|javascript|ecmascript|css)$#', $resource->mime_type)) {
return sprintf('<iframe src="%s" alt="%s"></iframe>', $url, $resource->title);
}
return __('Unable to render preview for this MIME type.');
}
} }

View File

@ -322,6 +322,11 @@ $ctl[] = array('regex' => '#^/p/([\-\w]+)/res/delete/(\d+)/$#',
'model' => 'IDF_Views_Wiki', 'model' => 'IDF_Views_Wiki',
'method' => 'deleteResource'); 'method' => 'deleteResource');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/res/raw/(.*)/$#',
'base' => $base,
'model' => 'IDF_Views_Wiki',
'method' => 'rawResource');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/page/(.*)/$#', $ctl[] = array('regex' => '#^/p/([\-\w]+)/page/(.*)/$#',
'base' => $base, 'base' => $base,
'model' => 'IDF_Views_Wiki', 'model' => 'IDF_Views_Wiki',

View File

@ -0,0 +1,55 @@
{extends "idf/wiki/base.html"}
{block extraheader}
{if !$rev.is_head}<meta name="ROBOTS" content="NOINDEX" />{/if}
{/block}
{block docclass}yui-t3{assign $inResourceView=true}{/block}
{block body}
{if !$rev.is_head}
{ashowuser 'submitter', $oldrev.get_submitter(), $request}{aurl 'url', 'IDF_Views_Wiki::viewResource', array($project.shortname, $resource.title)}
<div class="old-rev">
<p>{blocktrans}You are looking at an old revision of the resource
<a href="{$url}">{$resource.title}</a>. This revision was created
by {$submitter}.{/blocktrans}</p>
</div>
{/if}
<div id="wiki-resource">
<p class="desc">{$resource.summary}</p>
<p class="preview">{$rev.render()|unsafe}</p>
<ul>
<li>{trans 'File size'}: {$rev.filesize|size}</li>
<li>{trans 'MIME type'}: {$resource.mime_type}</li>
</ul>
{if ($isOwner or $isAdmin) and !$rev.is_head}{aurl 'url', 'IDF_Views_Wiki::deleteResourceRev', array($project.shortname, $rev.id)}
<p class="delp"><a href="{$url}" title="{trans 'Delete this revision'}"><img src="{media '/idf/img/trash.png'}" style="vertical-align: text-bottom;" alt="{trans 'Trash'}" /></a> <a href="{$url}">{trans 'Delete this revision'}</a></p>
{/if}
<p><strong>{trans 'Page Usage'}</strong></p>
{if $pagerevs.count() == 0}
<p>{trans 'This resource is not used on any pages yet.'}</p>
{else}
<ul>{foreach $pagerevs as $pagerev}
{assign $css = ''}
{if !$pagerev.is_head}{assign $css=' class="old-rev"'}{/if}
<li{$css|unsafe}><a href="{url 'IDF_Views_Wiki::viewPage', array($project.shortname, $pagerev.get_wikipage().title), array('rev'=>$pagerev.id)}">{$pagerev.get_wikipage().title} ({$pagerev.summary})</a></li>
{/foreach}</ul>
{/if}
</div>
{/block}
{block context}
{ashowuser 'submitter', $resource.get_submitter(), $request}
<p><strong>{trans 'Created:'}</strong> <span class="nobrk">{$resource.creation_dtime|dateago}</span><br /><span class="nobrk">{blocktrans}by {$submitter}{/blocktrans}</span></p>
{if $rev.creation_dtime != $resource.creation_dtime}<p>{ashowuser 'submitter', $rev.get_submitter(), $request}
<strong>{trans 'Updated:'}</strong> <span class="nobrk">{$rev.creation_dtime|dateago}</span><br /><span class="nobrk">{blocktrans}by {$submitter}{/blocktrans}</span></p>{/if}
{if $revs.count() > 0}
<p><strong>{trans 'Old Revisions'}</strong></p>
<ul>{foreach $revs as $old}
<li><a href="{url 'IDF_Views_Wiki::viewResource', array($project.shortname, $resource.title), array('rev'=>$old.id)}">{$old.summary}</a></li>
{/foreach}</ul>
{/if}
{/block}

View File

@ -802,6 +802,15 @@ p.desc {
width: 60%; width: 60%;
} }
p.preview img {
max-width: 60%;
}
p.preview iframe {
width: 60%;
height: 300px;
}
div.old-rev { div.old-rev {
padding: 1em 1em 0.1em 1em; padding: 1em 1em 0.1em 1em;
margin-bottom: 1em; margin-bottom: 1em;
@ -824,6 +833,9 @@ div.deprecated-page {
-webkit-border-radius: 5px; -webkit-border-radius: 5px;
} }
li.old-rev {
font-style: italic;
}
.delp { .delp {
float: right; float: right;