예제 #1
0
파일: Index.php 프로젝트: linuxwhy/tiki-1
 private function buildCondition($childNodes, $required)
 {
     $result = new ZendSearch\Lucene\Search\Query\Boolean();
     foreach ($childNodes as $child) {
         // Detect if child is a NOT, and reformulate on the fly to support the syntax
         if ($child instanceof ZendSearch\Lucene\Search\Query\Boolean) {
             $signs = $child->getSigns();
             if (count($signs) === 1 && $signs[0] === false) {
                 $subs = $child->getSubqueries();
                 $result->addSubquery(reset($subs), false);
                 continue;
             }
         }
         $result->addSubquery($child, $required);
     }
     return $result;
 }
예제 #2
0
 protected function buildQuery($keyword, $options)
 {
     // Allow *Token*
     \ZendSearch\Lucene\Search\Query\Wildcard::setMinPrefixLength(0);
     $query = new \ZendSearch\Lucene\Search\Query\Boolean();
     foreach (explode(" ", $keyword) as $k) {
         // Require at least 3 non-wildcard characters
         if (strlen($k) > 2) {
             $term = new \ZendSearch\Lucene\Index\Term("*" . $k . "*");
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Wildcard($term), true);
         }
     }
     // Add model filter
     if (isset($options['model']) && $options['model'] != "") {
         if (is_array($options['model'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['model'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($model, 'model'));
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['model'], 'model');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     // Add type filter
     if (isset($options['type']) && $options['type'] != "") {
         if (is_array($options['type'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['type'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($type), 'type');
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['type'], 'type');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     // Add custom filters
     if (isset($options['filters']) && is_array($options['filters'])) {
         foreach ($options['filters'] as $field => $value) {
             $term = new \ZendSearch\Lucene\Index\Term($value, $field);
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     if ($options['checkPermissions'] && !Yii::$app->request->isConsoleRequest) {
         $permissionQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         if (Yii::$app->user->isGuest) {
             // Guest Content
             $guestContentQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')), true);
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_CONTENT, 'type')), true);
             $guestContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
             $guestSpaceListQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach (Space::find()->where(['visibility' => Space::VISIBILITY_ALL])->all() as $space) {
                 $guestSpaceListQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
             }
             $guestContentQuery->addSubquery($guestSpaceListQuery, true);
             $permissionQuery->addSubquery($guestContentQuery);
             // Guest Spaces
             $guestSpacesQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $guestSpacesQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_SPACE, 'type')), true);
             $guestSpacesQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')), true);
             $permissionQuery->addSubquery($guestSpacesQuery);
             $permissionQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_TYPE_USER, 'type')));
         } else {
             //--- Public Content
             $permissionQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')));
             //--- Private Space Content
             $privateSpaceContentQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
             $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PRIVATE, 'visibility')), true);
             $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
             $privateSpacesListQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach (\humhub\modules\space\models\Membership::GetUserSpaces() as $space) {
                 $privateSpacesListQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
             }
             $privateSpaceContentQuery->addSubquery($privateSpacesListQuery, true);
             $permissionQuery->addSubquery($privateSpaceContentQuery);
         }
         $query->addSubquery($permissionQuery, true);
     }
     if (count($options['limitSpaces']) > 0) {
         $spaceBaseQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         $spaceBaseQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
         $spaceIdQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
         foreach ($options['limitSpaces'] as $space) {
             $spaceIdQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
         }
         $spaceBaseQuery->addSubquery($spaceIdQuery, true);
         $query->addSubquery($spaceBaseQuery, true);
     }
     return $query;
 }
예제 #3
0
 protected function buildQuery($keyword, $options)
 {
     $query = new \ZendSearch\Lucene\Search\Query\Boolean();
     foreach (explode(" ", $keyword) as $k) {
         // Require at least 3 non-wildcard characters
         if (strlen($k) > 2) {
             $term = new \ZendSearch\Lucene\Index\Term($k . "*");
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Wildcard($term), true);
         }
     }
     // Add model filter
     if (isset($options['model']) && $options['model'] != "") {
         if (is_array($options['model'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['model'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($model, 'model'));
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['model'], 'model');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     // Add type filter
     if (isset($options['type']) && $options['type'] != "") {
         if (is_array($options['type'])) {
             $boolQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
             foreach ($options['type'] as $model) {
                 $boolQuery->addTerm(new \ZendSearch\Lucene\Index\Term($type), 'type');
             }
             $query->addSubquery($boolQuery, true);
         } else {
             $term = new \ZendSearch\Lucene\Index\Term($options['type'], 'type');
             $query->addSubquery(new \ZendSearch\Lucene\Search\Query\Term($term), true);
         }
     }
     if ($options['checkPermissions'] && !Yii::$app->request->isConsoleRequest) {
         $permissionQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         //--- Public Content
         $permissionQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PUBLIC, 'visibility')));
         //--- Private Space Content
         $privateSpaceContentQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(self::DOCUMENT_VISIBILITY_PRIVATE, 'visibility')), true);
         $privateSpaceContentQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
         $privateSpacesListQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
         foreach (\humhub\modules\space\models\Membership::GetUserSpaces() as $space) {
             $privateSpacesListQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
         }
         $privateSpaceContentQuery->addSubquery($privateSpacesListQuery, true);
         $permissionQuery->addSubquery($privateSpaceContentQuery);
         $query->addSubquery($permissionQuery, true);
     }
     if (count($options['limitSpaces']) > 0) {
         $spaceBaseQuery = new \ZendSearch\Lucene\Search\Query\Boolean();
         $spaceBaseQuery->addSubquery(new \ZendSearch\Lucene\Search\Query\Term(new \ZendSearch\Lucene\Index\Term(Space::className(), 'containerModel')), true);
         $spaceIdQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm();
         foreach ($options['limitSpaces'] as $space) {
             $spaceIdQuery->addTerm(new \ZendSearch\Lucene\Index\Term($space->id, 'containerPk'));
         }
         $spaceBaseQuery->addSubquery($spaceIdQuery, true);
         $query->addSubquery($spaceBaseQuery, true);
     }
     return $query;
 }