File: _class_blogstats.php

package info (click to toggle)
b2evolution 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 12,976 kB
  • ctags: 5,460
  • sloc: php: 58,989; sh: 298; makefile: 36
file content (373 lines) | stat: -rw-r--r-- 11,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
 * b2evolution - http://b2evolution.net/
 *
 * Copyright (c)2003-2005 by Francois PLANQUE - http://fplanque.net/
 * Released under GNU GPL License - http://b2evolution.net/about/license.html
 *
 *	Filename:	/b2evocore/_class_blogstats.php
 *	Created on:	12/12/03
 *	Created by:	Travis Swicegood <travis@domain51productions.com>
 *	File Description
 *		This file contains the code for the blogstats class.  The main purpose of this class
 *		is to create information necessary for statistical information about the blog without
 *		loading everything as the ItemList class does.
 *
 *	Practical Use
 *		The first use of this class is to generate a total number of blog entries.  Searches
 *		can be limited to specific blogs and/or categories.  This number would them be used to
 *		create random number within a range to display a random blog entry.  Example: Blog
 *		that contains a list of quotes with a random quote being displayed each time a page
 *		is loaded.
 *
 *	Last Modified
 *		12/12/03 - Travis Swicegood: Created file.
 *
 * @deprecated functionality will be moved to ItemList
 * @package evocore
 */
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * @deprecated deprecated
 * @package evocore
 */
class BlogStats{
	var $blog;				// Blog # (1 = all blogs)
	var $request;			// SQL query string
	var $total_posts;		// Number of total posts (loaded via $this->get_post_total())

	function BlogStats(
		$blog = 1, 							// What blog to display (def: all blogs)
		$show_statuses = array(),			// What status to display?
//		$p = '',							// Specific post number to display
		$m = '',							// YearMonth(Day) to display
		$w = -1,							// Week number
		$cat = '',							// Category(s): 1,2,3
		$catsel = array(),					// Same as above except array
		$author = '',						// List of authors to restrict to
		$posts = '', 						// # of posts to display on the page
		$poststart = '',					// Start results at this position
		$postend = '',						// End results at this position
		$s = '',							// Search string
		$sentence = '',						// Search for sentence or for words
		$exact = '',						// Require exact match of title or contents
		$init_what_to_show = '',			// Type of display (example: "posts")
		$timestamp_min = '',				// Do not show posts before this timestamp
		$timestamp_max = 'now'  )			// Do not show posts after this timestamp
	{
	//////
	//	Handle global calls
		global $querycount;										// Total number of queries
		global $tableposts, $tablepostcats, $tablecategories;	// ?
		global $cache_categories;				// ?
		global $cat_array; 										// communication with recursive callback funcs
		global $DB;
		global $Settings;
		
	//////
	//	Which blog is used?
		$this->blog = $blog;

	////////
	// First let's clear some variables
		$whichcat = '';
		$whichauthor = '';
		$result = '';
		$where = '';
		$limits = '';
		$distinct = '';

		// WE ARE GOING TO CONSTRUCT THE "AND" CLOSE
		// THIS IS GOING TO LAST FOR MANY MANY LINES...

		// if a month is specified in the querystring, load that month
		if ($m != '')
		{
			$m = ''.intval($m);
			$where .= ' AND YEAR(post_issue_date)='. substr($m,0,4);
			if (strlen($m)>5)
				$where .= ' AND MONTH(post_issue_date)='. substr($m,4,2);
			if (strlen($m)>7)
				$where .= ' AND DAYOFMONTH(post_issue_date)='. substr($m,6,2);
			if (strlen($m)>9)
				$where .= ' AND HOUR(post_issue_date)='. substr($m,8,2);
			if (strlen($m)>11)
				$where .= ' AND MINUTE(post_issue_date)='. substr($m,10,2);
			if (strlen($m)>13)
				$where .= ' AND SECOND(post_issue_date)='. substr($m,12,2);
		}

		// If a week number is specified
		if( !empty($w) && ($w>=0) )
		{
			$where .= ' AND WEEK(post_issue_date,1)='.intval($w);
		}

		/*
		 * ----------------------------------------------------
		 * Search stuff:
		 * ----------------------------------------------------
		 */
		if(!empty($s))
		{
			$search = ' AND (';
			if ($exact)	// We want exact match of title or contents
				$n = '';
			else // The words/sentence are/is to be included in in the title or the contents
				$n = '%';
			if( ($sentence == '1') or ($sentence == 'sentence') )
			{ // Sentence search
				$s = trim($s);
				$search .= '(post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')';
			}
			else
			{	// Word search
				if( strtoupper( $sentence ) == 'OR' )
					$swords = 'OR';
				else
					$swords = 'AND';

				// puts spaces instead of commas
				$s = preg_replace('/, +/', '', $s);
				$s = str_replace(',', ' ', $s);
				$s = str_replace('"', ' ', $s);
				$s = trim($s);
				$s_array = explode(' ',$s);
				$join = '';
				for ( $i = 0; $i < count($s_array); $i++)
				{
					$search .= ' '.$join.' ( (post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\') ) ';
					$join = $swords;
				}
			}

			$search .= ')';

			//echo $search;
		}
		else
		{
			$search = '';
		}

		/*
		 * ----------------------------------------------------
		 * Category stuff:
		 * ----------------------------------------------------
		 */
		$eq = 'IN'; // default

		$cat_array = array();		// this is a global var

		// Check for cat string (which will be handled recursively)
		if ( ! ((empty($cat)) || ($cat == 'all') || ($cat == '0')) )
		{	// specified a category string:
			$cat = str_replace(',', ' ', $cat);
			if( strstr($cat,'-') )
			{	// We want to exclude cats
				$eq = 'NOT IN';
				$cats = explode('-',$cat);
				$req_cat_array = explode(' ',$cats[1]);
			}
			else
			{	// We want to include cats
				$req_cat_array = explode(' ',$cat);
			}

			// Getting required sub-categories:
			// and add everything to cat array
			// ----------------- START RECURSIVE CAT LIST ----------------
			cat_query();	// make sure the caches are loaded
			foreach( $req_cat_array as $cat_ID )
			{ // run recursively through the cats
				settype( $cat_ID, 'integer' ); // make sure
				if( ! in_array( $cat_ID, $cat_array ) )
				{	// Not already in list
					$cat_array[] = $cat_ID;
					cat_children( $cache_categories, ($blog==1)?0:$blog, $cat_ID, 'cat_req_dummy', 'cat_req',
												'cat_req_dummy', 'cat_req_dummy', 1 );
				}
			}
			// ----------------- END RECURSIVE CAT LIST ----------------
		}

		// Add explicit selections:
		if( ! empty( $catsel ))
		{
			// echo "Explicit selections!<br />";
			$cat_array = array_merge( $cat_array, $catsel );
			array_unique( $cat_array );
		}

		if( empty($cat_array) )
		{
			$whichcat='';
		}
		else
		{
			$whichcat .= ' AND postcat_cat_ID '.$eq.' ('.implode(",", $cat_array).') ';
			// echo $whichcat;
		}



		/*
		 * ----------------------------------------------------
		 * Author stuff:
		 * ----------------------------------------------------
		 */
		if((empty($author)) || ($author == 'all'))
		{
			$whichauthor='';
		}
		elseif (intval($author))
		{
			$author = intval($author);
			if (stristr($author, '-'))
			{
				$eq = '!=';
				$andor = 'AND';
				$author = explode('-', $author);
				$author = $author[1];
			} else {
				$eq = '=';
				$andor = 'OR';
			}
			$author_array = explode(' ', $author);
			$whichauthor .= ' AND post_author '.$eq.' '.$author_array[0];
			for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
				$whichauthor .= ' '.$andor.' post_author '.$eq.' '.$author_array[$i];
			}
		}

		$where .= $search.$whichcat.$whichauthor;


		/*
		 * ----------------------------------------------------
		 * Limits:
		 * ----------------------------------------------------
		 */
		if( !empty($poststart) )
		// fp removed && (!$m) && (!$w) && (!$whichcat) && (!$s)
		// fp added: when in backoffice: always page
		{
			// echo 'POSTSTART-POSTEND';
			if( $postend < $poststart )
			{
				$postend = $poststart + $Settings->get('posts_per_page') - 1;
			}

			if ($Settings->get('what_to_show') == 'posts' || $Settings->get('what_to_show') == 'paged')
			{
				$posts = $postend - $poststart + 1;
				$limits = ' LIMIT '.($poststart-1).','.$posts;
			}
			elseif ($Settings->get('what_to_show') == 'days')
			{
				$posts = $postend - $poststart + 1;
				$lastpostdate = get_lastpostdate( $blog, $show_statuses );
				$lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
				$lastpostdate = mysql2date('U',$lastpostdate);
				$startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400)));
				$otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400)));
				$where .= ' AND post_issue_date > \''.$otherdate.'\' AND post_issue_date < \''.$startdate.'\'';
			}
		}
		elseif( ($m) || ($p) ) // fp rem || ($w) || ($s) || ($whichcat) || ($author)
		{	// (no restriction if we request a month... some permalinks may point to the archive!)
			// echo 'ARCHIVE - no limits';
			$limits = '';
		}
		elseif ($Settings->get('what_to_show') == 'posts')
		{
			// echo 'LIMIT POSTS';
			$limits = ' LIMIT ' . $Settings->get('posts_per_page');
		}
		elseif( $Settings->get('what_to_show') == 'paged' )
		{
			// echo 'PAGED';
			$pgstrt = '';
			if ($paged) {
				$pgstrt = (intval($paged) -1) * $Settings->get('posts_per_page') . ', ';
			}
			$limits = 'LIMIT '.$pgstrt.$Settings->get('posts_per_page');
		}
		elseif ($Settings->get('what_to_show') == 'days')
		{
			// echo 'LIMIT DAYS';
			$lastpostdate = get_lastpostdate( $blog, $show_statuses );
			$lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
			$lastpostdate = mysql2date('U',$lastpostdate);
			$otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($Settings->get('posts_per_page')-1) * 86400)));
			$where .= ' AND post_issue_date > \''.$otherdate.'\'';
		}


		/*
		 * ----------------------------------------------------
		 *  Restrict to the statuses we want to show:
		 * ----------------------------------------------------
		 */
		$where .= ' AND '.statuses_where_clause( $show_statuses );

		/*
		 * ----------------------------------------------------
		 * Time limits:
		 * ----------------------------------------------------
		 */
		if( $timestamp_min == 'now' )
		{
			// echo 'hide past';
			$timestamp_min = time();
		}
		if( !empty($timestamp_min) )
		{	// Hide posts before
			// echo 'before';
			$date_min = date('Y-m-d H:i:s', $timestamp_min + ($Settings->get('time_difference') * 3600) );
			$where .= ' AND post_issue_date >= \''.$date_min.'\'';
		}

		if( $timestamp_max == 'now' )
		{
			// echo 'hide future';
			$timestamp_max = time();
		}
		if( !empty($timestamp_max) )
		{	// Hide posts after
			// echo 'after';
			$date_max = date('Y-m-d H:i:s', $timestamp_max + ($Settings->get('time_difference') * 3600) );
			$where .= ' AND post_issue_date <= \''.$date_max.'\'';
		}


		$this->request = "SELECT COUNT( DISTINCT post_id ) as total_posts ";

		$this->request .= "FROM
				($tableposts
					INNER JOIN $tablepostcats ON ID = postcat_post_ID)
					INNER JOIN $tablecategories ON postcat_cat_ID = cat_ID ";

		if( $blog == 1 )
		{	// Special case: we aggregate all cats from all blogs
			$this->request .= "WHERE 1 ";
		}
		else
		{
			$this->request .= "WHERE cat_blog_ID = $blog ";
		}


		if ($preview)
		{
			$this->request = 'SELECT 0 AS ID'; // dummy mysql query for the preview
		}

		//echo $this->request;
		$querycount++;
		$row = $DB->get_row( $this->request );
	}

}
?>