Difference between revisions of "Extension:MarketResearch"

From Organic Design wiki
(add "by vendor")
(moved into svn)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
<?php
+
{{svn|extensions|MarketResearch/MarketResearch.php}}
/**
 
* MarketResearch extension{{php}}{{Category:Extensions|MarketResearch}}
 
*
 
* 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.2.1, 2008-08-29');
 
 
 
$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&ReturnURL=$2&productid=$3';
 
$wgMarketResearchExpiry  = 600; # Number of seconds a cached entry should live for
 
$wgMarketResearchDescSize = 500; # Max characters to render of a description
 
 
 
# Extension credits that show up on Special:Version
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'MarketResearch',
 
'author'      => '[http://www.organicdesign.co.nz/nad Nad] for [http://www.wikiexpert.com 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 $wgTitle, $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchDescSize,
 
$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);
 
$return  = urlencode($wgTitle->getFullURL());
 
$html    = "";
 
 
 
# 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');
 
 
 
# Loop through results
 
foreach ($doc->getElementsByTagName('RESULT') as $result) {
 
 
# Title, description, vendor
 
foreach ($result->getElementsByTagName('TITLE') as $i)      $title  = $i->textContent;
 
foreach ($result->getElementsByTagName('VENDOR') as $i)      $vendor = $i->textContent;
 
foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc  = $i->textContent;
 
if (strlen($desc) > $wgMarketResearchDescSize) $desc = substr($desc, 0, $wgMarketResearchDescSize).'...';
 
 
 
# Cart links
 
$products = '<ul>';
 
foreach ($result->getElementsByTagName('BUY') as $i) {
 
$id = $i->getAttribute('id');
 
$products .= "<li><a href=\"".$jump->getLocalURL("k=$keywords&p=$id&r=$return")."\">".$i->textContent."</a></li>\n";
 
}
 
$products .= '</ul>';
 
 
 
# render this item
 
$html .= "<div class=\"marketresearch-item\">\n<h3>$title by $vendor</h3>\n<p>$desc</p>\n$products\n</div>";
 
}
 
} else $html = 'no valid XML returned';
 
 
 
return "<div class=\"marketresearch\">$html</div>";
 
}
 
 
 
/**
 
* 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, $wgRequest,
 
$wgMarketResearchTable, $wgMarketResearchPartner, $wgMarketResearchCart;
 
 
$this->setHeaders();
 
$title    = Title::makeTitle(NS_SPECIAL, 'MarketResearch');
 
$keywords = $wgRequest->getText('k');
 
$product  = $wgRequest->getText('p');
 
$return  = $wgRequest->getText('r');
 
 
 
# Read product from cache
 
if ($product) {
 
$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)) {
 
 
 
$cart = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchCart);
 
$cart = str_replace('$2', $return, $cart);
 
 
# 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)
 
 
 
foreach ($result->getElementsByTagName('TITLE') as $i)      $title  = $i->textContent;
 
foreach ($result->getElementsByTagName('VENDOR') as $i)      $vendor = $i->textContent;
 
foreach ($result->getElementsByTagName('DESCRIPTION') as $i) $desc  = $i->textContent;
 
 
$wgOut->addWikiText("== $title by $vendor ==\n");
 
$wgOut->addHTML($desc);
 
 
# Add to cart
 
$buy = str_replace('$3', $id, $cart);
 
$wgOut->addWikiText("\n[$buy Add to cart]\n");
 
}
 
}
 
}
 
} 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 = "<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
 
if ($wgLanguageCode == 'en') {
 
$wgMessageCache->addMessages(array(
 
'marketresearch' => "Details on This Report"
 
));
 
}
 
 
 
SpecialPage::addPage(new SpecialMarketResearch());
 
}
 

Latest revision as of 22:52, 27 October 2009

Info.svg This code is in our Git repository here.

Note: If there is no information in this page about this code and it's a MediaWiki extension, there may be something at mediawiki.org.