From 58ccb93f2d1824f3f8225bed09481bd7911a62bf Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Sat, 26 Nov 2011 23:25:46 +0100 Subject: [PATCH] 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. --- src/IDF/Views/Wiki.php | 60 +++++++++++++++++++- src/IDF/Wiki/ResourceRevision.php | 47 +++++++++++++++ src/IDF/conf/urls.php | 5 ++ src/IDF/templates/idf/wiki/viewResource.html | 55 ++++++++++++++++++ www/media/idf/css/style.css | 12 ++++ 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/IDF/templates/idf/wiki/viewResource.html diff --git a/src/IDF/Views/Wiki.php b/src/IDF/Views/Wiki.php index a70617b..f157062 100644 --- a/src/IDF/Views/Wiki.php +++ b/src/IDF/Views/Wiki.php @@ -275,7 +275,7 @@ class IDF_Views_Wiki } return Pluf_Shortcuts_RenderToResponse('idf/wiki/createResource.html', array( - 'resource_title' => $title, + 'page_title' => $title, 'form' => $form, ), $request); @@ -327,6 +327,64 @@ class IDF_Views_Wiki $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. */ diff --git a/src/IDF/Wiki/ResourceRevision.php b/src/IDF/Wiki/ResourceRevision.php index 43eadd4..59e319f 100644 --- a/src/IDF/Wiki/ResourceRevision.php +++ b/src/IDF/Wiki/ResourceRevision.php @@ -138,8 +138,55 @@ class IDF_Wiki_ResourceRevision extends Pluf_Model $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() { @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('%s', $url, $url, $resource->title); + } + + if (preg_match('#^text/(xml|html|sgml|javascript|ecmascript|css)$#', $resource->mime_type)) { + return sprintf('', $url, $resource->title); + } + + return __('Unable to render preview for this MIME type.'); + } } diff --git a/src/IDF/conf/urls.php b/src/IDF/conf/urls.php index 71e30f6..745a552 100644 --- a/src/IDF/conf/urls.php +++ b/src/IDF/conf/urls.php @@ -322,6 +322,11 @@ $ctl[] = array('regex' => '#^/p/([\-\w]+)/res/delete/(\d+)/$#', 'model' => 'IDF_Views_Wiki', 'method' => 'deleteResource'); +$ctl[] = array('regex' => '#^/p/([\-\w]+)/res/raw/(.*)/$#', + 'base' => $base, + 'model' => 'IDF_Views_Wiki', + 'method' => 'rawResource'); + $ctl[] = array('regex' => '#^/p/([\-\w]+)/page/(.*)/$#', 'base' => $base, 'model' => 'IDF_Views_Wiki', diff --git a/src/IDF/templates/idf/wiki/viewResource.html b/src/IDF/templates/idf/wiki/viewResource.html new file mode 100644 index 0000000..3cd4e8d --- /dev/null +++ b/src/IDF/templates/idf/wiki/viewResource.html @@ -0,0 +1,55 @@ +{extends "idf/wiki/base.html"} + +{block extraheader} +{if !$rev.is_head}{/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)} +
+

{blocktrans}You are looking at an old revision of the resource +{$resource.title}. This revision was created +by {$submitter}.{/blocktrans}

+
+{/if} + +
+

{$resource.summary}

+ +

{$rev.render()|unsafe}

+ + +{if ($isOwner or $isAdmin) and !$rev.is_head}{aurl 'url', 'IDF_Views_Wiki::deleteResourceRev', array($project.shortname, $rev.id)} +

{trans 'Trash'} {trans 'Delete this revision'}

+{/if} + +

{trans 'Page Usage'}

+{if $pagerevs.count() == 0} +

{trans 'This resource is not used on any pages yet.'}

+{else} + +{/if} +
+{/block} +{block context} +{ashowuser 'submitter', $resource.get_submitter(), $request} +

{trans 'Created:'} {$resource.creation_dtime|dateago}
{blocktrans}by {$submitter}{/blocktrans}

+{if $rev.creation_dtime != $resource.creation_dtime}

{ashowuser 'submitter', $rev.get_submitter(), $request} +{trans 'Updated:'} {$rev.creation_dtime|dateago}
{blocktrans}by {$submitter}{/blocktrans}

{/if} +{if $revs.count() > 0} +

{trans 'Old Revisions'}

+ +{/if} +{/block} diff --git a/www/media/idf/css/style.css b/www/media/idf/css/style.css index d2b4c47..ab59bbe 100644 --- a/www/media/idf/css/style.css +++ b/www/media/idf/css/style.css @@ -802,6 +802,15 @@ p.desc { width: 60%; } +p.preview img { + max-width: 60%; +} + +p.preview iframe { + width: 60%; + height: 300px; +} + div.old-rev { padding: 1em 1em 0.1em 1em; margin-bottom: 1em; @@ -824,6 +833,9 @@ div.deprecated-page { -webkit-border-radius: 5px; } +li.old-rev { + font-style: italic; +} .delp { float: right;