{"action":"created","comment":{"url":"https://api.github.com/repos/Wikia/app/pulls/comments/29574846","id":29574846,"diff_hunk":"@@ -37,17 +52,132 @@ public function getData() {\n \t *\n \t * @return array\n \t */\n-\tpublic function getContent() {\n+\tpublic function getContent( $params ) {\n \t\t$this->queryPageInstance = $this->getDataProvider();\n \n+\t\t/**\n+\t\t * 1. Prepare data of articles - title, last revision, link etc.\n+\t\t */\n+\t\t$articlesData = $this->fetchArticlesData();\n+\n+\t\t/**\n+\t\t * 2. Slice a sorting table to retrieve a page\n+\t\t */\n+\t\t$this->prepareParams( $params );\n+\t\tif ( !isset( $this->sortingArray ) ) {\n+\t\t\t$this->sortingArray = array_keys( $articlesData );\n+\t\t}\n+\t\t$ids = array_slice( $this->sortingArray, $this->offset, $this->limit, true );\n+\n+\t\t/**\n+\t\t * 3. Populate $content array with data for each article id\n+\t\t */\n \t\t$content = [];\n-\t\t$res = $this->queryPageInstance->doQuery( $this->offset, $this->limit );\n-\t\tif ( $res->numRows() > 0 ) {\n-\t\t\t$content = $this->prepareData( $res );\n+\t\tforeach ( $ids as $id ) {\n+\t\t\t$content[] = $articlesData[$id];\n \t\t}\n \t\treturn $content;\n \t}\n \n+\tprivate function prepareParams( $params ) {\n+\t\tglobal $wgMemc;\n+\n+\t\tif ( isset( $params['sort'] ) && isset( $this->sorting[ $params['sort'] ] ) ) {\n+\t\t\t$this->sortingArray = $wgMemc->get( wfSharedMemcKey( $this->getInsightType(), $params['sort'] ) );\n+\t\t}\n+\n+\t\tif ( isset( $params['offset'] ) ) {\n+\t\t\t$this->offset = intval( $params['offset'] );\n+\t\t}\n+\n+\t\tif ( isset( $params['limit'] ) ) {\n+\t\t\t$this->limit = intval( $params['limit'] );\n+\t\t}\n+\t}\n+\n+\tpublic function fetchArticlesData() {\n+\t\t$cacheKey = wfSharedMemcKey( $this->getInsightType(), 'articlesData' );\n+\t\t$this->cacheTtl = WikiaResponse::CACHE_STANDARD * 3;\n+\t\t$articlesData = WikiaDataAccess::cache( $cacheKey, $this->cacheTtl, function () {\n+\t\t\t$res = $this->queryPageInstance->doQuery();\n+\n+\t\t\tif ( $res->numRows() > 0 ) {\n+\t\t\t\t$articlesData = $this->prepareData( $res );\n+\t\t\t}\n+\n+\t\t\tif ( $this->arePageViewsRequired() ) {\n+\t\t\t\t$articlesIds = array_keys( $articlesData );\n+\t\t\t\t$pageViewsData = $this->getPageViewsData( $articlesIds );\n+\t\t\t\t$articlesData = $this->assignPageViewsData( $articlesData, $pageViewsData );\n+\t\t\t}\n+\n+\t\t\treturn $articlesData;\n+\t\t} );\n+\n+\t\treturn $articlesData;\n+\t}\n+\n+\tpublic function getPageViewsData( $articlesIds ) {\n+\t\t/**\n+\t\t * Get pv for the last 4 Sundays\n+\t\t */\n+\t\t$pvTimes = InsightsHelper::getLastFourTimeIds();\n+\n+\t\t$pvData = [];\n+\n+\t\tforeach( $pvTimes as $timeId ) {\n+\t\t\t$pvData[] = DataMartService::getPageViewsForArticles( $articlesIds, $timeId );\n+\t\t}\n+\n+\t\treturn $pvData;\n+\t}\n+\n+\tpublic function createSortingArray( $sortingArray, $key ) {\n+\t\tglobal $wgMemc;\n+\n+\t\tarsort( $sortingArray, $this->sorting[ $key ] );\n+\t\t$cacheKey = wfSharedMemcKey( $this->getInsightType(), $key );\n+\n+\t\t$wgMemc->set( $cacheKey, array_keys( $sortingArray ), $this->cacheTtl );\n+\t}\n+\n+\tpublic function assignPageViewsData( $articlesData, $pageViewsData ) {\n+\t\t$sortingData = [];\n+\n+\t\tforeach ( $articlesData as $articleId => $data ) {\n+\t\t\t$pv = [\n+\t\t\t\tintval( $pageViewsData[0][ $articleId ] ),\n+\t\t\t\tintval( $pageViewsData[1][ $articleId ] ),\n+\t\t\t\tintval( $pageViewsData[2][ $articleId ] ),\n+\t\t\t\tintval( $pageViewsData[3][ $articleId ] ),","path":"extensions/wikia/Insights/models/InsightsQuerypageModel.php","position":150,"original_position":150,"commit_id":"326a34b4a381aa68eb38e5a2070cf9d21105909f","original_commit_id":"52e6d881c11712397cd5619ca63c9c647c26da47","user":{"login":"lukaszkonieczny","id":3470655,"avatar_url":"https://avatars.githubusercontent.com/u/3470655?v=3","gravatar_id":"","url":"https://api.github.com/users/lukaszkonieczny","html_url":"https://github.com/lukaszkonieczny","followers_url":"https://api.github.com/users/lukaszkonieczny/followers","following_url":"https://api.github.com/users/lukaszkonieczny/following{/other_user}","gists_url":"https://api.github.com/users/lukaszkonieczny/gists{/gist_id}","starred_url":"https://api.github.com/users/lukaszkonieczny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukaszkonieczny/subscriptions","organizations_url":"https://api.github.com/users/lukaszkonieczny/orgs","repos_url":"https://api.github.com/users/lukaszkonieczny/repos","events_url":"https://api.github.com/users/lukaszkonieczny/events{/privacy}","received_events_url":"https://api.github.com/users/lukaszkonieczny/received_events","type":"User","site_admin":false},"body":"If there is no entry in db, ```$pageViewsData[X][ $articleId ]``` is not set so PHP notice ```undefined offset``` will be logged.","created_at":"2015-05-04T09:45:51Z","updated_at":"2015-05-04T09:45:51Z","html_url":"https://github.com/Wikia/app/pull/7072#discussion_r29574846","pull_request_url":"https://api.github.com/repos/Wikia/app/pulls/7072","_links":{"self":{"href":"https://api.github.com/repos/Wikia/app/pulls/comments/29574846"},"html":{"href":"https://github.com/Wikia/app/pull/7072#discussion_r29574846"},"pull_request":{"href":"https://api.github.com/repos/Wikia/app/pulls/7072"}}},"pull_request":{"url":"https://api.github.com/repos/Wikia/app/pulls/7072","id":34432804,"html_url":"https://github.com/Wikia/app/pull/7072","diff_url":"https://github.com/Wikia/app/pull/7072.diff","patch_url":"https://github.com/Wikia/app/pull/7072.patch","issue_url":"https://api.github.com/repos/Wikia/app/issues/7072","number":7072,"state":"open","locked":false,"title":"CE-1683 Page views display implementation","user":{"login":"adamkarminski","id":5011775,"avatar_url":"https://avatars.githubusercontent.com/u/5011775?v=3","gravatar_id":"","url":"https://api.github.com/users/adamkarminski","html_url":"https://github.com/adamkarminski","followers_url":"https://api.github.com/users/adamkarminski/followers","following_url":"https://api.github.com/users/adamkarminski/following{/other_user}","gists_url":"https://api.github.com/users/adamkarminski/gists{/gist_id}","starred_url":"https://api.github.com/users/adamkarminski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamkarminski/subscriptions","organizations_url":"https://api.github.com/users/adamkarminski/orgs","repos_url":"https://api.github.com/users/adamkarminski/repos","events_url":"https://api.github.com/users/adamkarminski/events{/privacy}","received_events_url":"https://api.github.com/users/adamkarminski/received_events","type":"User","site_admin":false},"body":"This code implements displaying number of views of pages in the Special:Insights feature. It is also a backend infrastructure for sorting and pagination.\r\n\r\n@Wikia/community-engineering ","created_at":"2015-04-30T06:53:31Z","updated_at":"2015-05-04T09:45:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e0683f0d9e382e752dec12c28bd926bc30aab204","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Wikia/app/pulls/7072/commits","review_comments_url":"https://api.github.com/repos/Wikia/app/pulls/7072/comments","review_comment_url":"https://api.github.com/repos/Wikia/app/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Wikia/app/issues/7072/comments","statuses_url":"https://api.github.com/repos/Wikia/app/statuses/326a34b4a381aa68eb38e5a2070cf9d21105909f","head":{"label":"Wikia:CE-1682-fetch-pageviews","ref":"CE-1682-fetch-pageviews","sha":"326a34b4a381aa68eb38e5a2070cf9d21105909f","user":{"login":"Wikia","id":1171011,"avatar_url":"https://avatars.githubusercontent.com/u/1171011?v=3","gravatar_id":"","url":"https://api.github.com/users/Wikia","html_url":"https://github.com/Wikia","followers_url":"https://api.github.com/users/Wikia/followers","following_url":"https://api.github.com/users/Wikia/following{/other_user}","gists_url":"https://api.github.com/users/Wikia/gists{/gist_id}","starred_url":"https://api.github.com/users/Wikia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wikia/subscriptions","organizations_url":"https://api.github.com/users/Wikia/orgs","repos_url":"https://api.github.com/users/Wikia/repos","events_url":"https://api.github.com/users/Wikia/events{/privacy}","received_events_url":"https://api.github.com/users/Wikia/received_events","type":"Organization","site_admin":false},"repo":{"id":5532313,"name":"app","full_name":"Wikia/app","owner":{"login":"Wikia","id":1171011,"avatar_url":"https://avatars.githubusercontent.com/u/1171011?v=3","gravatar_id":"","url":"https://api.github.com/users/Wikia","html_url":"https://github.com/Wikia","followers_url":"https://api.github.com/users/Wikia/followers","following_url":"https://api.github.com/users/Wikia/following{/other_user}","gists_url":"https://api.github.com/users/Wikia/gists{/gist_id}","starred_url":"https://api.github.com/users/Wikia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wikia/subscriptions","organizations_url":"https://api.github.com/users/Wikia/orgs","repos_url":"https://api.github.com/users/Wikia/repos","events_url":"https://api.github.com/users/Wikia/events{/privacy}","received_events_url":"https://api.github.com/users/Wikia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Wikia/app","description":"Wikia Application Code","fork":false,"url":"https://api.github.com/repos/Wikia/app","forks_url":"https://api.github.com/repos/Wikia/app/forks","keys_url":"https://api.github.com/repos/Wikia/app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Wikia/app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Wikia/app/teams","hooks_url":"https://api.github.com/repos/Wikia/app/hooks","issue_events_url":"https://api.github.com/repos/Wikia/app/issues/events{/number}","events_url":"https://api.github.com/repos/Wikia/app/events","assignees_url":"https://api.github.com/repos/Wikia/app/assignees{/user}","branches_url":"https://api.github.com/repos/Wikia/app/branches{/branch}","tags_url":"https://api.github.com/repos/Wikia/app/tags","blobs_url":"https://api.github.com/repos/Wikia/app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Wikia/app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Wikia/app/git/refs{/sha}","trees_url":"https://api.github.com/repos/Wikia/app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Wikia/app/statuses/{sha}","languages_url":"https://api.github.com/repos/Wikia/app/languages","stargazers_url":"https://api.github.com/repos/Wikia/app/stargazers","contributors_url":"https://api.github.com/repos/Wikia/app/contributors","subscribers_url":"https://api.github.com/repos/Wikia/app/subscribers","subscription_url":"https://api.github.com/repos/Wikia/app/subscription","commits_url":"https://api.github.com/repos/Wikia/app/commits{/sha}","git_commits_url":"https://api.github.com/repos/Wikia/app/git/commits{/sha}","comments_url":"https://api.github.com/repos/Wikia/app/comments{/number}","issue_comment_url":"https://api.github.com/repos/Wikia/app/issues/comments{/number}","contents_url":"https://api.github.com/repos/Wikia/app/contents/{+path}","compare_url":"https://api.github.com/repos/Wikia/app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Wikia/app/merges","archive_url":"https://api.github.com/repos/Wikia/app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Wikia/app/downloads","issues_url":"https://api.github.com/repos/Wikia/app/issues{/number}","pulls_url":"https://api.github.com/repos/Wikia/app/pulls{/number}","milestones_url":"https://api.github.com/repos/Wikia/app/milestones{/number}","notifications_url":"https://api.github.com/repos/Wikia/app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Wikia/app/labels{/name}","releases_url":"https://api.github.com/repos/Wikia/app/releases{/id}","created_at":"2012-08-23T20:55:58Z","updated_at":"2015-05-04T09:02:58Z","pushed_at":"2015-05-04T09:19:16Z","git_url":"git://github.com/Wikia/app.git","ssh_url":"git@github.com:Wikia/app.git","clone_url":"https://github.com/Wikia/app.git","svn_url":"https://github.com/Wikia/app","homepage":"http://dev.wikia.com","size":1396085,"stargazers_count":77,"watchers_count":77,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":57,"mirror_url":null,"open_issues_count":62,"forks":57,"open_issues":62,"watchers":77,"default_branch":"dev"}},"base":{"label":"Wikia:dev","ref":"dev","sha":"27cac4a9387c2736b7561f181d90d077d3fd367b","user":{"login":"Wikia","id":1171011,"avatar_url":"https://avatars.githubusercontent.com/u/1171011?v=3","gravatar_id":"","url":"https://api.github.com/users/Wikia","html_url":"https://github.com/Wikia","followers_url":"https://api.github.com/users/Wikia/followers","following_url":"https://api.github.com/users/Wikia/following{/other_user}","gists_url":"https://api.github.com/users/Wikia/gists{/gist_id}","starred_url":"https://api.github.com/users/Wikia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wikia/subscriptions","organizations_url":"https://api.github.com/users/Wikia/orgs","repos_url":"https://api.github.com/users/Wikia/repos","events_url":"https://api.github.com/users/Wikia/events{/privacy}","received_events_url":"https://api.github.com/users/Wikia/received_events","type":"Organization","site_admin":false},"repo":{"id":5532313,"name":"app","full_name":"Wikia/app","owner":{"login":"Wikia","id":1171011,"avatar_url":"https://avatars.githubusercontent.com/u/1171011?v=3","gravatar_id":"","url":"https://api.github.com/users/Wikia","html_url":"https://github.com/Wikia","followers_url":"https://api.github.com/users/Wikia/followers","following_url":"https://api.github.com/users/Wikia/following{/other_user}","gists_url":"https://api.github.com/users/Wikia/gists{/gist_id}","starred_url":"https://api.github.com/users/Wikia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wikia/subscriptions","organizations_url":"https://api.github.com/users/Wikia/orgs","repos_url":"https://api.github.com/users/Wikia/repos","events_url":"https://api.github.com/users/Wikia/events{/privacy}","received_events_url":"https://api.github.com/users/Wikia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Wikia/app","description":"Wikia Application Code","fork":false,"url":"https://api.github.com/repos/Wikia/app","forks_url":"https://api.github.com/repos/Wikia/app/forks","keys_url":"https://api.github.com/repos/Wikia/app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Wikia/app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Wikia/app/teams","hooks_url":"https://api.github.com/repos/Wikia/app/hooks","issue_events_url":"https://api.github.com/repos/Wikia/app/issues/events{/number}","events_url":"https://api.github.com/repos/Wikia/app/events","assignees_url":"https://api.github.com/repos/Wikia/app/assignees{/user}","branches_url":"https://api.github.com/repos/Wikia/app/branches{/branch}","tags_url":"https://api.github.com/repos/Wikia/app/tags","blobs_url":"https://api.github.com/repos/Wikia/app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Wikia/app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Wikia/app/git/refs{/sha}","trees_url":"https://api.github.com/repos/Wikia/app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Wikia/app/statuses/{sha}","languages_url":"https://api.github.com/repos/Wikia/app/languages","stargazers_url":"https://api.github.com/repos/Wikia/app/stargazers","contributors_url":"https://api.github.com/repos/Wikia/app/contributors","subscribers_url":"https://api.github.com/repos/Wikia/app/subscribers","subscription_url":"https://api.github.com/repos/Wikia/app/subscription","commits_url":"https://api.github.com/repos/Wikia/app/commits{/sha}","git_commits_url":"https://api.github.com/repos/Wikia/app/git/commits{/sha}","comments_url":"https://api.github.com/repos/Wikia/app/comments{/number}","issue_comment_url":"https://api.github.com/repos/Wikia/app/issues/comments{/number}","contents_url":"https://api.github.com/repos/Wikia/app/contents/{+path}","compare_url":"https://api.github.com/repos/Wikia/app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Wikia/app/merges","archive_url":"https://api.github.com/repos/Wikia/app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Wikia/app/downloads","issues_url":"https://api.github.com/repos/Wikia/app/issues{/number}","pulls_url":"https://api.github.com/repos/Wikia/app/pulls{/number}","milestones_url":"https://api.github.com/repos/Wikia/app/milestones{/number}","notifications_url":"https://api.github.com/repos/Wikia/app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Wikia/app/labels{/name}","releases_url":"https://api.github.com/repos/Wikia/app/releases{/id}","created_at":"2012-08-23T20:55:58Z","updated_at":"2015-05-04T09:02:58Z","pushed_at":"2015-05-04T09:19:16Z","git_url":"git://github.com/Wikia/app.git","ssh_url":"git@github.com:Wikia/app.git","clone_url":"https://github.com/Wikia/app.git","svn_url":"https://github.com/Wikia/app","homepage":"http://dev.wikia.com","size":1396085,"stargazers_count":77,"watchers_count":77,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":57,"mirror_url":null,"open_issues_count":62,"forks":57,"open_issues":62,"watchers":77,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/Wikia/app/pulls/7072"},"html":{"href":"https://github.com/Wikia/app/pull/7072"},"issue":{"href":"https://api.github.com/repos/Wikia/app/issues/7072"},"comments":{"href":"https://api.github.com/repos/Wikia/app/issues/7072/comments"},"review_comments":{"href":"https://api.github.com/repos/Wikia/app/pulls/7072/comments"},"review_comment":{"href":"https://api.github.com/repos/Wikia/app/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Wikia/app/pulls/7072/commits"},"statuses":{"href":"https://api.github.com/repos/Wikia/app/statuses/326a34b4a381aa68eb38e5a2070cf9d21105909f"}}}}