Difference between revisions of "Extension:MarketResearch"

From Organic Design wiki
m
(add unlisted special "jump" page)
Line 12: Line 12:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('MARKETRESEARCH_VERSION', '0.0.1, 2008-07-12');
+
define('MARKETRESEARCH_VERSION', '0.1.0, 2008-08-06');
  
 
$wgMarketResearchPartner = 'partnerid-not-set';
 
$wgMarketResearchPartner = 'partnerid-not-set';
Line 26: Line 26:
 
);
 
);
  
 +
$wgSpecialPages['MarketResearch'] = 'SpecialMarketResearch';
 
$wgExtensionFunctions[] = "wfSetupMarketResearch";  
 
$wgExtensionFunctions[] = "wfSetupMarketResearch";  
function wfSetupMarketResearch() {
 
global $wgParser;
 
$wgParser->setHook("marketresearch", "wfMarketResearchTag");
 
}
 
  
# The callback function for converting the input text to HTML output
+
require_once "$IP/includes/SpecialPage.php";
 +
 
 +
/**
 +
* The callback function for converting the input text to HTML output
 +
*/
 
function wfMarketResearchTag($input) {
 
function wfMarketResearchTag($input) {
 
global $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchCart;
 
global $wgMarketResearchPartner, $wgMarketResearchSearch, $wgMarketResearchCart;
Line 73: Line 74:
  
 
return "<div class=\"marketresearch\">$html</div>";
 
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;
 +
 +
$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());
 
}
 
}

Revision as of 01:58, 6 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.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';

  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;

# 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

";

}

/**

* 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()); }