mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00:00
APICHANGE: refactored DocumentationSearch to go back to a simple class rather than a controller. All requests interfacing with the search should go through DocumentationViewer. APICHANGE: migrated DocumentationSearch::opensearch() to its own controller. BUGFIX: declare default meta_data in the function rather than using the api to prevent inclusion order issues
This commit is contained in:
parent
b119b1eab8
commit
13fdb8dd80
11
_config.php
11
_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')
|
||||
));
|
||||
));
|
@ -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'));
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||
|
||||
<ShortName>$ShortName</ShortName>
|
||||
<Description>$Description</Description>
|
||||
<Tags>$Tags</Tags>
|
||||
<Contact>$Content</Contact>
|
||||
<% if ShortName %><ShortName>$ShortName</ShortName><% end_if %>
|
||||
<% if Description %><Description>$Description</Description><% end_if %>
|
||||
<% if Tags %><Tags>$Tags</Tags><% end_if %>
|
||||
<% if Contact %><Contact>$Content</Contact><% end_if %>
|
||||
|
||||
<Url type="application/rss+xml" template="http://example.com/?q={searchTerms}&pw={startPage?}&format=rss"/>
|
||||
</OpenSearchDescription>
|
||||
<% if SearchPageLink %><Url type="text/html" template="$SearchPageLink" /><% end_if %>
|
||||
<% if SearchPageRss %><Url type="application/rss+xml" template="$SearchPageRss" /><% end_if %>
|
||||
<% if SearchPageJson %><Url type="application/x-suggestions+json" template="$SearchPageJson" /><% end_if %>
|
||||
</OpenSearchDescription>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user