Commit 015fef5b authored by Alec Smecher's avatar Alec Smecher
Browse files

*5528* Added referral exclusions

parent f5d9243b
......@@ -40,6 +40,86 @@ class ReferralPlugin extends GenericPlugin {
return false;
}
/**
* Display verbs for the management interface.
*/
function getManagementVerbs() {
$verbs = array();
if ($this->getEnabled()) {
$verbs[] = array('settings', Locale::translate('plugins.generic.googleAnalytics.manager.settings'));
}
return parent::getManagementVerbs($verbs);
}
/*
* Execute a management verb on this plugin
* @param $verb string
* @param $args array
* @param $message string Location for the plugin to put a result msg
* @return boolean
*/
function manage($verb, $args, &$message) {
if (!parent::manage($verb, $args, $message)) return false;
switch ($verb) {
case 'settings':
$templateMgr =& TemplateManager::getManager();
$templateMgr->register_function('plugin_url', array(&$this, 'smartyPluginUrl'));
$journal =& Request::getJournal();
$this->import('ReferralPluginSettingsForm');
$form = new ReferralPluginSettingsForm($this, $journal->getId());
if (Request::getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
Request::redirect(null, 'manager', 'plugin');
return false;
} else {
$this->setBreadCrumbs(true);
$form->display();
}
} else {
$this->setBreadCrumbs(true);
$form->initData();
$form->display();
}
return true;
default:
// Unknown management verb
assert(false);
}
}
/**
* Set the page's breadcrumbs, given the plugin's tree of items
* to append.
* @param $subclass boolean
*/
function setBreadcrumbs($isSubclass = false) {
$templateMgr =& TemplateManager::getManager();
$pageCrumbs = array(
array(
Request::url(null, 'user'),
'navigation.user'
),
array(
Request::url(null, 'manager'),
'user.role.manager'
)
);
if ($isSubclass) $pageCrumbs[] = array(
Request::url(null, 'manager', 'plugins'),
'manager.plugins'
);
$templateMgr->assign('pageHierarchy', $pageCrumbs);
}
/**
* Intercept the load handler hook to present the user-facing
* referrals list if necessary.
*/
function handleLoadHandler($hookName, $args) {
$page =& $args[0];
$op =& $args[1];
......@@ -55,6 +135,9 @@ class ReferralPlugin extends GenericPlugin {
return false;
}
/**
* Intercept the author index page to add referral content
*/
function handleAuthorTemplateInclude($hookName, $args) {
$templateMgr =& $args[0];
$params =& $args[1];
......@@ -77,6 +160,9 @@ class ReferralPlugin extends GenericPlugin {
return false;
}
/**
* Intercept the article comments template to add referral content
*/
function handleReaderTemplateInclude($hookName, $args) {
$templateMgr =& $args[0];
$params =& $args[1];
......@@ -118,6 +204,10 @@ class ReferralPlugin extends GenericPlugin {
return false;
}
/**
* Intercept requests for article display to collect and record
* incoming referrals.
*/
function logArticleRequest(&$templateMgr) {
$article = $templateMgr->get_template_vars('article');
if (!$article) return false;
......@@ -133,7 +223,13 @@ class ReferralPlugin extends GenericPlugin {
// It exists -- increment the count
$referralDao->incrementReferralCount($article->getId(), $referrer);
} else {
// It's a new referral -- log it.
// It's a new referral. Log it unless it's excluded.
$journal = $templateMgr->get_template_vars('currentJournal');
$exclusions = $this->getSetting($journal->getId(), 'exclusions');
foreach (array_map('trim', explode("\n", "$exclusions")) as $exclusion) {
if (empty($exclusion)) continue;
if (preg_match($exclusion, $referrer)) return false;
}
$referral = new Referral();
$referral->setArticleId($article->getId());
$referral->setLinkCount(1);
......@@ -142,6 +238,7 @@ class ReferralPlugin extends GenericPlugin {
$referral->setDateAdded(Core::getCurrentDate());
$referralDao->insertReferral($referral);
}
return false;
}
/**
......
<?php
/**
* @file ReferralPluginSettingsForm.inc.php
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class ReferralPluginSettingsForm
* @ingroup plugins_generic_referral
*
* @brief Form for journal managers to modify referral plugin settings
*/
// $Id$
import('lib.pkp.classes.form.Form');
class ReferralPluginSettingsForm extends Form {
/** @var $journalId int */
var $journalId;
/** @var $plugin object */
var $plugin;
/**
* Constructor
* @param $plugin object
* @param $journalId int
*/
function ReferralPluginSettingsForm(&$plugin, $journalId) {
$this->journalId = $journalId;
$this->plugin =& $plugin;
parent::Form($plugin->getTemplatePath() . 'settingsForm.tpl');
}
/**
* Initialize form data.
*/
function initData() {
$journalId = $this->journalId;
$plugin =& $this->plugin;
$this->_data = array(
'exclusions' => $plugin->getSetting($journalId, 'exclusions')
);
}
/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars(array('exclusions'));
}
/**
* Save settings.
*/
function execute() {
$plugin =& $this->plugin;
$journalId = $this->journalId;
$plugin->updateSetting($journalId, 'exclusions', trim($this->getData('exclusions')), 'string');
}
}
?>
......@@ -32,4 +32,8 @@
<message key="plugins.generic.referral.confirmDelete">Are you sure you wish to delete this refback?</message>
<message key="plugins.generic.referral.nameRequired">A refback title is required.</message>
<message key="plugins.generic.referral.urlRequired">A refback URL is required.</message>
<message key="plugins.generic.referral.settings">Referral Plugin Settings</message>
<message key="plugins.generic.referral.settings.description">The Referral Plugin captures URLs that link to articles published within this journal so that they can be curated by the author and optionally published. This form allows the Journal Manager to control how these links are captured.</message>
<message key="plugins.generic.referral.excusions">Exclusions</message>
<message key="plugins.generic.referral.exclusions.description"><![CDATA[Enter a list of regular expression patterns, one per line, to be treated as exclusions for referral logging. This can be used to prevent search engine requests from cluttering the list of referrals. For more information on regular expressions, see <a href="http://ca.php.net/manual/en/ref.pcre.php" target="_new">http://ca.php.net/manual/en/ref.pcre.php</a>.]]></message>
</locale>
......@@ -17,4 +17,9 @@
<name>enabled</name>
<value>true</value>
</setting>
<setting type="string">
<name>exclusions</name>
<value>#^http://www.google.#
#^http://www.yahoo.#</value>
</setting>
</plugin_settings>
{**
* settingsForm.tpl
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* Referral plugin settings
*
* $Id$
*}
{strip}
{assign var="pageTitle" value="plugins.generic.referral.settings"}
{include file="common/header.tpl"}
{/strip}
<div id="referralSettings">
<div id="description">{translate key="plugins.generic.referral.settings.description"}</div>
<div class="separator"></div>
<br />
<form method="post" action="{plugin_url path="settings"}">
{include file="common/formErrors.tpl"}
<table width="100%" class="data">
<tr valign="top">
<td width="20%" class="label">{fieldLabel name="exclusions" key="plugins.generic.referral.excusions"}</td>
<td width="80%" class="value">
<textarea id="exclusions" name="exclusions" cols="40" rows="5">{$exclusions|escape}</textarea>
<br/>
<span class="instruct">{translate key="plugins.generic.referral.exclusions.description"}</span>
</td>
</tr>
</table>
<br/>
<input type="submit" name="save" class="button defaultButton" value="{translate key="common.save"}"/><input type="button" class="button" value="{translate key="common.cancel"}" onclick="history.go(-1)"/>
</form>
<p><span class="formRequired">{translate key="common.requiredField"}</span></p>
</div>
{include file="common/footer.tpl"}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment