@@ -10,46 +10,46 @@ |
||
| 10 | 10 | |
| 11 | 11 | class PopulateThemeSampleDataTaskTest extends SapphireTest |
| 12 | 12 | { |
| 13 | - protected $usesDatabase = true; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * Ensure that the "contact" user form is only created once |
|
| 17 | - */ |
|
| 18 | - public function testOnlyCreateContactFormOnce() |
|
| 19 | - { |
|
| 20 | - $createdMessage = 'Created "contact" UserDefinedForm'; |
|
| 21 | - |
|
| 22 | - $task = new PopulateThemeSampleDataTask; |
|
| 23 | - |
|
| 24 | - // Run the task |
|
| 25 | - $this->assertContains($createdMessage, $this->bufferedTask($task)); |
|
| 26 | - |
|
| 27 | - // Run a second time |
|
| 28 | - $this->assertNotContains($createdMessage, $this->bufferedTask($task)); |
|
| 29 | - |
|
| 30 | - // Change the page name |
|
| 31 | - $form = UserDefinedForm::get()->filter('URLSegment', 'contact')->first(); |
|
| 32 | - $form->URLSegment = 'testing'; |
|
| 33 | - $form->write(); |
|
| 34 | - |
|
| 35 | - // Ensure the old version is still detected in draft, so not recreated |
|
| 36 | - $this->assertNotContains($createdMessage, $this->bufferedTask($task)); |
|
| 37 | - |
|
| 38 | - // Delete the page, then ensure it's recreated again (DataObject::delete will remove staged versions) |
|
| 39 | - $form->delete(); |
|
| 40 | - $this->assertContains($createdMessage, $this->bufferedTask($task)); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Run a BuildTask while buffering its output, and return the result |
|
| 45 | - * |
|
| 46 | - * @param BuildTask $task |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - protected function bufferedTask(BuildTask $task) |
|
| 50 | - { |
|
| 51 | - ob_start(); |
|
| 52 | - $task->run(new HTTPRequest('GET', '/')); |
|
| 53 | - return ob_get_clean(); |
|
| 54 | - } |
|
| 13 | + protected $usesDatabase = true; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * Ensure that the "contact" user form is only created once |
|
| 17 | + */ |
|
| 18 | + public function testOnlyCreateContactFormOnce() |
|
| 19 | + { |
|
| 20 | + $createdMessage = 'Created "contact" UserDefinedForm'; |
|
| 21 | + |
|
| 22 | + $task = new PopulateThemeSampleDataTask; |
|
| 23 | + |
|
| 24 | + // Run the task |
|
| 25 | + $this->assertContains($createdMessage, $this->bufferedTask($task)); |
|
| 26 | + |
|
| 27 | + // Run a second time |
|
| 28 | + $this->assertNotContains($createdMessage, $this->bufferedTask($task)); |
|
| 29 | + |
|
| 30 | + // Change the page name |
|
| 31 | + $form = UserDefinedForm::get()->filter('URLSegment', 'contact')->first(); |
|
| 32 | + $form->URLSegment = 'testing'; |
|
| 33 | + $form->write(); |
|
| 34 | + |
|
| 35 | + // Ensure the old version is still detected in draft, so not recreated |
|
| 36 | + $this->assertNotContains($createdMessage, $this->bufferedTask($task)); |
|
| 37 | + |
|
| 38 | + // Delete the page, then ensure it's recreated again (DataObject::delete will remove staged versions) |
|
| 39 | + $form->delete(); |
|
| 40 | + $this->assertContains($createdMessage, $this->bufferedTask($task)); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Run a BuildTask while buffering its output, and return the result |
|
| 45 | + * |
|
| 46 | + * @param BuildTask $task |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + protected function bufferedTask(BuildTask $task) |
|
| 50 | + { |
|
| 51 | + ob_start(); |
|
| 52 | + $task->run(new HTTPRequest('GET', '/')); |
|
| 53 | + return ob_get_clean(); |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -14,54 +14,54 @@ |
||
| 14 | 14 | class FooterHolder extends RedirectorPage |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - private static $description = 'Holder page that displays all child pages as links in the footer'; |
|
| 17 | + private static $description = 'Holder page that displays all child pages as links in the footer'; |
|
| 18 | 18 | |
| 19 | - private static $singular_name = 'Footer Holder'; |
|
| 19 | + private static $singular_name = 'Footer Holder'; |
|
| 20 | 20 | |
| 21 | - private static $plural_name = 'Footer Holders'; |
|
| 21 | + private static $plural_name = 'Footer Holders'; |
|
| 22 | 22 | |
| 23 | - private static $defaults = [ |
|
| 24 | - 'ShowInMenus' => 0, |
|
| 25 | - 'ShowInSearch' => 0, |
|
| 26 | - ]; |
|
| 23 | + private static $defaults = [ |
|
| 24 | + 'ShowInMenus' => 0, |
|
| 25 | + 'ShowInSearch' => 0, |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - private static $table_name = 'FooterHolder'; |
|
| 28 | + private static $table_name = 'FooterHolder'; |
|
| 29 | 29 | |
| 30 | - public function getCMSFields() |
|
| 31 | - { |
|
| 32 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 33 | - $fields->removeByName('RedirectorDescHeader'); |
|
| 34 | - $fields->removeByName('RedirectionType'); |
|
| 35 | - $fields->removeByName('LinkToID'); |
|
| 36 | - $fields->removeByName('ExternalURL'); |
|
| 37 | - }); |
|
| 38 | - return parent::getCMSFields(); |
|
| 39 | - } |
|
| 30 | + public function getCMSFields() |
|
| 31 | + { |
|
| 32 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 33 | + $fields->removeByName('RedirectorDescHeader'); |
|
| 34 | + $fields->removeByName('RedirectionType'); |
|
| 35 | + $fields->removeByName('LinkToID'); |
|
| 36 | + $fields->removeByName('ExternalURL'); |
|
| 37 | + }); |
|
| 38 | + return parent::getCMSFields(); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Return the link to the first child page. |
|
| 43 | - */ |
|
| 44 | - public function redirectionLink() |
|
| 45 | - { |
|
| 46 | - $childPage = $this->Children()->first(); |
|
| 41 | + /** |
|
| 42 | + * Return the link to the first child page. |
|
| 43 | + */ |
|
| 44 | + public function redirectionLink() |
|
| 45 | + { |
|
| 46 | + $childPage = $this->Children()->first(); |
|
| 47 | 47 | |
| 48 | - if ($childPage) { |
|
| 49 | - // If we're linking to another redirectorpage then just return the URLSegment, to prevent a cycle of |
|
| 50 | - // redirector pages from causing an infinite loop. Instead, they will cause a 30x redirection loop in |
|
| 51 | - // the browser, but this can be handled sufficiently gracefully by the browser. |
|
| 52 | - if ($childPage instanceof RedirectorPage) { |
|
| 53 | - return $childPage->regularLink(); |
|
| 54 | - } |
|
| 55 | - // For all other pages, just return the link of the page. |
|
| 56 | - return $childPage->Link(); |
|
| 57 | - } |
|
| 58 | - } |
|
| 48 | + if ($childPage) { |
|
| 49 | + // If we're linking to another redirectorpage then just return the URLSegment, to prevent a cycle of |
|
| 50 | + // redirector pages from causing an infinite loop. Instead, they will cause a 30x redirection loop in |
|
| 51 | + // the browser, but this can be handled sufficiently gracefully by the browser. |
|
| 52 | + if ($childPage instanceof RedirectorPage) { |
|
| 53 | + return $childPage->regularLink(); |
|
| 54 | + } |
|
| 55 | + // For all other pages, just return the link of the page. |
|
| 56 | + return $childPage->Link(); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public function syncLinkTracking() |
|
| 61 | - { |
|
| 62 | - // If we don't have anything to link to, then we have a broken link. |
|
| 63 | - if (!$this->Children()) { |
|
| 64 | - $this->HasBrokenLink = true; |
|
| 65 | - } |
|
| 66 | - } |
|
| 60 | + public function syncLinkTracking() |
|
| 61 | + { |
|
| 62 | + // If we don't have anything to link to, then we have a broken link. |
|
| 63 | + if (!$this->Children()) { |
|
| 64 | + $this->HasBrokenLink = true; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -6,11 +6,11 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SitemapPage extends Page |
| 8 | 8 | { |
| 9 | - private static $description = 'Lists all pages on the site'; |
|
| 9 | + private static $description = 'Lists all pages on the site'; |
|
| 10 | 10 | |
| 11 | - private static $singular_name = 'Sitemap Page'; |
|
| 11 | + private static $singular_name = 'Sitemap Page'; |
|
| 12 | 12 | |
| 13 | - private static $plural_name = 'Sitemap Pages'; |
|
| 13 | + private static $plural_name = 'Sitemap Pages'; |
|
| 14 | 14 | |
| 15 | - private static $table_name = 'SitemapPage'; |
|
| 15 | + private static $table_name = 'SitemapPage'; |
|
| 16 | 16 | } |
@@ -6,20 +6,20 @@ |
||
| 6 | 6 | |
| 7 | 7 | class BaseHomePageController extends PageController |
| 8 | 8 | { |
| 9 | - public function getNewsPage() |
|
| 10 | - { |
|
| 11 | - return NewsHolder::get_one(NewsHolder::class); |
|
| 12 | - } |
|
| 9 | + public function getNewsPage() |
|
| 10 | + { |
|
| 11 | + return NewsHolder::get_one(NewsHolder::class); |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @param int $amount The amount of items to provide. |
|
| 16 | - */ |
|
| 17 | - public function getNewsItems($amount = 2) |
|
| 18 | - { |
|
| 19 | - $newsHolder = $this->getNewsPage(); |
|
| 20 | - if ($newsHolder) { |
|
| 21 | - $controller = NewsHolderController::create($newsHolder); |
|
| 22 | - return $controller->Updates()->limit($amount); |
|
| 23 | - } |
|
| 24 | - } |
|
| 14 | + /** |
|
| 15 | + * @param int $amount The amount of items to provide. |
|
| 16 | + */ |
|
| 17 | + public function getNewsItems($amount = 2) |
|
| 18 | + { |
|
| 19 | + $newsHolder = $this->getNewsPage(); |
|
| 20 | + if ($newsHolder) { |
|
| 21 | + $controller = NewsHolderController::create($newsHolder); |
|
| 22 | + return $controller->Updates()->limit($amount); |
|
| 23 | + } |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -17,247 +17,247 @@ |
||
| 17 | 17 | |
| 18 | 18 | class DatedUpdateHolder extends Page |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Meant as an abstract base class. |
|
| 22 | - * |
|
| 23 | - * {@inheritDoc} |
|
| 24 | - */ |
|
| 25 | - private static $hide_ancestor = DatedUpdateHolder::class; |
|
| 26 | - |
|
| 27 | - private static $update_name = 'Updates'; |
|
| 28 | - |
|
| 29 | - private static $update_class = DatedUpdatePage::class; |
|
| 30 | - |
|
| 31 | - private static $singular_name = 'Dated Update Holder'; |
|
| 32 | - |
|
| 33 | - private static $plural_name = 'Dated Update Holders'; |
|
| 34 | - |
|
| 35 | - private static $table_name = 'DatedUpdateHolder'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Find all distinct tags (TaxonomyTerms) associated with the DatedUpdatePages under this holder. |
|
| 39 | - */ |
|
| 40 | - public function UpdateTags() |
|
| 41 | - { |
|
| 42 | - $siteTree = DataObject::getSchema()->tableName(SiteTree::class); |
|
| 43 | - $taxonomy = DataObject::getSchema()->tableName(TaxonomyTerm::class); |
|
| 44 | - |
|
| 45 | - $tags = TaxonomyTerm::get() |
|
| 46 | - ->innerJoin('BasePage_Terms', sprintf('"%s"."ID"="BasePage_Terms"."TaxonomyTermID"', $taxonomy)) |
|
| 47 | - ->innerJoin( |
|
| 48 | - $siteTree, |
|
| 49 | - sprintf( |
|
| 50 | - '"%s"."ID" = "BasePage_Terms"."BasePageID" AND "%s"."ParentID" = \'%d\'', |
|
| 51 | - $siteTree, |
|
| 52 | - $siteTree, |
|
| 53 | - $this->ID |
|
| 54 | - ) |
|
| 55 | - ) |
|
| 56 | - ->sort('Name'); |
|
| 57 | - |
|
| 58 | - return $tags; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Wrapper to find all updates belonging to this holder, based on some filters. |
|
| 63 | - */ |
|
| 64 | - public function Updates($tagID = null, $dateFrom = null, $dateTo = null, $year = null, $monthNumber = null) |
|
| 65 | - { |
|
| 66 | - $className = Config::inst()->get($this->ClassName, 'update_class'); |
|
| 67 | - return static::AllUpdates($className, $this->ID, $tagID, $dateFrom, $dateTo, $year, $monthNumber); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Find all site's updates, based on some filters. |
|
| 72 | - * Omitting parameters will prevent relevant filters from being applied. The filters are ANDed together. |
|
| 73 | - * |
|
| 74 | - * @param string $className The name of the class to fetch. |
|
| 75 | - * @param int|null $parentID The ID of the holder to extract the updates from. |
|
| 76 | - * @param int|null $tagID The ID of the tag to filter the updates by. |
|
| 77 | - * @param string|null $dateFrom The beginning of a date filter range. |
|
| 78 | - * @param string|null $dateTo The end of the date filter range. If empty, only one day will be searched for. |
|
| 79 | - * @param int|null $year Numeric value of the year to show. |
|
| 80 | - * @param int|null $monthNumber Numeric value of the month to show. |
|
| 81 | - * |
|
| 82 | - * @returns DataList | PaginatedList |
|
| 83 | - */ |
|
| 84 | - public static function AllUpdates( |
|
| 85 | - $className = DatedUpdatePage::class, |
|
| 86 | - $parentID = null, |
|
| 87 | - $tagID = null, |
|
| 88 | - $dateFrom = null, |
|
| 89 | - $dateTo = null, |
|
| 90 | - $year = null, |
|
| 91 | - $monthNumber = null |
|
| 92 | - ) { |
|
| 93 | - |
|
| 94 | - $items = $className::get(); |
|
| 95 | - $dbTableName = DataObject::getSchema()->tableForField($className, 'Date'); |
|
| 96 | - if (!$dbTableName) { |
|
| 97 | - $dbTableName = DatedUpdatePage::class; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Filter by parent holder. |
|
| 101 | - if (isset($parentID)) { |
|
| 102 | - $items = $items->filter(['ParentID'=>$parentID]); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Filter down to a single tag. |
|
| 106 | - if (isset($tagID)) { |
|
| 107 | - $taxonomy = DataObject::getSchema()->tableName(TaxonomyTerm::class); |
|
| 108 | - $tableName = DataObject::getSchema()->tableName($className); |
|
| 109 | - |
|
| 110 | - $items = $items->innerJoin( |
|
| 111 | - 'BasePage_Terms', |
|
| 112 | - sprintf('"%s"."ID" = "BasePage_Terms"."BasePageID"', $tableName) |
|
| 113 | - )->innerJoin( |
|
| 114 | - $taxonomy, |
|
| 115 | - sprintf( |
|
| 116 | - '"BasePage_Terms"."TaxonomyTermID" = "%s"."ID" AND "TaxonomyTerm"."ID" = \'%d\'', |
|
| 117 | - $taxonomy, |
|
| 118 | - $tagID |
|
| 119 | - ) |
|
| 120 | - ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // Filter by date |
|
| 124 | - if (isset($dateFrom)) { |
|
| 125 | - if (!isset($dateTo)) { |
|
| 126 | - $dateTo = $dateFrom; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $items = $items->where([ |
|
| 130 | - sprintf('"%s"."Date" >= \'%s\'', $dbTableName, Convert::raw2sql("$dateFrom 00:00:00")), |
|
| 131 | - sprintf('"%s"."Date" <= \'%s\'', $dbTableName, Convert::raw2sql("$dateTo 23:59:59")) |
|
| 132 | - ]); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Filter down to single month. |
|
| 136 | - if (isset($year) && isset($monthNumber)) { |
|
| 137 | - $year = (int)$year; |
|
| 138 | - $monthNumber = (int)$monthNumber; |
|
| 139 | - |
|
| 140 | - $beginDate = sprintf("%04d-%02d-01 00:00:00", $year, $monthNumber); |
|
| 141 | - $endDate = date('Y-m-d H:i:s', strtotime("{$beginDate} +1 month")); |
|
| 142 | - |
|
| 143 | - $items = $items->where(array( |
|
| 144 | - sprintf('"%s"."Date" >= \'%s\'', $dbTableName, Convert::raw2sql($beginDate)), |
|
| 145 | - sprintf('"%s"."Date" < \'%s\'', $dbTableName, Convert::raw2sql($endDate)) |
|
| 146 | - )); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Unpaginated DataList. |
|
| 150 | - return $items; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Produce an ArrayList of available months out of the updates contained in the DataList. |
|
| 155 | - * |
|
| 156 | - * Here is an example of the returned structure: |
|
| 157 | - * ArrayList: |
|
| 158 | - * ArrayData: |
|
| 159 | - * YearName => 2013 |
|
| 160 | - * Months => ArrayList: |
|
| 161 | - * MonthName => Jan |
|
| 162 | - * MonthNumber => 1 |
|
| 163 | - * MonthLink => (page URL)year=2012&month=1 |
|
| 164 | - * Active => true |
|
| 165 | - * ArrayData: |
|
| 166 | - * YearName => 2012 |
|
| 167 | - * Months => ArrayList: |
|
| 168 | - * ... |
|
| 169 | - * |
|
| 170 | - * @param DataList $updates DataList DataList to extract months from. |
|
| 171 | - * @param string $link Link used as abase to construct the MonthLink. |
|
| 172 | - * @param int $currentYear Currently selected year, for computing the link active state. |
|
| 173 | - * @param int $currentMonthNumber Currently selected month, for computing the link active state. |
|
| 174 | - * |
|
| 175 | - * @returns ArrayList |
|
| 176 | - */ |
|
| 177 | - public static function ExtractMonths( |
|
| 178 | - DataList $updates, |
|
| 179 | - $link = null, |
|
| 180 | - $currentYear = null, |
|
| 181 | - $currentMonthNumber = null |
|
| 182 | - ) { |
|
| 183 | - // Set the link to current URL in the same way the HTTP::setGetVar does it. |
|
| 184 | - if (!isset($link)) { |
|
| 185 | - $link = Director::makeRelative($_SERVER['REQUEST_URI']); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $dates = $updates->dataQuery() |
|
| 189 | - ->groupby('YEAR("Date")') |
|
| 190 | - ->groupby('MONTH("Date")') |
|
| 191 | - ->query() |
|
| 192 | - ->setSelect([ |
|
| 193 | - 'Year' => 'YEAR("Date")', |
|
| 194 | - 'Month' => 'MONTH("Date")', |
|
| 195 | - ]) |
|
| 196 | - ->addWhere('"Date" IS NOT NULL') |
|
| 197 | - ->setOrderBy([ |
|
| 198 | - 'YEAR("Date")' => 'DESC', |
|
| 199 | - 'MONTH("Date")' => 'DESC', |
|
| 200 | - ]) |
|
| 201 | - ->execute(); |
|
| 202 | - |
|
| 203 | - $years = []; |
|
| 204 | - foreach ($dates as $date) { |
|
| 205 | - $monthNumber = $date['Month']; |
|
| 206 | - $year = $date['Year']; |
|
| 207 | - $dateObj = new DateTime(implode('-', [$year, $monthNumber, 1])); |
|
| 208 | - $monthName = $dateObj->Format('M'); |
|
| 209 | - |
|
| 210 | - // Set up the relevant year array, if not yet available. |
|
| 211 | - if (!isset($years[$year])) { |
|
| 212 | - $years[$year] = ['YearName'=>$year, 'Months' => []]; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // Check if the currently processed month is the one that is selected via GET params. |
|
| 216 | - $active = false; |
|
| 217 | - if (isset($year) && isset($monthNumber)) { |
|
| 218 | - $active = (((int)$currentYear)==$year && ((int)$currentMonthNumber)==$monthNumber); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // Build the link - keep the tag and date filter, but reset the pagination. |
|
| 222 | - if ($active) { |
|
| 223 | - // Allow clicking to deselect the month. |
|
| 224 | - $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 225 | - $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 226 | - } else { |
|
| 227 | - $link = HTTP::setGetVar('month', $monthNumber, $link, '&'); |
|
| 228 | - $link = HTTP::setGetVar('year', $year, $link, '&'); |
|
| 229 | - } |
|
| 230 | - $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 231 | - |
|
| 232 | - $years[$year]['Months'][$monthNumber] = array( |
|
| 233 | - 'MonthName'=>$monthName, |
|
| 234 | - 'MonthNumber'=>$monthNumber, |
|
| 235 | - 'MonthLink'=>$link, |
|
| 236 | - 'Active'=>$active |
|
| 237 | - ); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - // ArrayList will not recursively walk through the supplied array, so manually build nested ArrayLists. |
|
| 241 | - foreach ($years as &$year) { |
|
| 242 | - $year['Months'] = new ArrayList($year['Months']); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - // Reverse the list so the most recent years appear first. |
|
| 246 | - return new ArrayList($years); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function getDefaultRSSLink() |
|
| 250 | - { |
|
| 251 | - return $this->Link('rss'); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - public function getDefaultAtomLink() |
|
| 255 | - { |
|
| 256 | - return $this->Link('atom'); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - public function getSubscriptionTitle() |
|
| 260 | - { |
|
| 261 | - return $this->Title; |
|
| 262 | - } |
|
| 20 | + /** |
|
| 21 | + * Meant as an abstract base class. |
|
| 22 | + * |
|
| 23 | + * {@inheritDoc} |
|
| 24 | + */ |
|
| 25 | + private static $hide_ancestor = DatedUpdateHolder::class; |
|
| 26 | + |
|
| 27 | + private static $update_name = 'Updates'; |
|
| 28 | + |
|
| 29 | + private static $update_class = DatedUpdatePage::class; |
|
| 30 | + |
|
| 31 | + private static $singular_name = 'Dated Update Holder'; |
|
| 32 | + |
|
| 33 | + private static $plural_name = 'Dated Update Holders'; |
|
| 34 | + |
|
| 35 | + private static $table_name = 'DatedUpdateHolder'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Find all distinct tags (TaxonomyTerms) associated with the DatedUpdatePages under this holder. |
|
| 39 | + */ |
|
| 40 | + public function UpdateTags() |
|
| 41 | + { |
|
| 42 | + $siteTree = DataObject::getSchema()->tableName(SiteTree::class); |
|
| 43 | + $taxonomy = DataObject::getSchema()->tableName(TaxonomyTerm::class); |
|
| 44 | + |
|
| 45 | + $tags = TaxonomyTerm::get() |
|
| 46 | + ->innerJoin('BasePage_Terms', sprintf('"%s"."ID"="BasePage_Terms"."TaxonomyTermID"', $taxonomy)) |
|
| 47 | + ->innerJoin( |
|
| 48 | + $siteTree, |
|
| 49 | + sprintf( |
|
| 50 | + '"%s"."ID" = "BasePage_Terms"."BasePageID" AND "%s"."ParentID" = \'%d\'', |
|
| 51 | + $siteTree, |
|
| 52 | + $siteTree, |
|
| 53 | + $this->ID |
|
| 54 | + ) |
|
| 55 | + ) |
|
| 56 | + ->sort('Name'); |
|
| 57 | + |
|
| 58 | + return $tags; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Wrapper to find all updates belonging to this holder, based on some filters. |
|
| 63 | + */ |
|
| 64 | + public function Updates($tagID = null, $dateFrom = null, $dateTo = null, $year = null, $monthNumber = null) |
|
| 65 | + { |
|
| 66 | + $className = Config::inst()->get($this->ClassName, 'update_class'); |
|
| 67 | + return static::AllUpdates($className, $this->ID, $tagID, $dateFrom, $dateTo, $year, $monthNumber); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Find all site's updates, based on some filters. |
|
| 72 | + * Omitting parameters will prevent relevant filters from being applied. The filters are ANDed together. |
|
| 73 | + * |
|
| 74 | + * @param string $className The name of the class to fetch. |
|
| 75 | + * @param int|null $parentID The ID of the holder to extract the updates from. |
|
| 76 | + * @param int|null $tagID The ID of the tag to filter the updates by. |
|
| 77 | + * @param string|null $dateFrom The beginning of a date filter range. |
|
| 78 | + * @param string|null $dateTo The end of the date filter range. If empty, only one day will be searched for. |
|
| 79 | + * @param int|null $year Numeric value of the year to show. |
|
| 80 | + * @param int|null $monthNumber Numeric value of the month to show. |
|
| 81 | + * |
|
| 82 | + * @returns DataList | PaginatedList |
|
| 83 | + */ |
|
| 84 | + public static function AllUpdates( |
|
| 85 | + $className = DatedUpdatePage::class, |
|
| 86 | + $parentID = null, |
|
| 87 | + $tagID = null, |
|
| 88 | + $dateFrom = null, |
|
| 89 | + $dateTo = null, |
|
| 90 | + $year = null, |
|
| 91 | + $monthNumber = null |
|
| 92 | + ) { |
|
| 93 | + |
|
| 94 | + $items = $className::get(); |
|
| 95 | + $dbTableName = DataObject::getSchema()->tableForField($className, 'Date'); |
|
| 96 | + if (!$dbTableName) { |
|
| 97 | + $dbTableName = DatedUpdatePage::class; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Filter by parent holder. |
|
| 101 | + if (isset($parentID)) { |
|
| 102 | + $items = $items->filter(['ParentID'=>$parentID]); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Filter down to a single tag. |
|
| 106 | + if (isset($tagID)) { |
|
| 107 | + $taxonomy = DataObject::getSchema()->tableName(TaxonomyTerm::class); |
|
| 108 | + $tableName = DataObject::getSchema()->tableName($className); |
|
| 109 | + |
|
| 110 | + $items = $items->innerJoin( |
|
| 111 | + 'BasePage_Terms', |
|
| 112 | + sprintf('"%s"."ID" = "BasePage_Terms"."BasePageID"', $tableName) |
|
| 113 | + )->innerJoin( |
|
| 114 | + $taxonomy, |
|
| 115 | + sprintf( |
|
| 116 | + '"BasePage_Terms"."TaxonomyTermID" = "%s"."ID" AND "TaxonomyTerm"."ID" = \'%d\'', |
|
| 117 | + $taxonomy, |
|
| 118 | + $tagID |
|
| 119 | + ) |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // Filter by date |
|
| 124 | + if (isset($dateFrom)) { |
|
| 125 | + if (!isset($dateTo)) { |
|
| 126 | + $dateTo = $dateFrom; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $items = $items->where([ |
|
| 130 | + sprintf('"%s"."Date" >= \'%s\'', $dbTableName, Convert::raw2sql("$dateFrom 00:00:00")), |
|
| 131 | + sprintf('"%s"."Date" <= \'%s\'', $dbTableName, Convert::raw2sql("$dateTo 23:59:59")) |
|
| 132 | + ]); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Filter down to single month. |
|
| 136 | + if (isset($year) && isset($monthNumber)) { |
|
| 137 | + $year = (int)$year; |
|
| 138 | + $monthNumber = (int)$monthNumber; |
|
| 139 | + |
|
| 140 | + $beginDate = sprintf("%04d-%02d-01 00:00:00", $year, $monthNumber); |
|
| 141 | + $endDate = date('Y-m-d H:i:s', strtotime("{$beginDate} +1 month")); |
|
| 142 | + |
|
| 143 | + $items = $items->where(array( |
|
| 144 | + sprintf('"%s"."Date" >= \'%s\'', $dbTableName, Convert::raw2sql($beginDate)), |
|
| 145 | + sprintf('"%s"."Date" < \'%s\'', $dbTableName, Convert::raw2sql($endDate)) |
|
| 146 | + )); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Unpaginated DataList. |
|
| 150 | + return $items; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Produce an ArrayList of available months out of the updates contained in the DataList. |
|
| 155 | + * |
|
| 156 | + * Here is an example of the returned structure: |
|
| 157 | + * ArrayList: |
|
| 158 | + * ArrayData: |
|
| 159 | + * YearName => 2013 |
|
| 160 | + * Months => ArrayList: |
|
| 161 | + * MonthName => Jan |
|
| 162 | + * MonthNumber => 1 |
|
| 163 | + * MonthLink => (page URL)year=2012&month=1 |
|
| 164 | + * Active => true |
|
| 165 | + * ArrayData: |
|
| 166 | + * YearName => 2012 |
|
| 167 | + * Months => ArrayList: |
|
| 168 | + * ... |
|
| 169 | + * |
|
| 170 | + * @param DataList $updates DataList DataList to extract months from. |
|
| 171 | + * @param string $link Link used as abase to construct the MonthLink. |
|
| 172 | + * @param int $currentYear Currently selected year, for computing the link active state. |
|
| 173 | + * @param int $currentMonthNumber Currently selected month, for computing the link active state. |
|
| 174 | + * |
|
| 175 | + * @returns ArrayList |
|
| 176 | + */ |
|
| 177 | + public static function ExtractMonths( |
|
| 178 | + DataList $updates, |
|
| 179 | + $link = null, |
|
| 180 | + $currentYear = null, |
|
| 181 | + $currentMonthNumber = null |
|
| 182 | + ) { |
|
| 183 | + // Set the link to current URL in the same way the HTTP::setGetVar does it. |
|
| 184 | + if (!isset($link)) { |
|
| 185 | + $link = Director::makeRelative($_SERVER['REQUEST_URI']); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $dates = $updates->dataQuery() |
|
| 189 | + ->groupby('YEAR("Date")') |
|
| 190 | + ->groupby('MONTH("Date")') |
|
| 191 | + ->query() |
|
| 192 | + ->setSelect([ |
|
| 193 | + 'Year' => 'YEAR("Date")', |
|
| 194 | + 'Month' => 'MONTH("Date")', |
|
| 195 | + ]) |
|
| 196 | + ->addWhere('"Date" IS NOT NULL') |
|
| 197 | + ->setOrderBy([ |
|
| 198 | + 'YEAR("Date")' => 'DESC', |
|
| 199 | + 'MONTH("Date")' => 'DESC', |
|
| 200 | + ]) |
|
| 201 | + ->execute(); |
|
| 202 | + |
|
| 203 | + $years = []; |
|
| 204 | + foreach ($dates as $date) { |
|
| 205 | + $monthNumber = $date['Month']; |
|
| 206 | + $year = $date['Year']; |
|
| 207 | + $dateObj = new DateTime(implode('-', [$year, $monthNumber, 1])); |
|
| 208 | + $monthName = $dateObj->Format('M'); |
|
| 209 | + |
|
| 210 | + // Set up the relevant year array, if not yet available. |
|
| 211 | + if (!isset($years[$year])) { |
|
| 212 | + $years[$year] = ['YearName'=>$year, 'Months' => []]; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // Check if the currently processed month is the one that is selected via GET params. |
|
| 216 | + $active = false; |
|
| 217 | + if (isset($year) && isset($monthNumber)) { |
|
| 218 | + $active = (((int)$currentYear)==$year && ((int)$currentMonthNumber)==$monthNumber); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // Build the link - keep the tag and date filter, but reset the pagination. |
|
| 222 | + if ($active) { |
|
| 223 | + // Allow clicking to deselect the month. |
|
| 224 | + $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 225 | + $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 226 | + } else { |
|
| 227 | + $link = HTTP::setGetVar('month', $monthNumber, $link, '&'); |
|
| 228 | + $link = HTTP::setGetVar('year', $year, $link, '&'); |
|
| 229 | + } |
|
| 230 | + $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 231 | + |
|
| 232 | + $years[$year]['Months'][$monthNumber] = array( |
|
| 233 | + 'MonthName'=>$monthName, |
|
| 234 | + 'MonthNumber'=>$monthNumber, |
|
| 235 | + 'MonthLink'=>$link, |
|
| 236 | + 'Active'=>$active |
|
| 237 | + ); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + // ArrayList will not recursively walk through the supplied array, so manually build nested ArrayLists. |
|
| 241 | + foreach ($years as &$year) { |
|
| 242 | + $year['Months'] = new ArrayList($year['Months']); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + // Reverse the list so the most recent years appear first. |
|
| 246 | + return new ArrayList($years); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function getDefaultRSSLink() |
|
| 250 | + { |
|
| 251 | + return $this->Link('rss'); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + public function getDefaultAtomLink() |
|
| 255 | + { |
|
| 256 | + return $this->Link('atom'); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + public function getSubscriptionTitle() |
|
| 260 | + { |
|
| 261 | + return $this->Title; |
|
| 262 | + } |
|
| 263 | 263 | } |
@@ -7,9 +7,9 @@ |
||
| 7 | 7 | // @todo replace with QueuedJobs |
| 8 | 8 | class CleanupGeneratedPdfDailyTask // extends DailyTask |
| 9 | 9 | { |
| 10 | - public function process() |
|
| 11 | - { |
|
| 12 | - $task = new CleanupGeneratedPdfBuildTask(); |
|
| 13 | - $task->run(null); |
|
| 14 | - } |
|
| 10 | + public function process() |
|
| 11 | + { |
|
| 12 | + $task = new CleanupGeneratedPdfBuildTask(); |
|
| 13 | + $task->run(null); |
|
| 14 | + } |
|
| 15 | 15 | } |
@@ -7,24 +7,24 @@ |
||
| 7 | 7 | |
| 8 | 8 | class CleanupGeneratedPdfBuildTask extends BuildTask |
| 9 | 9 | { |
| 10 | - protected $title = 'Cleanup generated PDFs'; |
|
| 10 | + protected $title = 'Cleanup generated PDFs'; |
|
| 11 | 11 | |
| 12 | - protected $description = 'Removes generated PDFs on the site, forcing a regeneration of all exports to PDF ' |
|
| 13 | - . 'when users go to download them. This is most useful when templates have been changed so users should ' |
|
| 14 | - . 'receive a new copy'; |
|
| 12 | + protected $description = 'Removes generated PDFs on the site, forcing a regeneration of all exports to PDF ' |
|
| 13 | + . 'when users go to download them. This is most useful when templates have been changed so users should ' |
|
| 14 | + . 'receive a new copy'; |
|
| 15 | 15 | |
| 16 | - public function run($request) |
|
| 17 | - { |
|
| 18 | - $path = sprintf('%s/%s', BASE_PATH, BasePage::config()->generated_pdf_path); |
|
| 19 | - if (!file_exists($path)) { |
|
| 20 | - return false; |
|
| 21 | - } |
|
| 16 | + public function run($request) |
|
| 17 | + { |
|
| 18 | + $path = sprintf('%s/%s', BASE_PATH, BasePage::config()->generated_pdf_path); |
|
| 19 | + if (!file_exists($path)) { |
|
| 20 | + return false; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - exec(sprintf('if [ "$(ls -A %s 2> /dev/null)" != "" ]; then rm %s/*; fi', $path, $path), $output, $return_val); |
|
| 23 | + exec(sprintf('if [ "$(ls -A %s 2> /dev/null)" != "" ]; then rm %s/*; fi', $path, $path), $output, $return_val); |
|
| 24 | 24 | |
| 25 | - // output any errors |
|
| 26 | - if ($return_val != 0) { |
|
| 27 | - user_error(sprintf('%s failed: ', get_class($this)) . implode("\n", $output), E_USER_ERROR); |
|
| 28 | - } |
|
| 29 | - } |
|
| 25 | + // output any errors |
|
| 26 | + if ($return_val != 0) { |
|
| 27 | + user_error(sprintf('%s failed: ', get_class($this)) . implode("\n", $output), E_USER_ERROR); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -16,138 +16,138 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CwpSearchEngine |
| 18 | 18 | { |
| 19 | - use Configurable; |
|
| 20 | - use Extensible; |
|
| 21 | - use Injectable; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Default search options |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - * @config |
|
| 28 | - */ |
|
| 29 | - private static $search_options = [ |
|
| 30 | - 'hl' => 'true', |
|
| 31 | - ]; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Additional search options to send to search when spellcheck |
|
| 35 | - * is included |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - * @config |
|
| 39 | - */ |
|
| 40 | - private static $spellcheck_options = [ |
|
| 41 | - 'spellcheck' => 'true', |
|
| 42 | - 'spellcheck.collate' => 'true', |
|
| 43 | - // spellcheck.dictionary can also be configured to use '_spellcheck' |
|
| 44 | - // dictionary when indexing fields under the _spellcheckText column |
|
| 45 | - 'spellcheck.dictionary' => 'default', |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Build a SearchQuery for a new search |
|
| 50 | - * |
|
| 51 | - * @param string $keywords |
|
| 52 | - * @param array $classes |
|
| 53 | - * @return SearchQuery |
|
| 54 | - */ |
|
| 55 | - protected function getSearchQuery($keywords, $classes) |
|
| 56 | - { |
|
| 57 | - $query = new SearchQuery(); |
|
| 58 | - $query->classes = $classes; |
|
| 59 | - $query->search($keywords); |
|
| 60 | - $query->exclude('SiteTree_ShowInSearch', 0); |
|
| 61 | - $query->exclude('File_ShowInSearch', 0); |
|
| 62 | - |
|
| 63 | - // Artificially lower the amount of results to prevent too high resource usage. |
|
| 64 | - // on subsequent canView check loop. |
|
| 65 | - $query->limit(100); |
|
| 66 | - return $query; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Get solr search options for this query |
|
| 71 | - * |
|
| 72 | - * @param bool $spellcheck True if we should include spellcheck support |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - protected function getSearchOptions($spellcheck) |
|
| 76 | - { |
|
| 77 | - $options = $this->config()->search_options; |
|
| 78 | - if ($spellcheck) { |
|
| 79 | - $options = array_merge($options, $this->config()->spellcheck_options); |
|
| 80 | - } |
|
| 81 | - return $options; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get results for a search term |
|
| 86 | - * |
|
| 87 | - * @param string $keywords |
|
| 88 | - * @param array $classes |
|
| 89 | - * @param SolrIndex $searchIndex |
|
| 90 | - * @param int $limit Max number of results for this page |
|
| 91 | - * @param int $start Skip this number of records |
|
| 92 | - * @param bool $spellcheck True to enable spellcheck |
|
| 93 | - * @return CwpSearchResult |
|
| 94 | - */ |
|
| 95 | - protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false) |
|
| 96 | - { |
|
| 97 | - // Prepare options |
|
| 98 | - $query = $this->getSearchQuery($keywords, $classes); |
|
| 99 | - $options = $this->getSearchOptions($spellcheck); |
|
| 100 | - |
|
| 101 | - // Get results |
|
| 102 | - $solrResult = $searchIndex->search( |
|
| 103 | - $query, |
|
| 104 | - $start, |
|
| 105 | - $limit, |
|
| 106 | - $options |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - return CwpSearchResult::create($keywords, $solrResult); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get a CwpSearchResult for a given criterea |
|
| 114 | - * |
|
| 115 | - * @param string $keywords |
|
| 116 | - * @param array $classes |
|
| 117 | - * @param SolrIndex $searchIndex |
|
| 118 | - * @param int $limit Max number of results for this page |
|
| 119 | - * @param int $start Skip this number of records |
|
| 120 | - * @param bool $followSuggestions True to enable suggested searches to be returned immediately |
|
| 121 | - * @return CwpSearchResult|null |
|
| 122 | - */ |
|
| 123 | - public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false) |
|
| 124 | - { |
|
| 125 | - if (empty($keywords)) { |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - try { |
|
| 130 | - // Begin search |
|
| 131 | - $result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, true); |
|
| 132 | - |
|
| 133 | - // Return results if we don't need to refine this any further |
|
| 134 | - if (!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) { |
|
| 135 | - return $result; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Perform new search with the suggested terms |
|
| 139 | - $suggested = $result->getSuggestion(); |
|
| 140 | - $newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false); |
|
| 141 | - $newResult->setOriginal($keywords); |
|
| 142 | - |
|
| 143 | - // Compare new results to the original query |
|
| 144 | - if ($newResult->hasResults()) { |
|
| 145 | - return $newResult; |
|
| 146 | - } else { |
|
| 147 | - return $result; |
|
| 148 | - } |
|
| 149 | - } catch (Exception $e) { |
|
| 150 | - Injector::inst()->get(LoggerInterface::class)->warning($e); |
|
| 151 | - } |
|
| 152 | - } |
|
| 19 | + use Configurable; |
|
| 20 | + use Extensible; |
|
| 21 | + use Injectable; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Default search options |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + * @config |
|
| 28 | + */ |
|
| 29 | + private static $search_options = [ |
|
| 30 | + 'hl' => 'true', |
|
| 31 | + ]; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Additional search options to send to search when spellcheck |
|
| 35 | + * is included |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + * @config |
|
| 39 | + */ |
|
| 40 | + private static $spellcheck_options = [ |
|
| 41 | + 'spellcheck' => 'true', |
|
| 42 | + 'spellcheck.collate' => 'true', |
|
| 43 | + // spellcheck.dictionary can also be configured to use '_spellcheck' |
|
| 44 | + // dictionary when indexing fields under the _spellcheckText column |
|
| 45 | + 'spellcheck.dictionary' => 'default', |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Build a SearchQuery for a new search |
|
| 50 | + * |
|
| 51 | + * @param string $keywords |
|
| 52 | + * @param array $classes |
|
| 53 | + * @return SearchQuery |
|
| 54 | + */ |
|
| 55 | + protected function getSearchQuery($keywords, $classes) |
|
| 56 | + { |
|
| 57 | + $query = new SearchQuery(); |
|
| 58 | + $query->classes = $classes; |
|
| 59 | + $query->search($keywords); |
|
| 60 | + $query->exclude('SiteTree_ShowInSearch', 0); |
|
| 61 | + $query->exclude('File_ShowInSearch', 0); |
|
| 62 | + |
|
| 63 | + // Artificially lower the amount of results to prevent too high resource usage. |
|
| 64 | + // on subsequent canView check loop. |
|
| 65 | + $query->limit(100); |
|
| 66 | + return $query; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Get solr search options for this query |
|
| 71 | + * |
|
| 72 | + * @param bool $spellcheck True if we should include spellcheck support |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + protected function getSearchOptions($spellcheck) |
|
| 76 | + { |
|
| 77 | + $options = $this->config()->search_options; |
|
| 78 | + if ($spellcheck) { |
|
| 79 | + $options = array_merge($options, $this->config()->spellcheck_options); |
|
| 80 | + } |
|
| 81 | + return $options; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get results for a search term |
|
| 86 | + * |
|
| 87 | + * @param string $keywords |
|
| 88 | + * @param array $classes |
|
| 89 | + * @param SolrIndex $searchIndex |
|
| 90 | + * @param int $limit Max number of results for this page |
|
| 91 | + * @param int $start Skip this number of records |
|
| 92 | + * @param bool $spellcheck True to enable spellcheck |
|
| 93 | + * @return CwpSearchResult |
|
| 94 | + */ |
|
| 95 | + protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false) |
|
| 96 | + { |
|
| 97 | + // Prepare options |
|
| 98 | + $query = $this->getSearchQuery($keywords, $classes); |
|
| 99 | + $options = $this->getSearchOptions($spellcheck); |
|
| 100 | + |
|
| 101 | + // Get results |
|
| 102 | + $solrResult = $searchIndex->search( |
|
| 103 | + $query, |
|
| 104 | + $start, |
|
| 105 | + $limit, |
|
| 106 | + $options |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + return CwpSearchResult::create($keywords, $solrResult); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get a CwpSearchResult for a given criterea |
|
| 114 | + * |
|
| 115 | + * @param string $keywords |
|
| 116 | + * @param array $classes |
|
| 117 | + * @param SolrIndex $searchIndex |
|
| 118 | + * @param int $limit Max number of results for this page |
|
| 119 | + * @param int $start Skip this number of records |
|
| 120 | + * @param bool $followSuggestions True to enable suggested searches to be returned immediately |
|
| 121 | + * @return CwpSearchResult|null |
|
| 122 | + */ |
|
| 123 | + public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false) |
|
| 124 | + { |
|
| 125 | + if (empty($keywords)) { |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + try { |
|
| 130 | + // Begin search |
|
| 131 | + $result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, true); |
|
| 132 | + |
|
| 133 | + // Return results if we don't need to refine this any further |
|
| 134 | + if (!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) { |
|
| 135 | + return $result; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Perform new search with the suggested terms |
|
| 139 | + $suggested = $result->getSuggestion(); |
|
| 140 | + $newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false); |
|
| 141 | + $newResult->setOriginal($keywords); |
|
| 142 | + |
|
| 143 | + // Compare new results to the original query |
|
| 144 | + if ($newResult->hasResults()) { |
|
| 145 | + return $newResult; |
|
| 146 | + } else { |
|
| 147 | + return $result; |
|
| 148 | + } |
|
| 149 | + } catch (Exception $e) { |
|
| 150 | + Injector::inst()->get(LoggerInterface::class)->warning($e); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | } |
@@ -11,204 +11,204 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CwpSearchResult extends ViewableData |
| 13 | 13 | { |
| 14 | - private static $casting = [ |
|
| 15 | - 'Original' => 'Text', |
|
| 16 | - 'OriginalLink' => 'Text', |
|
| 17 | - 'Suggestion' => 'Text', |
|
| 18 | - 'SuggestionLink' => 'Text', |
|
| 19 | - 'Query' => 'Text', |
|
| 20 | - 'SearchLink' => 'Text', |
|
| 21 | - 'RSSLink' => 'Text', |
|
| 22 | - 'AtomLink' => 'Text', |
|
| 23 | - ]; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * List of results |
|
| 27 | - * |
|
| 28 | - * @var PaginatedList |
|
| 29 | - */ |
|
| 30 | - protected $matches; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Search terms for these results |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $query; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Suggested search keywords |
|
| 41 | - * Used when this search has suggested terms, but following suggestions isn't enabled |
|
| 42 | - * |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - protected $suggestion; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Original terms superceded by these result. |
|
| 49 | - * Used when a prior search had suggested terms, and follow suggestions is enabled. |
|
| 50 | - * |
|
| 51 | - * @var PaginatedList |
|
| 52 | - */ |
|
| 53 | - protected $original; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create a new CwpSearchResult |
|
| 57 | - * |
|
| 58 | - * @param string $terms |
|
| 59 | - * @param ArrayData $results Result from SolrIndex |
|
| 60 | - */ |
|
| 61 | - public function __construct($terms = '', ArrayData $results = null) |
|
| 62 | - { |
|
| 63 | - $this->query = $terms; |
|
| 64 | - if ($results) { |
|
| 65 | - // Clean up the results. |
|
| 66 | - $matches = $results->Matches; |
|
| 67 | - foreach ($matches as $result) { |
|
| 68 | - if (!$result->canView()) { |
|
| 69 | - $matches->remove($result); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $this->matches = $matches; |
|
| 74 | - $this->suggestion = $results->SuggestionNice; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get search results |
|
| 80 | - * |
|
| 81 | - * @return PaginatedList |
|
| 82 | - */ |
|
| 83 | - public function getResults() |
|
| 84 | - { |
|
| 85 | - return $this->matches; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Check if there are found results |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function hasResults() |
|
| 94 | - { |
|
| 95 | - return $this->matches && $this->matches->exists(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get search keywords matching these results |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - public function getQuery() |
|
| 104 | - { |
|
| 105 | - return $this->query; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get suggested search keywords |
|
| 110 | - * |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - public function getSuggestion() |
|
| 114 | - { |
|
| 115 | - return $this->suggestion; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get original search keywords superceded by these results |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function getOriginal() |
|
| 124 | - { |
|
| 125 | - return $this->original; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Set original keywords |
|
| 130 | - * |
|
| 131 | - * @param string $original |
|
| 132 | - */ |
|
| 133 | - public function setOriginal($original) |
|
| 134 | - { |
|
| 135 | - $this->original = $original; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the link to the suggested search |
|
| 140 | - * |
|
| 141 | - * @param string $format Optional output format |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getSuggestionLink($format = null) |
|
| 145 | - { |
|
| 146 | - return $this->getLink($this->suggestion, $format); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Gets the link to the given search |
|
| 151 | - * |
|
| 152 | - * @param string $format Optional output format |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - public function getSearchLink($format = null) |
|
| 156 | - { |
|
| 157 | - return $this->getLink($this->query, $format); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Gets the link to the original search, with suggestions disabled |
|
| 162 | - * |
|
| 163 | - * @param string $format Optional output format |
|
| 164 | - * @return string |
|
| 165 | - */ |
|
| 166 | - public function getOriginalLink($format = null) |
|
| 167 | - { |
|
| 168 | - return $this->getLink($this->original, $format) . "&suggestions=0"; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get link to these results in RSS format |
|
| 173 | - * |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - public function getRSSLink() |
|
| 177 | - { |
|
| 178 | - return $this->getLink($this->query, 'rss'); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get link to these results in atom format |
|
| 183 | - * |
|
| 184 | - * @return string |
|
| 185 | - */ |
|
| 186 | - public function getAtomLink() |
|
| 187 | - { |
|
| 188 | - return $this->getLink($this->query, 'atom'); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get a search link for given terms |
|
| 193 | - * |
|
| 194 | - * @param string $terms |
|
| 195 | - * @return string|null |
|
| 196 | - */ |
|
| 197 | - protected function getLink($terms, $format = null) |
|
| 198 | - { |
|
| 199 | - if (!$terms) { |
|
| 200 | - return null; |
|
| 201 | - } |
|
| 202 | - $link = 'search/SearchForm?Search='.rawurlencode($terms); |
|
| 203 | - if ($format) { |
|
| 204 | - $link .= '&format='.rawurlencode($format); |
|
| 205 | - } |
|
| 206 | - return $link; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function hasField($field) |
|
| 210 | - { |
|
| 211 | - // Fix customise not detecting custom field getters |
|
| 212 | - return array_key_exists($field, $this->config()->casting); |
|
| 213 | - } |
|
| 14 | + private static $casting = [ |
|
| 15 | + 'Original' => 'Text', |
|
| 16 | + 'OriginalLink' => 'Text', |
|
| 17 | + 'Suggestion' => 'Text', |
|
| 18 | + 'SuggestionLink' => 'Text', |
|
| 19 | + 'Query' => 'Text', |
|
| 20 | + 'SearchLink' => 'Text', |
|
| 21 | + 'RSSLink' => 'Text', |
|
| 22 | + 'AtomLink' => 'Text', |
|
| 23 | + ]; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * List of results |
|
| 27 | + * |
|
| 28 | + * @var PaginatedList |
|
| 29 | + */ |
|
| 30 | + protected $matches; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Search terms for these results |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $query; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Suggested search keywords |
|
| 41 | + * Used when this search has suggested terms, but following suggestions isn't enabled |
|
| 42 | + * |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + protected $suggestion; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Original terms superceded by these result. |
|
| 49 | + * Used when a prior search had suggested terms, and follow suggestions is enabled. |
|
| 50 | + * |
|
| 51 | + * @var PaginatedList |
|
| 52 | + */ |
|
| 53 | + protected $original; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create a new CwpSearchResult |
|
| 57 | + * |
|
| 58 | + * @param string $terms |
|
| 59 | + * @param ArrayData $results Result from SolrIndex |
|
| 60 | + */ |
|
| 61 | + public function __construct($terms = '', ArrayData $results = null) |
|
| 62 | + { |
|
| 63 | + $this->query = $terms; |
|
| 64 | + if ($results) { |
|
| 65 | + // Clean up the results. |
|
| 66 | + $matches = $results->Matches; |
|
| 67 | + foreach ($matches as $result) { |
|
| 68 | + if (!$result->canView()) { |
|
| 69 | + $matches->remove($result); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $this->matches = $matches; |
|
| 74 | + $this->suggestion = $results->SuggestionNice; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get search results |
|
| 80 | + * |
|
| 81 | + * @return PaginatedList |
|
| 82 | + */ |
|
| 83 | + public function getResults() |
|
| 84 | + { |
|
| 85 | + return $this->matches; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Check if there are found results |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function hasResults() |
|
| 94 | + { |
|
| 95 | + return $this->matches && $this->matches->exists(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get search keywords matching these results |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + public function getQuery() |
|
| 104 | + { |
|
| 105 | + return $this->query; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get suggested search keywords |
|
| 110 | + * |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + public function getSuggestion() |
|
| 114 | + { |
|
| 115 | + return $this->suggestion; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get original search keywords superceded by these results |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function getOriginal() |
|
| 124 | + { |
|
| 125 | + return $this->original; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Set original keywords |
|
| 130 | + * |
|
| 131 | + * @param string $original |
|
| 132 | + */ |
|
| 133 | + public function setOriginal($original) |
|
| 134 | + { |
|
| 135 | + $this->original = $original; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the link to the suggested search |
|
| 140 | + * |
|
| 141 | + * @param string $format Optional output format |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getSuggestionLink($format = null) |
|
| 145 | + { |
|
| 146 | + return $this->getLink($this->suggestion, $format); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Gets the link to the given search |
|
| 151 | + * |
|
| 152 | + * @param string $format Optional output format |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + public function getSearchLink($format = null) |
|
| 156 | + { |
|
| 157 | + return $this->getLink($this->query, $format); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Gets the link to the original search, with suggestions disabled |
|
| 162 | + * |
|
| 163 | + * @param string $format Optional output format |
|
| 164 | + * @return string |
|
| 165 | + */ |
|
| 166 | + public function getOriginalLink($format = null) |
|
| 167 | + { |
|
| 168 | + return $this->getLink($this->original, $format) . "&suggestions=0"; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get link to these results in RSS format |
|
| 173 | + * |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + public function getRSSLink() |
|
| 177 | + { |
|
| 178 | + return $this->getLink($this->query, 'rss'); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get link to these results in atom format |
|
| 183 | + * |
|
| 184 | + * @return string |
|
| 185 | + */ |
|
| 186 | + public function getAtomLink() |
|
| 187 | + { |
|
| 188 | + return $this->getLink($this->query, 'atom'); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get a search link for given terms |
|
| 193 | + * |
|
| 194 | + * @param string $terms |
|
| 195 | + * @return string|null |
|
| 196 | + */ |
|
| 197 | + protected function getLink($terms, $format = null) |
|
| 198 | + { |
|
| 199 | + if (!$terms) { |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | + $link = 'search/SearchForm?Search='.rawurlencode($terms); |
|
| 203 | + if ($format) { |
|
| 204 | + $link .= '&format='.rawurlencode($format); |
|
| 205 | + } |
|
| 206 | + return $link; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function hasField($field) |
|
| 210 | + { |
|
| 211 | + // Fix customise not detecting custom field getters |
|
| 212 | + return array_key_exists($field, $this->config()->casting); |
|
| 213 | + } |
|
| 214 | 214 | } |