All pastes #362205 Raw Edit

Something

public php v1 · immutable
·published 2007-02-18 16:46 UTC
rendered paste body
<?phpclass bj_entry {	var $limit;	var $offset;	var $normal = true;	var $cache_key = '';		#Query information in the WHERE clause.	var $args = array();	var $tags = '';	var $search = '';	var $sortby = 'posted';	var $sort = 'DESC';	function bj_entry($offset,$limit) {		$this->offset = $offset;		$this->limit = $limit;	}		function setID($v) {		$this->normal = false;		$this->args['ID'] = $v;	}		function setSecond($v) {		$this->normal = false;		$this->args['SECOND(posted)'] = $v;	}		function setMinute($v) {		$this->normal = false;		$this->args['MINUTE(posted)'] = $v;	}		function setHour($v) {		$this->normal = false;		$this->args['HOUR(posted)'] = $v;	}		function setDay($v) {		$this->normal = false;		$this->args['DAYOFMONTH(posted)'] = $v;	}		function setMonth($v) {		$this->normal = false;		$this->args['MONTH(posted)'] = $v;	}		function setYear($v) {		$this->normal = false;		$this->args['YEAR(posted)'] = $v;	}		function setTitle($v) {		$this->normal = false;		$this->args['title'] = $v;	}		function setShortname($v) {		$this->normal = false;		$this->args['shortname'] = $v;	}		function setSection($v) {		$this->args['section'] = $v;	}		function setAuthor($v) {		$this->args['author'] = $v;	}		function setPtype($v) {		$this->normal = false;		$this->args['ptype'] = $v;	}		#Extremely abormal stuff.	function setTags($v) {		if(strpos($v,',') !== false or strpos($v,'-') !== false) {			$this->normal = false;			$this->args['tags'] = str_replace(',','-',$v);			$this->tags .= " (";			$t = explode(",",$v);			foreach($t as $i=>$tag) {				if($i > 0)					$this->tags .= " OR ";				if(substr($tag,0,1) == "s")					$this->tags .= "tags NOT REGEXP '\"".substr($tag,1)."\"'";				else					$this->tags .= "tags REGEXP '\"".$tag."\"'";			}			$this->tags .= ")";		}		else {			$this->args['tags'] = $v;			$this->tags = " (tags REGEXP '\"".$v."\"')";		}	}		function setSearch($v) {		$this->normal = false;		$s = str_replace(' ','+',$v);		$s = explode('+',$s);		$this->args['search'] = $s;		$this->search .= " (((title LIKE '%".$search[0]."%') OR (content LIKE '%".$search[0]."%'))";		for ( $i = 1; $i < count($s); $i++)			$this->search .= " OR ((title LIKE '%".$s[$i]."%') OR (content LIKE '%".$s[$i]."%'))";		$this->search .= ')';	}		function sortBy($v) {		if($v != 'posted')			$this->normal = false;		$this->sortby = $v;	}		function sortOrder($v) {		if($v != 'DESC')			$this->normal = false;		$this->sort = $v;	}		function fetch() {		global $bj;		#Doing a normal cache fetch can only involve parsing one of these.		if($this->args['section'] and ($this->args['author'] or $this->args['tags']))			$this->normal = false;		elseif($this->args['author'] and ($this->args['section'] or $this->args['tags']))			$this->normal = false;		elseif($this->args['tags'] and ($this->args['author'] or $this->args['section']))			$this->normal = false;				/*		 * Dude, you have no idea how long it took to code this.		 * My head split in two, it's that bad. >_<		 */		#Make our cache key.		foreach($this->args as $arg=>$value)			$this->cache_key .= '-'.$arg.'-'.$value;		if(!$this->normal)			$this->cache_key .= '-sort-'.$this->sortby.'-'.$this->sort;		$this->cache_key = '-'.bj_shortname($this->cache_key);				#Do we normally do this?		if($this->normal)			foreach($this->args as $arg=>$value)				return $bj->cache->get_entries($arg,$value);		else {			add_action('query_extra_'.$this->cache_key,array($this,'query_key'));			return $bj->cache->get_entries($this->cache_key);		}	}		function query_key() {		$query = '';		#Args.		if(count($this->args) > 0) {			$query .= ' WHERE';			$and = '';			$i = 0;			foreach($this->args as $arg=>$value) {				if($arg != 'section' and $arg != 'tags') {					if($i > 0)						$and = ' AND';					$query .= $and.' '.$arg.' = \''.$value.'\'';					$i++;				}			}			if(!empty($this->tags))				$query .= $and.$this->tags;			if(!empty($this->search))				$query .= $and.$this->search;		}		#Order by...		$query .= ' ORDER BY '.$this->sortby.' '.$this->sort;		#And the final piece, offset and limit.		if($this->limit)			$query .= ' LIMIT '.$this->offset.','.$this->limit;		#Bon voyage, query!		return $query;	}		function gete($q) {		global $bj,$i;		parse_str($q,$args);		$i = 0;			$query = "SELECT * FROM ".$bj->db->entries." WHERE";		if(isset($args['id']))			$query .= " `ID` = '".intval($args['id'])."'";		else			$query .= " `ID` != '0'"; #Just a filler so each value can start with "AND".		if(isset($args['second']))			$query .= " AND SECOND(`posted`) = '".$args['second']."'";		if(isset($args['minute']))			$query .= " AND MINUTE(`posted`) = '".$args['minute']."'";		if(isset($args['hour']))			$query .= " AND HOUR(`posted`) = '".$args['hour']."'";		if(isset($args['day']))			$query .= " AND DAYOFMONTH(`posted`) = '".$args['day']."'";		if(isset($args['month']))			$query .= " AND MONTH(`posted`) = '".$args['month']."'";		if(isset($args['year']))			$query .= " AND YEAR(`posted`) = '".$args['year']."'";		if(isset($args['title']))			$query .= " AND `title` = '".$args['title']."'";		if(isset($args['shortname']))			$query .= " AND `shortname` = '".$args['shortname']."'";		if(isset($args['tag'])) {			$query .= " AND (";			$tags = explode(",",$args['tag']);			foreach($tags as $num=>$tag) {				if($num != 0)					$query .= " OR ";				if(substr($tag,0,1) == "-")					$query .= "`tags` NOT REGEXP '\"".substr($tag,1)."\"'";				else					$query .= "`tags` REGEXP '\"".$tag."\"'";			}			$query .= ")";		}		if(isset($args['section']))			$query .= " AND `section` = '".$args['section']."'";		if(isset($args['type']))			$query .= " AND `ptype` = '".$args['type']."'";		if(isset($args['author']))			$query .= " AND `author` = '".$args['author']."'";		if(isset($args['search'])) {			$search = str_replace('+',' ',$args['search']);			$search = explode(' ',$search);			$query .= " AND (((`title` LIKE '%".$search[0]."%') OR (`content` LIKE '%".$search[0]."%'))";			for ( $i = 1; $i < count($search); $i++)				$query .= " OR ((`title` LIKE '%".$search[$i]."%') OR (`content` LIKE '%".$search[$i]."%'))";			$query .= ')';		}		if(isset($args['sortby']))			$query .= " ORDER BY `".$args['sortby']."`";		else			$query .= " ORDER BY `posted`";		if(isset($args['order']))			$query .= " ".$args['order'];		else			$query .= " DESC";		if($args['inclimit'] != "false") {			$loffset = (isset($args['offset'])) ? $args['offset'] : $this->offset;			$limit = (isset($args['limit'])) ? $args['limit'] : $this->perpage;			$query .= " LIMIT ".$loffset.",".$limit;		}		#Limit and offset.		if($args['count'] == "true")			return mysql_num_rows($bj->db->query($query));		else			return $bj->db->get_rows($query);	}}?>