Difference between revisions of "Extension:SearchLog.php"
(that way works) |
(log the timestamp, user and keywords on search) |
||
Line 3: | Line 3: | ||
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | ||
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:SpecialPage}} | # - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:SpecialPage}} | ||
+ | # - Started: 2007-05-16 | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
Line 33: | Line 34: | ||
$title = Title::makeTitle(NS_SPECIAL,'SearchLog'); | $title = Title::makeTitle(NS_SPECIAL,'SearchLog'); | ||
− | # | + | # Get the dates of the first and last entries for the dropdown list range |
+ | if ($fh = fopen("$wgSearchLogPath/search.log",'r')) { | ||
+ | $first = fgets($fh,32); | ||
+ | fseek($fh,-1024,SEEK_END); | ||
+ | $last = end(split("/[\s]+/",fgets($fh,1024))); | ||
+ | fclose($fh); | ||
+ | } | ||
+ | |||
+ | # Construct a list of months | ||
+ | $months = ''; | ||
+ | |||
+ | # Render the months as a dropdown-list | ||
$wgOut->addHTML( | $wgOut->addHTML( | ||
wfElement('form',array('action' => $title->getLocalURL('action=submit'),'method' => 'POST'),null) | wfElement('form',array('action' => $title->getLocalURL('action=submit'),'method' => 'POST'),null) | ||
− | . | + | . "Select time period: <select name=\"month\"><option>Entire log</option>$months</select>" |
. wfElement('input',array('type' => 'submit')) | . wfElement('input',array('type' => 'submit')) | ||
. '</form>' | . '</form>' | ||
Line 56: | Line 68: | ||
# Append the data to the file | # Append the data to the file | ||
if ($fh = fopen("$wgSearchLogPath/search.log",'a')) { | if ($fh = fopen("$wgSearchLogPath/search.log",'a')) { | ||
− | + | $text = date('Ymd').','.$wgUser->getName().",$type,".$_REQUEST[$type]; | |
+ | fwrite($fh,"$text\n"); | ||
fclose($fh); | fclose($fh); | ||
} | } |
Revision as of 21:59, 22 May 2007
<?php
- Extension:SearchLog
{{#Security:*|dev}}{{#Security:view|*}}Template:Php
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:NadCategory:Extensions created with Template:SpecialPage
- - Started: 2007-05-16
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('SEARCHLOG_VERSION','0.0.0, 2007-05-21');
$wgSearchLogPath = dirname(__FILE__); $wgExtensionFunctions[] = 'wfSetupSearchLog';
$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:SearchLog', 'author' => 'User:Nad', 'description' => 'Logs usage of the search box and allows reporting of keyword totals during given time periods', 'url' => 'http://www.mediawiki.org/wiki/Extension:SearchLog', 'version' => SEARCHLOG_VERSION );
require_once "$IP/includes/SpecialPage.php";
class SpecialSearchLog extends SpecialPage {
# Constructor function SpecialSearchLog() { SpecialPage::SpecialPage('SearchLog'); }
# Override SpecialPage::execute() function execute() { global $wgOut; $title = Title::makeTitle(NS_SPECIAL,'SearchLog');
# Get the dates of the first and last entries for the dropdown list range if ($fh = fopen("$wgSearchLogPath/search.log",'r')) { $first = fgets($fh,32); fseek($fh,-1024,SEEK_END); $last = end(split("/[\s]+/",fgets($fh,1024))); fclose($fh); }
# Construct a list of months $months = ;
# Render the months as a dropdown-list $wgOut->addHTML( wfElement('form',array('action' => $title->getLocalURL('action=submit'),'method' => 'POST'),null) . "Select time period: <select name=\"month\"><option>Entire log</option>$months</select>" . wfElement('input',array('type' => 'submit')) . '</form>' ); }
}
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupSearchLog() { global $wgLanguageCode,$wgMessageCache,$wgSearchLogPath,$wgUser;
# If a search has been posted, log the info if (isset($_REQUEST['search'])) { if (isset($_REQUEST['go'])) $type = 'go'; else if (isset($_REQUEST['go'])) $type = 'fulltext'; else $type = 'other';
# Append the data to the file if ($fh = fopen("$wgSearchLogPath/search.log",'a')) { $text = date('Ymd').','.$wgUser->getName().",$type,".$_REQUEST[$type]; fwrite($fh,"$text\n"); fclose($fh); } }
# Add the messages used by the specialpage $wgMessageCache->addMessages(array( 'searchlog' => 'Search Log', ));
# Add the specialpage to the environment SpecialPage::addPage(new SpecialSearchLog()); } ?>