1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: class Wicked_Page_Search extends Wicked_Page {
 14: 
 15:      16:  17:  18: 
 19:     public $supportedModes = array(
 20:         Wicked::MODE_CONTENT => true,
 21:         Wicked::MODE_DISPLAY => true);
 22: 
 23:      24:  25:  26:  27: 
 28:     protected $_results = array();
 29: 
 30:      31:  32:  33:  34:  35:  36: 
 37:     public function content($searchtext = '')
 38:     {
 39:         if (empty($searchtext)) {
 40:             return array();
 41:         }
 42:         return array(
 43:             'titles' => $GLOBALS['wicked']->searchTitles($searchtext),
 44:             'pages' => $GLOBALS['wicked']->searchText($searchtext, false));
 45:     }
 46: 
 47:      48:  49:  50:  51:  52:  53:  54:  55:  56: 
 57:     public function preDisplay($mode, $params)
 58:     {
 59:         $this->_results = $this->content($params);
 60:     }
 61: 
 62:      63:  64:  65:  66:  67:  68: 
 69:     public function display($searchtext)
 70:     {
 71:         global $notification;
 72: 
 73:         if (!$searchtext) {
 74:             require WICKED_TEMPLATES . '/pagelist/search.inc';
 75:             require WICKED_TEMPLATES . '/pagelist/footer.inc';
 76:             return true;
 77:         }
 78: 
 79:         Horde::addScriptFile('tables.js', 'horde', true);
 80: 
 81:         $template = $GLOBALS['injector']->createInstance('Horde_Template');
 82: 
 83:         
 84:         $exact = array();
 85:         $page = new Wicked_Page_StandardPage($searchtext);
 86:         if ($GLOBALS['wicked']->pageExists($searchtext)) {
 87:             $exact[] = array('author' => htmlspecialchars($page->author()),
 88:                              'created' => $page->formatVersionCreated(),
 89:                              'name' => htmlspecialchars($page->pageName()),
 90:                              'context' => false,
 91:                              'url' => $page->pageUrl(),
 92:                              'version' => $page->version(),
 93:                              'class' => '');
 94:         } else {
 95:             $exact[] = array('author' => '',
 96:                              'created' => '',
 97:                              'name' => htmlspecialchars($searchtext),
 98:                              'context' => sprintf(_("%s does not exist. You can create it now."), '<strong>' . htmlspecialchars($searchtext) . '</strong>'),
 99:                              'url' => Wicked::url($searchtext, false),
100:                              'version' => '',
101:                              'class' => 'newpage');
102:         }
103: 
104:         
105:         $titles = array();
106:         foreach ($this->_results['titles'] as $page) {
107:             if (!empty($page['page_history'])) {
108:                 $page = new Wicked_Page_StandardHistoryPage($page);
109:             } else {
110:                 $page = new Wicked_Page_StandardPage($page);
111:             }
112: 
113:             $titles[] = array('author' => $page->author(),
114:                               'created' => $page->formatVersionCreated(),
115:                               'name' => $page->pageName(),
116:                               'context' => false,
117:                               'url' => $page->pageUrl(),
118:                               'version' => $page->version(),
119:                               'class' => '');
120:         }
121: 
122:         
123:         $pages = array();
124:         foreach ($this->_results['pages'] as $page) {
125:             if (!empty($page['page_history'])) {
126:                 $page = new Wicked_Page_StandardHistoryPage($page);
127:             } else {
128:                 $page = new Wicked_Page_StandardPage($page);
129:             }
130: 
131:             $pages[] = array('author' => $page->author(),
132:                              'created' => $page->formatVersionCreated(),
133:                              'name' => $page->pageName(),
134:                              'context' => $this->getContext($page, $searchtext),
135:                              'url' => $page->pageUrl(),
136:                              'version' => $page->version(),
137:                              'class' => '');
138:         }
139: 
140:         $template->set('hits', false, true);
141: 
142:         $template->set('th_page', _("Page"), true);
143:         $template->set('th_version', _("Current Version"), true);
144:         $template->set('th_author', _("Last Author"), true);
145:         $template->set('th_updated', _("Last Update"), true);
146: 
147:         
148:         require WICKED_TEMPLATES . '/pagelist/search.inc';
149: 
150:         
151:         $template->set('title', _("Exact Match"), true);
152:         $template->set('pages', $exact, true);
153:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/results_header.html');
154:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/pagelist.html');
155:         require WICKED_TEMPLATES . '/pagelist/results_footer.inc';
156: 
157:         
158:         $template->set('title', _("Page Title Matches"), true);
159:         $template->set('pages', $titles, true);
160:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/results_header.html');
161:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/pagelist.html');
162:         require WICKED_TEMPLATES . '/pagelist/results_footer.inc';
163: 
164:         
165:         $template->set('title', _("Page Text Matches"), true);
166:         $template->set('pages', $pages, true);
167:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/results_header.html');
168:         echo $template->fetch(WICKED_TEMPLATES . '/pagelist/pagelist.html');
169:         require WICKED_TEMPLATES . '/pagelist/results_footer.inc';
170:         echo '</div>';
171: 
172:         return true;
173:     }
174: 
175:     public function getContext($page, $searchtext)
176:     {
177:         try {
178:             $text = strip_tags($page->displayContents(false));
179:         } catch (Wicked_Exception $e) {
180:             $text = $page->getText();
181:         }
182:         if (preg_match('/.{0,100}' . preg_quote($searchtext, '/') . '.{0,100}/i', $text, $context)) {
183:             return preg_replace('/' . preg_quote($searchtext, '/') . '/i', '<span class="match">' . htmlspecialchars($searchtext) . '</span>', htmlspecialchars($context[0]));
184:         }
185:         return '';
186:     }
187: 
188:     public function pageName()
189:     {
190:         return 'Search';
191:     }
192: 
193:     public function pageTitle()
194:     {
195:         return _("Search");
196:     }
197: 
198: }
199: