1<?php 2 3use ComboStrap\Analytics; 4use ComboStrap\LogUtility; 5use ComboStrap\Page; 6use ComboStrap\Sqlite; 7 8/** 9 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved. 10 * 11 * This source code is licensed under the GPL license found in the 12 * COPYING file in the root directory of this source tree. 13 * 14 * @license GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html) 15 * @author ComboStrap <support@combostrap.com> 16 * 17 */ 18 19require_once(__DIR__ . '/../class/'.'Analytics.php'); 20 21/** 22 * Class action_plugin_combo_analytics 23 * Update the analytics data 24 */ 25class action_plugin_combo_analytics extends DokuWiki_Action_Plugin 26{ 27 28 29 public function register(Doku_Event_Handler $controller) 30 { 31 32 /** 33 * Called on every page write 34 * https://www.dokuwiki.org/devel:event:io_wikipage_write 35 * On update to an existing page this event is called twice, 36 * once for the transfer of the old version to the attic (rev will have a value) 37 * and once to write the new version of the page into the wiki (rev is false) 38 */ 39 //$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_update_analytics', array()); 40 41 /** 42 * Analytics to refresh because they have lost or gain a backlinks 43 * are done via Sqlite table (The INDEXER_TASKS_RUN gives a way to 44 * manipulate this queue) 45 */ 46 $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'handle_refresh_analytics', array()); 47 48 } 49 50 public function handle_update_analytics(Doku_Event $event, $param) 51 { 52 53 $rev = $event->data[3]; 54 if ($rev===false){ 55 $id = $event->data[2]; 56 $page = new Page($id); 57 $page->refreshAnalytics(); 58 } 59 60 61 } 62 63 public function handle_refresh_analytics(Doku_Event $event, $param) 64 { 65 66 /** 67 * Check that the actual page has analytics data 68 * (if there is a cache, it's pretty quick) 69 */ 70 global $ID; 71 Analytics::process($ID,true); 72 73 /** 74 * Check the analytics to refresh 75 */ 76 $sqlite = Sqlite::getSqlite(); 77 $res = $sqlite->query("SELECT ID FROM ANALYTICS_TO_REFRESH"); 78 if (!$res) { 79 LogUtility::msg("There was a problem during the select: {$sqlite->getAdapter()->getDb()->errorInfo()}"); 80 } 81 $rows = $sqlite->res2arr($res,true); 82 $sqlite->res_close($res); 83 foreach($rows as $row){ 84 $page = new Page($row['ID']); 85 $page->refreshAnalytics(); 86 } 87 88 } 89} 90 91 92 93