Skip to content
This repository
Browse code

ENHANCEMENT Added File.ShowInSearch flag to mirror the existing SiteT…

…ree.ShowInSearch flag - e.g. useful to limit visibility of user-uploaded files. Enforced in MySQLDatabase->searchEngine().
  • Loading branch information...
commit 83ad8d48a99fcc82c2c0c4a24a4ed6923c506a1e 1 parent 73cca09
Ingo Schommer authored September 15, 2011
5  core/model/MySQLDatabase.php
@@ -767,6 +767,11 @@ public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $
767 767
 	 	
768 768
 		// Always ensure that only pages with ShowInSearch = 1 can be searched
769 769
 		$extraFilters['SiteTree'] .= " AND ShowInSearch <> 0";
  770
+		
  771
+		// File.ShowInSearch was added later, keep the database driver backwards compatible 
  772
+		// by checking for its existence first
  773
+		$fields = $this->fieldList('File');
  774
+		if(array_key_exists('ShowInSearch', $fields)) $extraFilters['File'] .= " AND ShowInSearch <> 0";
770 775
 
771 776
 		$limit = $start . ", " . (int) $pageLength;
772 777
 		
10  filesystem/File.php
@@ -74,7 +74,9 @@ class File extends DataObject {
74 74
 		"Title" => "Varchar(255)",
75 75
 		"Filename" => "Text",
76 76
 		"Content" => "Text",
77  
-		"Sort" => "Int"
  77
+		"Sort" => "Int",
  78
+		// Only applies to files, doesn't inherit for folder
  79
+		'ShowInSearch' => 'Boolean(1)',
78 80
 	);
79 81
 	
80 82
 	static $has_one = array(
@@ -90,8 +92,10 @@ class File extends DataObject {
90 92
 		"BackLinkTracking" => "SiteTree",
91 93
 	);
92 94
 	
93  
-	static $defaults = array();
94  
-	
  95
+	static $defaults = array(
  96
+		"ShowInSearch" => 1,
  97
+	);
  98
+
95 99
 	static $extensions = array(
96 100
 		"Hierarchy",
97 101
 	);
22  tests/search/SearchFormTest.php
@@ -170,7 +170,7 @@ function testInheritedRestrictedPagesNotInlucded() {
170 170
 		$member->logOut();
171 171
 	}
172 172
 	
173  
-	function testDisabledShowInSearchFlagNotIncluded() {
  173
+	function testDisabledShowInSearchFlagNotIncludedForSiteTree() {
174 174
 		$sf = new SearchForm($this->mockController, 'SearchForm');
175 175
 		
176 176
 		$page = $this->objFromFixture('SiteTree', 'dontShowInSearchPage');
@@ -181,6 +181,26 @@ function testDisabledShowInSearchFlagNotIncluded() {
181 181
 			'Page with "Show in Search" disabled doesnt show'
182 182
 		);
183 183
 	}
  184
+	
  185
+	function testDisabledShowInSearchFlagNotIncludedForFiles() {
  186
+		$sf = new SearchForm($this->mockController, 'SearchForm');
  187
+		
  188
+		$dontShowInSearchFile = $this->objFromFixture('File', 'dontShowInSearchFile');
  189
+		$showInSearchFile = $this->objFromFixture('File', 'showInSearchFile');
  190
+		$results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile'));
  191
+		$this->assertNotContains(
  192
+			$dontShowInSearchFile->ID,
  193
+			$results->column('ID'),
  194
+			'File with "Show in Search" disabled doesnt show'
  195
+		);
  196
+		
  197
+		$results = $sf->getResults(null, array('Search'=>'showInSearchFile'));
  198
+		$this->assertContains(
  199
+			$showInSearchFile->ID,
  200
+			$results->column('ID'),
  201
+			'File with "Show in Search" enabled can be found'
  202
+		);
  203
+	}
184 204
 
185 205
 	function testSearchTitleAndContentWithSpecialCharacters() {
186 206
 		$sf = new SearchForm($this->mockController, 'SearchForm');
9  tests/search/SearchFormTest.yml
@@ -33,4 +33,11 @@ SiteTree:
33 33
       ShowInSearch: 0
34 34
    pageWithSpecialChars:
35 35
       Title: Brötchen
36  
-      Content: Frisch vom B&auml;cker
  36
+      Content: Frisch vom B&auml;cker
  37
+File:
  38
+   showInSearchFile:
  39
+      Title: showInSearchFile
  40
+      ShowInSearch: 1
  41
+   dontShowInSearchFile:
  42
+      Title: dontShowInSearchFile
  43
+      ShowInSearch: 0

0 notes on commit 83ad8d4

Please sign in to comment.
Something went wrong with that request. Please try again.