Beispiel #1
0
 /**
  * Get the search.
  *
  * @param integer $searchType : the type of the search (see constants)
  * @return array of CMS_page the result pages
  * @access public
  */
 function getSearch($keywords, $user, $public = false, $withPageContent = false)
 {
     if (is_a($user, 'CMS_profile_user')) {
         $cms_language = $user->getLanguage();
     } else {
         $cms_language = new CMS_language('fr');
     }
     $results = array();
     $count = 0;
     /*$messages = array();
     		$message = '';*/
     $where = $order = '';
     $foundLinkToIDs = $foundLinkFromIDs = $foundPagesFromTemplate = $foundPagesFromRow = $matches = array();
     // Clean keywords
     $keywords = SensitiveIO::sanitizeSQLString($keywords);
     $keywords = strtr($keywords, ",;", "  ");
     $blocks = array();
     $blocks = array_map("trim", array_unique(explode(" ", $keywords)));
     $cleanedBlocks = array();
     foreach ($blocks as $block) {
         if ($block !== '' || sensitiveIO::isPositiveInteger($block)) {
             $block = str_replace(array('%', '_'), array('\\%', '\\_'), $block);
             $cleanedBlocks[] = $block;
         }
     }
     // Separate block codes
     if ($cleanedBlocks) {
         $allDatas = array();
         $allCodes = CMS_search::getAllCodes();
         foreach ($allCodes as $code) {
             $datas = array();
             foreach (array_keys($cleanedBlocks) as $key) {
                 if (strstr($cleanedBlocks[$key], $code . ':')) {
                     $datas[] = $cleanedBlocks[$key];
                     unset($cleanedBlocks[$key]);
                 }
             }
             if ($datas) {
                 $allDatas[$code] = $datas;
             }
         }
         $allDatas[self::SEARCH_TYPE_DEFAULT] = $cleanedBlocks;
         // Get IDs from all specific codes
         $foundIDs = array();
         $allLinksNumber = 0;
         foreach ($allCodes as $code) {
             switch ($code) {
                 case self::SEARCH_TYPE_LINKTO:
                     if (isset($allDatas[self::SEARCH_TYPE_LINKTO])) {
                         $foundLinkToIDs = array();
                         $where = '';
                         $count = 0;
                         foreach ($allDatas[self::SEARCH_TYPE_LINKTO] as $block) {
                             $tabValues = explode(':', $block);
                             if (SensitiveIO::isPositiveInteger($tabValues[1])) {
                                 $where .= $count ? ' or ' : '';
                                 $count++;
                                 $where .= " start_lre = '" . $tabValues[1] . "' ";
                             }
                         }
                         if ($where) {
                             $select = ' stop_lre ';
                             $from = 'linx_real_public';
                             $sql = "\n\t\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t\t" . $select . "\n\t\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t\t" . $from . "\n\t\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t\t" . $where;
                             $q = new CMS_query($sql);
                             $arr = array();
                             while ($arr = $q->getArray()) {
                                 $foundLinkToIDs[] = $arr["stop_lre"];
                             }
                             // Count links number
                             $allLinksNumber += count($foundLinkToIDs);
                             $where = $select = '';
                         }
                     }
                     break;
                 case self::SEARCH_TYPE_LINKFROM:
                     if (isset($allDatas[self::SEARCH_TYPE_LINKFROM])) {
                         $foundLinkFromIDs = array();
                         $where = '';
                         $count = 0;
                         /*$messagesIDs = array();*/
                         foreach ($allDatas[self::SEARCH_TYPE_LINKFROM] as $block) {
                             $tabValues = explode(':', $block);
                             if (SensitiveIO::isPositiveInteger($tabValues[1])) {
                                 $where .= $count ? ' or ' : '';
                                 $count++;
                                 $where .= " stop_lre = '" . $tabValues[1] . "' ";
                             }
                         }
                         if ($where) {
                             $select = ' start_lre ';
                             $from = 'linx_real_public';
                             $sql = "\n\t\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t\t" . $select . "\n\t\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t\t" . $from . "\n\t\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t\t" . $where;
                             $q = new CMS_query($sql);
                             $arr = array();
                             while ($arr = $q->getArray()) {
                                 $foundLinkFromIDs[] = $arr["start_lre"];
                             }
                             // Count links number
                             $allLinksNumber += count($foundLinkFromIDs);
                             $where = $select = '';
                         }
                     }
                     break;
                 case self::SEARCH_TYPE_TEMPLATE:
                     if (isset($allDatas[self::SEARCH_TYPE_TEMPLATE])) {
                         $foundPagesFromTemplate = array();
                         foreach ($allDatas[self::SEARCH_TYPE_TEMPLATE] as $block) {
                             $tabValues = explode(':', $block);
                             if (SensitiveIO::isPositiveInteger($tabValues[1])) {
                                 $foundPagesFromTemplate = array_unique(array_merge(CMS_pageTemplatesCatalog::getPagesByTemplate($tabValues[1]), $foundPagesFromTemplate));
                             }
                         }
                         $allLinksNumber += count($foundPagesFromTemplate);
                     }
                     break;
                 case self::SEARCH_TYPE_ROW:
                     if (isset($allDatas[self::SEARCH_TYPE_ROW])) {
                         $foundPagesFromRow = array();
                         foreach ($allDatas[self::SEARCH_TYPE_ROW] as $block) {
                             $tabValues = explode(':', $block);
                             if (SensitiveIO::isPositiveInteger($tabValues[1])) {
                                 $foundPagesFromRow = array_unique(array_merge(CMS_rowsCatalog::getPagesByRow($tabValues[1]), CMS_rowsCatalog::getPagesByRow($tabValues[1], false, true), $foundPagesFromRow));
                             }
                         }
                         $allLinksNumber += count($foundPagesFromRow);
                     }
                     break;
             }
         }
         $foundIDs = array_unique(array_merge($foundLinkToIDs, $foundLinkFromIDs, $foundPagesFromTemplate, $foundPagesFromRow));
         // Main sql requests (for pageId, pages codenames and keywords)
         if ($allDatas[self::SEARCH_TYPE_DEFAULT]) {
             $count = 0;
             $where = '';
             foreach ($allDatas[self::SEARCH_TYPE_DEFAULT] as $key => $block) {
                 if (SensitiveIO::isPositiveInteger($block)) {
                     $where .= $count ? ' or ' : '';
                     $count++;
                     $where .= " (page_pbd like '%" . $block . "%')";
                     unset($allDatas[self::SEARCH_TYPE_DEFAULT][$key]);
                 }
             }
             $order = '';
             if ($allDatas[self::SEARCH_TYPE_DEFAULT]) {
                 $suffix = $public ? '_public' : '_edited';
                 if (!$withPageContent) {
                     //Search in page metadatas
                     //$count = 0;
                     foreach ($allDatas[self::SEARCH_TYPE_DEFAULT] as $block) {
                         $where .= $count ? ' or ' : '';
                         $count++;
                         $where .= " (\n\t\t\t\t\t\t\t\ttitle_pbd like '%" . $block . "%'\n\t\t\t\t\t\t\t\tor linkTitle_pbd like '%" . $block . "%'\n\t\t\t\t\t\t\t\tor keywords_pbd like '%" . $block . "%'\n\t\t\t\t\t\t\t\tor description_pbd like '%" . $block . "%'\n\t\t\t\t\t\t\t\tor category_pbd like '%" . $block . "%'\n\t\t\t\t\t\t\t\tor codename_pbd = '" . $block . "'\n\t\t\t\t\t\t\t)";
                     }
                     if ($foundIDs) {
                         $where .= " and page_pbd in (" . implode($foundIDs, ',') . ") ";
                     }
                     // Set SQL
                     $sql = "\n\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\tpage_pbd\n\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\tpagesBaseData" . $suffix . "\n\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t" . $where . "\n\t\t\t\t\t\t";
                     $q = new CMS_query($sql);
                     //pr($sql);
                     $results = array();
                     $count = 0;
                     $foundIDs = array();
                     while ($id = $q->getValue('page_pbd')) {
                         $foundIDs[] = $id;
                     }
                     $order = "\n\t\t\t\t\t \t\torder by title_pbd asc\n\t\t\t\t\t\t";
                 } else {
                     //Search in page content (fulltext search)
                     $keywords = implode(' ', $allDatas[self::SEARCH_TYPE_DEFAULT]);
                     $selects = array('pagesBaseData' . $suffix => array('page' => 'page_pbd', 'match' => 'title_pbd,linkTitle_pbd,keywords_pbd,description_pbd,codename_pbd'), 'blocksVarchars' . $suffix => array('page' => 'page', 'match' => 'value'), 'blocksTexts' . $suffix => array('page' => 'page', 'match' => 'value', 'entities' => true), 'blocksImages' . $suffix => array('page' => 'page', 'match' => 'label'), 'blocksFiles' . $suffix => array('page' => 'page', 'match' => 'label'));
                     $matches = array();
                     foreach ($selects as $table => $select) {
                         // Set SQL
                         $sql = "\n\t\t\t\t\t\t\t\tselect \n\t\t\t\t\t\t\t\t\t" . $select['page'] . " as pageId, MATCH (" . $select['match'] . ") AGAINST ('" . sensitiveIO::sanitizeSQLString($keywords) . "') as m1\n\t\t\t\t\t\t\t\t\t" . (isset($select['entities']) && $keywords != htmlentities($keywords) ? " , MATCH (" . $select['match'] . ") AGAINST ('" . sensitiveIO::sanitizeSQLString(htmlentities($keywords)) . "') as m2 " : '') . "\n\t\t\t\t\t\t\t\tfrom \n\t\t\t\t\t\t\t\t\t" . $table . "\n\t\t\t\t\t\t\t\twhere \n\t\t\t\t\t\t\t\t\tMATCH (" . $select['match'] . ") AGAINST ('" . sensitiveIO::sanitizeSQLString($keywords) . "')\n\t\t\t\t\t\t\t\t\t" . (isset($select['entities']) && $keywords != htmlentities($keywords) ? " or MATCH (" . $select['match'] . ") AGAINST ('" . sensitiveIO::sanitizeSQLString(htmlentities($keywords)) . "') " : '') . "\n\t\t\t\t\t\t\t\t";
                         //pr($sql);
                         $q = new CMS_query($sql);
                         while ($r = $q->getArray()) {
                             if (!isset($matches[$r['pageId']]) || isset($matches[$r['pageId']]) && $r['m1'] > $matches[$r['pageId']]) {
                                 $matches[$r['pageId']] = $r['m1'];
                             }
                             if (isset($r['m2']) && (!isset($matches[$r['pageId']]) || isset($matches[$r['pageId']]) && $r['m2'] > $matches[$r['pageId']])) {
                                 $matches[$r['pageId']] = $r['m2'];
                             }
                         }
                     }
                     //sort page Ids by relevance
                     arsort($matches, SORT_NUMERIC);
                     //$matches = array_keys($matches);
                     $order = "\n\t\t\t\t\t \t\torder by field(page_pbd, " . implode(',', array_reverse(array_keys($matches))) . ") desc\n\t\t\t\t\t\t";
                     $foundIDs = $foundIDs ? array_intersect(array_keys($matches), $foundIDs) : array_keys($matches);
                 }
             } else {
                 $order = " order by page_pbd ";
             }
         }
         if ($foundIDs) {
             $select = ' page_pbd ';
             $from = $public ? 'pagesBaseData_public' : 'pagesBaseData_edited';
             $where .= $where && $foundIDs ? " and " : '';
             $where .= $foundIDs ? " page_pbd in (" . implode($foundIDs, ',') . ") " : '';
             if ($where) {
                 // Set SQL
                 $sql = "\n\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t" . $select . "\n\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t" . $from . "\n\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t" . $where . "\n\t\t\t\t\t\t" . $order . "\n\t\t\t\t\t";
                 $q = new CMS_query($sql);
                 //pr($sql);
                 $results = array();
                 $count = 0;
                 while ($arr = $q->getArray()) {
                     $id = $arr["page_pbd"];
                     if ($user->hasPageClearance($id, CLEARANCE_PAGE_VIEW)) {
                         $count++;
                         $results[$id] = $id;
                     }
                 }
             }
         }
     } else {
         // No results
         $count = 0;
     }
     return array('nbresult' => $count, 'nblinksresult' => $allLinksNumber, 'results' => $results, 'score' => $matches);
 }
Beispiel #2
0
 /**
  * Search module objects by Id
  *
  * @param string $keyword : the search keywords
  * @param CMS_profile_user $user : the user which make the search
  * @param booolean : public search (default : false)
  * @param array : the results score returned by reference
  * @return array : results elements Ids
  * @access public
  */
 function search($keyword, &$user, $public = false, &$score = array())
 {
     $search = new CMS_search();
     $pageResults = $search->getSearch($keyword, $user, $public, true);
     $score = $pageResults['score'];
     return $pageResults['results'];
 }
Beispiel #3
0
    $checkedElements['groups'] = true;
}
//Templates/Rows
if ($cms_user->hasAdminClearance(CLEARANCE_ADMINISTRATION_EDIT_TEMPLATES)) {
    //templates
    $elements['templates'] = $cms_language->getMessage(MESSAGE_PAGE_PAGE_TEMPLATES);
    $checkedElements['templates'] = true;
}
if ($cms_user->hasAdminClearance(CLEARANCE_ADMINISTRATION_TEMPLATES)) {
    //rows
    $elements['rows'] = $cms_language->getMessage(MESSAGE_PAGE_ROWS_TEMPLATES);
    $checkedElements['rows'] = true;
}
//define here special search codes for each elements
$searchCodes = array();
$searchCodes[MOD_STANDARD_CODENAME] = CMS_search::getAllCodes();
$searchCodes['users'] = array('user', 'group');
if ($elements) {
    foreach ($searchCodes as $searchElement => $searchCode) {
        foreach ($searchCode as $code) {
            if (io::strpos($search, $code . ':') !== false && isset($elements[$searchElement])) {
                $checkedElements = array();
                $checkedElements[$searchElement] = true;
            }
        }
    }
    if (io::strpos($search, 'row:') !== false && isset($elements['templates'])) {
        $checkedElements['templates'] = true;
    }
    $searchPanel .= "{\n\t\txtype: \t\t'checkboxgroup',\n\t\tid: \t\t'searchCheckboxgroup',\n\t\tfieldLabel: '" . $cms_language->getJsMessage(MESSAGE_PAGE_FIELD_SEARCH_IN) . "',\n\t\tcolumns: \t1,\n\t\titems: [\n\t\t\t{boxLabel: '<em style=\"font-style:italic;\">" . $cms_language->getJSMessage(MESSAGE_PAGE_CHECK_ALL) . "</em>', checked: " . (sizeof($elements) == sizeof($checkedElements) ? 'true' : 'false') . ", listeners: {'check':function(field, checked) {\n\t\t\t\tif (searchWindow.ok) {\n\t\t\t\t\tsearchWindow.ok = false;\n\t\t\t\t\tExt.getCmp('searchCheckboxgroup').items.each(function(el, group, index){\n\t\t\t\t\t\tif (index == 0) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tel.setValue(checked);\n\t\t\t\t\t}, this);\n\t\t\t\t\tsearchWindow.ok = true;\n\t\t\t\t\t//launch search\n\t\t\t\t\tsearchWindow.search();\n\t\t\t\t}\n\t\t\t}, scope:this}},\n\t\t";
    foreach ($elements as $element => $label) {
Beispiel #4
0
require_once dirname(__FILE__) . '/../../cms_rc_admin.php';
//load interface instance
$view = CMS_view::getInstance();
//set default display mode for this page
$view->setDisplayMode(CMS_view::SHOW_JSON);
//This file is an admin file. Interface must be secure
$view->setSecure();
$query = sensitiveIO::request('query', '', '');
$start = sensitiveIO::request('start', 'sensitiveIO::isPositiveInteger', 0);
$limit = sensitiveIO::request('limit', 'sensitiveIO::isPositiveInteger', 10);
if (!$query || io::strlen($query) < 3) {
    CMS_grandFather::raiseError('Missing query or query is too short : ' . $query);
    $view->show();
}
//lauch search
$results = CMS_search::getSearch($query, $cms_user, false, false);
//pr($results);
$pages = array();
$count = 0;
if (isset($results['results']) && is_array($results['results'])) {
    foreach ($results['results'] as $result) {
        if ($count >= $start && sizeof($pages) < $limit) {
            $page = CMS_tree::getPageById($result);
            if ($page && !$page->hasError()) {
                $pages[] = array('pageId' => $page->getID(), 'title' => $page->getTitle() . ' (' . $page->getID() . ')', 'status' => $page->getStatus()->getHTML(true, $cms_user, MOD_STANDARD_CODENAME, $page->getID()), 'lineage' => CMS_tree::getLineage(APPLICATION_ROOT_PAGE_ID, $page->getID(), false));
            } else {
                $results['nbresult']--;
            }
        }
        $count++;
    }