Sorvis has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311684

Change subject: Initial extension: log search terms and created special page to 
view them
......................................................................

Initial extension: log search terms and created special page to view them

Change-Id: I1767978618b023fd5964f9e28263d2c29655835f
---
A SearchStats.body.php
A SearchStats.hooks.php
A SearchStats.i18n.php
A SearchStats.php
A SpecialSearchStats.php
A table.sql
6 files changed, 204 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SearchStats 
refs/changes/84/311684/1

diff --git a/SearchStats.body.php b/SearchStats.body.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/SearchStats.body.php
diff --git a/SearchStats.hooks.php b/SearchStats.hooks.php
new file mode 100644
index 0000000..dbeea28
--- /dev/null
+++ b/SearchStats.hooks.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Hooks for SearchStats extension
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+class SearchStatsHooks {
+       
+       /**
+       * Add search_query table
+       */
+       public static function onLoadExtensionSchemaUpdates( DatabaseUpdater 
$updater)
+       {
+               $updater->addExtensionTable( 'search_query',
+                       __DIR__ . '/table.sql' );
+               return true;
+       }
+       
+       /**
+       * A search was done that went directly to a page
+       * https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchCreateLink
+       */
+       public static function onSpecialSearchCreateLink( $t, &$params ) 
+       {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->insert( 'search_query', 
+                       array(array('sq_query' => $t)),
+                       __METHOD__,
+                       array()
+                       );
+       }
+
+       /**
+       * A search was done that found no match
+       * https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchNogomatch
+       */      
+       public static function onSpecialSearchNogomatch( &$title ) 
+       { 
+       /*
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->insert( 'search_query', 
+                       array(array('sq_query' => $title)),
+                       __METHOD__,
+                       array()
+                       );
+                       */
+       }
+
+}
+
diff --git a/SearchStats.i18n.php b/SearchStats.i18n.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/SearchStats.i18n.php
diff --git a/SearchStats.php b/SearchStats.php
new file mode 100644
index 0000000..3e53c12
--- /dev/null
+++ b/SearchStats.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * SearchStats extension
+ *
+ * For more info see http://mediawiki.org/wiki/Extension:SearchStats
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Steven Orvis, 2016
+ * @license GNU General Public Licence 2.0 or later
+ */
+ 
+ # Alert the user that this is not a valid access point to MediaWiki if they 
try to access the special pages file directly.
+if ( !defined( 'MEDIAWIKI' ) ) {
+       echo <<<EOT
+To install my extension, put the following line in LocalSettings.php:
+require_once( "\$IP/extensions/SearchStats/SearchStats.php" );
+EOT;
+       exit( 1 );
+}
+
+$wgExtensionCredits['other'][] = array(
+       'path' => __FILE__,
+       'name' => 'Search Stats',
+       'author' => array(
+               'Steven Orvis',
+       ),
+       'version'  => '0.1.0',
+       'url' => 'https://www.mediawiki.org/wiki/Extension:SearchStats',
+       'descriptionmsg' => 'Tracks internal searches to allow identifing 
commonally seeked pages on the wiki.',
+);
+
+/* Setup */
+
+// Autoload classes
+$wgAutoloadClasses['SpecialSearchStats'] = __DIR__ . 
'/SpecialSearchStats.php'; # Location of the SpecialSearchStats class (Tell 
MediaWiki to load this file)
+
+// Register files
+$wgAutoloadClasses['SearchStatsHooks'] = __DIR__ . '/SearchStats.hooks.php';
+
+// Register hooks
+$wgHooks['LoadExtensionSchemaUpdates'][] = 
'SearchStatsHooks::onLoadExtensionSchemaUpdates';
+$wgHooks['SpecialSearchCreateLink'][] = 
'SearchStatsHooks::onSpecialSearchCreateLink';
+$wgHooks['SpecialSearchNogomatch'][] = 
'SearchStatsHooks::onSpecialSearchNogomatch';
+
+// Register special pages
+$wgSpecialPages['SearchStats'] = 'SpecialSearchStats'; # Tell MediaWiki about 
the new special page and its class name
+
+/* Configuration */
+
diff --git a/SpecialSearchStats.php b/SpecialSearchStats.php
new file mode 100644
index 0000000..6a7432f
--- /dev/null
+++ b/SpecialSearchStats.php
@@ -0,0 +1,90 @@
+<?php
+/**
+ * SearchStats extension
+ *
+ * For more info see http://mediawiki.org/wiki/Extension:SearchStats
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Steven Orvis, 2016
+ * @license GNU General Public Licence 2.0 or later
+ */
+
+class SpecialSearchStats extends SpecialPage {
+       function __construct() {
+               parent::__construct( 'SearchStats' );
+       }
+
+       function execute( $par ) {
+               $request = $this->getRequest();
+               $output = $this->getOutput();
+               $this->setHeaders();
+
+               # Get request data from, e.g.
+               $param = $request->getText( 'param' );
+               
+               # Get database connection
+               $dbr = wfGetDB(DB_SLAVE);
+               
+               $wikitext = '';
+
+               $wikitext .= $this->displayRecentSearches($dbr);        
+               $wikitext .= $this->displayCommonSearches($dbr);
+               
+               # Write the page
+               $output->addWikiText( $wikitext );
+       }
+       
+       private function displayRecentSearches($dbr)
+       {
+               $wikitext = '';
+               
+               # Display the recent searches
+               $wikitext .= "==Recent Searches With No Direct Match==\n";
+               # Get the recent searches
+               $recentStats = $dbr->select(
+                                       'search_query',                         
                                        # table
+                                       array('sq_id', 'sq_query', 
'sq_timestamp'),     # columns
+                                       '',                                     
                                                # conditions
+                                       __METHOD__,
+                                       array('ORDER BY' => 'sq_id DESC LIMIT 
10')              # options       
+               );
+               
+               foreach( $recentStats as $row){
+                       $wikitext .= "* '''" . $row->sq_query . "''' ''at " . 
$row->sq_timestamp . "'' \n";
+               }
+               
+               return $wikitext;
+       }
+       
+       private function displayCommonSearches($dbr)
+       {
+               $wikitext = '';
+               
+                               # Display the most common searches
+               $wikitext .= "==Common Searches With No Direct Match==\n";
+               
+               # Get the top searches
+               $recentStats = $dbr->select(
+                                       'search_query',                         
                                        # table
+                                       array('count(*) AS QUERYCOUNT', 
'sq_query', ),  # columns
+                                       '',                                     
                                                # conditions
+                                       __METHOD__,
+                                       array( 'GROUP BY' => 'sq_query',
+                                       'ORDER BY' => 'QUERYCOUNT DESC LIMIT 
10')               # options       
+               );
+               
+               # Start a table to display data in
+               $wikitext .= "{| class=\"wikitable sortable\" \n !Term \n 
!Times \n";
+               
+               # Output the table
+               foreach( $recentStats as $row){
+                       $wikitext .= "|- \n |" . $row->sq_query . "\n |" . 
$row->QUERYCOUNT . " \n";
+               }
+               
+               # End the table
+               $wikitext .= "|} \n";
+               
+               return $wikitext;
+       }
+}
\ No newline at end of file
diff --git a/table.sql b/table.sql
new file mode 100644
index 0000000..779dfa3
--- /dev/null
+++ b/table.sql
@@ -0,0 +1,12 @@
+-- MySQL version of the database schema for the Upload Wizard extension.
+-- Licence: GNU GPL v2+
+-- Author: Steven Orvis
+
+-- This stores the searches that the user performs on the wiki
+CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/search_query (
+  sq_id              INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
+  sq_query            VARCHAR(255)        NOT NULL,
+  sq_timestamp            TIMESTAMP        NOT NULL DEFAULT CURRENT_TIMESTAMP
+) /*$wgDBTableOptions*/;
+
+CREATE INDEX /*i*/sq_query ON /*_*/search_query (sq_query);

-- 
To view, visit https://gerrit.wikimedia.org/r/311684
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1767978618b023fd5964f9e28263d2c29655835f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SearchStats
Gerrit-Branch: master
Gerrit-Owner: Sorvis <ste...@huntingpa.info>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to