aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/wiki.php
diff options
context:
space:
mode:
authorRasmus Fuhse <fuhse@data-quest.de>2024-05-08 23:48:43 +0000
committerRasmus Fuhse <fuhse@data-quest.de>2024-05-08 23:48:43 +0000
commit1ed2587c0b55d7587477c7269fe5e30b098ad592 (patch)
treed5d579ab086ed238fe946dd653418e9531f30d8d /app/controllers/course/wiki.php
parent3474ea5022e9bb4d7fc861472d35be3bcb7ca735 (diff)
Resolve "Suchfunktion Wiki"
Closes #4126 Merge request studip/studip!2972
Diffstat (limited to 'app/controllers/course/wiki.php')
-rw-r--r--app/controllers/course/wiki.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/app/controllers/course/wiki.php b/app/controllers/course/wiki.php
index 567e285..cbd63cf 100644
--- a/app/controllers/course/wiki.php
+++ b/app/controllers/course/wiki.php
@@ -976,6 +976,30 @@ class Course_WikiController extends AuthenticatedController
Sidebar::Get()->addWidget($search);
}
+ public function searchpage_action(WikiPage $page)
+ {
+ if (!$page->isReadable()) {
+ throw new AccessDeniedException();
+ }
+ Navigation::activateItem('/course/wiki/allpages');
+ if (!Request::get('search')) {
+ throw new Exception('No search text.');
+ }
+ $search = str_replace(['\\', '_', '%'], ['\\\\', '\\_', '\\%'], Request::get('search'));
+ $this->versions = WikiVersion::findBySQL("`page_id` = :page_id AND (`wiki_versions`.`content` LIKE :searchterm OR `wiki_versions`.`name` LIKE :searchterm) ORDER BY `mkdate` DESC ", [
+ 'page_id' => $page->id,
+ 'searchterm' => '%' . $search . '%'
+ ]);
+
+ $search = new SearchWidget($this->searchURL());
+ $search->addNeedle(
+ _('Im Wiki suchen'),
+ 'search',
+ true
+ );
+ Sidebar::Get()->addWidget($search);
+ }
+
public function pdf_action(WikiPage $page)
{
if (!$page->isReadable()) {
@@ -1217,4 +1241,41 @@ class Course_WikiController extends AuthenticatedController
return $from_end ? mb_strlen($str0) - $length : $length;
}
+
+ public function findTextualHits($text, $search, $length = 80)
+ {
+ $content = Studip\Markup::removeHtml($text);
+ $offset = 0;
+ $output = [];
+
+ // find all occurences
+ while ($offset < mb_strlen($content)) {
+ $pos = mb_stripos($content, Request::get('search'), $offset);
+ if ($pos === false) {
+ break;
+ }
+ $offset = $pos + 1;
+
+ // show max 200 chars
+ $fragment = '';
+ $split_fragment = preg_split(
+ '/(' . preg_quote(Request::get('search'), '/') . ')/i',
+ mb_substr($content, max(0, $pos - floor($length / 2)), $length),
+ -1,
+ PREG_SPLIT_DELIM_CAPTURE
+ );
+ for ($i = 0; $i < count($split_fragment); ++$i) {
+ if ($i % 2) {
+ $fragment .= '<span class="wiki_highlight">';
+ $fragment .= htmlready($split_fragment[$i], false);
+ $fragment .= '</span>';
+ } else {
+ $fragment .= htmlready($split_fragment[$i], false);
+ }
+ }
+ $found_in_fragment = (count($split_fragment) - 1) / 2; // number of hits in fragment
+ $output[] = '...' . $fragment . '...';
+ }
+ return implode('<br>', $output);
+ }
}