[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/ -> functions_rebuild.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  /**
  12   * Completely recount the board statistics (useful if they become out of sync)
  13   */
  14  function rebuild_stats()
  15  {
  16      global $db;
  17  
  18      $query = $db->simple_select("forums", "SUM(threads) AS numthreads, SUM(posts) AS numposts, SUM(unapprovedthreads) AS numunapprovedthreads, SUM(unapprovedposts) AS numunapprovedposts, SUM(deletedthreads) AS numdeletedthreads, SUM(deletedposts) AS numdeletedposts");
  19      $stats = $db->fetch_array($query);
  20  
  21      $query = $db->simple_select("users", "COUNT(uid) AS users");
  22      $stats['numusers'] = $db->fetch_field($query, 'users');
  23  
  24      update_stats($stats, true);
  25  }
  26  
  27  /**
  28   * Completely rebuild the counters for a particular forum (useful if they become out of sync)
  29   *
  30   * @param int $fid The forum ID
  31   */
  32  function rebuild_forum_counters($fid)
  33  {
  34      global $db;
  35  
  36      // Fetch the number of threads and replies in this forum (Approved only)
  37      $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies) AS replies, SUM(unapprovedposts) AS unapprovedposts, SUM(deletedposts) AS deletedposts', "fid='$fid' AND visible='1'");
  38      $count = $db->fetch_array($query);
  39      $count['posts'] = $count['threads'] + $count['replies'];
  40  
  41      // Fetch the number of threads and replies in this forum (Unapproved only)
  42      $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies)+SUM(unapprovedposts)+SUM(deletedposts) AS impliedunapproved', "fid='$fid' AND visible='0'");
  43      $count2 = $db->fetch_array($query);
  44       $count['unapprovedthreads'] = $count2['threads'];
  45      $count['unapprovedposts'] += $count2['impliedunapproved']+$count2['threads'];
  46  
  47      // Fetch the number of threads and replies in this forum (Soft deleted only)
  48      $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies)+SUM(unapprovedposts)+SUM(deletedposts) AS implieddeleted', "fid='$fid' AND visible='-1'");
  49      $count3 = $db->fetch_array($query);
  50       $count['deletedthreads'] = $count3['threads'];
  51      $count['deletedposts'] += $count3['implieddeleted']+$count3['threads'];
  52  
  53      update_forum_counters($fid, $count);
  54      update_forum_lastpost($fid);
  55  }
  56  
  57  /**
  58   * Completely rebuild the counters for a particular thread (useful if they become out of sync)
  59   *
  60   * @param int $tid The thread ID
  61   */
  62  function rebuild_thread_counters($tid)
  63  {
  64      global $db;
  65  
  66       $thread = get_thread($tid);
  67      $count = array();
  68  
  69       $query = $db->simple_select("posts", "COUNT(pid) AS replies", "tid='{$tid}' AND pid!='{$thread['firstpost']}' AND visible='1'");
  70       $count['replies'] = $db->fetch_field($query, "replies");
  71  
  72      // Unapproved posts
  73      $query = $db->simple_select("posts", "COUNT(pid) AS unapprovedposts", "tid='{$tid}' AND pid != '{$thread['firstpost']}' AND visible='0'");
  74      $count['unapprovedposts'] = $db->fetch_field($query, "unapprovedposts");
  75  
  76      // Soft deleted posts
  77      $query = $db->simple_select("posts", "COUNT(pid) AS deletedposts", "tid='{$tid}' AND pid != '{$thread['firstpost']}' AND visible='-1'");
  78      $count['deletedposts'] = $db->fetch_field($query, "deletedposts");
  79  
  80      // Attachment count
  81      $query = $db->query("
  82              SELECT COUNT(aid) AS attachment_count
  83              FROM ".TABLE_PREFIX."attachments a
  84              LEFT JOIN ".TABLE_PREFIX."posts p ON (a.pid=p.pid)
  85              WHERE p.tid='$tid' AND a.visible=1
  86      ");
  87      $count['attachmentcount'] = $db->fetch_field($query, "attachment_count");
  88  
  89      update_thread_counters($tid, $count);
  90      update_thread_data($tid);
  91  }
  92  
  93  /**
  94   * Completely rebuild poll counters for a particular poll (useful if they become out of sync)
  95   *
  96   * @param int $pid The poll ID
  97   */
  98  function rebuild_poll_counters($pid)
  99  {
 100      global $db;
 101  
 102      $query = $db->simple_select("polls", "pid, numoptions", "pid='".(int)$pid."'");
 103      $poll = $db->fetch_array($query);
 104  
 105      $votes = array();
 106      $query = $db->simple_select("pollvotes", "voteoption, COUNT(vid) AS vote_count", "pid='{$poll['pid']}'", array('group_by' => 'voteoption'));
 107      while($vote = $db->fetch_array($query))
 108      {
 109          $votes[$vote['voteoption']] = $vote['vote_count'];
 110      }
 111  
 112      $voteslist = '';
 113      $numvotes = 0;
 114      for($i = 1; $i <= $poll['numoptions']; ++$i)
 115      {
 116          if(trim($voteslist != ''))
 117          {
 118              $voteslist .= "||~|~||";
 119          }
 120  
 121          if(!isset($votes[$i]) || (int)$votes[$i] <= 0)
 122          {
 123              $votes[$i] = "0";
 124          }
 125          $voteslist .= $votes[$i];
 126          $numvotes = $numvotes + $votes[$i];
 127      }
 128  
 129      $updatedpoll = array(
 130          "votes" => $db->escape_string($voteslist),
 131          "numvotes" => (int)$numvotes
 132      );
 133      $db->update_query("polls", $updatedpoll, "pid='{$poll['pid']}'");
 134  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref