function getNext() { global $wgRequest, $wgCookiePrefix, $wgCategoryNames, $wgUser; $iv = new ImportvideoYoutube(); $dbw = wfGetDB(DB_MASTER); $dbr = wfGetDB(DB_SLAVE); $cat = $wgRequest->getVal('va_cat') ? Title::makeTitleSafe(NS_CATEGORY, $wgRequest->getVal('va_cat')) : null; // get a list $cookiename = $wgCookiePrefix . "VAskip"; $skipids = ""; if (isset($_COOKIE[$cookiename])) { $ids = array_unique(split(",", $_COOKIE[$cookiename])); $good = array(); //safety first foreach ($ids as $id) { if (preg_match("@[^0-9]@", $id)) { continue; } $good[] = $id; } $skipids = " AND va_page NOT IN (" . implode(",", $good) . ") "; } for ($i = 0; $i < 30; $i++) { $r = rand(0, 2); // if it's been in use for more than x minutes, forget 'em $ts = wfTimestamp(TS_MW, time() - 10 * 60); $sql = "SELECT va_page, va_id\n\t\t\t\t\tFROM videoadder "; if ($cat) { // TODO: to avoid join should we just put catinfo in the page table? $sql .= " LEFT JOIN page ON va_page = page_id "; } $sql .= "\n\t\t\t\t\tWHERE va_page NOT IN (5, 5791) AND\n\t\t\t\t\tva_template_ns is NULL and va_skipped_accepted is NULL\n\t\t\t\t\tAND (va_inuse is NULL or va_inuse < '{$ts}')\n\t\t\t"; if ($cat) { $cats = array_flip($wgCategoryNames); $mask = $cats[$cat->getText()]; $sql .= " AND page_catinfo & {$mask} = {$mask} "; } $sql .= " {$skipids} "; if ($r < 2) { // get the most popular page that has no video $sql .= " ORDER BY va_page_counter DESC LIMIT 1"; } else { // get the mostly recently edited page that has no video $sql .= " ORDER BY va_page_touched DESC LIMIT 1"; } $res = $dbr->query($sql); if ($row = $dbr->fetchObject($res)) { $title = Title::newFromID($row->va_page); if ($title && !$this->hasProblems($title, $dbr)) { $iv->getTopResults($title, 1, wfMsg("howto", $title->getText())); } } // get the next title to deal with if (sizeof($iv->mResults) > 0) { // mark it as in use, so we don't get multiple people processing the same page $dbw->update("videoadder", array("va_inuse" => wfTimestampNow()), array("va_page" => $row->va_page)); return array($title, $iv->mResults[0]); } // set va_skipped_accepted to 2 because we have no results, so we skip it again $dbw->update("videoadder", array("va_skipped_accepted" => 2), array("va_page" => $row->va_page)); } return null; }