/** * 给出一段文本,为其中的 wiki 关键词加上链接 * * @param string $text 需要高亮的文本 * @param array $blackList 黑名单,黑名单中的词不会被高亮 * @return string */ public static function highlight($text, array $blackList = array()) { $entryKeyword = new EntryKeyword(); $text = addslashes($text); $text = preg_replace('/\\n+/', ' ', $text); $text = trim($text); $params = ''; if ($blackList) { $blackList = array_unique($blackList); array_walk($blackList, function (&$_keyword) { $_keyword = "'{$_keyword}'"; }); $params = 'keyword NOT IN (' . implode(',', $blackList) . ')'; } $_keywods = $entryKeyword->find($params); $searches = array(); foreach ($_keywods as $keywod) { //$searches[] = '/(?<=>)([^<]*)('.preg_quote($keywod->keyword, '/').')/'; $searches[] = '/(' . preg_quote($keywod->keyword, '/') . ')/'; } // $text = preg_replace($searches, '$1<a href="/wiki/$2" target="_blank" class="wiki-highlighted-keyword">$2</a>', $text, 1); $text = preg_replace($searches, '<a href="/wiki/$1" target="_blank" class="wiki-highlighted-keyword" title="点击查看关于「$1」的解释">$1</a>', $text, 1); // $text = preg_replace('/ +/', '', $text); $text = stripslashes($text); return $text; }
public function wikiAction() { $keyword = $this->dispatcher->getParam('keyword'); $entry = null; $keyword = EntryKeyword::findFirst("keyword='{$keyword}'"); if ($keyword) { $entry = $keyword->entry; } if (!$entry || $entry->status != 'published') { throw new Exception\ResourceNotFoundException('Request entry not found'); } $this->view->setVar('keyword', $keyword); }
private function initialize() { $keywords = EntryKeyword::find(); foreach ($keywords as $keyword) { if (!isset(self::$keywords[$keyword->keyword])) { $keywordLength = mb_strlen($keyword->keyword, self::$encoding); self::$keywords[$keyword->keyword] = true; self::$maxLength = max(self::$maxLength, $keywordLength); self::$firstCharCheck[self::mbStrGet($keyword->keyword, 0)] = true; for ($i = 0; $i < $keywordLength; $i++) { self::$allCharCheck[self::mbStrGet($keyword->keyword, $i)] = true; } } } }
public function updateEntry($data) { $textData = isset($data['text']) ? $data['text'] : array(); $synonymies = isset($data['synonymies']) ? $data['synonymies'] : array(); $categoryData = isset($data['categories']) ? $data['categories'] : array(); if ($textData) { unset($data['text']); $text = new EntryTexts(); $text->assign($textData); $this->text = $text; } $keywords = array(); if ($synonymies) { unset($data['synonymies']); $synonymiesArray = is_array($synonymies) ? $synonymies : explode(',', $synonymies); $synonymiesArray[] = $data['title']; $entryKeyword = new EntryKeyword(); $entryKeyword->fullUpdateEntryKeywords($this->id, $synonymiesArray); } if ($categoryData) { unset($data['categories']); $cateEntry = new CategoryEntry(); $cateEntry->fullUpdateCategoriesEntries($this->id, $categoryData); } $this->assign($data); $pinyin = new Pinyin(); $this->initial = substr($pinyin->transformUcwords($this->title), 0, 1); if (!$this->save()) { throw new RuntimeException('Create post failed'); } return $this; }