]> git.openstreetmap.org Git - nominatim.git/blob - wikidata/pull_wikipedia_logs.php
README: tiny markdown syntax error
[nominatim.git] / wikidata / pull_wikipedia_logs.php
1 <?php
2
3 for ($iTimestamp = mktime(0, 0, 0, 5, 1, 2013); $iTimestamp < mktime(0, 0, 0, 6, 15, 2013); $iTimestamp += 24*60*60) {
4     $sYear = date('Y', $iTimestamp);
5     $sMonth = date('Y-m', $iTimestamp);
6     $sDay = date('Ymd', $iTimestamp);
7
8     for ($iHour = 0; $iHour < 24; $iHour++) {
9         $sFilename = sprintf('pagecounts-'.$sDay.'-%02d0000', $iHour);
10         echo $sFilename."\n";
11         if (!file_exists($sFilename.'.gz')) {
12             exec('wget http://dumps.wikimedia.org/other/pagecounts-raw/'.$sYear.'/'.$sMonth.'/'.$sFilename.'.gz');
13         }
14
15         exec('gzip -dc '.$sFilename.'.gz'.' | grep -e "^[a-z]\{2\} [^ :]\+ [0-9]\+" > hour.txt');
16
17         $hPrevTotals = @fopen('totals.txt', 'r');
18         $hDayTotals = @fopen('hour.txt', 'r');
19         $hNewTotals = @fopen('newtotals.txt', 'w');
20
21         $sPrevKey = $sDayKey = true;
22         $sPrevLine = true;
23         $sDayLine = true;
24
25         do {
26             if ($sPrevKey === $sDayKey) {
27                 if ($sPrevLine !== true) fputs($hNewTotals, "$sPrevKey ".($iPrevValue+$iDayValue)."\n");
28                 $sPrevLine = true;
29                 $sDayLine = true;
30             } elseif ($sDayKey !== false && ($sPrevKey > $sDayKey || $sPrevKey === false)) {
31                 fputs($hNewTotals, "$sDayKey ".($iDayValue)."\n");
32                 $sDayLine = true;
33             } elseif ($sPrevKey !== false && ($sDayKey > $sPrevKey || $sDayKey === false)) {
34                 fputs($hNewTotals, "$sPrevKey ".($iPrevValue)."\n");
35                 $sPrevLine = true;
36             }
37
38             if ($sPrevLine === true) {
39                 $sPrevLine = $hPrevTotals?fgets($hPrevTotals, 4096):false;
40                 if ($sPrevLine !== false) {
41                     $aPrevLine = explode(' ', $sPrevLine);
42                     $sPrevKey = $aPrevLine[0].' '.$aPrevLine[1];
43                     $iPrevValue = (int)$aPrevLine[2];
44                 } else {
45                     $sPrevKey = false;
46                     $iPrevValue =  0;
47                 }
48             }
49
50             if ($sDayLine === true) {
51                 $sDayLine = $hDayTotals?fgets($hDayTotals, 4096):false;
52                 if ($sDayLine !== false) {
53                     preg_match('#^([a-z]{2}) ([^ :]+) ([0-9]+) [0-9]+$#', $sDayLine, $aMatch);
54                     $sDayKey = $aMatch[1].' '.$aMatch[2];
55                     $iDayValue = (int)$aMatch[3];
56                 } else {
57                     $sDayKey = false;
58                     $iDayValue = 0;
59                 }
60             }
61         } while ($sPrevLine !== false || $sDayLine !== false);
62
63         @fclose($hPrevTotals);
64         @fclose($hDayTotals);
65         @fclose($hNewTotals);
66
67         @unlink('totals.txt');
68         rename('newtotals.txt', 'totals.txt');
69     }
70 }
71
72 // Notes:
73 /*
74     gzip -dc $FILE.gz | grep -e "^en [^ :]\+ [0-9]\+" |
75     sed "s#\(^[a-z]\{2\}\) \([^ :]\+\) \([0-9]\+\) [0-9]\+#update wikipedia_article set hit_count = coalesce(hit_count,0) + \3 where language = '\1'
76       and title = catch_decode_url_part('\2');#g" | /opt/mapquest/stdbase-dev$
77  cat totals.txt | sed "s#\(^[a-z]\{2\}\) \([^ ]\+\) \([0-9]\+\)\$#update entity_link set hits = s,0) + \3 where target = '\1wiki' and value = catch_decode_url_part('\2');#g"
78  cat totals.txt | sed "s#\(^[a-z]\{2\}\) \([^ ]\+\) \([0-9]\+\)\$#update entity_link set hits = coalesce(hits,0) + \3 where target = '\1wiki' and value = catch_decode_url_part('\2');#g"
79 */