Semantic MediaWiki search form parser hook

From semanticweb.org

Jump to: navigation, search

This adds a <search> tag to the parser hooks that will insert a SMW search box into a page like the one on Special:Ask.

Contents

[edit] Arguments

  • rows -- how many rows the textarea part of the search should be
  • sortby -- name of the column to put in the sort by box
  • sortenabled -- disable the sort options for this search box
  • order -- either ASC or DESC

[edit] Styling

The form is contained in a div classed smwSearchForm. The sort options are in a div classed smwSortBy. The text box in the sort area is classed text and the form's submit button smwSearchButton.

[edit] Using

[edit] Save the code to file

This code should go into a file called SMW_AskForm.php in the includes directory of the SemanticMediaWiki extension.

<?php
/**
 * This adds a <search> tag to the parser hooks.  It will insert a 
 * SMW search box into a page like the one on Special:Ask.  
 * 
 * Arguments:
 *
 * rows -- how many rows the textarea part of the search should be
 * sortby -- name of the column to put in the sort by box
 * sortenabled -- disable the sort options for this search box
 * order -- either ASC or DESC
 */

$wgHooks['ParserBeforeStrip'][] = 'smwAskFormParser';

function smwAskFormParser() {
	global $wgParser;

	$wgParser->setHook("search", "smwRenderSearchForm");
	
	return true;
}


# used to render <search>
#  input will be go into the search's text area
function smwRenderSearchForm( $input, $argv ) {
  global $smwgQEnabled, $smwgQSortingSupport;

	# return error if querying is not enabled
	if( ! $smwgQEnabled )	{
		return smwfEncodeMessages(array(wfMsgForContent('smw_iq_disabled')));
	}
	
	$spectitle = Title::makeTitle( NS_SPECIAL, 'Ask' );		

	# estimate the number of lines we will need for the box, 
	# one more line than any entered input if it has more than
	# 4 lines and 4 lines otherwise
	if( is_numeric($argv["rows"]) && $argv["rows"] > 0 ) {
		$rows = $argv["rows"];
	}
	else if( substr_count($input, "\n") > 4 ) {
		$rows = substr_count($input, "\n") + 1;
	}
	else {
		$rows = 4;
	}
	
	if( isset($argv["sortby"])) {
		$sort = $argv["sortby"];
	}
	
	# default order is ascending
	if( !isset($argv["order"]) || ($order != "ASC" && $order != "DESC")) {
		$order = "ASC";
	}
	else {
		$order = $argv["order"];
	}
	
	$search_form .= '<div class="smwSearchForm"><form name="ask" action="' .
		$spectitle->escapeLocalURL() . '" method="get">'
		. '<input type="hidden" name="title" value="' .
		$spectitle->getPrefixedText() . '"/>' ;
	$search_form .= '<textarea name="query" rows="'.$rows.'">' .
		htmlspecialchars($input) . '</textarea>' . "\n";
	
	# only include sorting if it is enabled globally and
	# not turned off in the arguments
	if ($smwgQSortingSupport && !$argv['sortenabled'] == 'no' ) {
		$search_form .= '<div class="smwSortBy"';
		$search_form .=  'Sort by column' . 
			' <input class="text" type="text" name="sort" value="' .
			htmlspecialchars($sort) . '"/> <select name="order"><option ';
		// TODO: don't show sort widgets if sorting is not enabled
		if ($order == 'ASC') $search_form .= 'selected="selected" ';
		$search_form .=  'value="ASC">' . 'Ascending' . '</option><option ';
		if ($order == 'DESC') $search_form .= 'selected="selected" ';
		$search_form .=  'value="DESC">' . 'Descending' .
		'</option></select></div>';
	}
	
	$search_form .= ' <input class="button smwSearchButton" type="submit" value="'
		. 'Find results' . '" />';

	$search_form .= '</form></div>';

  return $search_form;
}

[edit] Enabling the parser hook

In your LocalSettings.php, include SMW_AskForm.php after you include SMW_Settings.php.

require_once("$IP/extensions/SemanticMediaWiki/includes/SMW_Settings.php");
enableSemantics('yourdomain.com');
require_once("$IP/extensions/SemanticMediaWiki/includes/SMW_AskForm.php");
Personal tools