Exemplo n.º 1
0
 public function Search($Search, $Offset = 0, $Limit = 20)
 {
     $BaseUrl = C('Plugins.Solr.SearchUrl', 'http://localhost:8983/solr/select/?');
     if (!$BaseUrl) {
         throw new Gdn_UserException("The search url has not been configured.");
     }
     if (!$Search) {
         return array();
     }
     // Escepe the search.
     $Search = preg_replace('`([][+&|!(){}^"~*?:\\\\-])`', "\\\\\$1", $Search);
     // Add the category watch.
     $Categories = CategoryModel::CategoryWatch();
     if ($Categories === FALSE) {
         return array();
     } elseif ($Categories !== TRUE) {
         $Search = 'CategoryID:(' . implode(' ', $Categories) . ') AND ' . $Search;
     }
     // Build the search url.
     $BaseUrl .= strpos($BaseUrl, '?') === FALSE ? '?' : '&';
     $Query = array('q' => $Search, 'start' => $Offset, 'rows' => $Limit);
     $Url = $BaseUrl . http_build_query($Query);
     // Grab the data.
     $Curl = curl_init($Url);
     curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
     $CurlResult = curl_exec($Curl);
     curl_close($Curl);
     // Parse the result into the form that the search controller expects.
     $Xml = new SimpleXMLElement($CurlResult);
     $Result = array();
     if (!isset($Xml->result)) {
         return array();
     }
     foreach ($Xml->result->children() as $Doc) {
         $Row = array();
         foreach ($Doc->children() as $Field) {
             $Name = (string) $Field['name'];
             $Row[$Name] = (string) $Field;
         }
         // Add the url.
         switch ($Row['DocType']) {
             case 'Discussion':
                 $Row['Url'] = '/discussion/' . $Row['PrimaryID'] . '/' . Gdn_Format::Url($Row['Title']);
                 break;
             case 'Comment':
                 $Row['Url'] = "/discussion/comment/{$Row['PrimaryID']}/#Comment_{$Row['PrimaryID']}";
                 break;
         }
         // Fix the time.
         $Row['DateInserted'] = strtotime($Row['DateInserted']);
         $Result[] = $Row;
     }
     // Join the users into the result.
     Gdn_DataSet::Join($Result, array('table' => 'User', 'parent' => 'UserID', 'prefix' => '', 'Name', 'Photo'));
     return $Result;
 }
 public function getByDiscussionEventRange($Offset = false, $Limit = false, $BeginDate = false, $EndDate = false, $Where = array())
 {
     $BeginDate = $BeginDate ? Date('Y-m-d', StrToTime($BeginDate)) : Date('Y-m-d');
     $EndDate = $EndDate ? Date('Y-m-d', StrToTime($EndDate)) : Date('Y-m-d', PHP_INT_MAX);
     $this->SQL->select('d.*')->from('Discussion d')->where('d.DiscussionEventDate >=', $BeginDate)->where('d.DiscussionEventDate <=', $EndDate)->orderBy('d.DiscussionEventDate');
     // Determine category watching
     if ($this->Watching && !isset($Where['d.CategoryID'])) {
         $Watch = CategoryModel::CategoryWatch();
         if ($Watch !== true) {
             $Where['d.CategoryID'] = $Watch;
         }
     }
     $this->SQL->where($Where);
     if ($Offset !== false && $Limit !== false) {
         $this->SQL->limit($Limit, $Offset);
     }
     return $this->SQL->get();
 }
 /**
  * Execute comment search query.
  *
  * @since 2.0.0
  * @access public
  *
  * @param object $SearchModel SearchModel (Dashboard)
  * @return object SQL result.
  */
 public function commentSql($SearchModel, $AddMatch = true)
 {
     if ($AddMatch) {
         // Get permission and limit search categories if necessary.
         $Perms = CategoryModel::CategoryWatch();
         if ($Perms !== true) {
             $this->SQL->whereIn('d.CategoryID', $Perms);
         }
         // Build search part of query
         $SearchModel->AddMatchSql($this->SQL, 'c.Body', 'c.DateInserted');
     }
     // Build base query
     $this->SQL->select('c.CommentID as PrimaryID, d.Name as Title, c.Body as Summary, c.Format, d.CategoryID, c.Score')->select("'/discussion/comment/', c.CommentID, '/#Comment_', c.CommentID", "concat", 'Url')->select('c.DateInserted')->select('c.InsertUserID as UserID')->select("'Comment'", '', 'RecordType')->from('Comment c')->join('Discussion d', 'd.DiscussionID = c.DiscussionID');
     if ($AddMatch) {
         // Exectute query
         $Result = $this->SQL->GetSelect();
         // Unset SQL
         $this->SQL->reset();
     } else {
         $Result = $this->SQL;
     }
     return $Result;
 }
Exemplo n.º 4
0
 /**
  * Count how many discussions match the given criteria.
  *
  * @since 2.0.0
  * @access public
  *
  * @param array $Wheres SQL conditions.
  * @param bool $ForceNoAnnouncements Not used.
  * @return int Number of discussions.
  */
 public function getUnreadCount($Wheres = '', $ForceNoAnnouncements = false)
 {
     if (is_array($Wheres) && count($Wheres) == 0) {
         $Wheres = '';
     }
     // Check permission and limit to categories as necessary
     if ($this->Watching) {
         $Perms = CategoryModel::CategoryWatch();
     } else {
         $Perms = self::CategoryPermissions();
     }
     if (!$Wheres || count($Wheres) == 1 && isset($Wheres['d.CategoryID'])) {
         // Grab the counts from the faster category cache.
         if (isset($Wheres['d.CategoryID'])) {
             $CategoryIDs = (array) $Wheres['d.CategoryID'];
             if ($Perms === false) {
                 $CategoryIDs = array();
             } elseif (is_array($Perms)) {
                 $CategoryIDs = array_intersect($CategoryIDs, $Perms);
             }
             if (count($CategoryIDs) == 0) {
                 return 0;
             } else {
                 $Perms = $CategoryIDs;
             }
         }
         $Categories = CategoryModel::categories();
         $Count = 0;
         foreach ($Categories as $Cat) {
             if (is_array($Perms) && !in_array($Cat['CategoryID'], $Perms)) {
                 continue;
             }
             $Count += (int) $Cat['CountDiscussions'];
         }
         return $Count;
     }
     if ($Perms !== true) {
         $this->SQL->whereIn('c.CategoryID', $Perms);
     }
     $this->EventArguments['Wheres'] =& $Wheres;
     $this->fireEvent('BeforeGetUnreadCount');
     // @see 'BeforeGet' for consistency in count vs. results
     $this->SQL->select('d.DiscussionID', 'count', 'CountDiscussions')->from('Discussion d')->join('Category c', 'd.CategoryID = c.CategoryID')->join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = ' . Gdn::session()->UserID, 'left')->where('d.CountComments >', 'COALESCE(w.CountComments, 0)', true, false)->where($Wheres);
     $Result = $this->SQL->get()->firstRow()->CountDiscussions;
     return $Result;
 }
 /**
  * Execute comment search query.
  * 
  * @since 2.0.0
  * @access public
  * 
  * @param object $SearchModel SearchModel (Dashboard)
  * @return object SQL result.
  */
 public function CommentSql($SearchModel)
 {
     // Get permission and limit search categories if necessary
     $Perms = CategoryModel::CategoryWatch();
     if ($Perms !== TRUE) {
         $this->SQL->WhereIn('d.CategoryID', $Perms);
     }
     // Build search part of query
     $SearchModel->AddMatchSql($this->SQL, 'c.Body', 'c.DateInserted');
     // Build base query
     $this->SQL->Select('c.CommentID as PrimaryID, d.Name as Title, c.Body as Summary, c.Format, d.CategoryID')->Select("'/discussion/comment/', c.CommentID, '/#Comment_', c.CommentID", "concat", 'Url')->Select('c.DateInserted')->Select('c.InsertUserID as UserID')->From('Comment c')->Join('Discussion d', 'd.DiscussionID = c.DiscussionID');
     // Exectute query
     $Result = $this->SQL->GetSelect();
     // Unset SQL
     $this->SQL->Reset();
     return $Result;
 }
Exemplo n.º 6
0
 /**
  * Count how many discussions match the given criteria.
  * 
  * @since 2.0.0
  * @access public
  * 
  * @param array $Wheres SQL conditions.
  * @param bool $ForceNoAnnouncements Not used.
  * @return int Number of discussions.
  */
 public function GetCount($Wheres = '', $ForceNoAnnouncements = FALSE)
 {
     $Session = Gdn::Session();
     $UserID = $Session->UserID > 0 ? $Session->UserID : 0;
     if (is_array($Wheres) && count($Wheres) == 0) {
         $Wheres = '';
     }
     // Check permission and limit to categories as necessary
     if ($this->Watching) {
         $Perms = CategoryModel::CategoryWatch();
     } else {
         $Perms = self::CategoryPermissions();
     }
     if (!$Wheres || count($Wheres) == 1 && isset($Wheres['d.CategoryID'])) {
         // Grab the counts from the faster category cache.
         if (isset($Wheres['d.CategoryID'])) {
             if (is_array($Perms) && !in_array($Wheres['d.CategoryID'], $Perms)) {
                 return 0;
             } else {
                 $Perms = array($Wheres['d.CategoryID']);
             }
         }
         $Categories = CategoryModel::Categories();
         $Count = 0;
         foreach ($Categories as $Cat) {
             if (is_array($Perms) && !in_array($Cat['CategoryID'], $Perms)) {
                 continue;
             }
             $Count += (int) $Cat['CountDiscussions'];
         }
         return $Count;
     }
     //      if($Perms !== TRUE) {
     //         $this->SQL->WhereIn('c.CategoryID', $Perms);
     //      }
     //
     //      $this->EventArguments['Wheres'] = &$Wheres;
     //		$this->FireEvent('BeforeGetCount'); // @see 'BeforeGet' for consistency in count vs. results
     //
     //      // Small optimization for basic queries
     //      if ($Wheres == '') {
     //         $this->SQL
     //            ->Select('c.CountDiscussions', 'sum', 'CountDiscussions')
     //            ->From('Category c');
     //      } else {
     //         $this->SQL
     //	         ->Select('d.DiscussionID', 'count', 'CountDiscussions')
     //	         ->From('Discussion d')
     //            ->Join('Category c', 'd.CategoryID = c.CategoryID')
     //	         ->Join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = '.$UserID, 'left')
     //            ->Where($Wheres);
     //      }
     //
     //      $Result = $this->SQL
     //         ->Get()
     //         ->FirstRow()
     //         ->CountDiscussions;
     //
     //      if (isset($Count) && $Result != $Count) {
     //         throw new Exception("Result: $Result, Count: $Count");
     //      }
     //
     //      return $Result;
 }
   /**
    * Count how many discussions match the given criteria.
    * 
    * @since 2.0.0
    * @access public
    * 
	 * @param array $Wheres SQL conditions.
	 * @param bool $ForceNoAnnouncements Not used.
	 * @return int Number of discussions.
	 */
   public function GetCount($Wheres = '', $ForceNoAnnouncements = FALSE) {
      $Session = Gdn::Session();
      $UserID = $Session->UserID > 0 ? $Session->UserID : 0;
      if (is_array($Wheres) && count($Wheres) == 0)
         $Wheres = '';
      
      // Check permission and limit to categories as necessary
      if ($this->Watching)
         $Perms = CategoryModel::CategoryWatch();
      else
         $Perms = self::CategoryPermissions();
      if($Perms !== TRUE) {
         $this->SQL->WhereIn('c.CategoryID', $Perms);
      }
      
      $this->EventArguments['Wheres'] = &$Wheres;
		$this->FireEvent('BeforeGetCount'); // @see 'BeforeGet' for consistency in count vs. results
         
      // Small optimization for basic queries
      if ($Wheres == '') {
         $this->SQL
            ->Select('c.CountDiscussions', 'sum', 'CountDiscussions')
            ->From('Category c');
      } else {
         $this->SQL
	         ->Select('d.DiscussionID', 'count', 'CountDiscussions')
	         ->From('Discussion d')
            ->Join('Category c', 'd.CategoryID = c.CategoryID')
	         ->Join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = '.$UserID, 'left')
            ->Where($Wheres);
      }
      
      return $this->SQL
         ->Get()
         ->FirstRow()
         ->CountDiscussions;
   }
Exemplo n.º 8
0
 /**
  * Count how many discussions match the given criteria.
  *
  * @since 2.0.0
  * @access public
  *
  * @param array $Wheres SQL conditions.
  * @param bool $ForceNoAnnouncements Not used.
  * @return stdObject containing:
  *           CountDiscussions => Number of unread discussions.
  *           CountComments => Number of unread comments.
  */
 public function getUnreadCount($Wheres = '', $ForceNoAnnouncements = false)
 {
     if (is_array($Wheres) && count($Wheres) == 0) {
         $Wheres = '';
     }
     // Check permission and limit to categories as necessary
     if ($this->Watching) {
         $Perms = CategoryModel::CategoryWatch();
     } else {
         $Perms = self::CategoryPermissions();
     }
     if ($Perms !== true) {
         $this->SQL->whereIn('c.CategoryID', $Perms);
     }
     $this->EventArguments['Wheres'] =& $Wheres;
     $this->fireEvent('BeforeGetUnreadCount');
     // @see 'BeforeGet' for consistency in count vs. results
     $this->SQL->select('d.DiscussionID', 'count', 'CountTotalDiscussions')->select('d.CountComments', 'sum', 'CountTotalComments')->select('w.CountComments', 'sum', 'CountReadComments')->select('w.DiscussionID', 'count', 'CountReadDiscussions')->from('Discussion d')->join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = ' . Gdn::session()->UserID, 'left')->where($Wheres);
     $TempResult = $this->SQL->get()->firstRow();
     $Result->CountDiscussions = (int) $TempResult->CountTotalDiscussions - (int) $TempResult->CountReadDiscussions;
     $Result->CountComments = (int) $TempResult->CountTotalComments - (int) $TempResult->CountReadComments;
     return $Result;
 }