Extension:MarketResearch
<?php /**
* MarketResearch extensionTemplate:Php
* * 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.0, 2008-08-06');
$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';
- 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;
# Add keywords and product id into feed URL $keywords = preg_split('/[\x00-\x1f+,]+/', trim($input)); $search = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchSearch); $search = str_replace('$2', join('+', $keywords), $search);
# Read the feed into a DOM object $doc = new DOMDocument(); $doc->loadXML(file_get_contents($search)); if (is_object($doc)) {
# Add partner id into buy URL $cart = str_replace('$1', $wgMarketResearchPartner, $wgMarketResearchCart);
# 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=\"".str_replace('$2', $id, $cart)."\">$id</a> \n";
}
$products .= '';
# render this item
$html .= "
$title
\n$desc
\n$products\n";
} } else $html = 'no valid XML returned';
return "
";
}
/**
* 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;
$this->setHeaders(); $title = Title::makeTitle(NS_SPECIAL, 'MarketResearch');
if (empty($param)) $wgOut->addWikiText('no product specified!'); else { $wgOut->addWikiText($param); }
} }
/**
* Install the hook and special page */
function wfSetupMarketResearch() { global $wgParser, $wgRequest, $wgOut, $egSelenium, $wgLanguageCode, $wgMessageCache; $wgParser->setHook("marketresearch", "wfMarketResearchTag");
# Add any messages if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'marketresearch' => "Market research jump page" )); }
SpecialPage::addPage(new SpecialMarketResearch()); }