|
|
(8 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.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' => '[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 $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 = '<ul>';
| |
− | foreach($result->getElementsByTagName('BUY') as $i) {
| |
− | $id = $i->getAttribute('id');
| |
− | $products .= "<li><a href=\"".str_replace('$2', $id, $cart)."\">$id</a></li>\n";
| |
− | }
| |
− | $products .= '</ul>';
| |
− | | |
− | # render this item
| |
− | $html .= "<div class=\"marketresearch-item\">\n<h3>$title</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;
| |
− |
| |
− | $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());
| |
− | } | |