/**
  * Returns page views count in last month.
  * @param $pageIds array|int Array of article ids (or single integer to check one article)
  * @return array|int
  */
 protected static function getPageViews($pageIds)
 {
     global $wgStatsDB, $wgCityId, $wgDevelEnvironment, $wgStatsDBEnabled;
     wfProfileIn(__METHOD__);
     if (empty($pageIds)) {
         wfProfileOut(__METHOD__);
         return is_array($pageIds) ? array() : 0;
     }
     //fix for BugId:33086
     //use DataMart for pageviews data as all the other
     //data sources are stale/obsolete
     $data = array();
     $ids = is_array($pageIds) ? $pageIds : array($pageIds);
     $rows = DataMartService::getTopArticlesByPageview($wgCityId, $ids, null, false, count($ids));
     foreach ($rows as $id => $pv) {
         $data[intval($id)] = intval($pv);
     }
     // If asked with plain integer return a single integer too
     if (!is_array($pageIds)) {
         $data = !empty($data) ? reset($data) : 0;
     }
     wfProfileOut(__METHOD__);
     return $data;
 }
 /**
  * Get the top articles by pageviews optionally filtering by category and/or namespaces
  *
  * @requestParam array $namespaces [OPTIONAL] The ID's of the namespaces (e.g. 0, 14, 6, etc.) to use as a filter, comma separated
  * @requestParam string $category [OPTIONAL] The name of a category (e.g. Characters) to use as a filter
  * @requestParam string $expand [OPTIONAL] if set will expand result with getDetails data
  *
  * @responseParam array $items The list of top articles by pageviews matching the optional filtering
  * @responseParam string $basepath domain of a wiki to create a url for an article
  *
  * @example
  * @example &namespaces=0,14
  * @example &category=Characters
  * @example &category=Characters&namespaces=14
  */
 public function getTop()
 {
     wfProfileIn(__METHOD__);
     $this->cors->setHeaders($this->response);
     $namespaces = self::processNamespaces($this->request->getArray(self::PARAMETER_NAMESPACES, null), __METHOD__);
     $category = $this->request->getVal(self::PARAMETER_CATEGORY, null);
     $expand = $this->request->getBool(static::PARAMETER_EXPAND, false);
     $limit = $this->request->getInt(static::PARAMETER_LIMIT, 0);
     $baseArticleId = $this->getRequest()->getVal(self::PARAMETER_BASE_ARTICLE_ID, false);
     if ($baseArticleId !== false) {
         $this->validateBaseArticleIdOrThrow($baseArticleId);
     }
     $ids = null;
     if (!empty($category)) {
         $category = Title::makeTitleSafe(NS_CATEGORY, str_replace(' ', '_', $category), false, false);
         if (!is_null($category)) {
             $category = self::followRedirect($category);
             $ids = self::getCategoryMembers($category->getFullText(), 5000, '', '', 'timestamp', 'desc');
             if (!empty($ids)) {
                 $ids = $ids[0];
                 array_walk($ids, function (&$val) {
                     $val = $val['pageid'];
                 });
             }
         } else {
             wfProfileOut(__METHOD__);
             throw new InvalidParameterApiException(self::PARAMETER_CATEGORY);
         }
     }
     //This DataMartService method has
     //separate caching
     $articles = DataMartService::getTopArticlesByPageview($this->wg->CityId, $ids, $namespaces, false, self::MAX_ITEMS + 1);
     $collection = [];
     if (!empty($articles)) {
         $mainPageId = Title::newMainPage()->getArticleID();
         if (isset($articles[$mainPageId])) {
             unset($articles[$mainPageId]);
         }
         $articleIds = array_keys($articles);
         $ids = [];
         foreach (array_keys($articles) as $i) {
             if ($i == $mainPageId) {
                 continue;
             }
             //data is cached on a per-article basis
             //to avoid one article requiring purging
             //the whole collection
             $cache = $this->wg->Memc->get(self::getCacheKey($i, self::ARTICLE_CACHE_ID));
             if (!is_array($cache)) {
                 $ids[] = $i;
             } else {
                 $collection[$cache['id']] = $cache;
             }
         }
         $articles = null;
         if (count($ids) > 0) {
             $titles = Title::newFromIDs($ids);
             if (!empty($titles)) {
                 foreach ($titles as $t) {
                     $id = $t->getArticleID();
                     $article = ['id' => $id, 'title' => $t->getText(), 'url' => $t->getLocalURL(), 'ns' => $t->getNamespace()];
                     $collection[$id] = $article;
                     $this->wg->Memc->set(self::getCacheKey($id, self::ARTICLE_CACHE_ID), $article, 86400);
                 }
             }
             $titles = null;
         }
         //sort articles correctly
         $result = [];
         foreach ($articleIds as $id) {
             if (isset($collection[$id])) {
                 $result[] = $collection[$id];
             }
         }
         $collection = $result;
     } else {
         wfProfileOut(__METHOD__);
         if ($baseArticleId === false) {
             throw new NotFoundApiException();
         }
     }
     if ($baseArticleId !== false) {
         $collection = $this->rerankPopularToArticle($collection, $baseArticleId);
     }
     $limitCollectionSize = self::MAX_ITEMS;
     if ($limit > 0 && $limit < self::MAX_ITEMS) {
         $limitCollectionSize = $limit;
     }
     if (count($collection) > $limitCollectionSize) {
         $collection = array_slice($collection, 0, $limitCollectionSize);
     }
     if ($expand) {
         $collection = $this->expandArticlesDetails($collection);
     }
     $this->setResponseData(['basepath' => $this->wg->Server, 'items' => $collection], ['imgFields' => 'thumbnail', 'urlFields' => ['thumbnail', 'url']], self::getMetadataCacheTime());
     $batches = null;
     wfProfileOut(__METHOD__);
 }
 protected function getTopArticles($wikiId, $lang)
 {
     return \WikiaDataAccess::cache(wfSharedMemcKey("CombinedSearchService", $wikiId, $lang), self::TOP_ARTICLES_CACHE_TIME, function () use($wikiId, $lang) {
         $timer = Time::start(["CombinedSearchService", "getTopArticles"]);
         $requestedFields = ["title", "url", "id", "score", "pageid", "lang", "wid", "article_quality_i", Utilities::field('html', $lang)];
         $topArticlesMap = \DataMartService::getTopArticlesByPageview($wikiId, null, [NS_MAIN], false, self::TOP_ARTICLES_PER_WIKI + 1);
         $query = " +(" . Utilities::valueForField("wid", $wikiId) . ") ";
         $query .= " +( " . implode(" OR ", array_map(function ($x) {
             return Utilities::valueForField("pageid", $x);
         }, array_keys($topArticlesMap))) . ") ";
         $query .= " +(is_main_page:false) ";
         $searchConfig = new Config();
         $searchConfig->setLimit(self::TOP_ARTICLES_PER_WIKI)->setQuery($query)->setPage(1)->setRequestedFields($requestedFields)->setDirectLuceneQuery(true)->setWikiId($wikiId);
         $resultSet = (new Factory())->getFromConfig($searchConfig)->search();
         $currentResults = $resultSet->toArray($requestedFields);
         $articles = [];
         foreach ($currentResults as $article) {
             $articles[$article['pageid']] = $this->processArticle($article);
             if (sizeof($articles) >= self::TOP_ARTICLES_PER_WIKI) {
                 break;
             }
         }
         $result = [];
         foreach ($topArticlesMap as $id => $a) {
             if (isset($articles[$id])) {
                 $result[] = $articles[$id];
             }
         }
         $timer->stop();
         return $result;
     });
 }
 /**
  *
  * Returns Categories under a given Tag
  *
  * @param $content
  * @param $requestTag
  *
  * @responseReturn Array|false Categories or false if tag was not found
  */
 private function getTagCategories($content, $requestTag)
 {
     wfProfileIn(__METHOD__);
     $ret = false;
     foreach ($content as $tag) {
         if ($requestTag == $tag['title']) {
             $ret = $tag['categories'];
         }
     }
     if (!empty($ret)) {
         $sort = $this->request->getVal('sort');
         if (!empty($sort)) {
             if ($sort == 'alpha') {
                 usort($ret, function ($a, $b) {
                     return strcasecmp($a['title'], $b['title']);
                 });
             } else {
                 if ($sort == 'hot') {
                     $hot = array_keys(DataMartService::getTopArticlesByPageview($this->wg->CityId, array_reduce($ret, function ($ret, $item) {
                         $ret[] = $item['id'];
                         return $ret;
                     }), null, false, count($ret)));
                     $sorted = [];
                     $left = [];
                     foreach ($ret as $value) {
                         $key = array_search($value['id'], $hot);
                         if ($key === false) {
                             $left[] = $value;
                         } else {
                             $sorted[$key] = $value;
                         }
                     }
                     ksort($sorted);
                     $ret = array_merge($sorted, $left);
                 } else {
                     wfProfileOut(__METHOD__);
                     throw new InvalidParameterApiException('sort');
                 }
             }
         }
         //Use 'id' instead of image_id
         foreach ($ret as &$value) {
             if (!empty($value['image_id'])) {
                 $value['id'] = $value['image_id'];
             }
             unset($value['image_id']);
         }
         $this->response->setVal('items', $ret);
     } else {
         if ($requestTag !== '') {
             wfProfileOut(__METHOD__);
             throw new InvalidParameterApiException('tag');
         }
     }
     wfProfileOut(__METHOD__);
 }
Beispiel #5
0
 /**
  * Return array of most visited articles
  * Author: Inez Korczynski (inez at wikia.com)
  * Author: Adrian 'ADi' Wieczorek (adi(at)wikia.com)
  * @return array
  */
 public static final function GetMostVisitedArticles($limit = 7, $ns = array(NS_MAIN), $fillUpMostPopular = true)
 {
     wfProfileIn(__METHOD__);
     global $wgCityId;
     $results = array();
     $data = DataMartService::getTopArticlesByPageview($wgCityId, null, $ns, false, 2 * $limit);
     if (!empty($data)) {
         $mainPage = Title::newMainPage();
         foreach ($data as $article_id => $row) {
             $title = Title::newFromID($article_id);
             if (is_object($title)) {
                 if (!$mainPage->equals($title)) {
                     $article = array('url' => $title->getLocalUrl(), 'text' => $title->getPrefixedText(), 'count' => $row['pageviews']);
                     $results[] = $article;
                 }
             }
         }
         self::removeAdultPages($results);
         if (!empty($results)) {
             $results = array_slice($results, 0, $limit);
         }
     }
     wfProfileOut(__METHOD__);
     return $results;
 }
 /**
  * @param string $sCategoryDBKey
  * @param array $mNamespace
  * @param bool $limit
  * @param bool $negative
  * @return array
  */
 public static function getMostVisited($sCategoryDBKey, $mNamespace = null, $limit = false, $negative = false)
 {
     global $wgCityId, $wgStatsDBEnabled;
     wfProfileIn(__METHOD__);
     if (empty($wgStatsDBEnabled)) {
         Wikia::log(__METHOD__, ' Stats DB is disabled');
         wfProfileOut(__METHOD__);
         return array();
     }
     $where = array('cl_to' => $sCategoryDBKey);
     if (!empty($mNamespace)) {
         $where[] = 'page_namespace ' . ($negative ? 'NOT ' : '') . 'IN(' . implode(',', $mNamespace) . ')';
     }
     $dbr = wfGetDB(DB_SLAVE);
     $res = $dbr->select(array('page', 'categorylinks'), array('page_id', 'cl_to'), $where, __METHOD__, array('ORDER BY' => 'page_title'), array('categorylinks' => array('INNER JOIN', 'cl_from = page_id')));
     if ($dbr->numRows($res) > 0) {
         Wikia::log(__METHOD__, ' Found some data in categories. Proceeding ');
         $aCategoryArticles = self::tableFromResult($res);
         $catKeys = array_keys($aCategoryArticles);
         Wikia::log(__METHOD__, ' Searching for prepared data');
         //fix BugId:33086
         //use DataMart for pageviews data
         //as all the other data sources are disabled/obsolete
         $items = DataMartService::getTopArticlesByPageview($wgCityId, $catKeys, $mNamespace, $negative, count($catKeys));
         if (is_array($items) && count($items) > 0) {
             Wikia::log(__METHOD__, ' Found some data. Lets find a commmon part with categories ');
             $aResult = array();
             $aResultCount = 0;
             $keys = array();
             $reversedCatKeys = array_flip($catKeys);
             $time = microtime(true);
             foreach ($items as $id => $pv) {
                 $key = intval($id);
                 $keys[$key] = $key;
             }
             foreach ($keys as $key) {
                 if (isset($reversedCatKeys[$key])) {
                     $aResultCount++;
                     unset($aCategoryArticles[$key]);
                     $aResult[$key] = array('page_id' => $key);
                     if (!empty($limit) && $aResultCount >= $limit) {
                         self::logProcessingTime($time);
                         wfProfileOut(__METHOD__);
                         return $aResult;
                     }
                 }
             }
             self::logProcessingTime($time);
             $ret = !empty($aResult) ? $aResult + $aCategoryArticles : $aCategoryArticles;
             if (!empty($limit) && count($ret) > $limit) {
                 $ret = array_slice($ret, 0, $limit, true);
             }
             wfProfileOut(__METHOD__);
             return $ret;
         } else {
             Wikia::log(__METHOD__, 'No data at all. Quitting.');
             wfProfileOut(__METHOD__);
             return array();
         }
     } else {
         Wikia::log(__METHOD__, ' No articles in category found - quitting');
         wfProfileOut(__METHOD__);
         return array();
     }
 }
 public function getTopWikiArticles()
 {
     if ($this->wikiId == 3125 && $this->app->wg->develEnvironment) {
         // mock data for devbox
         return array(555118, 515985, 1000000001, 8917, 538188, 555190, 557683, 462105, 558329, 558347, 352876, 508820, 558551, 558346, 522515, 558349, 508610, 508604, 464461, 9947, 499151, 11095, 558620, 508883, 16585, 494944, 555346, 28027, 527672, 12581, 542097, 508274, 558004, 557802, 478287, 7396, 330385, 542128, 510727, 347365, 540972, 541716, 2793, 558453, 7582, 12260, 540187, 539434, 66998, 510708, 515860, 557859, 12655, 231308, 8241, 383993, 12645, 386142, 542154, 1000000005, 295154, 558059, 508034, 508121, 525634, 11092, 516886, 11420, 1881, 3126, 11945, 102675, 346620, 265165, 526114, 340164, 557784, 463607, 13012, 141183, 539437, 557807, 2051, 558103, 197844, 2502, 2519, 542106, 555347, 87058, 555348, 539432, 557785, 527578, 305450, 539436, 499717, 479686, 336449, 514548, 11021, 246281, 526287, 11093, 463600, 495341, 103097, 542052, 540180, 10996, 7991, 555349, 10391, 558466, 71619, 14889, 17924, 555350, 1000000002, 8740, 273630, 344941, 522500, 11094, 1000000000, 526588, 558436, 197894, 10608, 501630, 508541, 526338, 16527, 2022, 13134, 545200, 197863, 265238, 558060, 508401, 463459, 13250, 557655, 330668, 273256, 330653, 508421, 220998, 2345, 309380, 522504, 557808, 542303, 11131, 10606, 490365, 71603, 231298, 481029, 547547, 276156, 161089, 502996, 396534, 540703, 3369, 476459, 508286, 273550, 490298, 548270, 492123, 0, 277937, 158170, 490297, 519330, 554509, 508694, 503268, 526985, 1797, 3681, 492134, 469776, 508792, 499138, 16502, 482416, 542100, 198083, 7487, 11011, 548103, 464776, 17576, 383534, 529481, 231726, 520467);
     }
     $articles = DataMartService::getTopArticlesByPageview($this->wikiId, null, null, null, self::MAX_ELEM_IN_RANK);
     $articleId = array();
     foreach ($articles as $id => $a) {
         $articleId[] = $id;
     }
     return $articleId;
 }