[OJS 3.1.2] [Resolved!] Custom Generic Plugin - Triggering Hook Functions

Hi Everyone,

I am using OJS 3.1.2 and have inherited code from a previous developer for a custom plugin that was made for OJS 3.1.1.4. I have been having a hard time figuring out how to trigger the function (handleRequest) from hook (LoadHandler) in the following code.

I’ve done some logging and from the logs it looks like the handleRequest function is never called and just skipped over. I don’t know what’s wrong. When I disable and enable the plugin I get the following in my log file. You’ll notice that nothing inside handleRequest is logged. Everything in the register function is logged just not inside the subroutines. :woman_shrugging:t2:

2019-09-09 10:18:18 function register :: start 

2019-09-09 10:18:18 function register :: register plugin enabled 

2019-09-09 10:18:18 function register :: completed handleRequest function call 

2019-09-09 10:18:18 function register :: completed handleSubscriptionRequired function call 

2019-09-09 10:18:18 function register :: completed handleTemplateDisplay function call

I know this one might be a bit sticky to debug because it’s custom, but if you have any suggestions of what I can try or approach, it would be much appreciated. I think I’ve reached a blocker in terms of development for this. :pensive:

Thank you,
Rachel

import('lib.pkp.classes.plugins.GenericPlugin');
class IterOJSAccessPlugin extends GenericPlugin {

	private $iter_environment = 'www-dev';
	

	private $journalCodesInterested = array(
	 		'renref' => 8,
	 		'emw' => 20,
	 		'qua' => 10,
	 		'confrat' => 15,
			'eth' => 6,
			'reed' => 7,
			'aestimatio' => 9,
			'interpretatioa' => 122,
			'iterbetatest' => 10000
	);
	

	function register($category, $path) {

		$success = parent::register($category, $path);
		// date timestamp for error log  
		$date = date_create();
		$date = date_format($date, 'Y-m-d H:i:s');

		error_log(print_r($date, true)." function register :: start \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');
		
		if ($success && $this->getEnabled()) {
			error_log(print_r($date, true)." function register :: register plugin enabled \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

			HookRegistry::register ('LoadHandler', array(&$this, 'handleRequest'));
			error_log(print_r($date, true)." function register :: completed handleRequest function call \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');
			

			HookRegistry::register ('IssueAction::subscriptionRequired', array(&$this, 'handleSubscriptionRequired'));
			error_log(print_r($date, true)." function register :: completed handleSubscriptionRequired function call \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

			HookRegistry::register ('TemplateManager::display', array(&$this, 'handleTemplateDisplay'));
			error_log(print_r($date, true)." function register :: completed handleTemplateDisplay function call \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

		}
		return $success;
	}
	

    function getName() { 
        return 'IterOJSAccess'; 
    }
    

    function getDisplayName() { 
        return 'Iter OJS Access'; 
    }
    

    function getDescription() { 
        return 'Iter OJS Access plugin controls journal access according to Iter user database'; 
    }
	
	function isSitePlugin() {
		return FALSE;
	}

	//Nothing inside handleRequest gets logged at all
	function handleRequest($hookName, $args) {
		//header('Location: http://library.utoronto.ca');

		$date = date_create();
		$date = date_format($date, 'Y-m-d H:i:s');

		error_log(print_r($date, true)." function handleRequest :: start \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

		$page =& $args[0];
		$op =& $args[1];
		$sourceFile =& $args[2];

		error_log(print_r($date, true)." function handleRequest :: Page parameter value  ".print_r($page)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');
		error_log(print_r($date, true)." function handleRequest :: Op parameter value ".print_r($op)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');
		error_log(print_r($date, true)." function handleRequest :: Source File parameter value ".print_r($sourceFile )."\r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

		
		if ($page == 'article') { // only interested in article views or downloads
			
			error_log(print_r($date, true)." function handleRequest :: if page is an article  \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

			if ($op == 'view' || $op == 'download') { // we do not care $op == 'viewFile' as it's called within view page to embed PDF
				
			 error_log(print_r($date, true)." function handleRequest :: If View or Download  \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

				$matches = array();
				error_log(print_r($date, true)." function handleRequest :: Matches Array Value ".print_r($matches)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

				if (preg_match('/\w*\/(view|download)\/[0-9]+\/[0-9]+/i', Request::getRequestPath(), $matches) === 1) {

			
					$journal = Request::getJournal();
					error_log(print_r($date, true)." function handleRequest :: Journal ".print_r($journal)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

					$journal_abbrev = $journal->_data['path'];
					error_log(print_r($date, true)." function handleRequest :: Journal Abbrev ".print_r($journal_abbrev)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

					$user_ip = $_SERVER['REMOTE_ADDR'];
					error_log(print_r($date, true)." function handleRequest :: User Ip ".print_r($user_ip)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

   					$iterResourceId = isset($this->journalCodesInterested[$journal_abbrev]) ? $this->journalCodesInterested[$journal_abbrev]:0;
					error_log(print_r($date, true)." function handleRequest :: ITER Resource ID ".print_r($iterResourceId)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

					$this->isIterSubscriber($user_ip, $iterResourceId, FALSE); 
					error_log(print_r($date, true)." function handleRequest :: Current Object ".print_r($this)." \r\n", 3, '/var/www/html/toronto/plugins/themes/test_log');

				}
			}
		}
	
		return FALSE;
	}

Hi @wangra,

This is a bit of gotcha: when you introduce a plugin to the filesystem, some parts of it will work, but because it hasn’t been registered in the database, other parts won’t. The plugin installation process (e.g. plugin gallery) takes care of this, but when you’re coding up a plugin yourself (or avoiding the plugin installation process) you have to register the plugin with the database yourself.

To do this, use the lib/pkp/tools/installPluginVersion.php tool.

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Hi @asmecher

Thank you for your response, I tried to run the installation script and it resulted in this.

Command:
php installPluginVersion.php /var/www/html/toronto/plugins/themes/ITER-ojs-plugin/version.xml

Output:
PHP Warning: uasort() expects parameter 1 to be array, null given in /data/apache-html/67_stable-3_1_2-toronto/lib/pkp/classes/plugins/PluginRegistry.inc.php on line 154

I cracked open PluginRegistry.inc.php and right at the end of the function call uasort() is the problem area. Please let me know if I’ve run the script correctly or if maybe there is something else I haven’t taken into account. I really appreciate your help! :star2:

149 HookRegistry::call(‘PluginRegistry::categoryLoaded::’ . $category, array(&$plugins));
150
151 // Sort the plugins by priority before returning.
152 uasort($plugins, function($a, $b) {
153 return $a->getSeq() - $b->getSeq();
154 });

Thank you,
Rachel

Hi @wangra,

Do you see an entry for your plugin in the versions database table? (The product column must match the installation directory name. This can be finicky.)

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Hi @asmecher

Thank you for the continual support in helping me solve this mystery :female_detective:. I noticed that the product column was not matching the directory name of the custom plugin so I cleared my dev environment and reinstalled the plugin with matching directory name. I tried to run the same lib/pkp/tools/installPluginVersion.php tool but it throws the same error.

error message:
PHP Warning: uasort() expects parameter 1 to be array, null given in /data/apache-html/69_stable-3_1_2-toronto/lib/pkp/classes/plugins/PluginRegistry.inc.php on line 154

I have also tried to uninstall the plugin and delete the plugin database row from the versions table and still cannot run the plugin installation tool. I’m now wondering if I should be digging into why I can’t run the installation tool or look at the DB?

I know this question isn’t an easy one to answer, custom plugins I’m learning are a bit sticky. :confused:

Thank you,
Rachel

Hi @wangra,

That warning may just be cosmetic – it’s been resolved in the codebase for the next release with this change. Have you checked whether the version entry gets created after correcting the mismatch between directory name and product name?

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Hi @asmecher,

I really appreciate your help! :star2:I have figured out the mystery with the custom plugin. Turns out it I was installing it into the wrong folder! Yikes! :flushed: I have ssh access and was sending the custom plugin into the themes folder not the generic folder. I can finally see the updated db entry for the plugin now. Phew! Thank you for your patience and time!

Rachel

Hi @wangra,

Not a problem, glad you’re making progress!

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Hi @asmecher - thanks for the hint checking the versions table - solved my problem of a not enabled theme plug-in, too!

for ojs 2.8.4 there is not such tool at lib/pkp/tools/installPluginVersion.php . How we can register our own custom-made generic plugin to database ?