diff --git a/_config.php b/_config.php
index 5703ac6..d04cd9e 100755
--- a/_config.php
+++ b/_config.php
@@ -11,13 +11,4 @@
// define your own rule in your mysite/_config.php
Director::addRules(100, array(
'dev/docs' => 'DocumentationViewer'
-));
-
-// the default meta data for the OpenSearch library. More descriptive values
-// can be set in your mysite file
-DocumentationSearch::set_meta_data(array(
- 'ShortName' => _t('DocumentationViewer.OPENSEARCHNAME', 'Documentation Search'),
- 'Description' => _t('DocumentationViewer.OPENSEARCHDESC', 'Search the documentation'),
- 'Contact' => Email::getAdminEmail(),
- 'Tags' => _t('DocumentationViewer.OPENSEARCHTAGS', 'Documentation')
-));
+));
\ No newline at end of file
diff --git a/code/DocumentationSearch.php b/code/DocumentationSearch.php
index 8cd20ca..17f991e 100644
--- a/code/DocumentationSearch.php
+++ b/code/DocumentationSearch.php
@@ -20,7 +20,7 @@
* @package sapphiredocs
*/
-class DocumentationSearch extends Controller {
+class DocumentationSearch {
/**
* @var bool - Is search enabled
@@ -30,7 +30,7 @@ class DocumentationSearch extends Controller {
/**
* @var string - OpenSearch metadata. Please use {@link DocumentationSearch::set_meta_data()}
*/
- private static $meta_data;
+ private static $meta_data = array();
/**
* @var DataObjectSet - Results
@@ -88,11 +88,6 @@ class DocumentationSearch extends Controller {
*/
private static $index_location = 'sapphiredocs';
- static $allowed_actions = array(
- 'buildindex',
- 'opensearch'
- );
-
/**
* Generate an array of every single documentation page installed on the system.
*
@@ -171,8 +166,7 @@ class DocumentationSearch extends Controller {
$this->totalResults = $index->numDocs();
}
catch(Zend_Search_Lucene_Exception $e) {
- user_error('DocumentationSearch::performSearch() could not perform search as index does not exist.
- Please run /dev/tasks/RebuildLuceneDocsIndex', E_USER_ERROR);
+ user_error($e .'. Ensure you have run the rebuld task (/dev/tasks/RebuildLuceneDocsIndex)', E_USER_ERROR);
}
}
@@ -309,8 +303,11 @@ class DocumentationSearch extends Controller {
if($index) $index->optimize();
}
+ /**
+ * @return String
+ */
public function getTitle() {
- return ($this->outputController) ? $this->outputController->Title : "";
+ return ($this->outputController) ? $this->outputController->Title : _t('DocumentationSearch.SEARCH', 'Search');
}
/**
@@ -335,16 +332,16 @@ class DocumentationSearch extends Controller {
$data = self::$meta_data;
return array(
- 'Description' => (isset($data['description'])) ? $data['description'] : "",
- 'Tags' => (isset($data['tags'])) ? $data['tags'] : "",
- 'Contact' => (isset($data['contact'])) ? $data['contact'] : "",
- 'ShortName' => (isset($data['shortname'])) ? $data['shortname'] : ""
+ 'Description' => (isset($data['description'])) ? $data['description'] : _t('DocumentationViewer.OPENSEARCHDESC', 'Search the documentation'),
+ 'Tags' => (isset($data['tags'])) ? $data['tags'] : _t('DocumentationViewer.OPENSEARCHTAGS', 'documentation'),
+ 'Contact' => (isset($data['contact'])) ? $data['contact'] : Email::getAdminEmail(),
+ 'ShortName' => (isset($data['shortname'])) ? $data['shortname'] : _t('DocumentationViewer.OPENSEARCHNAME', 'Documentation Search')
);
}
public function renderResults() {
if(!$this->results) $this->performSearch();
- if(!$this->outputController) $this->outputController = $this;
+ if(!$this->outputController) return user_error('Call renderResults() on a DocumentationViewer instance.', E_USER_ERROR);
$request = $this->outputController->getRequest();
@@ -355,21 +352,44 @@ class DocumentationSearch extends Controller {
// alter the fields for the opensearch xml.
$title = ($title = $this->getTitle()) ? ' | '. $title : "";
+ $link = Controller::join_links($this->outputController->Link(), 'DocumentationOpenSearch_Controller/description/');
+
$data->setField('Title', $data->Title . $title);
-
- $data->setField('DescriptionURL', 'DocumentationSearch/opensearch/');
+ $data->setField('DescriptionURL', $link);
array_unshift($templates, 'OpenSearchResults');
}
return $this->outputController->customise($data)->renderWith($templates);
}
+}
+
+
+/**
+ * Public facing controller for handling search.
+ *
+ * @package sapphiredocs
+ */
+
+class DocumentationOpenSearch_Controller extends Controller {
- /**
- * Returns the opensearch description of the search results
- */
- public function opensearch() {
- $data = self::get_meta_data();
+ function index() {
+ return $this->httpError('404');
+ }
+
+ function description() {
+ $viewer = new DocumentationViewer();
+
+ if(!DocumentationViewer::canView()) return Security::permissionFailure($this);
+
+ $data = DocumentationSearch::get_meta_data();
+ $link = Director::absoluteBaseUrl() .
+ $data['SearchPageLink'] = Controller::join_links(
+ $viewer->Link(),
+ 'results/?query={searchTerms}&start={startIndex}&length={count}'
+ );
+
+ $data['SearchPageRss'] = $data['SearchPageLink'] . '&format=rss';
return $this->customise(new ArrayData($data))->renderWith(array('OpenSearchDescription'));
}
diff --git a/code/DocumentationViewer.php b/code/DocumentationViewer.php
index 3c0a6d3..b514869 100755
--- a/code/DocumentationViewer.php
+++ b/code/DocumentationViewer.php
@@ -54,10 +54,8 @@ class DocumentationViewer extends Controller {
function init() {
parent::init();
-
- $canAccess = (Director::isDev() || Director::is_cli() || !self::$check_permission || Permission::check(self::$check_permission));
- if(!$canAccess) return Security::permissionFailure($this);
+ if(!$this->canView()) return Security::permissionFailure($this);
// javascript
Requirements::javascript(THIRDPARTY_DIR .'/jquery/jquery.js');
@@ -88,6 +86,15 @@ class DocumentationViewer extends Controller {
Requirements::customScript('jQuery(document).ready(function() {SyntaxHighlighter.all();});');
}
+
+ /**
+ * Can the user view this documentation. Hides all functionality for private wikis
+ *
+ * @return bool
+ */
+ public function canView() {
+ return (Director::isDev() || Director::is_cli() || !self::$check_permission || Permission::check(self::$check_permission));
+ }
/**
* Overloaded to avoid "action doesnt exist" errors - all URL parts in this
diff --git a/code/tasks/RebuildLuceneDocsIndex.php b/code/tasks/RebuildLuceneDocsIndex.php
index d15d8e8..19f3ce2 100644
--- a/code/tasks/RebuildLuceneDocsIndex.php
+++ b/code/tasks/RebuildLuceneDocsIndex.php
@@ -42,7 +42,9 @@ class RebuildLuceneDocsIndex extends BuildTask {
$index = Zend_Search_Lucene::open(DocumentationSearch::get_index_location());
$index->removeReference();
}
- catch (Zend_Search_Lucene_Exception $e) {}
+ catch (Zend_Search_Lucene_Exception $e) {
+ user_error($e);
+ }
try {
$index = Zend_Search_Lucene::create(DocumentationSearch::get_index_location());
@@ -53,7 +55,7 @@ class RebuildLuceneDocsIndex extends BuildTask {
// includes registration
$pages = DocumentationSearch::get_all_documentation_pages();
-
+
if($pages) {
$count = 0;
diff --git a/templates/OpenSearchDescription.ss b/templates/OpenSearchDescription.ss
index de8a8b4..3e11727 100644
--- a/templates/OpenSearchDescription.ss
+++ b/templates/OpenSearchDescription.ss
@@ -1,10 +1,13 @@
- $ShortName
- $Description
- $Tags
- $Content
+ <% if ShortName %>$ShortName<% end_if %>
+ <% if Description %>$Description<% end_if %>
+ <% if Tags %>$Tags<% end_if %>
+ <% if Contact %>$Content<% end_if %>
-
-
\ No newline at end of file
+ <% if SearchPageLink %><% end_if %>
+ <% if SearchPageRss %><% end_if %>
+ <% if SearchPageJson %><% end_if %>
+
+