Extension:MarketResearch

From Organic Design wiki
Revision as of 23:15, 21 July 2008 by Nad (talk | contribs)

<?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.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';

  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' );

$wgExtensionFunctions[] = "wfSetupMarketResearch"; function wfSetupMarketResearch() { global $wgParser; $wgParser->setHook("marketresearch", "wfMarketResearchTag"); }

  1. 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 .= "

\n

$title

\n

$desc

\n$products\n

";

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

return "

$html

";

}