http://www.mediawiki.org/wiki/Special:Code/MediaWiki/82841

Revision: 82841
Author:   demon
Date:     2011-02-26 12:00:46 +0000 (Sat, 26 Feb 2011)
Log Message:
-----------
Fixes for r82838:
* Give extension usual 4-file-structure (entry, code, i18n, aliases)
* Kill ancient SpecialPage::addPage(), $wgMessageCache->addMessages(), 
wfElement() calls
* Wrap fopen() calls in wfSuppressWarnings() just in case
* Tweak permissions--introduce searchlog-read permission, given to * by default 
so no functional change--to get rid of the $wgSearchLogGroup which doesn't work 
like it pretends to
* Make $wgSearchLogFile false by default and hide it in a method (which will 
default to the format desired)
* Indention, braces, other misc. code style fixes to bring this closer to 2011 
(version saying 2008 should probably be bumped :p)

Modified Paths:
--------------
    trunk/extensions/SearchLog/SearchLog.php

Added Paths:
-----------
    trunk/extensions/SearchLog/SearchLog.alias.php
    trunk/extensions/SearchLog/SearchLog.i18n.php
    trunk/extensions/SearchLog/SearchLog_body.php

Added: trunk/extensions/SearchLog/SearchLog.alias.php
===================================================================
--- trunk/extensions/SearchLog/SearchLog.alias.php                              
(rev 0)
+++ trunk/extensions/SearchLog/SearchLog.alias.php      2011-02-26 12:00:46 UTC 
(rev 82841)
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Aliases for extension SearchLog
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+$specialPageAliases = array();
+
+/** English
+ * @author Brion Vibber
+ */
+$specialPageAliases['en'] = array(
+       'SearchLog' => array( 'SearchLog' ),
+);


Property changes on: trunk/extensions/SearchLog/SearchLog.alias.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/SearchLog/SearchLog.i18n.php
===================================================================
--- trunk/extensions/SearchLog/SearchLog.i18n.php                               
(rev 0)
+++ trunk/extensions/SearchLog/SearchLog.i18n.php       2011-02-26 12:00:46 UTC 
(rev 82841)
@@ -0,0 +1,22 @@
+<?php
+/* 
+ * Localization file for SearchLog extension
+ */
+
+$messages = array();
+
+/**
+ * English
+ * @author Nad
+ */
+$messages['en'] = array(
+       'searchlog'            => 'Search Log',
+       'searchlog-desc'       => 'Logs usage of the search box and allows 
reporting of keyword totals during given time periods',
+       'searchlog-cantopen'   => "Couldn't open log file <tt>$1</tt>",
+       'searchlog-heading'    => 'Search keywords used over $1 period',
+       'searchlog-unicode'    => 'Display raw unicode',
+       'searchlog-timeperiod' => 'Select time period:',
+       'searchlog-entire'     => 'Entire log',
+       'searchlog-phrase'     => 'Search phrase',
+       'searchlog-occurences' => 'Number of occurences during period',
+);


Property changes on: trunk/extensions/SearchLog/SearchLog.i18n.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/extensions/SearchLog/SearchLog.php
===================================================================
--- trunk/extensions/SearchLog/SearchLog.php    2011-02-26 10:55:44 UTC (rev 
82840)
+++ trunk/extensions/SearchLog/SearchLog.php    2011-02-26 12:00:46 UTC (rev 
82841)
@@ -3,138 +3,68 @@
 # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 # - Author: [http://www.organicdesign.co.nz/nad User:Nad]
 # - Started: 2007-05-16
- 
-if (!defined('MEDIAWIKI')) die('Not an entry point.');
- 
-define('SEARCHLOG_VERSION','1.0.8, 2008-2-08');
- 
-$wgSearchLogPath          = dirname(__FILE__);
-$wgSearchLogFile = 
"$wgSearchLogPath/logs/".preg_replace('/^www./','',$_SERVER['SERVER_NAME']);
-$wgSearchLogEntireLog     = 'Entire log'; # Should be a message
+
+if ( !defined('MEDIAWIKI') ) {
+       die( 'Not an entry point.' );
+}
+
+define( 'SEARCHLOG_VERSION', '1.0.8, 2008-2-08' );
+
+$dir = dirname( __FILE__ ) . '/';
+
+// Search log config
+$wgSearchLogPath          = $dir . "logs/";
+$wgSearchLogFile          = false; // defaults to your server name unless 
customized
 $wgSearchLogDateFormat    = '%b %Y';
-$wgSearchLogReportHeading = "Search keywords used over '''\$1''' period"; # 
Should be a message
-$wgSearchLogGroup         = ''; # restrict viewing to a particular group by 
setting this
-$wgExtensionFunctions[]   = 'wfSetupSearchLog';
- 
+
+// Classes and messages and special pages, oh my.
+$wgAutoloadClasses['SpecialSearchLog'] = $dir . 'SearchLog_body.php';
+$wgExtensionMessageFiles['SearchLog']  = $dir . 'SearchLog.i18n.php';
+$wgExtensionAliasesFiles['SearchLog']  = $dir . 'SearchLog.alias.php';
+$wgSpecialPages['SearchLog'] = 'SpecialSearchLog';
+
+// Permissions
+$wgAvailableRights[] = 'searchlog-read';
+$wgGroupPermissions['*']['searchlog-read'];
+
+// Credits
 $wgExtensionCredits['specialpage'][] = array(
-       'name'        => 'Special:SearchLog',
-       'author'      => '[http://www.organicdesign.co.nz/nad 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() {
-               global $wgSearchLogGroup;
-               SpecialPage::SpecialPage('SearchLog',$wgSearchLogGroup);
-               }
- 
-       # Override SpecialPage::execute()
-       function execute() {
-               global 
$wgOut,$wgSearchLogFile,$wgSearchLogEntireLog,$wgSearchLogReportHeading,$wgSearchLogDateFormat,$wgRequest;
-               $this->setHeaders();
-               $title  = Title::makeTitle(NS_SPECIAL,'SearchLog');
-               $period = isset($_REQUEST['period']) ? $_REQUEST['period'] : 
false;
-               $error  = '';
- 
-               # Get the dates of the first and last entries for the dropdown 
list range
-               if ($fh = fopen($wgSearchLogFile,'r')) {
-                       if 
(preg_match('/^([0-9]{4})([0-9]{2})[0-9]{2},/',fread($fh,16),$match)) 
list(,$y1,$m1) = $match;
-                       $len = fstat($fh);
-                       $len = $len['size'] % 1024;
-                       fseek($fh,-$len,SEEK_END);
-                       $end = explode("\n",fread($fh,$len));
-                       $end = $end[count($end)-2];
-                       if 
(preg_match('/^([0-9]{4})([0-9]{2})[0-9]{2},/',$end,$match)) list(,$y2,$m2) = 
$match;
-                       fclose($fh);
-                       }
- 
-                # Construct a list of months if first and last successfully 
obtained
-                $months = '';
-                if ($y1 && $y2) while (($y1*100+$m1) <= ($y2*100+$m2)) {
-                        $p = 
strftime($wgSearchLogDateFormat,mktime(0,0,0,$m1,1,$y1));
-                        $selected = $p == $period ? ' selected' : '';
-                        $months .= "<option$selected>$p</option>\n";
-                        if ($m1 == 12) { $m1 = 1; $y1++; } else $m1++;
-                        }
- 
-               # Render the months as a dropdown-list
-               $wgOut->addHTML(
-                               "<fieldset><legend>Select time period: 
</legend>"
-                               . wfElement('form',array('action' => 
$title->getLocalURL('action=submit'),'method' => 'POST'),null)
-                               . "<select 
name=\"period\"><option>$wgSearchLogEntireLog</option>$months</select>"
-                               . wfElement('input',array('type' => 'submit'))
-                               . "<br /><br />"
-                               . wfCheckLabel("Display raw 
Unicode","wpEscapeChars", "wpEscapeChars", $checked = true)
-                               . '</form></fieldset>'
-                       );
- 
-               # Generate a report if a period was specified
-               if ($period === false) $period = $wgSearchLogEntireLog;
-               if ($period) {
-                       $heading = 
str_replace('$1',$period,$wgSearchLogReportHeading);
-                       $wgOut->addWikiText("== $heading ==",true);
-                       if ($fh = fopen($wgSearchLogFile,'r')) {
- 
-                               # Scan the file and sum the search phrases over 
the period
-                               $total = array();
-                               while (!feof($fh)) {
-                                       if 
(preg_match('/^([0-9]{4})([0-9]{2})([0-9]{2}),(.+?),(.+?),(.+?),(.+)$/',fgets($fh),$match))
 {
-                                               
list(,$y,$m,$d,$time,$user,$type,$phrase) = $match;
-                                               $p = 
strftime($wgSearchLogDateFormat,mktime(0,0,0,$m,1,$y));
-                                               $i = strtolower(trim($phrase));
-                                               if ($period == 
$wgSearchLogEntireLog || $period == $p) $total[$i] = isset($total[$i]) ? 
++$total[$i] : 1;
-                                               }
-                                       }
-                               fclose($fh);
- 
-                                # Render the totals in a table
-                                $table  = "\n<table class=\"sortable\" 
id=\"searchlog\">\n";
-                                $table .= "<tr><th>Search 
phrase</th><th>Number of occurences during period</th></tr>";
-                                foreach ($total as $k => $v) {
-                                 if($wgRequest->getBool('wpEscapeChars')) {
-                                   $k = preg_replace("/&/", "&#38;", $k);
-                                 }
-                                 $table .= 
"<tr><td>[[$k]]</td><td>$v</td></tr>\n";
-                               }
-                                 $table .= "</table>\n";
- 
-                               } else $error = "Couldn't open log file 
<tt>$wgSearchLogFile</tt>";
-                       if ($error) $wgOut->addWikiText($error,true);
-                       else $wgOut->addWikiText($table,true);
-                       }
-               }
- 
-       }
- 
+       'name'           => 'Special:SearchLog',
+       'author'         => '[http://www.organicdesign.co.nz/nad User:Nad]',
+       'descriptionmsg' => 'searchlog-desc',
+       'url'            => 'http://www.mediawiki.org/wiki/Extension:SearchLog',
+       'version'        => SEARCHLOG_VERSION
+);
+
+// Hooks/ext functions
+$wgExtensionFunctions[]   = 'wfSetupSearchLog';
+
 # Called from $wgExtensionFunctions array when initialising extensions
 function wfSetupSearchLog() {
-       global $wgLanguageCode,$wgMessageCache,$wgSearchLogFile,$wgUser;
- 
+       global $wgUser;
+
        # If a search has been posted, log the info
-       if (isset($_REQUEST['search'])) {
+       if ( isset($_REQUEST['search'] ) ) {
                $search = $_REQUEST['search'];
-               if (isset($_REQUEST['fulltext'])) $type = 'fulltext';
-               elseif (isset($_REQUEST['go'])) $type = 'go';
-               else $type = 'other';
- 
+               if ( isset( $_REQUEST['fulltext'] ) ) {
+                       $type = 'fulltext';
+               } elseif ( isset( $_REQUEST['go'] ) ) {
+                       $type = 'go';
+               } else {
+                       $type = 'other';
+               }
+
                # Append the data to the file
-               if (empty($search)) $search = $_REQUEST[$type];
-               if ($fh = fopen($wgSearchLogFile,'a')) {
-                       $text = 
date('Ymd,H:i:s,').$wgUser->getName().",$type,$search";
-                       fwrite($fh,"$text\n");
-                       fclose($fh);
-                       }
+               if ( empty( $search ) ) {
+                       $search = $_REQUEST[$type];
                }
- 
-       # Add the messages used by the specialpage
-       $wgMessageCache->addMessages(array('searchlog' => 'Search Log'));
- 
-       # Add the specialpage to the environment
-       SpecialPage::addPage(new SpecialSearchLog());
+               wfSuppressWarnings();
+               $fh = fopen( SpecialSearchLog::getLogName(), 'a' );
+               wfRestoreWarnings();
+               if ( $fh ) {
+                       $text = date( 'Ymd,H:i:s,' ) . $wgUser->getName() . 
",$type,$search";
+                       fwrite( $fh, "$text\n" );
+                       fclose( $fh );
+               }
        }
+}

Added: trunk/extensions/SearchLog/SearchLog_body.php
===================================================================
--- trunk/extensions/SearchLog/SearchLog_body.php                               
(rev 0)
+++ trunk/extensions/SearchLog/SearchLog_body.php       2011-02-26 12:00:46 UTC 
(rev 82841)
@@ -0,0 +1,127 @@
+<?php
+/**
+ * Class for Special:SearchLog
+ */
+class SpecialSearchLog extends SpecialPage {
+       const ENTIRE_LOG = 'searchlog-entire';
+
+       public function __construct() {
+               parent::__construct( 'SearchLog', 'searchlog-read' );
+       }
+
+       /**
+        * Get the location of the current log file. Will always be in 
$wgSearchLogPath/logs/
+        * Default (if $wgSearchLogFile is not set) is to return your hostname 
(minus the www.)
+        * @return String
+        */
+       public static function getLogName() {
+               global $wgSearchLogPath, $wgSearchLogFile, $wgServer;
+               if( !$wgSearchLogFile ) {
+                       $file = str_replace( 'www.', '', parse_url( $wgServer, 
PHP_URL_HOST ) );
+               } else {
+                       $file = $wgSearchLogFile;
+               }
+               return $wgSearchLogPath . $file;
+       }
+
+       public function execute( $par ) {
+               global $wgOut, $wgSearchLogDateFormat, $wgRequest, $wgLang;
+
+               $this->setHeaders();
+               $period = $wgRequest->getVar( 'period', false );
+               $match = array();
+
+               # Get the dates of the first and last entries for the dropdown 
list range
+               wfSuppressWarnings();
+               $fh = fopen( self::getLogName(), 'r' );
+               wfRestoreWarnings();
+               if ( $fh ) {
+                       if ( preg_match('/^([0-9]{4})([0-9]{2})[0-9]{2},/', 
fread( $fh ,16 ), $match ) ) {
+                               list(,$y1,$m1) = $match;
+                       }
+                       $len = fstat( $fh );
+                       $len = $len['size'] % 1024;
+                       fseek( $fh, -$len, SEEK_END );
+                       $end = explode( "\n", fread( $fh, $len ) );
+                       $end = $end[ count( $end ) -2 ];
+                       if ( preg_match('/^([0-9]{4})([0-9]{2})[0-9]{2},/', 
$end, $match ) ) {
+                               list(,$y2,$m2) = $match;
+                       }
+                       fclose( $fh );
+               }
+
+               # Construct a list of months if first and last successfully 
obtained
+               $months = '';
+               if ( $y1 && $y2 ) {
+                       while ( ( $y1 * 100 + $m1 ) <= ( $y2 * 100 + $m2 ) ) {
+                               $p = strftime( $wgSearchLogDateFormat, mktime( 
0, 0, 0, $m1, 1, $y1 ) );
+                               $selected = $p == $period ? ' selected' : '';
+                               $months .= "<option$selected>$p</option>\n";
+                               if ( $m1 == 12 ) {
+                                       $m1 = 1; $y1++;
+                               } else {
+                                       $m1++;
+                               }
+                       }
+               }
+
+               # Render the months as a dropdown-list
+               $wgOut->addHTML(
+                       "<fieldset><legend>" . wfMsgHtml( 
'searchlog-timeperiod' ) . "</legend>"
+                       . Html::element( 'form', array( 'method' => 'POST',
+                               'action' => $this->getTitle()->getLocalURL( 
'action=submit' ) ) )
+                       . "<select name=\"period\">"
+                       . Html::element( 'option', array( 'value' => 
self::ENTIRE_LOG ),
+                               wfMsg( 'searchlog-entire' ) )
+                       . "$months</select>"
+                       . Html::input( 'input', '', array( 'type' => 'submit' ) 
)
+                       . "<br /><br />"
+                       . Xml::checkLabel( wfMsg( 'searchlog-unicode' ), 
'wpEscapeChars', 'wpEscapeChars', true )
+                       . '</form></fieldset>'
+               );
+
+               # Generate a report if a period was specified
+               if ( $period === false ) {
+                       $period = self::ENTIRE_LOG;
+               }
+               if ( $period ) {
+                       $headingPeriod = $period == self::ENTIRE_LOG ? 
wfMsgNoTrans( self::ENTIRE_LOG ) : $period;
+                       $heading = wfMsgNoTrans( 'searchlog-heading', 
$headingPeriod );
+                       $wgOut->addWikiText( "== $heading ==", true );
+                       wfSuppressWarnings();
+                       $fh = fopen( self::getLogName(), 'r' );
+                       wfRestoreWarnings();
+                       if ( $fh ) {
+                               # Scan the file and sum the search phrases over 
the period
+                               $total = array();
+                               while ( !feof( $fh ) ) {
+                                       if ( 
preg_match('/^([0-9]{4})([0-9]{2})([0-9]{2}),(.+?),(.+?),(.+?),(.+)$/', fgets( 
$fh ), $match ) ) {
+                                               
list(,$y,$m,$d,$time,$user,$type,$phrase) = $match;
+                                               $p = strftime( 
$wgSearchLogDateFormat, mktime( 0, 0, 0, $m, 1, $y ) );
+                                               $i = strtolower( trim( $phrase 
) );
+                                               if ( $period == 
self::ENTIRE_LOG || $period == $p ) {
+                                                       $total[$i] = isset( 
$total[$i] ) ? ++$total[$i] : 1;
+                                               }
+                                       }
+                               }
+                               fclose( $fh );
+
+                               # Render the totals in a table
+                               $table  = "\n<table class=\"sortable\" 
id=\"searchlog\">\n"
+                                       . "<tr><th>" . wfMsgHtml( 
'searchlog-phrase' ) ."</th>"
+                                       . "<th>" . wfMsgHtml( 
'searchlog-occurances' ) . "</th></tr>";
+                               foreach ( $total as $phrase => $count ) {
+                                       if( $wgRequest->getBool( 
'wpEscapeChars' ) ) {
+                                               $phrase = preg_replace( "/&/", 
"&#38;", $phrase );
+                                       }
+                                       $count = $wgLang->formatNum( $count );
+                                       $table .= 
"<tr><td>[[$phrase]]</td><td>$count</td></tr>\n";
+                               }
+                               $table .= "</table>\n";
+                               $wgOut->addWikiText( $table, true );
+                       } else {
+                               $wgOut->addWikiMsg( "searchlog-cantopen", true 
);
+                       }
+               }
+       }
+}


Property changes on: trunk/extensions/SearchLog/SearchLog_body.php
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to