[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/dao/articlecommentstatus.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/dao/articlestatus.class.php" ); 8 lt_include( PLOG_CLASS_PATH."class/summary/dao/summarystatsconstants.class.php" ); 9 10 /** 11 * This class implements a few methods that can be used to obtain the list of most recent blogs, posts, commets, 12 * most commented articles, etc. It is mainly used by the summary.php script but it can also be used by users 13 * wishing to integrate the summary page in their sites. 14 * 15 * @see BlogInfo 16 * @see Article 17 * 18 * \ingroup DAO 19 */ 20 class SummaryStats extends Model 21 { 22 23 var $_now; 24 var $_startTime; 25 var $_summaryPageShowMax; 26 27 function SummaryStats() 28 { 29 // initialize ADOdb 30 $this->Model(); 31 32 $this->_now = $this->getNow(); 33 $this->_startTime = $this->getStartTime( SUMMARY_DEFAULT_TIME_FENCE ); 34 35 // get the summary_page_show_max from config 36 $config =& Config::getConfig(); 37 $this->_summaryPageShowMax = $config->getValue( "summary_page_show_max", SUMMARY_DEFAULT_PAGE_SHOW_MAX ); 38 } 39 40 /** 41 * Returns the most commented articles so far 42 * 43 * @param maxPosts The maximum number of posts to return 44 * @return An array of Article objects with the most commented articles 45 */ 46 function getMostCommentedArticles( $maxPosts = 0 ) 47 { 48 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 49 lt_include( PLOG_CLASS_PATH."class/dao/commentscommon.class.php" ); 50 $articles = new Articles(); 51 52 $maxPosts > 0 ? $max = $maxPosts : $max = $this->_summaryPageShowMax; 53 54 $prefix = $this->getPrefix(); 55 $query = "SELECT a.* 56 FROM {$prefix}articles AS a, 57 {$prefix}blogs b 58 WHERE a.blog_id = b.id 59 AND a.status = ".POST_STATUS_PUBLISHED." 60 AND b.status = ".BLOG_STATUS_ACTIVE." 61 AND a.date >= ".$this->_startTime." 62 AND a.date <= ".$this->_now." 63 AND a.in_summary_page = '1' 64 AND a.num_nonspam_comments > 0 65 AND b.show_in_summary = '1' 66 ORDER BY a.num_nonspam_comments DESC 67 LIMIT 0, $max"; 68 69 $result = $this->Execute( $query ); 70 71 if( !$result ){ 72 return Array(); 73 } 74 75 $posts = Array(); 76 while( $row = $result->FetchRow()) { 77 array_push( $posts, $articles->mapRow($row)); 78 } 79 80 $result->Close(); 81 82 return $posts; 83 } 84 85 /** 86 * Returns an array with the most read articles 87 * 88 * @param maxPosts The maximum number of posts to return 89 * @return an array of Article objects with information about the posts 90 * TODO: performance tuning 91 */ 92 function getMostReadArticles( $maxPosts = 0 ) 93 { 94 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 95 96 $prefix = $this->getPrefix(); 97 $articles = new Articles(); 98 99 $query = "SELECT a.* 100 FROM {$prefix}articles a, {$prefix}blogs b 101 WHERE a.status = ".POST_STATUS_PUBLISHED." 102 AND a.blog_id = b.id 103 AND b.status = ".BLOG_STATUS_ACTIVE." 104 AND a.date <= ".$this->_now." AND a.date > ".$this->_startTime." 105 AND in_summary_page = '1' 106 AND b.show_in_summary = '1' 107 ORDER BY a.num_reads DESC"; 108 109 if( $maxPosts > 0 ) 110 $query .= " LIMIT 0,".$maxPosts; 111 else 112 $query .= " LIMIT 0,".$this->_summaryPageShowMax; 113 114 $result = $this->Execute( $query ); 115 116 if( !$result ) 117 return Array(); 118 119 $posts = Array(); 120 while( $row = $result->FetchRow()) { 121 $post = $articles->mapRow($row); 122 array_push( $posts, $post ); 123 } 124 125 $result->Close(); 126 127 return $posts; 128 } 129 130 /** 131 *returns list with the most recently created blogs 132 * 133 * @param maxBlogs The maximum number of blogs to return, or '0' to get all of them 134 * @return An array of BlogInfo objects 135 * @see BlogInfo 136 */ 137 function getRecentBlogs( $maxBlogs = 0 ) 138 { 139 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 140 $query = "SELECT * 141 FROM ".$this->getPrefix()."blogs 142 WHERE status = ".BLOG_STATUS_ACTIVE." 143 AND show_in_summary = '1' 144 AND create_date > ".$this->_startTime." 145 ORDER BY create_date DESC"; 146 147 if( $maxBlogs > 0 ) 148 $query .= " LIMIT 0,".$maxBlogs; 149 else 150 $query .= " LIMIT 0,".$this->_summaryPageShowMax; 151 152 $result = $this->Execute( $query ); 153 154 if( !$result ){ 155 return Array(); 156 } 157 158 $blogs = Array(); 159 $blogsDao = new Blogs(); 160 while( $row = $result->FetchRow()) { 161 $blog = $blogsDao->mapRow( $row ); 162 $blogs[$blog->getId()] = $blog; 163 } 164 165 $result->Close(); 166 167 return $blogs; 168 } 169 170 /** 171 * returns an array with the most active blogs 172 * 173 * @param maxBlogs How many blogs to return 174 * @return An array of BlogInfo objects 175 * @see BlogInfo 176 */ 177 function getMostActiveBlogs( $maxBlogs = 0 ) 178 { 179 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 180 181 $prefix = $this->getPrefix(); 182 183 $query = "SELECT SUM(((a.num_reads + a.num_comments/5) / (TO_DAYS(NOW()) - TO_DAYS(a.date) + 1)) )/COUNT(a.id) as rank, 184 b.id AS blog_id 185 FROM {$prefix}articles AS a 186 INNER JOIN {$prefix}blogs AS b 187 ON b.id = a.blog_id AND b.status = ".BLOG_STATUS_ACTIVE." 188 WHERE a.date >= ".$this->_startTime." 189 AND a.date <= ".$this->_now." 190 AND a.in_summary_page = '1' 191 AND b.show_in_summary = '1' 192 GROUP BY b.id 193 ORDER BY rank DESC"; 194 195 if( $maxBlogs > 0 ) 196 $query .= " LIMIT 0,".$maxBlogs; 197 else 198 $query .= " LIMIT 0,".$this->_summaryPageShowMax; 199 200 $result = $this->Execute( $query ); 201 202 if( !$result ){ 203 return Array(); 204 } 205 206 $blogs = Array(); 207 $blogsDao = new Blogs(); 208 while( $row = $result->FetchRow()) { 209 $blog = $blogsDao->getBlogInfo( $row["blog_id"] ); 210 $blogs[$blog->getId()] = $blog; 211 } 212 213 $result->Close(); 214 215 return $blogs; 216 } 217 218 /** 219 * returns a list with the most recent articles, but only one per blog so that 220 * one blog cannot clog the whole main page because they've posted 100 posts today. Posts that were posted 221 * in categories not shown in the main page of each blog will not be shown! 222 * 223 * @param maxPosts The maximum number of posts to return 224 * @return An array of Article objects with the most recent articles. 225 */ 226 function getRecentArticles( $globaArticleCategoryId = ALL_GLOBAL_ARTICLE_CATEGORIES, $maxPosts = 0 ) 227 { 228 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 229 230 $prefix = $this->getPrefix(); 231 232 $query = "SELECT a.id AS id, a.blog_id AS blog_id 233 FROM {$prefix}articles a, 234 {$prefix}blogs b 235 WHERE a.date >= ".$this->_startTime." AND a.date <= ".$this->_now." 236 AND a.blog_id = b.id 237 AND b.status = ".BLOG_STATUS_ACTIVE." 238 AND a.status = ".POST_STATUS_PUBLISHED." 239 AND b.show_in_summary = '1' 240 AND a.in_summary_page = '1'"; 241 242 if($globaArticleCategoryId != ALL_GLOBAL_ARTICLE_CATEGORIES) 243 $query .= " AND a.global_category_id = '".Db::qstr($globaArticleCategoryId)."'"; 244 245 $query .= " ORDER BY a.date DESC"; 246 247 if( $maxPosts <= 0 ) 248 $maxPosts = $this->_summaryPageShowMax; 249 250 // the multiplier here isn't a very elegant solution but what we're trying to avoid 251 // here is a situation where if the limit is '10', then a blog posting 10 articles in one 252 // go would use all these 10 'slots' in the result set. Then when the list of posts is 253 // post-processed, there would only be one article left... which is definitely not 254 // what we'd like 255 $query .= " LIMIT 0,".$maxPosts * 15; 256 257 $result = $this->Execute( $query ); 258 259 if( !$result ) 260 return Array(); 261 262 $blogs = Array(); 263 $posts = Array(); 264 $i = 0; 265 266 $articles = new Articles(); 267 while( ($row = $result->FetchRow()) && ($i < $maxPosts) ) { 268 if (!in_array($row["blog_id"], $blogs)) 269 { 270 $blogs[] = $row["blog_id"]; 271 array_push( $posts, $articles->getArticle($row["id"]) ); 272 $i++; 273 } 274 } 275 276 $result->Close(); 277 278 return $posts; 279 } 280 281 function getPostsByGlobalCategory( $globaArticleCategoryId = ALL_GLOBAL_ARTICLE_CATEGORIES, 282 $page = -1, 283 $itemsPerPage = SUMMARY_DEFAULT_ITEMS_PER_PAGE ) 284 { 285 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 286 287 $prefix = $this->getPrefix(); 288 289 $query = "SELECT a.id AS id 290 FROM {$prefix}articles a, 291 {$prefix}blogs b 292 WHERE a.date <= ".$this->_now." 293 AND a.blog_id = b.id 294 AND b.status = ".BLOG_STATUS_ACTIVE." 295 AND a.status = ".POST_STATUS_PUBLISHED." 296 AND a.in_summary_page = '1' 297 AND b.show_in_summary = '1'"; 298 299 if($globaArticleCategoryId != ALL_GLOBAL_ARTICLE_CATEGORIES) 300 $query .= " AND a.global_category_id = '".Db::qstr($globaArticleCategoryId)."'"; 301 302 $query .= " ORDER BY a.date DESC"; 303 304 $result = $this->Execute( $query, $page, $itemsPerPage ); 305 306 if( !$result ) 307 return Array(); 308 309 $posts = Array(); 310 $articles = new Articles(); 311 while( $row = $result->FetchRow() ) { 312 // if we call Articles::getArticle() we'll be using the cached data 313 // if it was already there, instead of mapping the whole row here 314 array_push( $posts, $articles->getArticle( $row["id"] )); 315 } 316 317 $result->Close(); 318 319 return $posts; 320 } 321 322 function getNumPostsByGlobalCategory( $globaArticleCategoryId = ALL_GLOBAL_ARTICLE_CATEGORIES ) 323 { 324 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 325 326 $prefix = $this->getPrefix(); 327 328 329 $query =" a.date <= ".$this->_now." 330 AND a.blog_id = b.id 331 AND b.status = ".BLOG_STATUS_ACTIVE." 332 AND a.status = ".POST_STATUS_PUBLISHED." 333 AND b.show_in_summary = '1' 334 AND a.in_summary_page = '1'"; 335 336 if($globaArticleCategoryId != ALL_GLOBAL_ARTICLE_CATEGORIES) 337 $query .= " AND a.global_category_id = '".Db::qstr($globaArticleCategoryId)."'"; 338 339 return( $this->getNumItems( "{$prefix}articles a, {$prefix}blogs b", $query, "a.id" )); 340 } 341 342 /** 343 * @private 344 */ 345 function getNow() 346 { 347 lt_include( PLOG_CLASS_PATH.'class/data/timestamp.class.php' ); 348 349 $time = new Timestamp(); 350 $now = $time->getTimestamp(); 351 352 return $now; 353 } 354 355 /** 356 * @private 357 */ 358 function getStartTime( $duration ) 359 { 360 lt_include( PLOG_CLASS_PATH.'class/data/timestamp.class.php' ); 361 362 $time = new Timestamp(); 363 $time->subtractSeconds( $duration * 24 * 60 * 60 ); 364 $startTime = $time->getYear().$time->getMonth(); 365 if( $time->getDay() < 10 ) 366 $startTime .= "0"; 367 $startTime .= $time->getDay(); 368 $startTime .= "000000"; 369 370 return $startTime; 371 } 372 /** 373 * Returns all the most recently posted articles by the given user id 374 * 375 * @param userId 376 * @param maxPosts 377 * @return An Array of Article objects 378 */ 379 function getUserRecentArticles( $userId, $maxPosts = 10 ) 380 { 381 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 382 // query to load the data 383 $query = "SELECT * FROM ".$this->getPrefix()."articles WHERE user_id = ".Db::qstr( $userId )." AND status = ".POST_STATUS_PUBLISHED." 384 ORDER BY date DESC"; 385 386 // process it 387 $res = $this->Execute( $query, 1, $maxPosts ); 388 if( !$res ) 389 return( Array()); 390 391 $posts = Array(); 392 $articles = new Articles(); 393 while( $row = $res->FetchRow()) { 394 $posts[] = $articles->mapRow( $row ); 395 } 396 397 return( $posts ); 398 } 399 } 400 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |