CINXE.COM

User:Dsimic/Traffic stats calculation - Wikipedia

<!DOCTYPE html> <html class="client-nojs skin-theme-clientpref-day mf-expand-sections-clientpref-0 mf-font-size-clientpref-small mw-mf-amc-clientpref-0" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>User:Dsimic/Traffic stats calculation - Wikipedia</title> <script>(function(){var className="client-js skin-theme-clientpref-day mf-expand-sections-clientpref-0 mf-font-size-clientpref-small mw-mf-amc-clientpref-0";var cookie=document.cookie.match(/(?:^|; )enwikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"a4a80de3-d3c1-4efc-a501-699c6e6a81f1","wgCanonicalNamespace":"User","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":2,"wgPageName":"User:Dsimic/Traffic_stats_calculation","wgTitle":"Dsimic/Traffic stats calculation","wgCurRevisionId":1077564765, "wgRevisionId":1077564765,"wgArticleId":45421324,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgPageViewLanguage":"en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"User:Dsimic/Traffic_stats_calculation","wgRelevantArticleId":45421324,"wgIsProbablyEditable":true,"wgRelevantPageIsProbablyEditable":true,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgRelevantUserName":"Dsimic","wgNoticeProject":"wikipedia","wgCiteReferencePreviewsActive":false,"wgFlaggedRevsParams":{"tags":{"status":{"levels":1}}},"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":true,"wgPopupsFlags":0,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","pageVariantFallbacks":"en"},"wgMFMode":"stable","wgMFAmc":false,"wgMFAmcOutreachActive":false,"wgMFAmcOutreachUserEligible":false,"wgMFLazyLoadImages":true,"wgMFEditNoticesFeatureConflict":false,"wgMFDisplayWikibaseDescriptions":{"search":true,"watchlist": true,"tagline":false,"nearby":true},"wgMFIsSupportedEditRequest":true,"wgMFScriptPath":"","wgWMESchemaEditAttemptStepOversample":false,"wgWMEPageLength":20000,"wgRelatedArticlesCompat":[],"wgEditSubmitButtonLabelPublish":true,"wgSectionTranslationTargetLanguages":["ace","ady","alt","am","ami","an","ang","ann","anp","ar","ary","arz","as","ast","av","avk","awa","ay","az","azb","ba","ban","bar","bbc","bcl","bdr","be","bew","bg","bho","bi","bjn","blk","bm","bn","bo","bpy","br","bs","btm","bug","ca","cdo","ce","ceb","ch","chr","ckb","co","cr","crh","cs","cu","cy","da","dag","de","dga","din","diq","dsb","dtp","dv","dz","ee","el","eml","eo","es","et","eu","fa","fat","ff","fi","fj","fo","fon","fr","frp","frr","fur","fy","gag","gan","gcr","gl","glk","gn","gom","gor","gpe","gu","guc","gur","guw","gv","ha","hak","haw","he","hi","hif","hr","hsb","ht","hu","hy","hyw","ia","iba","ie","ig","igl","ilo","io","is","it","iu","ja","jam","jv","ka","kaa","kab","kbd","kbp","kcg","kg","kge","ki","kk","kl", "km","kn","ko","koi","krc","ks","ku","kus","kv","kw","ky","lad","lb","lez","lg","li","lij","lld","lmo","ln","lo","lt","ltg","lv","mad","mai","map-bms","mdf","mg","mhr","mi","min","mk","ml","mn","mni","mnw","mos","mr","mrj","ms","mt","mwl","my","myv","mzn","nah","nan","nap","nb","nds","nds-nl","ne","new","nia","nl","nn","nqo","nr","nso","ny","oc","om","or","os","pa","pag","pam","pap","pcd","pcm","pdc","pl","pms","pnb","ps","pt","pwn","qu","rm","rn","ro","rsk","rue","rup","rw","sa","sah","sat","sc","scn","sco","sd","se","sg","sgs","sh","shi","shn","si","sk","skr","sl","sm","smn","sn","so","sq","sr","srn","ss","st","stq","su","sv","sw","szl","ta","tay","tcy","tdd","te","tet","tg","th","ti","tk","tl","tly","tn","to","tpi","tr","trv","ts","tt","tum","tw","ty","tyv","udm","ur","uz","ve","vec","vep","vi","vls","vo","vro","wa","war","wo","wuu","xal","xh","xmf","yi","yo","yue","za","zgh","zh","zu"],"isLanguageSearcherCXEntrypointEnabled":false,"mintEntrypointLanguages":["ace","ast","azb","bcl", "bjn","bh","crh","ff","fon","ig","is","ki","ks","lmo","min","sat","ss","tn","vec"],"wgCheckUserClientHintsHeadersJsApi":["brands","architecture","bitness","fullVersionList","mobile","model","platform","platformVersion"],"GEHomepageSuggestedEditsEnableTopics":true,"wgGETopicsMatchModeEnabled":false,"wgGEStructuredTaskRejectionReasonTextInputEnabled":false,"wgGELevelingUpEnabledForUser":false,"wgMinervaPermissions":{"watchable":true,"watch":false},"wgMinervaFeatures":{"beta":false,"donate":true,"mobileOptionsLink":true,"categories":false,"pageIssues":true,"talkAtTop":true,"historyInPageActions":false,"overflowSubmenu":false,"tabsOnSpecials":true,"personalMenu":false,"mainMenuExpanded":false,"echo":true,"nightMode":true},"wgMinervaDownloadNamespaces":[0]};RLSTATE={"ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading","ext.pygments":"ready","skins.minerva.styles":"ready", "skins.minerva.content.styles.images":"ready","mediawiki.hlist":"ready","skins.minerva.codex.styles":"ready","skins.minerva.icons":"ready","skins.minerva.amc.styles":"ready","ext.wikimediamessages.styles":"ready","mobile.init.styles":"ready","ext.wikimediaBadges":"ready"};RLPAGEMODULES=["ext.pygments.view","site","mediawiki.page.ready","skins.minerva.scripts","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.gadget.switcher","ext.urlShortener.toolbar","ext.centralauth.centralautologin","ext.popups","mobile.init","ext.echo.centralauth","ext.eventLogging","ext.wikimediaEvents","ext.navigationTiming","ext.cx.eventlogging.campaigns","ext.cx.entrypoints.languagesearcher.init","mw.externalguidance.init","ext.checkUser.clientHints"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=en&amp;modules=ext.pygments%2CwikimediaBadges%7Cext.wikimediamessages.styles%7Cmediawiki.hlist%7Cmobile.init.styles%7Cskins.minerva.amc.styles%7Cskins.minerva.codex.styles%7Cskins.minerva.content.styles.images%7Cskins.minerva.icons%2Cstyles&amp;only=styles&amp;skin=minerva"> <script async="" src="/w/load.php?lang=en&amp;modules=startup&amp;only=scripts&amp;raw=1&amp;skin=minerva"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=minerva"> <meta name="generator" content="MediaWiki 1.44.0-wmf.6"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="noindex,follow,max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="theme-color" content="#eaecf0"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=0.25, maximum-scale=5.0"> <meta property="og:title" content="User:Dsimic/Traffic stats calculation - Wikipedia"> <meta property="og:type" content="website"> <link rel="manifest" href="/w/api.php?action=webapp-manifest"> <link rel="alternate" type="application/x-wiki" title="Edit this page" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=edit"> <link rel="apple-touch-icon" href="/static/apple-touch/wikipedia.png"> <link rel="icon" href="/static/favicon/wikipedia.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="Wikipedia (en)"> <link rel="EditURI" type="application/rsd+xml" href="//en.wikipedia.org/w/api.php?action=rsd"> <link rel="canonical" href="https://en.wikipedia.org/wiki/User:Dsimic/Traffic_stats_calculation"> <link rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en"> <link rel="dns-prefetch" href="//meta.wikimedia.org" /> <link rel="dns-prefetch" href="login.wikimedia.org"> </head> <body class="mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-2 ns-subject mw-editable page-User_Dsimic_Traffic_stats_calculation rootpage-User_Dsimic stable skin-minerva action-view skin--responsive mw-mf-amc-disabled mw-mf"><div id="mw-mf-viewport"> <div id="mw-mf-page-center"> <a class="mw-mf-page-center__mask" href="#"></a> <header class="header-container header-chrome"> <div class="minerva-header"> <nav class="navigation-drawer toggle-list view-border-box"> <input type="checkbox" id="main-menu-input" class="toggle-list__checkbox" role="button" aria-haspopup="true" aria-expanded="false" aria-labelledby="mw-mf-main-menu-button"> <label role="button" for="main-menu-input" id="mw-mf-main-menu-button" aria-hidden="true" data-event-name="ui.mainmenu" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet toggle-list__toggle"> <span class="minerva-icon minerva-icon--menu"></span> <span></span> </label> <div id="mw-mf-page-left" class="menu view-border-box"> <ul id="p-navigation" class="toggle-list__list"> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--home" href="/wiki/Main_Page" data-mw="interface"> <span class="minerva-icon minerva-icon--home"></span> <span class="toggle-list-item__label">Home</span> </a> </li> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--random" href="/wiki/Special:Random" data-mw="interface"> <span class="minerva-icon minerva-icon--die"></span> <span class="toggle-list-item__label">Random</span> </a> </li> <li class="toggle-list-item skin-minerva-list-item-jsonly"> <a class="toggle-list-item__anchor menu__item--nearby" href="/wiki/Special:Nearby" data-event-name="menu.nearby" data-mw="interface"> <span class="minerva-icon minerva-icon--mapPin"></span> <span class="toggle-list-item__label">Nearby</span> </a> </li> </ul> <ul id="p-personal" class="toggle-list__list"> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--login" href="/w/index.php?title=Special:UserLogin&amp;returnto=User%3ADsimic%2FTraffic+stats+calculation" data-event-name="menu.login" data-mw="interface"> <span class="minerva-icon minerva-icon--logIn"></span> <span class="toggle-list-item__label">Log in</span> </a> </li> </ul> <ul id="pt-preferences" class="toggle-list__list"> <li class="toggle-list-item skin-minerva-list-item-jsonly"> <a class="toggle-list-item__anchor menu__item--settings" href="/w/index.php?title=Special:MobileOptions&amp;returnto=User%3ADsimic%2FTraffic+stats+calculation" data-event-name="menu.settings" data-mw="interface"> <span class="minerva-icon minerva-icon--settings"></span> <span class="toggle-list-item__label">Settings</span> </a> </li> </ul> <ul id="p-donation" class="toggle-list__list"> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--donate" href="https://donate.wikimedia.org/?wmf_source=donate&amp;wmf_medium=sidebar&amp;wmf_campaign=en.wikipedia.org&amp;uselang=en&amp;wmf_key=minerva" data-event-name="menu.donate" data-mw="interface"> <span class="minerva-icon minerva-icon--heart"></span> <span class="toggle-list-item__label">Donate</span> </a> </li> </ul> <ul class="hlist"> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--about" href="/wiki/Wikipedia:About" data-mw="interface"> <span class="toggle-list-item__label">About Wikipedia</span> </a> </li> <li class="toggle-list-item "> <a class="toggle-list-item__anchor menu__item--disclaimers" href="/wiki/Wikipedia:General_disclaimer" data-mw="interface"> <span class="toggle-list-item__label">Disclaimers</span> </a> </li> </ul> </div> <label class="main-menu-mask" for="main-menu-input"></label> </nav> <div class="branding-box"> <a href="/wiki/Main_Page"> <span><img src="/static/images/mobile/copyright/wikipedia-wordmark-en.svg" alt="Wikipedia" width="120" height="18" style="width: 7.5em; height: 1.125em;"/> </span> </a> </div> <form action="/w/index.php" method="get" class="minerva-search-form"> <div class="search-box"> <input type="hidden" name="title" value="Special:Search"/> <input class="search skin-minerva-search-trigger" id="searchInput" type="search" name="search" placeholder="Search Wikipedia" aria-label="Search Wikipedia" autocapitalize="sentences" title="Search Wikipedia [f]" accesskey="f"> <span class="search-box-icon-overlay"><span class="minerva-icon minerva-icon--search"></span> </span> </div> <button id="searchIcon" class="cdx-button cdx-button--size-large cdx-button--icon-only cdx-button--weight-quiet skin-minerva-search-trigger"> <span class="minerva-icon minerva-icon--search"></span> <span>Search</span> </button> </form> <nav class="minerva-user-navigation" aria-label="User navigation"> </nav> </div> </header> <main id="content" class="mw-body"> <div class="banner-container"> <div id="siteNotice"></div> </div> <div class="pre-content heading-holder"> <div class="page-heading"> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-namespace">User</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Dsimic/Traffic stats calculation</span></h1> <div class="tagline"></div> </div> <ul id="p-associated-pages" class="minerva__tab-container"> <li class="minerva__tab selected"> <a class="minerva__tab-text" href="/wiki/User:Dsimic/Traffic_stats_calculation" rel="" data-event-name="tabs.subject">User page</a> </li> <li class="minerva__tab "> <a class="minerva__tab-text" href="/wiki/User_talk:Dsimic/Traffic_stats_calculation" rel="discussion" data-event-name="tabs.talk">Talk</a> </li> </ul> <nav class="page-actions-menu"> <ul id="p-views" class="page-actions-menu__list"> <li id="language-selector" class="page-actions-menu__list-item"> <a role="button" href="" data-mw="interface" data-event-name="menu.languages" title="Language" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet language-selector disabled"> <span class="minerva-icon minerva-icon--language"></span> <span>Language</span> </a> </li> <li id="page-actions-watch" class="page-actions-menu__list-item"> <a role="button" id="ca-watch" href="/w/index.php?title=Special:UserLogin&amp;returnto=User%3ADsimic%2FTraffic+stats+calculation" data-event-name="menu.watch" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet menu__item--page-actions-watch"> <span class="minerva-icon minerva-icon--star"></span> <span>Watch</span> </a> </li> <li id="page-actions-edit" class="page-actions-menu__list-item"> <a role="button" id="ca-edit" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=edit" data-event-name="menu.edit" data-mw="interface" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet edit-page menu__item--page-actions-edit"> <span class="minerva-icon minerva-icon--edit"></span> <span>Edit</span> </a> </li> </ul> </nav> <!-- version 1.0.2 (change every time you update a partial) --> <div id="mw-content-subtitle"><div class="subpages">&lt; <bdi dir="ltr"><a href="/wiki/User:Dsimic" title="User:Dsimic">User:Dsimic</a></bdi></div></div> </div> <div id="bodyContent" class="content"> <div id="mw-content-text" class="mw-body-content"><script>function mfTempOpenSection(id){var block=document.getElementById("mf-section-"+id);block.className+=" open-block";block.previousSibling.className+=" open-block";}</script><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><section class="mf-section-0" id="mf-section-0"><div id="toc" class="toc" role="navigation" aria-labelledby="mw-toc-heading"><input type="checkbox" role="button" id="toctogglecheckbox" class="toctogglecheckbox" style="display:none"><div class="toctitle" lang="en" dir="ltr"><h2 id="mw-toc-heading">Contents</h2><span class="toctogglespan"><label class="toctogglelabel" for="toctogglecheckbox"></label></span></div> <ul> <li class="toclevel-1 tocsection-1"><a href="#Automated_monthly_statistics_calculation"><span class="tocnumber">1</span> <span class="toctext">Automated monthly statistics calculation</span></a> <ul> <li class="toclevel-2 tocsection-2"><a href="#Source_code"><span class="tocnumber">1.1</span> <span class="toctext">Source code</span></a></li> <li class="toclevel-2 tocsection-3"><a href="#Output_example"><span class="tocnumber">1.2</span> <span class="toctext">Output example</span></a></li> </ul> </li> </ul> </div> </section><div class="mw-heading mw-heading2 section-heading" onclick="mfTempOpenSection(1)"><span class="indicator mf-icon mf-icon-expand mf-icon--small"></span><h2 id="Automated_monthly_statistics_calculation">Automated monthly statistics calculation</h2><span class="mw-editsection"> <a role="button" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=edit&amp;section=1" title="Edit section: Automated monthly statistics calculation" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet "> <span class="minerva-icon minerva-icon--edit"></span> <span>edit</span> </a> </span> </div><section class="mf-section-1 collapsible-block" id="mf-section-1"> <style data-mw-deduplicate="TemplateStyles:r1236090951">.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}</style><div role="note" class="hatnote navigation-not-searchable">See also: <a href="/wiki/User_talk:Dsimic#Wikiviewstats_utility" title="User talk:Dsimic">User talk:Dsimic § Wikiviewstats utility</a>, <a href="/wiki/User_talk:Dsimic#An_alternative_data_source_for_traffic_stats_calculation" title="User talk:Dsimic">User talk:Dsimic § An alternative data source for traffic stats calculation</a>, and <a href="/wiki/User:Dsimic#CREATED-ARTICLES" title="User:Dsimic">User:Dsimic § Articles I've created</a></div> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1236090951"><div role="note" class="hatnote navigation-not-searchable">Further information: <a href="/wiki/Wikipedia:Statistics" title="Wikipedia:Statistics">Wikipedia:Statistics</a>, <a href="/wiki/Wikipedia:VIEWSSTATS" class="mw-redirect" title="Wikipedia:VIEWSSTATS">Wikipedia:Statistics § Page views</a>, and <a href="/wiki/Wikipedia:Pageview_statistics" title="Wikipedia:Pageview statistics">Wikipedia:Pageview statistics</a></div> <p>Below is a rather simple <a href="/wiki/PHP" title="PHP">PHP</a> program that fetches monthly <a href="/wiki/Page_view" class="mw-redirect" title="Page view">page views</a> statistics provided by the <a href="https://wikitech.wikimedia.org/wiki/Analytics/PageviewAPI" class="extiw" title="wikitech:Analytics/PageviewAPI">Pageview API</a> in <a href="/wiki/JSON" title="JSON">JSON</a> format (that's a public <a href="/wiki/API" title="API">API</a> developed and maintained by the <a href="/wiki/Wikimedia_Foundation" title="Wikimedia Foundation">Wikimedia Foundation</a>, see also its detailed <a class="external text" href="https://wikimedia.org/api/rest_v1/?doc#!/Pageviews_data/get_metrics_pageviews_per_article_project_access_agent_article_granularity_start_end">REST API documentation</a>), for a specified list of articles, and calculates their total monthly views and average views per day. The fetched page views statistics don't include <a href="/wiki/Web_crawler" title="Web crawler">spider</a>- or <a href="/wiki/Internet_bot" title="Internet bot">bot</a>-generated traffic. The program is intended to be run interactively from a <a href="/wiki/Command-line_interface" title="Command-line interface">command-line interface</a> (CLI); instead of running it locally, on a machine capable of executing PHP <a href="/wiki/Scripting_language" title="Scripting language">scripts</a>, you may also use some of the freely available online PHP <a href="/wiki/Integrated_development_environment" title="Integrated development environment">development environments</a>. </p><p>Initially, this program used the page views statistics provided by <span class="url"><a rel="nofollow" class="external text" href="http://stats.grok.se/">stats<wbr></wbr>.grok<wbr></wbr>.se</a></span> in JSON format, but that <a href="/wiki/Web_service" title="Web service">web service</a> unfortunately became no longer updated around mid-January 2016, and it remains defunct as of June 2016<sup class="plainlinks noexcerpt noprint asof-tag update" style="display:none;"><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Dsimic/Traffic_stats_calculation&amp;action=edit">[update]</a></sup>. If needed, you can also have a look at that <a href="/wiki/Special:PermanentLink/723114012" title="Special:PermanentLink/723114012">older version</a> of the program code and documentation. </p><p>As pretty much everything else here on Wikipedia, I'm releasing this program code under the terms of the <a href="/wiki/Wikipedia:CC_BY-SA" class="mw-redirect" title="Wikipedia:CC BY-SA">CC BY-SA 3.0</a> license, so please feel free to use it and modify according to your needs. Of course, feel free to use <a href="/wiki/User_talk:Dsimic" title="User talk:Dsimic">my talk page</a> to <span class="noprint plainlinks" title="Edit User talk:Dsimic"><a class="external text" href="https://en.wikipedia.org/w/index.php?title=User_talk:Dsimic&amp;action=edit&amp;section=new">leave me a message</a></span> in case you have any questions, suggestions, bug reports, etc. </p> <div class="mw-heading mw-heading3"><h3 id="Source_code">Source code</h3><span class="mw-editsection"> <a role="button" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=edit&amp;section=2" title="Edit section: Source code" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet "> <span class="minerva-icon minerva-icon--edit"></span> <span>edit</span> </a> </span> </div> <p>Before running this program, you need to modify the list of articles contained in the <code>$articles</code> <a href="/wiki/Variable_(computer_science)" title="Variable (computer science)">variable</a> (what's in the code below is the list of articles <a class="external text" href="https://tools.wmflabs.org/sigma/created.py?name=Dsimic&amp;server=enwiki&amp;max=100&amp;ns=,,&amp;redirects=none">I've created</a> or started), and to modify the month and year for which statistics are to be fetched and calculated, which are specified through the <code>FETCH_MONTH</code> and <code>FETCH_YEAR</code> <a href="/wiki/Constant_(computer_programming)" title="Constant (computer programming)">constants</a>, respectively. When the program is configured to calculate statistics for the current month, it takes into account only the whole/elapsed days; as a result, running the program on the first day of the month to calculate current month statistics isn't supported. Also, in case whole days are missing in the statistics data available from the Pageview API, the program doesn't count in such zero-page-views days when calculating the averages. The <code>FETCH_PROJECT</code> constant selects the encyclopedia: <code>en.wikipedia.org</code> is for the <a href="/wiki/English_Wikipedia" title="English Wikipedia">English Wikipedia</a>, <code>de.wikipedia.org</code> is for the <a href="/wiki/German_Wikipedia" title="German Wikipedia">German Wikipedia</a>, etc. </p><p>Just as a note, getting ready-to-run PHP code of this program is as easy as viewing the <a href="/wiki/Wiki_code" class="mw-redirect" title="Wiki code">Wiki code</a> of this page and copying what's between the <code>&lt;syntaxhighlight lang="php" line&gt;</code> and <code>&lt;/syntaxhighlight&gt;</code> <a href="/wiki/HTML_tag" class="mw-redirect" title="HTML tag">tags</a>. The program code below is the latest available version, and it is updated on this page after any improvements or <a href="/wiki/Bugfix" class="mw-redirect" title="Bugfix">bugfixes</a> are implemented. </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span></span><span class="linenos" data-line="1"></span><span class="cp">&lt;?php</span> <span class="linenos" data-line="2"></span> <span class="linenos" data-line="3"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'FETCH_MONTH'</span><span class="p">,</span> <span class="s1">'01'</span><span class="p">);</span> <span class="c1">// MM</span> <span class="linenos" data-line="4"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'FETCH_YEAR'</span><span class="p">,</span> <span class="s1">'2016'</span><span class="p">);</span> <span class="c1">// YYYY</span> <span class="linenos" data-line="5"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'FETCH_PROJECT'</span><span class="p">,</span> <span class="s1">'en.wikipedia.org'</span><span class="p">);</span> <span class="c1">// "en.wikipedia.org", "de.wikipedia.org", etc.</span> <span class="linenos" data-line="6"></span> <span class="linenos" data-line="7"></span><span class="nv">$articles</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">'Stagefright (bug)'</span><span class="p">,</span> <span class="linenos" data-line="8"></span> <span class="s1">'Row hammer'</span><span class="p">,</span> <span class="linenos" data-line="9"></span> <span class="s1">'Address generation unit'</span><span class="p">,</span> <span class="linenos" data-line="10"></span> <span class="s1">'UniDIMM'</span><span class="p">,</span> <span class="linenos" data-line="11"></span> <span class="s1">'kdump (Linux)'</span><span class="p">,</span> <span class="linenos" data-line="12"></span> <span class="s1">'kernfs (BSD)'</span><span class="p">,</span> <span class="linenos" data-line="13"></span> <span class="s1">'kernfs (Linux)'</span><span class="p">,</span> <span class="linenos" data-line="14"></span> <span class="s1">'ftrace'</span><span class="p">,</span> <span class="linenos" data-line="15"></span> <span class="s1">'Android Runtime'</span><span class="p">,</span> <span class="linenos" data-line="16"></span> <span class="s1">'WebScaleSQL'</span><span class="p">,</span> <span class="linenos" data-line="17"></span> <span class="s1">'Intel X99'</span><span class="p">,</span> <span class="linenos" data-line="18"></span> <span class="s1">'HipHop Virtual Machine'</span><span class="p">,</span> <span class="linenos" data-line="19"></span> <span class="s1">'kpatch'</span><span class="p">,</span> <span class="linenos" data-line="20"></span> <span class="s1">'kGraft'</span><span class="p">,</span> <span class="linenos" data-line="21"></span> <span class="s1">'CoreOS'</span><span class="p">,</span> <span class="linenos" data-line="22"></span> <span class="s1">'ARM Cortex-A17'</span><span class="p">,</span> <span class="linenos" data-line="23"></span> <span class="s1">'Solid-state storage'</span><span class="p">,</span> <span class="linenos" data-line="24"></span> <span class="s1">'Port Control Protocol'</span><span class="p">,</span> <span class="linenos" data-line="25"></span> <span class="s1">'zswap'</span><span class="p">,</span> <span class="linenos" data-line="26"></span> <span class="s1">'Emdebian Grip'</span><span class="p">,</span> <span class="linenos" data-line="27"></span> <span class="s1">'ThinkPad 8'</span><span class="p">,</span> <span class="linenos" data-line="28"></span> <span class="s1">'Laravel'</span><span class="p">,</span> <span class="linenos" data-line="29"></span> <span class="s1">'OpenLMI'</span><span class="p">,</span> <span class="linenos" data-line="30"></span> <span class="s1">'Open vSwitch'</span><span class="p">,</span> <span class="linenos" data-line="31"></span> <span class="s1">'Distributed Overlay Virtual Ethernet'</span><span class="p">,</span> <span class="linenos" data-line="32"></span> <span class="s1">'Management Component Transport Protocol'</span><span class="p">,</span> <span class="linenos" data-line="33"></span> <span class="s1">'Buildroot'</span><span class="p">,</span> <span class="linenos" data-line="34"></span> <span class="s1">'dm-cache'</span><span class="p">,</span> <span class="linenos" data-line="35"></span> <span class="s1">'bcache'</span><span class="p">,</span> <span class="linenos" data-line="36"></span> <span class="s1">'SATA Express'</span><span class="p">,</span> <span class="linenos" data-line="37"></span> <span class="s1">'OpenZFS'</span><span class="p">,</span> <span class="linenos" data-line="38"></span> <span class="s1">'List of Eurocrem packages'</span><span class="p">,</span> <span class="linenos" data-line="39"></span> <span class="s1">'M.2'</span><span class="p">,</span> <span class="linenos" data-line="40"></span> <span class="s1">'Eurocrem'</span><span class="p">);</span> <span class="linenos" data-line="41"></span> <span class="linenos" data-line="42"></span><span class="c1">// ---------------------------------------------</span> <span class="linenos" data-line="43"></span><span class="c1">// obviously, configurable stuff ends here</span> <span class="linenos" data-line="44"></span><span class="c1">// ---------------------------------------------</span> <span class="linenos" data-line="45"></span> <span class="linenos" data-line="46"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'CHUNK_SIZE'</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span> <span class="c1">// articles, imposed by the Pageview API rate limit (see below)</span> <span class="linenos" data-line="47"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'CHUNK_SLEEP'</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">// seconds, also related to the API rate limit</span> <span class="linenos" data-line="48"></span> <span class="linenos" data-line="49"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'EXIT_SUCCESS'</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">// program exit codes</span> <span class="linenos" data-line="50"></span><span class="nb">define</span><span class="p">(</span><span class="s1">'EXIT_FAILURE'</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="linenos" data-line="51"></span> <span class="linenos" data-line="52"></span><span class="nb">set_time_limit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="linenos" data-line="53"></span><span class="nb">ini_set</span><span class="p">(</span><span class="s1">'memory_limit'</span><span class="p">,</span> <span class="mi">67108864</span><span class="p">);</span> <span class="linenos" data-line="54"></span><span class="nb">ini_set</span><span class="p">(</span><span class="s1">'default_socket_timeout'</span><span class="p">,</span> <span class="mi">90</span><span class="p">);</span> <span class="linenos" data-line="55"></span> <span class="linenos" data-line="56"></span><span class="c1">// a few short helper functions</span> <span class="linenos" data-line="57"></span> <span class="linenos" data-line="58"></span><span class="k">function</span> <span class="nf">plural_output</span><span class="p">(</span><span class="nv">$value</span><span class="p">,</span> <span class="nv">$unit</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="59"></span> <span class="k">return</span> <span class="p">(</span><span class="nb">number_format</span><span class="p">(</span><span class="nv">$value</span><span class="p">)</span> <span class="o">.</span> <span class="s2">" </span><span class="si">{</span><span class="nv">$unit</span><span class="si">}</span><span class="s2">"</span> <span class="o">.</span> <span class="p">((</span><span class="nb">abs</span><span class="p">(</span><span class="nv">$value</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">?</span> <span class="s1">'s'</span> <span class="o">:</span> <span class="s1">''</span><span class="p">));</span> <span class="linenos" data-line="60"></span><span class="p">}</span> <span class="linenos" data-line="61"></span> <span class="linenos" data-line="62"></span><span class="k">function</span> <span class="nf">progress_message</span><span class="p">(</span><span class="nv">$message</span> <span class="o">=</span> <span class="s1">'.'</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="63"></span> <span class="k">static</span> <span class="nv">$last_message</span> <span class="o">=</span> <span class="k">null</span><span class="p">;</span> <span class="linenos" data-line="64"></span> <span class="linenos" data-line="65"></span> <span class="nv">$now</span> <span class="o">=</span> <span class="nb">microtime</span><span class="p">(</span><span class="k">true</span><span class="p">);</span> <span class="linenos" data-line="66"></span> <span class="nv">$ret_val</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span> <span class="linenos" data-line="67"></span> <span class="linenos" data-line="68"></span> <span class="k">if</span> <span class="p">((</span><span class="nv">$last_message</span> <span class="o">===</span> <span class="k">null</span><span class="p">)</span> <span class="o">||</span> <span class="linenos" data-line="69"></span> <span class="p">((</span><span class="nv">$now</span> <span class="o">-</span> <span class="nv">$last_message</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.5</span><span class="p">))</span> <span class="p">{</span> <span class="c1">// one message every 0.5 seconds</span> <span class="linenos" data-line="70"></span> <span class="k">echo</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span> <span class="linenos" data-line="71"></span> <span class="linenos" data-line="72"></span> <span class="nv">$last_message</span> <span class="o">=</span> <span class="nv">$now</span><span class="p">;</span> <span class="linenos" data-line="73"></span> <span class="nv">$ret_val</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span> <span class="c1">// the message has been printed</span> <span class="linenos" data-line="74"></span> <span class="p">}</span> <span class="linenos" data-line="75"></span> <span class="linenos" data-line="76"></span> <span class="k">return</span> <span class="p">(</span><span class="nv">$ret_val</span><span class="p">);</span> <span class="linenos" data-line="77"></span><span class="p">}</span> <span class="linenos" data-line="78"></span> <span class="linenos" data-line="79"></span><span class="c1">// prepare the cURL handles for all articles</span> <span class="linenos" data-line="80"></span> <span class="linenos" data-line="81"></span><span class="k">echo</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Fetching statistics data: "</span><span class="p">);</span> <span class="linenos" data-line="82"></span> <span class="linenos" data-line="83"></span><span class="nv">$start_time</span> <span class="o">=</span> <span class="nb">microtime</span><span class="p">(</span><span class="k">true</span><span class="p">);</span> <span class="linenos" data-line="84"></span><span class="nv">$handles</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span> <span class="linenos" data-line="85"></span> <span class="linenos" data-line="86"></span><span class="nv">$articles_total</span> <span class="o">=</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$articles</span><span class="p">);</span> <span class="linenos" data-line="87"></span><span class="nv">$day_of_month</span> <span class="o">=</span> <span class="o">@</span><span class="nb">date</span><span class="p">(</span><span class="s1">'j'</span><span class="p">);</span> <span class="linenos" data-line="88"></span><span class="nv">$current_month</span> <span class="o">=</span> <span class="p">(</span><span class="nx">FETCH_MONTH</span> <span class="o">==</span> <span class="o">@</span><span class="nb">date</span><span class="p">(</span><span class="s1">'m'</span><span class="p">));</span> <span class="linenos" data-line="89"></span> <span class="linenos" data-line="90"></span><span class="k">if</span> <span class="p">(</span><span class="nv">$articles_total</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// a small sanity check</span> <span class="linenos" data-line="91"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"no articles specified!</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="92"></span> <span class="k">exit</span><span class="p">(</span><span class="nx">EXIT_FAILURE</span><span class="p">);</span> <span class="linenos" data-line="93"></span><span class="p">}</span> <span class="linenos" data-line="94"></span> <span class="linenos" data-line="95"></span><span class="k">if</span> <span class="p">(</span><span class="nv">$current_month</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nv">$day_of_month</span> <span class="o">==</span> <span class="mi">1</span><span class="p">))</span> <span class="p">{</span> <span class="c1">// account only the whole days, also knowing</span> <span class="linenos" data-line="96"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"no elapsed days in current month!</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="c1">// that the Pageview API rejects invalid dates</span> <span class="linenos" data-line="97"></span> <span class="k">exit</span><span class="p">(</span><span class="nx">EXIT_FAILURE</span><span class="p">);</span> <span class="linenos" data-line="98"></span><span class="p">}</span> <span class="linenos" data-line="99"></span> <span class="linenos" data-line="100"></span><span class="nv">$days_total</span> <span class="o">=</span> <span class="o">!</span><span class="nv">$current_month</span> <span class="linenos" data-line="101"></span> <span class="o">?</span> <span class="nb">cal_days_in_month</span><span class="p">(</span><span class="nx">CAL_GREGORIAN</span><span class="p">,</span> <span class="nx">FETCH_MONTH</span><span class="p">,</span> <span class="nx">FETCH_YEAR</span><span class="p">)</span> <span class="linenos" data-line="102"></span> <span class="o">:</span> <span class="p">(</span><span class="nv">$day_of_month</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span> <span class="linenos" data-line="103"></span> <span class="linenos" data-line="104"></span><span class="nv">$fetch_range</span> <span class="o">=</span> <span class="nx">FETCH_YEAR</span> <span class="o">.</span> <span class="nx">FETCH_MONTH</span> <span class="o">.</span> <span class="s1">'01/'</span> <span class="o">.</span> <span class="linenos" data-line="105"></span> <span class="nx">FETCH_YEAR</span> <span class="o">.</span> <span class="nx">FETCH_MONTH</span> <span class="o">.</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s1">'%02d'</span><span class="p">,</span> <span class="nv">$days_total</span><span class="p">);</span> <span class="linenos" data-line="106"></span> <span class="linenos" data-line="107"></span><span class="k">for</span> <span class="p">(</span><span class="nv">$id</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$id</span> <span class="o">&lt;</span> <span class="nv">$articles_total</span><span class="p">;</span> <span class="nv">$id</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="108"></span> <span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]</span> <span class="o">=</span> <span class="nb">curl_init</span><span class="p">();</span> <span class="linenos" data-line="109"></span> <span class="linenos" data-line="110"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_URL</span><span class="p">,</span> <span class="s1">'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/'</span> <span class="o">.</span> <span class="linenos" data-line="111"></span> <span class="nx">FETCH_PROJECT</span> <span class="o">.</span> <span class="s1">'/all-access/user/'</span> <span class="o">.</span> <span class="linenos" data-line="112"></span> <span class="nb">rawurlencode</span><span class="p">(</span><span class="nb">ucfirst</span><span class="p">(</span><span class="nv">$articles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]))</span> <span class="o">.</span> <span class="s2">"/daily/</span><span class="si">{</span><span class="nv">$fetch_range</span><span class="si">}</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="113"></span> <span class="linenos" data-line="114"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_HEADER</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span> <span class="linenos" data-line="115"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_RETURNTRANSFER</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="linenos" data-line="116"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_SSL_VERIFYPEER</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span> <span class="linenos" data-line="117"></span> <span class="linenos" data-line="118"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_CONNECTTIMEOUT</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span> <span class="linenos" data-line="119"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_TIMEOUT</span><span class="p">,</span> <span class="mi">60</span><span class="p">);</span> <span class="linenos" data-line="120"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_DNS_CACHE_TIMEOUT</span><span class="p">,</span> <span class="mi">3600</span><span class="p">);</span> <span class="linenos" data-line="121"></span> <span class="linenos" data-line="122"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_FORBID_REUSE</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span> <span class="linenos" data-line="123"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_FRESH_CONNECT</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span> <span class="linenos" data-line="124"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_MAXCONNECTS</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span> <span class="linenos" data-line="125"></span> <span class="linenos" data-line="126"></span> <span class="nb">curl_setopt</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">],</span> <span class="nx">CURLOPT_USERAGENT</span><span class="p">,</span> <span class="s1">'https://en.wikipedia.org/wiki/User_talk:Dsimic'</span><span class="p">);</span> <span class="linenos" data-line="127"></span><span class="p">}</span> <span class="linenos" data-line="128"></span> <span class="linenos" data-line="129"></span><span class="nx">progress_message</span><span class="p">();</span> <span class="linenos" data-line="130"></span> <span class="linenos" data-line="131"></span><span class="c1">// run the cURL handles in chunks because the Pageview API imposes a rate limit,</span> <span class="linenos" data-line="132"></span><span class="c1">// which, as of June 1, 2016, is specified at 10 requests per second, although</span> <span class="linenos" data-line="133"></span><span class="c1">// it seems to be happily handling *much* higher rates</span> <span class="linenos" data-line="134"></span> <span class="linenos" data-line="135"></span><span class="nv">$handle_all</span> <span class="o">=</span> <span class="nb">curl_multi_init</span><span class="p">();</span> <span class="linenos" data-line="136"></span><span class="nv">$chunks</span> <span class="o">=</span> <span class="nb">ceil</span><span class="p">(</span><span class="mf">1.0</span> <span class="o">*</span> <span class="nv">$articles_total</span> <span class="o">/</span> <span class="nx">CHUNK_SIZE</span><span class="p">);</span> <span class="linenos" data-line="137"></span><span class="nv">$output</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span> <span class="linenos" data-line="138"></span><span class="nv">$error_messages</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">'Parsing JSON data failed'</span> <span class="o">=&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="linenos" data-line="139"></span> <span class="linenos" data-line="140"></span><span class="nv">$views_total</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="linenos" data-line="141"></span><span class="nv">$failures</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="linenos" data-line="142"></span><span class="nv">$days_available</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span> <span class="linenos" data-line="143"></span><span class="nv">$php_version</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">'.'</span><span class="p">,</span> <span class="nb">phpversion</span><span class="p">(),</span> <span class="mi">3</span><span class="p">);</span> <span class="linenos" data-line="144"></span> <span class="linenos" data-line="145"></span><span class="k">if</span> <span class="p">((</span><span class="nv">$php_version</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="c1">// available since PHP 5.5.0</span> <span class="linenos" data-line="146"></span> <span class="p">(</span><span class="nv">$php_version</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">))</span> <span class="p">{</span> <span class="linenos" data-line="147"></span> <span class="nb">curl_multi_setopt</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">,</span> <span class="nx">CURLMOPT_PIPELINING</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="linenos" data-line="148"></span> <span class="nb">curl_multi_setopt</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">,</span> <span class="nx">CURLMOPT_MAXCONNECTS</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span> <span class="linenos" data-line="149"></span><span class="p">}</span> <span class="linenos" data-line="150"></span> <span class="linenos" data-line="151"></span><span class="k">for</span> <span class="p">(</span><span class="nv">$chunk</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$chunk</span> <span class="o">&lt;</span> <span class="nv">$chunks</span><span class="p">;</span> <span class="nv">$chunk</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// fetch one chunk at a time</span> <span class="linenos" data-line="152"></span> <span class="nv">$id_limit</span> <span class="o">=</span> <span class="nb">min</span><span class="p">((</span><span class="nv">$chunk</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="nx">CHUNK_SIZE</span><span class="p">,</span> <span class="nv">$articles_total</span><span class="p">);</span> <span class="linenos" data-line="153"></span> <span class="linenos" data-line="154"></span> <span class="k">for</span> <span class="p">(</span><span class="nv">$id</span> <span class="o">=</span> <span class="nv">$chunk</span> <span class="o">*</span> <span class="nx">CHUNK_SIZE</span><span class="p">;</span> <span class="nv">$id</span> <span class="o">&lt;</span> <span class="nv">$id_limit</span><span class="p">;</span> <span class="nv">$id</span><span class="o">++</span><span class="p">)</span> <span class="c1">// all articles in this chunk</span> <span class="linenos" data-line="155"></span> <span class="nb">curl_multi_add_handle</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">,</span> <span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]);</span> <span class="linenos" data-line="156"></span> <span class="linenos" data-line="157"></span> <span class="k">do</span> <span class="p">{</span> <span class="c1">// fetch the articles stats data in JSON format...</span> <span class="linenos" data-line="158"></span> <span class="nv">$status</span> <span class="o">=</span> <span class="nb">curl_multi_exec</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">,</span> <span class="nv">$running</span><span class="p">);</span> <span class="linenos" data-line="159"></span> <span class="nx">progress_message</span><span class="p">();</span> <span class="linenos" data-line="160"></span> <span class="p">}</span> <span class="k">while</span> <span class="p">((</span><span class="nv">$status</span> <span class="o">==</span> <span class="nx">CURLM_CALL_MULTI_PERFORM</span><span class="p">)</span> <span class="o">||</span> <span class="linenos" data-line="161"></span> <span class="p">(</span><span class="nv">$running</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">));</span> <span class="linenos" data-line="162"></span> <span class="linenos" data-line="163"></span> <span class="k">for</span> <span class="p">(</span><span class="nv">$id</span> <span class="o">=</span> <span class="nv">$chunk</span> <span class="o">*</span> <span class="nx">CHUNK_SIZE</span><span class="p">;</span> <span class="nv">$id</span> <span class="o">&lt;</span> <span class="nv">$id_limit</span><span class="p">;</span> <span class="nv">$id</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// ... and process it</span> <span class="linenos" data-line="164"></span> <span class="nv">$json</span> <span class="o">=</span> <span class="nb">curl_multi_getcontent</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]);</span> <span class="linenos" data-line="165"></span> <span class="linenos" data-line="166"></span> <span class="k">if</span> <span class="p">((</span><span class="nv">$json</span> <span class="o">==</span> <span class="s1">''</span><span class="p">)</span> <span class="o">||</span> <span class="c1">// is the JSON Ok?</span> <span class="linenos" data-line="167"></span> <span class="p">((</span><span class="nv">$json</span> <span class="o">=</span> <span class="nb">json_decode</span><span class="p">(</span><span class="nv">$json</span><span class="p">,</span> <span class="k">true</span><span class="p">))</span> <span class="o">===</span> <span class="k">null</span><span class="p">)</span> <span class="o">||</span> <span class="linenos" data-line="168"></span> <span class="o">!</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="s1">'items'</span><span class="p">,</span> <span class="nv">$json</span><span class="p">)</span> <span class="o">||</span> <span class="linenos" data-line="169"></span> <span class="o">!</span><span class="nb">is_array</span><span class="p">(</span><span class="nv">$json</span><span class="p">[</span><span class="s1">'items'</span><span class="p">]))</span> <span class="p">{</span> <span class="linenos" data-line="170"></span> <span class="linenos" data-line="171"></span> <span class="o">++</span><span class="nv">$failures</span><span class="p">;</span> <span class="linenos" data-line="172"></span> <span class="linenos" data-line="173"></span> <span class="k">if</span> <span class="p">((</span><span class="nv">$message</span> <span class="o">=</span> <span class="nb">curl_error</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]))</span> <span class="o">!=</span> <span class="s1">''</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// for some reason, curl_errno()</span> <span class="linenos" data-line="174"></span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="nv">$message</span><span class="p">,</span> <span class="nv">$error_messages</span><span class="p">))</span> <span class="p">{</span> <span class="c1">// always returns zero here</span> <span class="linenos" data-line="175"></span> <span class="nv">$errno</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$error_messages</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="linenos" data-line="176"></span> <span class="nv">$error_messages</span><span class="p">[</span><span class="nv">$message</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$errno</span><span class="p">;</span> <span class="linenos" data-line="177"></span> <span class="p">}</span> <span class="linenos" data-line="178"></span> <span class="k">else</span> <span class="c1">// already seen</span> <span class="linenos" data-line="179"></span> <span class="nv">$errno</span> <span class="o">=</span> <span class="nv">$error_messages</span><span class="p">[</span><span class="nv">$message</span><span class="p">];</span> <span class="linenos" data-line="180"></span> <span class="p">}</span> <span class="linenos" data-line="181"></span> <span class="k">else</span> <span class="c1">// below -1 are the cURL errors</span> <span class="linenos" data-line="182"></span> <span class="nv">$errno</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="linenos" data-line="183"></span> <span class="linenos" data-line="184"></span> <span class="nv">$output</span><span class="p">[</span><span class="nv">$id</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$errno</span><span class="p">;</span> <span class="linenos" data-line="185"></span> <span class="p">}</span> <span class="linenos" data-line="186"></span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// fetched JSON data is Ok</span> <span class="linenos" data-line="187"></span> <span class="nv">$views</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="linenos" data-line="188"></span> <span class="linenos" data-line="189"></span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$json</span><span class="p">[</span><span class="s1">'items'</span><span class="p">]</span> <span class="k">as</span> <span class="nv">$json_item</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="190"></span> <span class="nv">$views</span> <span class="o">+=</span> <span class="nv">$json_item</span><span class="p">[</span><span class="s1">'views'</span><span class="p">];</span> <span class="linenos" data-line="191"></span> <span class="linenos" data-line="192"></span> <span class="k">if</span> <span class="p">(</span><span class="nv">$json_item</span><span class="p">[</span><span class="s1">'views'</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="c1">// complete days may be missing</span> <span class="linenos" data-line="193"></span> <span class="nv">$days_available</span><span class="p">[</span><span class="nv">$json_item</span><span class="p">[</span><span class="s1">'timestamp'</span><span class="p">]]</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span> <span class="linenos" data-line="194"></span> <span class="p">}</span> <span class="linenos" data-line="195"></span> <span class="linenos" data-line="196"></span> <span class="nv">$views_total</span> <span class="o">+=</span> <span class="nv">$views</span><span class="p">;</span> <span class="linenos" data-line="197"></span> <span class="nv">$output</span><span class="p">[</span><span class="nv">$id</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$views</span><span class="p">;</span> <span class="linenos" data-line="198"></span> <span class="p">}</span> <span class="linenos" data-line="199"></span> <span class="linenos" data-line="200"></span> <span class="nb">curl_multi_remove_handle</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">,</span> <span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]);</span> <span class="linenos" data-line="201"></span> <span class="nb">curl_close</span><span class="p">(</span><span class="nv">$handles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]);</span> <span class="linenos" data-line="202"></span> <span class="linenos" data-line="203"></span> <span class="nx">progress_message</span><span class="p">();</span> <span class="c1">// done with this chunk</span> <span class="linenos" data-line="204"></span> <span class="p">}</span> <span class="linenos" data-line="205"></span> <span class="linenos" data-line="206"></span> <span class="k">if</span> <span class="p">(</span><span class="nv">$chunk</span> <span class="o">!=</span> <span class="p">(</span><span class="nv">$chunks</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="p">{</span> <span class="c1">// don't sleep after the last chunk</span> <span class="linenos" data-line="207"></span> <span class="nv">$message</span> <span class="o">=</span> <span class="s1">'#'</span><span class="p">;</span> <span class="c1">// all this results in smooth progress messages</span> <span class="linenos" data-line="208"></span> <span class="nv">$limit</span> <span class="o">=</span> <span class="nx">CHUNK_SLEEP</span> <span class="o">*</span> <span class="mi">4</span><span class="p">;</span> <span class="linenos" data-line="209"></span> <span class="linenos" data-line="210"></span> <span class="k">for</span> <span class="p">(</span><span class="nv">$i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o">&lt;=</span> <span class="nv">$limit</span><span class="p">;</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="211"></span> <span class="k">if</span> <span class="p">(</span><span class="nx">progress_message</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span> <span class="o">===</span> <span class="k">true</span><span class="p">)</span> <span class="c1">// print only one "marker"</span> <span class="linenos" data-line="212"></span> <span class="nv">$message</span> <span class="o">=</span> <span class="s1">'.'</span><span class="p">;</span> <span class="linenos" data-line="213"></span> <span class="linenos" data-line="214"></span> <span class="nb">usleep</span><span class="p">(</span><span class="mi">250000</span><span class="p">);</span> <span class="linenos" data-line="215"></span> <span class="p">}</span> <span class="linenos" data-line="216"></span> <span class="p">}</span> <span class="linenos" data-line="217"></span><span class="p">}</span> <span class="linenos" data-line="218"></span> <span class="linenos" data-line="219"></span><span class="nb">curl_multi_close</span><span class="p">(</span><span class="nv">$handle_all</span><span class="p">);</span> <span class="linenos" data-line="220"></span><span class="k">echo</span><span class="p">(</span><span class="s2">" done.</span><span class="se">\n\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="221"></span> <span class="linenos" data-line="222"></span><span class="c1">// done fetching all chunks of the stats data, generate and print the output...</span> <span class="linenos" data-line="223"></span> <span class="linenos" data-line="224"></span><span class="nb">arsort</span><span class="p">(</span><span class="nv">$output</span><span class="p">,</span> <span class="nx">SORT_NUMERIC</span><span class="p">);</span> <span class="linenos" data-line="225"></span> <span class="linenos" data-line="226"></span><span class="nv">$error_messages</span> <span class="o">=</span> <span class="nb">array_flip</span><span class="p">(</span><span class="nv">$error_messages</span><span class="p">);</span> <span class="linenos" data-line="227"></span><span class="nv">$articles_ok</span> <span class="o">=</span> <span class="nv">$articles_total</span> <span class="o">-</span> <span class="nv">$failures</span><span class="p">;</span> <span class="linenos" data-line="228"></span><span class="nv">$first_error</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span> <span class="linenos" data-line="229"></span> <span class="linenos" data-line="230"></span><span class="k">foreach</span> <span class="p">(</span><span class="nv">$output</span> <span class="k">as</span> <span class="nv">$id</span> <span class="o">=&gt;</span> <span class="nv">$views</span><span class="p">)</span> <span class="linenos" data-line="231"></span> <span class="k">if</span> <span class="p">(</span><span class="nv">$views</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="linenos" data-line="232"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"- </span><span class="si">{</span><span class="nv">$articles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]</span><span class="si">}</span><span class="s2">: total "</span> <span class="o">.</span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$views</span><span class="p">,</span> <span class="s1">'view'</span><span class="p">)</span> <span class="o">.</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="233"></span> <span class="k">else</span> <span class="p">{</span> <span class="linenos" data-line="234"></span> <span class="k">if</span> <span class="p">(</span><span class="nv">$first_error</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nv">$articles_ok</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span> <span class="c1">// display an empty line before</span> <span class="linenos" data-line="235"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="c1">// the first failure message</span> <span class="linenos" data-line="236"></span> <span class="nv">$first_error</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span> <span class="linenos" data-line="237"></span> <span class="p">}</span> <span class="linenos" data-line="238"></span> <span class="linenos" data-line="239"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"&gt; </span><span class="si">{</span><span class="nv">$articles</span><span class="p">[</span><span class="nv">$id</span><span class="p">]</span><span class="si">}</span><span class="s2">: failure (</span><span class="si">{</span><span class="nv">$error_messages</span><span class="p">[</span><span class="nv">$views</span><span class="p">]</span><span class="si">}</span><span class="s2">)</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="240"></span> <span class="p">}</span> <span class="linenos" data-line="241"></span> <span class="linenos" data-line="242"></span><span class="c1">// ... and the final summary</span> <span class="linenos" data-line="243"></span> <span class="linenos" data-line="244"></span><span class="nv">$days_missing</span> <span class="o">=</span> <span class="nv">$days_total</span> <span class="o">-</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$days_available</span><span class="p">);</span> <span class="linenos" data-line="245"></span><span class="nv">$month_name</span> <span class="o">=</span> <span class="o">@</span><span class="nb">date</span><span class="p">(</span><span class="s1">'F'</span><span class="p">,</span> <span class="o">@</span><span class="nb">strtotime</span><span class="p">(</span><span class="nx">FETCH_YEAR</span> <span class="o">.</span> <span class="s1">'-'</span> <span class="o">.</span> <span class="nx">FETCH_MONTH</span> <span class="o">.</span> <span class="s1">'-01'</span><span class="p">));</span> <span class="linenos" data-line="246"></span> <span class="linenos" data-line="247"></span><span class="nv">$elapsed_time</span> <span class="o">=</span> <span class="nb">microtime</span><span class="p">(</span><span class="k">true</span><span class="p">)</span> <span class="o">-</span> <span class="nv">$start_time</span><span class="p">;</span> <span class="linenos" data-line="248"></span><span class="nv">$elapsed_min</span> <span class="o">=</span> <span class="nb">intval</span><span class="p">(</span><span class="nv">$elapsed_time</span> <span class="o">/</span> <span class="mi">60</span><span class="p">);</span> <span class="linenos" data-line="249"></span><span class="nv">$elapsed_sec</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nv">$elapsed_time</span> <span class="o">-</span> <span class="nv">$elapsed_min</span> <span class="o">*</span> <span class="mi">60</span><span class="p">);</span> <span class="linenos" data-line="250"></span> <span class="linenos" data-line="251"></span><span class="k">echo</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Done, </span><span class="si">{</span><span class="nv">$month_name</span><span class="si">}</span><span class="s2"> "</span> <span class="o">.</span> <span class="nx">FETCH_YEAR</span> <span class="o">.</span> <span class="s1">' statistics for '</span> <span class="o">.</span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$articles_ok</span><span class="p">,</span> <span class="s1">'article'</span><span class="p">)</span> <span class="o">.</span> <span class="linenos" data-line="252"></span> <span class="s1">' fetched in '</span> <span class="o">.</span> <span class="p">((</span><span class="nv">$elapsed_min</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="linenos" data-line="253"></span> <span class="o">?</span> <span class="p">(</span><span class="nx">plural_output</span><span class="p">(</span><span class="nv">$elapsed_min</span><span class="p">,</span> <span class="s1">'minute'</span><span class="p">)</span> <span class="o">.</span> <span class="s1">' and '</span><span class="p">)</span> <span class="linenos" data-line="254"></span> <span class="o">:</span> <span class="s1">''</span><span class="p">)</span> <span class="o">.</span> <span class="linenos" data-line="255"></span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$elapsed_sec</span><span class="p">,</span> <span class="s1">'second'</span><span class="p">)</span> <span class="o">.</span> <span class="s2">".</span><span class="se">\n</span><span class="s2">"</span> <span class="o">.</span> <span class="linenos" data-line="256"></span> <span class="p">((</span><span class="nv">$failures</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="linenos" data-line="257"></span> <span class="o">?</span> <span class="p">(</span><span class="s1">'Fetching the views statistics failed for '</span> <span class="o">.</span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$failures</span><span class="p">,</span> <span class="s1">'article'</span><span class="p">)</span> <span class="o">.</span> <span class="s2">".</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span> <span class="linenos" data-line="258"></span> <span class="o">:</span> <span class="s1">''</span><span class="p">));</span> <span class="linenos" data-line="259"></span> <span class="linenos" data-line="260"></span><span class="k">if</span> <span class="p">(</span><span class="nv">$days_total</span> <span class="o">&gt;</span> <span class="nv">$days_missing</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// it's entirely possible that</span> <span class="linenos" data-line="261"></span> <span class="nv">$views_daily</span> <span class="o">=</span> <span class="nb">intval</span><span class="p">(</span><span class="nv">$views_total</span> <span class="o">/</span> <span class="p">(</span><span class="nv">$days_total</span> <span class="o">-</span> <span class="nv">$days_missing</span><span class="p">));</span> <span class="c1">// all days were missing</span> <span class="linenos" data-line="262"></span> <span class="linenos" data-line="263"></span> <span class="k">echo</span><span class="p">(</span><span class="s1">'Total '</span> <span class="o">.</span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$views_total</span><span class="p">,</span> <span class="s1">'view'</span><span class="p">)</span> <span class="o">.</span> <span class="s1">', averaging in '</span> <span class="o">.</span> <span class="linenos" data-line="264"></span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$views_daily</span><span class="p">,</span> <span class="s1">'view'</span><span class="p">)</span> <span class="o">.</span> <span class="s1">' per day ('</span> <span class="o">.</span> <span class="linenos" data-line="265"></span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$days_total</span><span class="p">,</span> <span class="p">(</span><span class="nv">$current_month</span> <span class="o">?</span> <span class="s1">'whole '</span> <span class="o">:</span> <span class="s1">''</span><span class="p">)</span> <span class="o">.</span> <span class="s1">'day'</span><span class="p">)</span> <span class="o">.</span> <span class="linenos" data-line="266"></span> <span class="s1">' in '</span> <span class="o">.</span> <span class="p">(</span><span class="nv">$current_month</span> <span class="o">?</span> <span class="s1">'the current'</span> <span class="o">:</span> <span class="s1">'that'</span><span class="p">)</span> <span class="o">.</span> <span class="s1">' month'</span> <span class="o">.</span> <span class="linenos" data-line="267"></span> <span class="p">((</span><span class="nv">$days_missing</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="linenos" data-line="268"></span> <span class="o">?</span> <span class="p">(</span><span class="s1">', with the statistics unavailable for '</span> <span class="o">.</span> <span class="nx">plural_output</span><span class="p">(</span><span class="nv">$days_missing</span><span class="p">,</span> <span class="s1">'day'</span><span class="p">))</span> <span class="linenos" data-line="269"></span> <span class="o">:</span> <span class="s1">''</span><span class="p">)</span> <span class="o">.</span> <span class="linenos" data-line="270"></span> <span class="s2">").</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="271"></span><span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// no statistics data</span> <span class="linenos" data-line="272"></span> <span class="k">echo</span><span class="p">(</span><span class="s1">'Sorry, no statistics data is available at the moment for '</span> <span class="o">.</span> <span class="linenos" data-line="273"></span> <span class="p">(</span><span class="nv">$current_month</span> <span class="o">?</span> <span class="s1">'the current'</span> <span class="o">:</span> <span class="s1">'that'</span><span class="p">)</span> <span class="o">.</span> <span class="s2">" month.</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="274"></span> <span class="linenos" data-line="275"></span> <span class="nv">$errno</span> <span class="o">=</span> <span class="p">(((</span><span class="nv">$days_total</span> <span class="o">!=</span> <span class="nv">$days_missing</span><span class="p">)</span> <span class="o">?</span> <span class="mi">10</span> <span class="o">:</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="c1">// just in case, perform some additional</span> <span class="linenos" data-line="276"></span> <span class="p">((</span><span class="nv">$views_total</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">?</span> <span class="mi">20</span> <span class="o">:</span> <span class="mi">0</span><span class="p">));</span> <span class="c1">// sanity checks on the internal logic</span> <span class="linenos" data-line="277"></span> <span class="linenos" data-line="278"></span> <span class="k">if</span> <span class="p">(</span><span class="nv">$errno</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="linenos" data-line="279"></span> <span class="k">echo</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Internal errors detected (error code: </span><span class="si">{</span><span class="nv">$errno</span><span class="si">}</span><span class="s2">), please report on "</span> <span class="o">.</span> <span class="linenos" data-line="280"></span> <span class="s2">"https://en.wikipedia.org/wiki/User_talk:Dsimic by providing complete program output.</span><span class="se">\n</span><span class="s2">"</span><span class="p">);</span> <span class="linenos" data-line="281"></span> <span class="linenos" data-line="282"></span> <span class="k">exit</span><span class="p">(</span><span class="nx">EXIT_FAILURE</span><span class="p">);</span> <span class="linenos" data-line="283"></span> <span class="p">}</span> <span class="linenos" data-line="284"></span><span class="p">}</span> <span class="linenos" data-line="285"></span> <span class="linenos" data-line="286"></span><span class="k">exit</span><span class="p">(</span><span class="nx">EXIT_SUCCESS</span><span class="p">);</span> <span class="linenos" data-line="287"></span> <span class="linenos" data-line="288"></span><span class="cp">?&gt;</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Output_example">Output example</h3><span class="mw-editsection"> <a role="button" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=edit&amp;section=3" title="Edit section: Output example" class="cdx-button cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--icon-only cdx-button--weight-quiet "> <span class="minerva-icon minerva-icon--edit"></span> <span>edit</span> </a> </span> </div> <p>Below is an example of the output produced when the program from above is run. The program sorts the articles by their total page views in descending order, so the article that has received the largest number of page views is first in the printed list. In the <code>Fetching statistics data</code> line, dots (<code>.</code>) represent the progress updates during the processing of each article chunk, while the <a href="/wiki/Number_sign" title="Number sign">hash marks</a> (<code>#</code>) represent the beginning of processing for each new article chunk. This chunking is necessary because the <a href="https://wikitech.wikimedia.org/wiki/Analytics/PageviewAPI" class="extiw" title="wikitech:Analytics/PageviewAPI">Pageview API</a> imposes a <a class="external text" href="https://wikimedia.org/api/rest_v1/?doc#!/Pageviews_data/get_metrics_pageviews_per_article_project_access_agent_article_granularity_start_end">rate limit</a> on the API queries it receives, which, as of June 1, 2016<sup class="plainlinks noexcerpt noprint asof-tag update" style="display:none;"><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Dsimic/Traffic_stats_calculation&amp;action=edit">[update]</a></sup>, is specified at 10 requests per second. </p> <div class="mw-highlight mw-highlight-lang-text mw-content-ltr" dir="ltr"><pre><span></span>Fetching statistics data: ...#.#.#. done. - M.2: total 64,598 views - SATA Express: total 21,724 views - Laravel: total 16,115 views - Stagefright (bug): total 12,717 views - CoreOS: total 11,593 views - Android Runtime: total 9,493 views - Intel X99: total 7,928 views - HipHop Virtual Machine: total 5,944 views - Row hammer: total 3,896 views - Open vSwitch: total 3,769 views - Solid-state storage: total 3,006 views - dm-cache: total 2,044 views - OpenZFS: total 2,011 views - kpatch: total 1,927 views - UniDIMM: total 1,924 views - ARM Cortex-A17: total 1,758 views - Port Control Protocol: total 1,621 views - Buildroot: total 1,397 views - bcache: total 1,323 views - kdump (Linux): total 1,184 views - zswap: total 1,052 views - Eurocrem: total 1,032 views - Management Component Transport Protocol: total 961 views - ftrace: total 921 views - Address generation unit: total 723 views - kGraft: total 630 views - kernfs (Linux): total 598 views - ThinkPad 8: total 427 views - Distributed Overlay Virtual Ethernet: total 409 views - WebScaleSQL: total 317 views - Emdebian Grip: total 284 views - kernfs (BSD): total 280 views - OpenLMI: total 229 views - List of Eurocrem packages: total 99 views Done, January 2016 statistics for 34 articles fetched in 7 seconds. Total 183,934 views, averaging in 5,933 views per day (31 days in that month). </pre></div> <!-- NewPP limit report Parsed by mw‐web.codfw.main‐6d64f599dc‐699kr Cached time: 20241202140643 Cache expiry: 2592000 Reduced expiry: false Complications: [show‐toc] CPU time usage: 0.107 seconds Real time usage: 0.301 seconds Preprocessor visited node count: 570/1000000 Post‐expand include size: 3372/2097152 bytes Template argument size: 134/2097152 bytes Highest expansion depth: 14/100 Expensive parser function count: 2/500 Unstrip recursion depth: 0/20 Unstrip post‐expand size: 58668/5000000 bytes Lua time usage: 0.018/10.000 seconds Lua memory usage: 933293/52428800 bytes Number of Wikibase entities loaded: 0/400 --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 181.786 1 -total 18.28% 33.238 1 Template:See_also 11.99% 21.789 2 Template:As_of 11.57% 21.030 1 Template:URL 6.29% 11.438 2 Template:DMCA 4.74% 8.610 2 Template:Dated_maintenance_category 3.69% 6.704 3 Template:Main_other 3.64% 6.610 2 Template:FULLROOTPAGENAME 2.68% 4.867 2 Template:Ns_has_subpages 2.41% 4.372 1 Template:Details --> <!-- Saved in parser cache with key enwiki:pcache:45421324:|#|:idhash:canonical and timestamp 20241202140643 and revision id 1077564765. Rendering was triggered because: page-view --> </section></div> <!-- MobileFormatter took 0.009 seconds --><!--esi <esi:include src="/esitest-fa8a495983347898/content" /> --><noscript><img src="https://login.wikimedia.org/wiki/Special:CentralAutoLogin/start?useformat=mobile&amp;type=1x1&amp;usesul3=0" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://en.wikipedia.org/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;oldid=1077564765">https://en.wikipedia.org/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;oldid=1077564765</a>"</div></div> </div> <div class="post-content" id="page-secondary-actions"> </div> </main> <footer class="mw-footer minerva-footer" role="contentinfo"> <a class="last-modified-bar" href="/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;action=history"> <div class="post-content last-modified-bar__content"> <span class="minerva-icon minerva-icon-size-medium minerva-icon--modified-history"></span> <span class="last-modified-bar__text modified-enhancement" data-user-name="Uanfala" data-user-gender="unknown" data-timestamp="1647475596"> <span>Last edited on 17 March 2022, at 00:06</span> </span> <span class="minerva-icon minerva-icon-size-small minerva-icon--expand"></span> </div> </a> <div class="post-content footer-content"> <div id="p-lang"> <h4>Languages</h4> <section> <ul id="p-variants" class="minerva-languages"></ul> <ul class="minerva-languages"></ul> <p>This page is not available in other languages.</p> </section> </div> <div class="minerva-footer-logo"><img src="/static/images/mobile/copyright/wikipedia-wordmark-en.svg" alt="Wikipedia" width="120" height="18" style="width: 7.5em; height: 1.125em;"/> </div> <ul id="footer-info" class="footer-info hlist hlist-separated"> <li id="footer-info-lastmod"> This page was last edited on 17 March 2022, at 00:06<span class="anonymous-show">&#160;(UTC)</span>.</li> <li id="footer-info-copyright">Content is available under <a class="external" rel="nofollow" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">CC BY-SA 4.0</a> unless otherwise noted.</li> </ul> <ul id="footer-places" class="footer-places hlist hlist-separated"> <li id="footer-places-privacy"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/wiki/Wikipedia:About">About Wikipedia</a></li> <li id="footer-places-disclaimers"><a href="/wiki/Wikipedia:General_disclaimer">Disclaimers</a></li> <li id="footer-places-contact"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact Wikipedia</a></li> <li id="footer-places-wm-codeofconduct"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct">Code of Conduct</a></li> <li id="footer-places-developers"><a href="https://developer.wikimedia.org">Developers</a></li> <li id="footer-places-statslink"><a href="https://stats.wikimedia.org/#/en.wikipedia.org">Statistics</a></li> <li id="footer-places-cookiestatement"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement">Cookie statement</a></li> <li id="footer-places-terms-use"><a href="https://foundation.m.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use">Terms of Use</a></li> <li id="footer-places-desktop-toggle"><a id="mw-mf-display-toggle" href="//en.wikipedia.org/w/index.php?title=User:Dsimic/Traffic_stats_calculation&amp;mobileaction=toggle_view_desktop" data-event-name="switch_to_desktop">Desktop</a></li> </ul> </div> </footer> </div> </div> <div class="mw-notification-area" data-mw="interface"></div> <!-- v:8.3.1 --> <script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw-web.codfw.main-746fb5b8df-45hvm","wgBackendResponseTime":189,"wgPageParseReport":{"limitreport":{"cputime":"0.107","walltime":"0.301","ppvisitednodes":{"value":570,"limit":1000000},"postexpandincludesize":{"value":3372,"limit":2097152},"templateargumentsize":{"value":134,"limit":2097152},"expansiondepth":{"value":14,"limit":100},"expensivefunctioncount":{"value":2,"limit":500},"unstrip-depth":{"value":0,"limit":20},"unstrip-size":{"value":58668,"limit":5000000},"entityaccesscount":{"value":0,"limit":400},"timingprofile":["100.00% 181.786 1 -total"," 18.28% 33.238 1 Template:See_also"," 11.99% 21.789 2 Template:As_of"," 11.57% 21.030 1 Template:URL"," 6.29% 11.438 2 Template:DMCA"," 4.74% 8.610 2 Template:Dated_maintenance_category"," 3.69% 6.704 3 Template:Main_other"," 3.64% 6.610 2 Template:FULLROOTPAGENAME"," 2.68% 4.867 2 Template:Ns_has_subpages"," 2.41% 4.372 1 Template:Details"]},"scribunto":{"limitreport-timeusage":{"value":"0.018","limit":"10.000"},"limitreport-memusage":{"value":933293,"limit":52428800}},"cachereport":{"origin":"mw-web.codfw.main-6d64f599dc-699kr","timestamp":"20241202140643","ttl":2592000,"transientcontent":false}}});});</script> <script>(window.NORLQ=window.NORLQ||[]).push(function(){var ns,i,p,img;ns=document.getElementsByTagName('noscript');for(i=0;i<ns.length;i++){p=ns[i].nextSibling;if(p&&p.className&&p.className.indexOf('lazy-image-placeholder')>-1){img=document.createElement('img');img.setAttribute('src',p.getAttribute('data-src'));img.setAttribute('width',p.getAttribute('data-width'));img.setAttribute('height',p.getAttribute('data-height'));img.setAttribute('alt',p.getAttribute('data-alt'));p.parentNode.replaceChild(img,p);}}});</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10