[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> functions_rebuild.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: functions_rebuild.php 5297 2010-12-28 22:01:14Z Tomm $
  10   */
  11  
  12  /**
  13   * Completely recount the board statistics (useful if they become out of sync)
  14   */
  15  function rebuild_stats()
  16  {
  17      global $db;
  18  
  19      $query = $db->simple_select("forums", "SUM(threads) AS numthreads");
  20      $stats['numthreads'] = $db->fetch_field($query, 'numthreads');
  21      
  22      if(!$stats['numthreads'])
  23      {
  24          $stats['numthreads'] = 0;
  25      }
  26      
  27      $query = $db->simple_select("forums", "SUM(posts) AS numposts");
  28      $stats['numposts'] = $db->fetch_field($query, 'numposts');
  29      
  30      if(!$stats['numposts'])
  31      {
  32          $stats['numposts'] = 0;
  33      }
  34      
  35      $query = $db->simple_select("forums", "SUM(unapprovedthreads) AS numunapprovedthreads");
  36      $stats['numunapprovedthreads'] = $db->fetch_field($query, 'numunapprovedthreads');
  37      
  38      if(!$stats['numunapprovedthreads'])
  39      {
  40          $stats['numunapprovedthreads'] = 0;
  41      }
  42      
  43      $query = $db->simple_select("forums", "SUM(unapprovedposts) AS numunapprovedposts");
  44      $stats['numunapprovedposts'] = $db->fetch_field($query, 'numunapprovedposts');
  45      
  46      if(!$stats['numunapprovedposts'])
  47      {
  48          $stats['numunapprovedposts'] = 0;
  49      }
  50  
  51      $query = $db->simple_select("users", "COUNT(uid) AS users");
  52      $stats['numusers'] = $db->fetch_field($query, 'users');
  53      
  54      if(!$stats['numusers'])
  55      {
  56          $stats['numusers'] = 0;
  57      }
  58  
  59      update_stats($stats);
  60  }
  61  
  62  /**
  63   * Completely rebuild the counters for a particular forum (useful if they become out of sync)
  64   */
  65  function rebuild_forum_counters($fid)
  66  {
  67      global $db;
  68  
  69      // Fetch the number of threads and replies in this forum (Approved only)
  70      $query = $db->query("
  71          SELECT COUNT(tid) AS threads, SUM(replies) AS replies
  72          FROM ".TABLE_PREFIX."threads
  73          WHERE fid='$fid' AND visible='1' AND closed    NOT LIKE 'moved|%'
  74      ");
  75      $count = $db->fetch_array($query);
  76      $count['posts'] = $count['threads'] + $count['replies'];
  77      
  78      if(!$count['posts'])
  79      {
  80          $count['posts'] = 0;
  81      }
  82  
  83      // Fetch the number of threads and replies in this forum (Unapproved only)
  84      $query = $db->query("
  85          SELECT COUNT(tid) AS threads, SUM(replies) AS impliedunapproved
  86          FROM ".TABLE_PREFIX."threads
  87          WHERE fid='$fid' AND visible='0' AND closed NOT LIKE 'moved|%'
  88      ");
  89      $count2 = $db->fetch_array($query); 
  90       $count['unapprovedthreads'] = $count2['threads']; 
  91      $count['unapprovedposts'] = $count2['impliedunapproved']+$count2['threads'];
  92      
  93      if(!$count['unapprovedthreads'])
  94      {
  95          $count['unapprovedthreads'] = 0;
  96      }
  97  
  98      $query = $db->query("
  99          SELECT SUM(unapprovedposts) AS posts
 100          FROM ".TABLE_PREFIX."threads
 101          WHERE fid='$fid' AND closed NOT LIKE 'moved|%'
 102      ");
 103      $count['unapprovedposts'] += $db->fetch_field($query, "posts");
 104      
 105      if(!$count['unapprovedposts'])
 106      {
 107          $count['unapprovedposts'] = 0;
 108      }
 109  
 110      update_forum_counters($fid, $count);
 111  }
 112  
 113  /**
 114   * Completely rebuild the counters for a particular thread (useful if they become out of sync)
 115   * 
 116   * @param int The thread ID 
 117   * @param array Optional thread array so we don't have to query it 
 118   */
 119  function rebuild_thread_counters($tid)
 120  {
 121      global $db;
 122  
 123      if(!$thread['tid']) 
 124       { 
 125           $thread = get_thread($tid); 
 126       } 
 127            
 128       $query = $db->simple_select("posts", "COUNT(*) AS replies", "tid='{$tid}' AND pid!='{$thread['firstpost']}' AND visible='1'"); 
 129       $count['replies'] = $db->fetch_field($query, "replies");
 130      if($count['replies'] < 0)
 131      {
 132          $count['replies'] = 0;
 133      }
 134  
 135      // Unapproved posts
 136      $query = $db->simple_select("posts", "COUNT(pid) AS totunposts", "tid='{$tid}' AND pid != '{$thread['firstpost']}' AND visible='0'");
 137      $count['unapprovedposts'] = $db->fetch_field($query, "totunposts");
 138      
 139      if(!$count['unapprovedposts'])
 140      {
 141          $count['unapprovedposts'] = 0;
 142      }
 143  
 144      // Attachment count
 145      $query = $db->query("
 146              SELECT COUNT(aid) AS attachment_count
 147              FROM ".TABLE_PREFIX."attachments a
 148              LEFT JOIN ".TABLE_PREFIX."posts p ON (a.pid=p.pid)
 149              WHERE p.tid='$tid'
 150      ");
 151      $count['attachmentcount'] = $db->fetch_field($query, "attachment_count");
 152      
 153      if(!$count['attachmentcount'])
 154      {
 155          $count['attachmentcount'] = 0;
 156      }
 157  
 158      update_thread_counters($tid, $count);
 159  }
 160  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1