/**
  * Remove a relationship between two users and clear caches afterwards.
  *
  * @param $user1 Integer: user ID of the first user
  * @param $user2 Integer: user ID of the second user
  */
 public function removeRelationshipByUserID($user1, $user2)
 {
     global $wgUser, $wgMemc;
     if ($user1 != $wgUser->getID() && $user2 != $wgUser->getID()) {
         return false;
         // only logged in user should be able to delete
     }
     // must delete record for each user involved in relationship
     $dbw = wfGetDB(DB_MASTER);
     $dbw->delete('user_relationship', array('r_user_id' => $user1, 'r_user_id_relation' => $user2), __METHOD__);
     $dbw->delete('user_relationship', array('r_user_id' => $user2, 'r_user_id_relation' => $user1), __METHOD__);
     $wgMemc->delete(wfForeignMemcKey('huiji', '', 'relationship', 'profile', "{$user1}-1"));
     $wgMemc->delete(wfForeignMemcKey('huiji', '', 'relationship', 'profile', "{$user2}-1"));
     $wgMemc->delete(wfForeignMemcKey('huiji', '', 'relationship', 'profile', "{$user1}-2"));
     $wgMemc->delete(wfForeignMemcKey('huiji', '', 'relationship', 'profile', "{$user2}-2"));
     // RelationshipRemovedByUserID hook
     wfRunHooks('RelationshipRemovedByUserID', array($user1, $user2));
     // Update social statistics for both users
     $stats = new UserStatsTrack($user1, '');
     $stats->updateRelationshipCount(1);
     $stats->updateRelationshipCount(2);
     $stats->clearCache();
     $stats = new UserStatsTrack($user2, '');
     $stats->updateRelationshipCount(1);
     $stats->updateRelationshipCount(2);
     $stats->clearCache();
 }
 /**
  * This function was originally in the UserStats directory, in the file
  * CreatedOpinionsCount.php.
  * This function here updates the stats_opinions_created column in the
  * user_stats table every time the user creates a new blog post.
  *
  * This is hooked into two separate hooks (todo: find out why), ArticleSave
  * and ArticleSaveComplete. Their arguments are mostly the same and both
  * have $article as the first argument.
  *
  * @param $article Object: Article object representing the page that was/is
  *                         (being) saved
  * @return Boolean: true
  */
 public static function updateCreatedOpinionsCount(&$article)
 {
     global $wgOut, $wgUser;
     $aid = $article->getTitle()->getArticleID();
     // Shortcut, in order not to perform stupid queries (cl_from = 0...)
     if ($aid == 0) {
         return true;
     }
     $dbr = wfGetDB(DB_SLAVE);
     $res = $dbr->select('categorylinks', 'cl_to', array('cl_from' => $aid), __METHOD__);
     foreach ($res as $row) {
         $ctg = Title::makeTitle(NS_CATEGORY, $row->cl_to);
         $ctgname = $ctg->getText();
         $blogCat = wfMsgForContent('blog-category');
         $userBlogCat = wfMsgForContent('blog-by-user-category', $blogCat);
         if (strpos($ctgname, $userBlogCat) !== false) {
             $user_name = trim(str_replace($userBlogCat, '', $ctgname));
             $u = User::idFromName($user_name);
             if ($u) {
                 $stats = new UserStatsTrack($u, $user_name);
                 // Copied from UserStatsTrack::updateCreatedOpinionsCount()
                 // Throughout this code, we could use $u and $user_name
                 // instead of $stats->user_id and $stats->user_name but
                 // there's no point in doing that because we have to call
                 // clearCache() in any case
                 if (!$wgUser->isAnon() && $stats->user_id) {
                     $ctg = $userBlogCat . ' ' . $stats->user_name;
                     $parser = new Parser();
                     $ctgTitle = Title::newFromText($parser->preprocess(trim($ctg), $wgOut->getTitle(), $wgOut->parserOptions()));
                     $ctgTitle = $ctgTitle->getDBkey();
                     $dbw = wfGetDB(DB_MASTER);
                     $opinions = $dbw->select(array('page', 'categorylinks'), array('COUNT(*) AS CreatedOpinions'), array('cl_to' => $ctgTitle, 'page_namespace' => NS_BLOG), __METHOD__, array(), array('categorylinks' => array('INNER JOIN', 'page_id = cl_from')));
                     // Please die in a fire, PHP.
                     // selectField() would be ideal above but it returns
                     // insane results (over 300 when the real count is
                     // barely 10) so we have to f**k around with a
                     // foreach() loop that we don't even need in theory
                     // just because PHP is...PHP.
                     $opinionsCreated = 0;
                     foreach ($opinions as $opinion) {
                         $opinionsCreated = $opinion->CreatedOpinions;
                     }
                     $res = $dbw->update('user_stats', array('stats_opinions_created' => $opinionsCreated), array('stats_user_id' => $stats->user_id), __METHOD__);
                     $stats->clearCache();
                 }
             }
         }
     }
     return true;
 }