function bb2_db_num_rows($result)
{
    return wfNumRows($result);
}
global $IP;
require_once "../LocalSettings.php";
require_once "{$IP}/Setup.php";
$wgTitle = Title::newFromText("Rebuild links script");
set_time_limit(0);
$wgDBuser = "******";
$wgDBpassword = $wgDBadminpassword;
$sql = "DROP TABLE IF EXISTS watchlist";
wfQuery($sql, DB_MASTER);
$sql = "CREATE TABLE watchlist (\n  wl_user int(5) unsigned NOT NULL,\n  wl_page int(8) unsigned NOT NULL,\n  UNIQUE KEY (wl_user, wl_page)\n) ENGINE=MyISAM PACK_KEYS=1";
wfQuery($sql, DB_MASTER);
$lc = new LinkCache();
# Now, convert!
$sql = "SELECT user_id,user_watch FROM user";
$res = wfQuery($sql, DB_SLAVE);
$nu = wfNumRows($res);
$sql = "INSERT into watchlist (wl_user,wl_page) VALUES ";
$i = $n = 0;
while ($row = wfFetchObject($res)) {
    $list = explode("\n", $row->user_watch);
    $bits = array();
    foreach ($list as $title) {
        if ($id = $lc->addLink($title) and !$bits[$id]++) {
            $sql .= ($i++ ? "," : "") . "({$row->user_id},{$id})";
        }
    }
    if ($n++ % 100 == 0) {
        echo "{$n} of {$nu} users done...\n";
    }
}
echo "{$n} users done.\n";
 /**
  * Return the image history of this image, line by line.
  * starts with current version, then old versions.
  * uses $this->historyLine to check which line to return:
  *  0      return line for current version
  *  1      query for old versions, return first one
  *  2, ... return next old version from above query
  *
  * @access public
  */
 function nextHistoryLine()
 {
     $fname = 'Image::nextHistoryLine()';
     $dbr =& wfGetDB(DB_SLAVE);
     $this->checkDBSchema($dbr);
     if ($this->historyLine == 0) {
         // called for the first time, return line from cur
         $this->historyRes = $dbr->select('image', array('img_size', 'img_description', 'img_user', 'img_user_text', 'img_timestamp', "'' AS oi_archive_name"), array('img_name' => $this->title->getDBkey()), $fname);
         if (0 == wfNumRows($this->historyRes)) {
             return FALSE;
         }
     } else {
         if ($this->historyLine == 1) {
             $this->historyRes = $dbr->select('oldimage', array('oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user', 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'), array('oi_name' => $this->title->getDBkey()), $fname, array('ORDER BY' => 'oi_timestamp DESC'));
         }
     }
     $this->historyLine++;
     return $dbr->fetchObject($this->historyRes);
 }
 function fixUserOptions()
 {
     print "Fixing user options...";
     $res = wfQuery("SELECT user_id,user_options FROM user", DB_MASTER);
     $total = wfNumRows($res);
     $n = 0;
     print " ({$total} total)\n";
     while ($row = wfFetchObject($res)) {
         $id = intval($row->user_id);
         $option = wfStrencode($this->rewriteUserOptions($row->user_options));
         wfQuery("UPDATE user SET user_options='{$option}' WHERE user_id={$id} LIMIT 1", DB_MASTER);
         if (++$n % 50 == 0) {
             print "{$n}\n";
         }
     }
     wfFreeResult($res);
 }