Difference between revisions of "Extension:MarketResearch"

From Organic Design wiki
(add unlisted special "jump" page)
(add database cache of feeds and initial jump-page content)
Line 12: Line 12:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('MARKETRESEARCH_VERSION', '0.1.0, 2008-08-06');
+
define('MARKETRESEARCH_VERSION', '0.1.1, 2008-08-07');
  
 
$wgMarketResearchPartner = 'partnerid-not-set';
 
$wgMarketResearchPartner = 'partnerid-not-set';
 
$wgMarketResearchSearch  = 'http://www.marketresearch.com/feed/search_results.asp?bquery=$2&partnerid=$1';
 
$wgMarketResearchSearch  = 'http://www.marketresearch.com/feed/search_results.asp?bquery=$2&partnerid=$1';
 
$wgMarketResearchCart    = 'http://www.marketresearch.com/feed/cart/addtocart.asp?partnerid=$1&productid=$2';
 
$wgMarketResearchCart    = 'http://www.marketresearch.com/feed/cart/addtocart.asp?partnerid=$1&productid=$2';
 +
$wgMarketResearchExpiry  = 60;
  
 
# Extension credits that show up on Special:Version
 
# Extension credits that show up on Special:Version
Line 35: Line 36:
 
  */
 
  */
 
function wfMarketResearchTag($input) {
 
function wfMarketResearchTag($input) {
global $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchCart;
+
global $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchCart, $wgMarketResearchTable, $wgMarketResearchExpiry;
  
 
# Add keywords and product id into feed URL
 
# Add keywords and product id into feed URL
$keywords = preg_split('/[\x00-\x1f+,]+/', trim($input));
+
$keywords = preg_split('/[\x00-\x1f+,]+/', strtolower(trim($input)));
$search = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchSearch);
+
$search   = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchSearch);
$search = str_replace('$2', join('+', $keywords), $search);
+
$search   = str_replace('$2', join('+', $keywords), $search);
 +
$keywords = join(',', $keywords);
 +
$ts      = time();
 +
$db      = &wfGetDB(DB_MASTER);
 +
 
 +
# Remove expired items & attempt to read current item
 +
$xml = false;
 +
if ($wgMarketResearchTable) {
 +
$db->delete($wgMarketResearchTable, array("mrc_time < ".($ts-$wgMarketResearchExpiry)), __FUNCTION__);
 +
if ($row = $db->selectRow($wgMarketResearchTable, 'mrc_xml', "mrc_keywords = '$keywords'", __FUNCTION__))
 +
$xml = $row->mrc_xml;
 +
}
 +
 
 +
# If no XML yet, read from feed and store in cache
 +
if ($xml === false) {
 +
$xml = file_get_contents($search);
 +
if ($wgMarketResearchTable) {
 +
$row = array(
 +
'mrc_keywords' => $keywords,
 +
'mrc_time'    => $ts,
 +
'mrc_xml'      => $xml
 +
);
 +
$db->insert($wgMarketResearchTable, $row, __FUNCTION__);
 +
}
 +
}
  
 
# Read the feed into a DOM object
 
# Read the feed into a DOM object
 
$doc = new DOMDocument();
 
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($search));
+
$doc->loadXML($xml);
 
if (is_object($doc)) {
 
if (is_object($doc)) {
+
 
# Add partner id into buy URL
+
$jump = Title::makeTitle(NS_SPECIAL, 'MarketResearch');
$cart = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchCart);
+
$cart = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchCart); # Add partner id into buy URL
  
 
# Loop through results
 
# Loop through results
Line 57: Line 82:
 
 
 
# Description
 
# Description
foreach ($result->getElementsByTagName('DESCRIPTION') as $i); $desc = $i->textContent;
+
foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc = $i->textContent;
 
if (strlen($desc) > 200) $desc = substr($desc, 0, 200).'...';
 
if (strlen($desc) > 200) $desc = substr($desc, 0, 200).'...';
  
 
# Cart links
 
# Cart links
 
$products = '<ul>';
 
$products = '<ul>';
foreach($result->getElementsByTagName('BUY') as $i) {
+
foreach ($result->getElementsByTagName('BUY') as $i) {
 
$id = $i->getAttribute('id');
 
$id = $i->getAttribute('id');
$products .= "<li><a href=\"".str_replace('$2', $id, $cart)."\">$id</a></li>\n";
+
$products .= "<li><a href=\"".$jump->getLocalURL()."/$keywords/$id\">".$i->textContent."</a></li>\n";
 
}
 
}
 
$products .= '</ul>';
 
$products .= '</ul>';
Line 86: Line 111:
 
   
 
   
 
public function execute($param) {
 
public function execute($param) {
global $wgOut, $wgParser;
+
global $wgOut, $wgParser, $wgMarketResearchTable;
 
 
 
$this->setHeaders();
 
$this->setHeaders();
 
$title = Title::makeTitle(NS_SPECIAL, 'MarketResearch');
 
$title = Title::makeTitle(NS_SPECIAL, 'MarketResearch');
  
if (empty($param)) $wgOut->addWikiText('no product specified!');
+
# Read product from cache
else {
+
if ($param) {
$wgOut->addWikiText($param);
+
list($keywords, $product) = explode('/', $param);
 +
$db = &wfGetDB(DB_SLAVE);
 +
 
 +
if ($row = $db->selectRow($wgMarketResearchTable, 'mrc_xml', "mrc_keywords = '$keywords'", __FUNCTION__)) {
 +
$doc = new DOMDocument();
 +
$xml = $row->mrc_xml;
 +
$doc->loadXML($xml);
 +
} else $doc = false;
 +
 
 +
if (is_object($doc)) {
 +
 
 +
# Loop through results
 +
foreach ($doc->getElementsByTagName('RESULT') as $result) {
 +
foreach ($result->getElementsByTagName('BUY') as $i) {
 +
$id = $i->getAttribute('id');
 +
if ($id === $product) {
 +
 +
# This is our product (should have selected it with an XPath query)
 +
 
 +
# Title
 +
foreach ($result->getElementsByTagName('TITLE') as $i) $title = $i->textContent;
 +
 +
# Description
 +
foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc = $i->textContent;
 +
 +
$wgOut->addWikiText("== $title ==\n");
 +
$wgOut->addWikiText($desc);
 +
 +
}
 +
}
 +
}
 +
} else $wgOut->addWikiText('no valid XML returned');
 
}
 
}
 +
else $wgOut->addWikiText('no product specified!');
  
 
}
 
}
Line 103: Line 160:
 
  */
 
  */
 
function wfSetupMarketResearch() {
 
function wfSetupMarketResearch() {
global $wgParser, $wgRequest, $wgOut, $egSelenium, $wgLanguageCode, $wgMessageCache;
+
global $wgParser, $wgRequest, $wgOut, $egSelenium, $wgLanguageCode, $wgMessageCache, $wgMarketResearchTable;
 
$wgParser->setHook("marketresearch", "wfMarketResearchTag");
 
$wgParser->setHook("marketresearch", "wfMarketResearchTag");
 +
 +
# Create cache DB table if it doesn't exist
 +
$db = &wfGetDB(DB_MASTER);
 +
$wgMarketResearchTable = $db->tableName('MarketResearchCache');
 +
if (!$db->tableExists($wgMarketResearchTable)) {
 +
$query = "CREATE TABLE $wgMarketResearchTable (mrc_keywords TINYBLOB, mrc_time INTEGER NOT NULL, mrc_xml MEDIUMBLOB);";
 +
$result = $db->query($query);
 +
}
 +
 +
# If the table couldn't be created, disable IPN and add error to site notice
 +
if (!$db->tableExists($wgMarketResearchTable)) {
 +
global $wgSiteNotice;
 +
$wgMarketResearchTable = false;
 +
$wgSiteNotice = "<div class=\"errorbox\"><b>MarketResearch extension problem! Could not create database table, caching functionality is disabled.</b><br>Please ensure the wiki database user has CREATE permission, or add the table manually with the following query:<br><tt>$query</tt></div>";
 +
}
  
 
# Add any messages
 
# Add any messages

Revision as of 02:17, 7 August 2008

<?php /**

* MarketResearch extensionTemplate:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
*
* Version 0.0.1 started 2008-07-12
* 
* @package MediaWiki
* @subpackage Extensions
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('MARKETRESEARCH_VERSION', '0.1.1, 2008-08-07');

$wgMarketResearchPartner = 'partnerid-not-set'; $wgMarketResearchSearch = 'http://www.marketresearch.com/feed/search_results.asp?bquery=$2&partnerid=$1'; $wgMarketResearchCart = 'http://www.marketresearch.com/feed/cart/addtocart.asp?partnerid=$1&productid=$2'; $wgMarketResearchExpiry = 60;

  1. Extension credits that show up on Special:Version

$wgExtensionCredits['parserhook'][] = array( 'name' => 'MarketResearch', 'author' => 'Nad for WikiExpert', 'url' => 'http://www.organicdesign.co.nz/Extension:MarketResearch', 'description' => 'Display market research information' );

$wgSpecialPages['MarketResearch'] = 'SpecialMarketResearch'; $wgExtensionFunctions[] = "wfSetupMarketResearch";

require_once "$IP/includes/SpecialPage.php";

/**

* The callback function for converting the input text to HTML output
*/

function wfMarketResearchTag($input) { global $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchCart, $wgMarketResearchTable, $wgMarketResearchExpiry;

# Add keywords and product id into feed URL $keywords = preg_split('/[\x00-\x1f+,]+/', strtolower(trim($input))); $search = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchSearch); $search = str_replace('$2', join('+', $keywords), $search); $keywords = join(',', $keywords); $ts = time(); $db = &wfGetDB(DB_MASTER);

# Remove expired items & attempt to read current item $xml = false; if ($wgMarketResearchTable) { $db->delete($wgMarketResearchTable, array("mrc_time < ".($ts-$wgMarketResearchExpiry)), __FUNCTION__); if ($row = $db->selectRow($wgMarketResearchTable, 'mrc_xml', "mrc_keywords = '$keywords'", __FUNCTION__)) $xml = $row->mrc_xml; }

# If no XML yet, read from feed and store in cache if ($xml === false) { $xml = file_get_contents($search); if ($wgMarketResearchTable) { $row = array( 'mrc_keywords' => $keywords, 'mrc_time' => $ts, 'mrc_xml' => $xml ); $db->insert($wgMarketResearchTable, $row, __FUNCTION__); } }

# Read the feed into a DOM object $doc = new DOMDocument(); $doc->loadXML($xml); if (is_object($doc)) {

$jump = Title::makeTitle(NS_SPECIAL, 'MarketResearch'); $cart = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchCart); # Add partner id into buy URL

# Loop through results foreach ($doc->getElementsByTagName('RESULT') as $result) {

# Title foreach ($result->getElementsByTagName('TITLE') as $i) $title = $i->textContent;

# Description foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc = $i->textContent; if (strlen($desc) > 200) $desc = substr($desc, 0, 200).'...';

# Cart links

$products = '

    '; foreach ($result->getElementsByTagName('BUY') as $i) { $id = $i->getAttribute('id'); $products .= "
  • <a href=\"".$jump->getLocalURL()."/$keywords/$id\">".$i->textContent."</a>
  • \n";

    }

    $products .= '

';

# render this item

$html .= "

\n

$title

\n

$desc

\n$products\n

";

} } else $html = 'no valid XML returned';

return "

$html

";

}

/**

* An unlisted special page used as the "jump page" to a product
*/

class SpecialMarketResearch extends SpecialPage {

function __construct() { SpecialPage::SpecialPage('MarketResearch', , false, false, false, false); }

public function execute($param) { global $wgOut, $wgParser, $wgMarketResearchTable;

$this->setHeaders(); $title = Title::makeTitle(NS_SPECIAL, 'MarketResearch');

# Read product from cache if ($param) { list($keywords, $product) = explode('/', $param); $db = &wfGetDB(DB_SLAVE);

if ($row = $db->selectRow($wgMarketResearchTable, 'mrc_xml', "mrc_keywords = '$keywords'", __FUNCTION__)) { $doc = new DOMDocument(); $xml = $row->mrc_xml; $doc->loadXML($xml); } else $doc = false;

if (is_object($doc)) {

# Loop through results foreach ($doc->getElementsByTagName('RESULT') as $result) { foreach ($result->getElementsByTagName('BUY') as $i) { $id = $i->getAttribute('id'); if ($id === $product) {

# This is our product (should have selected it with an XPath query)

# Title foreach ($result->getElementsByTagName('TITLE') as $i) $title = $i->textContent;

# Description foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc = $i->textContent;

$wgOut->addWikiText("== $title ==\n"); $wgOut->addWikiText($desc);

} } } } else $wgOut->addWikiText('no valid XML returned'); } else $wgOut->addWikiText('no product specified!');

} }

/**

* Install the hook and special page
*/

function wfSetupMarketResearch() { global $wgParser, $wgRequest, $wgOut, $egSelenium, $wgLanguageCode, $wgMessageCache, $wgMarketResearchTable; $wgParser->setHook("marketresearch", "wfMarketResearchTag");

# Create cache DB table if it doesn't exist $db = &wfGetDB(DB_MASTER); $wgMarketResearchTable = $db->tableName('MarketResearchCache'); if (!$db->tableExists($wgMarketResearchTable)) { $query = "CREATE TABLE $wgMarketResearchTable (mrc_keywords TINYBLOB, mrc_time INTEGER NOT NULL, mrc_xml MEDIUMBLOB);"; $result = $db->query($query); }

# If the table couldn't be created, disable IPN and add error to site notice if (!$db->tableExists($wgMarketResearchTable)) { global $wgSiteNotice; $wgMarketResearchTable = false;

$wgSiteNotice = "

MarketResearch extension problem! Could not create database table, caching functionality is disabled.
Please ensure the wiki database user has CREATE permission, or add the table manually with the following query:
$query

";

}

# Add any messages if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'marketresearch' => "Market research jump page" )); }

SpecialPage::addPage(new SpecialMarketResearch()); }