private function getPopularPages() { global $wgDBname; #--- blank variables $page = $date = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db =& $this->getDB(); $db->selectDB(!defined(WIKIA_API_QUERY_DBNAME) ? WIKIA_API_QUERY_DBNAME : $wgDBname); if (is_null($db)) { //throw new DBConnectionError($db, 'Connection error'); throw new WikiaApiQueryError(0); } /* revision was added for Gamespot project - they need last_edit timestamp */ /* its a hack, a better way would be to make wkpoppages an API generator */ /* Nef @ 20071026 */ $this->addTables(array("page_visited", "page", "revision")); $this->addFields(array('article_id', 'page_title', 'rev_timestamp AS last_edit', 'count as sum_cnt')); $this->addWhere(" page_id = article_id "); $this->addWhere(" rev_id = page_latest "); #--- identifier of page if (!is_null($page)) { if (!$this->isInt($page)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'P', $page); $this->addWhereFld("page_id", $page); } #--- if (!empty($ctime)) { if (!$this->isInt($ctime)) { throw new WikiaApiQueryError(1); } } #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->addOption("LIMIT", $limit); $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->addOption("OFFSET", $offset); $this->setCacheKey($lcache_key, 'LO', $offset); } #--- order by $this->addOption("ORDER BY", "sum_cnt desc"); #--- group by #$this->addOption( "GROUP BY", "article_id" ); $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $data[$row->article_id] = array("id" => $row->article_id, "title" => $row->page_title, "last_edit" => wfTimestamp(TS_ISO_8601, $row->last_edit), "counter" => $row->sum_cnt); ApiResult::setContent($data[$row->article_id], $row->page_title); } $db->freeResult($res); $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function removeVoteArticle() { global $wgDBname; $page = $vote = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); #--- $ip = F::app()->wg->Request->getIP(); $user_id = $this->getUser()->getId(); $browserId = $this->getBrowser(); #--- database instance - DB_MASTER $db =& $this->getDB(); $db->selectDB(!defined(WIKIA_API_QUERY_DBNAME) ? WIKIA_API_QUERY_DBNAME : $wgDBname); try { if (empty($page)) { throw new WikiaApiQueryError(1); } if (empty($user_id) && empty($browserId)) { throw new WikiaApiQueryError(2); } else { if (!empty($user_id)) { $this->addWhereFld('user_id', $user_id); } elseif (!empty($browserId)) { $this->addWhereFld('unique_id', $browserId); } } #--- must be int if (!$this->isInt($page) && !$this->isInt($vote)) { throw new WikiaApiQueryError(1); } #--- $this->addWhereFld('article_id', $page); if (!empty($vote)) { $this->addWhereFld('vote', $vote); } #--- $this->setTable('page_vote'); #--- if ($this->delete(__METHOD__)) { #--- remove cache votes from memcache $this->removeAllCacheVote(__METHOD__, $page, $user_id, $ip, $browserId); $voteAvg = $this->getAvgPageVoteFromDB($db, $page); #-- result $data = $this->setApiResult(array('remove' => 1, 'avgvote' => $voteAvg)); } else { throw new DBError($db, 'Database query failed'); } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $this->setIndexTagName('fault'); $data = $e->getText(); } $this->getResult()->setIndexedTagName($data, $this->getIndexTagName()); $this->getResult()->addValue('item', $this->getModuleName(), $data); }
private function getPagesByCategory() { global $wgDBname; #--- blank variables $category = $order = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); #--- $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db =& $this->getDB(); $db->selectDB(!defined(WIKIA_API_QUERY_DBNAME) ? WIKIA_API_QUERY_DBNAME : $wgDBname); if (is_null($db)) { throw new WikiaApiQueryError(0); } #--- check categories $cats = explode('|', $category); $encodedCats = array(); $memcKeyCats = ""; foreach ($cats as $cat) { $categoryTitle = Title::newFromText($cat); if (is_object($categoryTitle)) { $encodedCats[] = $db->strencode($categoryTitle->getDbKey()); $memcKeyCats .= str_replace(" ", "_", $categoryTitle->getDbKey()); } } if (empty($encodedCats)) { throw new WikiaApiQueryError(1, "Missing category"); } $this->setCacheKey($lcache_key, 'CCX', $memcKeyCats); # check order by to use proper table from DB $orderCache = 0; if (!empty($order)) { switch ($order) { case "edit": $order_field = "page.page_touched DESC"; $orderCache = 1; break; case "random": $orderCache = wfRandom(); $this->addWhere("page_random >= " . $orderCache); $order_field = "page.page_random"; break; default: $order_field = "page.page_id DESC"; break; } } else { if (count($encodedCats) > 1) { $order_field = "page.page_id DESC"; #"categorylinks.cl_to, categorylinks.cl_from DESC"; $orderCache = 3; } else { $order_field = "page.page_id DESC"; $orderCache = 4; } } $this->setCacheKey($lcache_key, 'ORD', $orderCache); #--- limit if (!empty($limit)) { if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'L', $limit); } if (!empty($offset)) { if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'LO', $offset); } # if user categorylinks in query sort($encodedCats); $data = array(); // check data from cache ... $cached = ""; #$this->getDataFromCache($lcache_key); if (!is_array($cached)) { foreach ($encodedCats as $id => $category) { #--- $pages = $this->getCategoryPages($db, $category, $limit); #--- no pages if (empty($pages)) { continue; } $this->resetQueryParams(); # build main query on page table $this->addTables(array("page")); $this->addFields(array('page_id', 'page_namespace', 'page_title', 'page_touched', 'page_random')); $this->addWhere("page_id in ('" . implode("','", $pages) . "')"); $this->addWhere("page_is_redirect = 0"); #-- limit; $this->addOption("LIMIT", $limit); $this->addOption("OFFSET", $offset); $this->addOption("ORDER BY", "{$order_field}"); #--- $res = $this->select(__METHOD__); #--- while ($row = $db->fetchObject($res)) { $data[$row->page_id] = array("id" => $row->page_id, "namespace" => $row->page_namespace, "title" => $row->page_title, "url" => htmlspecialchars(Title::makeTitle($row->page_namespace, $row->page_title)->getFullURL()), "category" => $category); ApiResult::setContent($data[$row->page_id], $row->page_title); } $db->freeResult($res); if (count($data) >= $limit) { break; } } #--- set in memc $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function getLastEditPages() { global $wgDBname; #--- blank variables $nspace = $user = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db =& $this->getDB(); $db->selectDB($wgDBname); if (is_null($db)) { //throw new DBConnectionError($db, 'Connection error'); throw new WikiaApiQueryError(0); } $this->addTables(array("page")); $this->addFields(array('page_id', 'page_title', 'page_namespace', 'page_touched')); #--- identifier of namespace if (!is_null($nspace)) { //error_log("nspace=".urldecode($nspace)."\n"); $namespace_keys = array_map('intval', explode(',', urldecode($nspace))); if (empty($namespace_keys)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'N', $nspace); $this->addWhere('page_namespace in (' . $db->makeList($namespace_keys) . ')'); } #--- if (!empty($ctime)) { if (!$this->isInt($ctime)) { throw new WikiaApiQueryError(1); } } #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->addOption("LIMIT", $limit); $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->addOption("OFFSET", $offset); $this->setCacheKey($lcache_key, 'LO', $limit); } #--- order by $this->addOption("ORDER BY", "page_latest DESC"); $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $data[$row->page_id] = array("id" => $row->page_id, "namespace" => $row->page_namespace, "title" => $row->page_title, "editdate" => $row->page_touched); ApiResult::setContent($data[$row->page_id], $row->page_title); } $db->freeResult($res); $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function getPagesByCategory() { global $wgDBname; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db =& $this->getDB(); $db->selectDB(!defined(WIKIA_API_QUERY_DBNAME) ? WIKIA_API_QUERY_DBNAME : $wgDBname); if (is_null($db)) { //throw new DBConnectionError($db, 'Connection error'); throw new WikiaApiQueryError(0); } $this->addTables(array("categorylinks")); $this->addFields(array('cl_to', 'count(*) as cnt')); #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->addOption("LIMIT", $limit); $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->addOption("OFFSET", $offset); $this->setCacheKey($lcache_key, 'LO', $limit); } #--- order by $this->addOption("ORDER BY", "cnt desc"); #--- group by $this->addOption("GROUP BY", "cl_to"); $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $data[$row->cl_to] = array("count" => $row->cnt, "url" => htmlspecialchars(Title::makeTitle(NS_CATEGORY, $row->cl_to)->getFullURL())); ApiResult::setContent($data[$row->cl_to], $row->cl_to); } $db->freeResult($res); $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function getMostVisitedPages() { global $wgDBname; #--- blank variables $nspace = $user = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance if (is_null($pagename)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'P', $pagename); #--- if (!empty($ctime)) { if (!$this->isInt($ctime)) { throw new WikiaApiQueryError(1); } } #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'LO', $limit); } $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { #check to take data from article $templateTitle = Title::newFromText($pagename, NS_MEDIAWIKI); if ($templateTitle instanceof Title && $templateTitle->exists()) { $templateArticle = new Article($templateTitle); $templateContent = $templateArticle->getContent(); $lines = explode("\n\n", $templateContent); foreach ($lines as $line) { $title = Title::NewFromText($line); if (is_object($title)) { #--- $article['url'] = $title->getLocalUrl(); $article['text'] = $title->getPrefixedText(); $results[] = $article; } } if (!empty($results)) { $results = array_slice($results, $offset, $limit); } $data = array(); if (!empty($results)) { foreach ($results as $id => $result) { $data[$id] = $result; ApiResult::setContent($data[$id], $result['text']); } } $this->saveCacheData($lcache_key, $data, $ctime); } } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function getMostAccessPages() { global $wgDBname; #--- blank variables $nspace = $user = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db =& $this->getDB(); #$db->selectDB( (!defined(WIKIA_API_QUERY_DBNAME)) ? WIKIA_API_QUERY_DBNAME : $wgDBname ); $db->selectDB($wgDBname); if (is_null($db)) { //throw new DBConnectionError($db, 'Connection error'); throw new WikiaApiQueryError(0); } $this->addTables(array("recentchanges")); $this->addFields(array('rc_id', 'rc_title', 'rc_namespace', 'rc_timestamp')); #--- identifier of namespace if (!is_null($nspace)) { //error_log("nspace=".urldecode($nspace)."\n"); $namespace_keys = preg_split("/\\,+/", urldecode($nspace)); if (empty($namespace_keys)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'N', $nspace); $this->addWhere("rc_namespace in ('" . implode("','", $namespace_keys) . "')"); } #--- if (!empty($ctime)) { if (!$this->isInt($ctime)) { throw new WikiaApiQueryError(1); } } #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->addOption("LIMIT", $limit); $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->addOption("OFFSET", $offset); $this->setCacheKey($lcache_key, 'LO', $limit); } #--- user if (!is_null($user)) { if (!$this->isInt($user)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'U', $user); $this->addWhereFld("rc_user", $user); } #--- order by $this->addOption("ORDER BY", "rc_timestamp desc"); $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $data[$row->rc_id] = array("id" => $row->rc_id, "namespace" => $row->rc_namespace, "title" => $row->rc_title); ApiResult::setContent($data[$row->rc_id], $row->rc_title); } $db->freeResult($res); $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }
private function getTopEditUsers() { global $wgDBname, $wgCityId, $wgStatsDBEnabled; if (empty($wgStatsDBEnabled)) { return false; } #--- blank variables $nspace = $user = null; #--- initial parameters (dbname, limit, offset ...) extract($this->getInitialParams()); #--- request parameters () extract($this->extractRequestParams()); $this->initCacheKey($lcache_key, __METHOD__); try { #--- database instance $db = $this->getDB(); if (is_null($db)) { throw new WikiaApiQueryError(0); } $cid = empty($wgCityId) ? WikiFactory::DBtoID($wgDBname) : $wgCityId; $this->addTables(array("events_local_users")); $this->addFields(array('user_id', 'user_name', 'edits')); $this->addWhere("user_id > 0 and user_is_blocked = 0 and user_is_closed = 0"); if (!empty($cid)) { $this->addWhere("wiki_id = {$cid}"); } #--- user if (!is_null($user)) { if (!$this->isInt($user)) { throw new WikiaApiQueryError(1); } $this->setCacheKey($lcache_key, 'U', $user); $this->addWhere("user_id = '" . intval($user) . "'"); } #--- if (!empty($ctime)) { if (!$this->isInt($ctime)) { throw new WikiaApiQueryError(1); } } #--- limit if (!empty($limit)) { //WikiaApiQuery::DEF_LIMIT if (!$this->isInt($limit)) { throw new WikiaApiQueryError(1); } $this->addOption("LIMIT", $limit); $this->setCacheKey($lcache_key, 'L', $limit); } #--- offset if (!empty($offset)) { //WikiaApiQuery::DEF_LIMIT_COUNT if (!$this->isInt($offset)) { throw new WikiaApiQueryError(1); } $this->addOption("OFFSET", $offset); $this->setCacheKey($lcache_key, 'LO', $limit); } #--- order by $this->addOption("ORDER BY", "edits DESC"); $data = array(); // check data from cache ... $cached = $this->getDataFromCache($lcache_key); if (!is_array($cached)) { $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $data[$row->user_id] = array("user_id" => $row->user_id, "cnt" => $row->edits, "user_name" => $row->user_name); ApiResult::setContent($data[$row->user_id], $row->user_name); } $db->freeResult($res); $this->saveCacheData($lcache_key, $data, $ctime); } else { // ... cached $data = $cached; } } catch (WikiaApiQueryError $e) { // getText(); } catch (DBQueryError $e) { $e = new WikiaApiQueryError(0, 'Query error: ' . $e->getText()); } catch (DBConnectionError $e) { $e = new WikiaApiQueryError(0, 'DB connection error: ' . $e->getText()); } catch (DBError $e) { $e = new WikiaApiQueryError(0, 'Error in database: ' . $e->getLogMessage()); } // is exception if (isset($e)) { $data = $e->getText(); $this->getResult()->setIndexedTagName($data, 'fault'); } else { $this->getResult()->setIndexedTagName($data, 'item'); } $this->getResult()->addValue('query', $this->getModuleName(), $data); }