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.0.1, 2008-07-12');
$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' );
$wgExtensionFunctions[] = "wfSetupMarketResearch"; function wfSetupMarketResearch() { global $wgParser; $wgParser->setHook("marketresearch", "wfMarketResearchTag"); }
- 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 "
";
}