Extension:FindTpl

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
FindTpl
Release status: beta
Implementation Tag
Description Finds articles with template
Author(s) RUNA (Runa_cgtalk)
Latest version 0.1 (March 2010)
MediaWiki tested on 1.10.2, 1.15.1
License GPL
Download See the section labeled code below
Translate the FindTpl extension if it is available at translatewiki.net
Check usage and version matrix.

Form to search specified template in pages[edit]

Displays a search form for the template specified in the parameter. Search results are displayed below. Search results - a list of articles that caused a specific template given the input values of parameters.

Installation[edit]

To install this extension

  • Create file in your /extension directory: FindTpl.php (see #Code section)
  • Add the following to LocalSettings.php:
require_once("$IP/extensions/FindTpl.php");

Syntax[edit]

<FindTpl>{{Template_name}}</FindTpl>

Code[edit]

<?php
// MediaWiki FindTpl Extension Ver 0.1
// set up MediaWiki to react to the "<FindTpl>" tag
$wgExtensionFunctions[] = "wfFindTpl";
function wfFindTpl() {
	global $wgParser;
	$wgParser->setHook( "FindTpl", "RenderFindTpl" );
	return true;
}

function RenderFindTpl( $input, $argv, $parser ) {
	global $wgScriptPath, $wgRequest, $wgHooks;
	$parser->disableCache();
	preg_match("/^{{(.*)}}/i", $input, $matches);
	$tpl_name = $matches[1];
	$dbr =& wfGetDB( DB_SLAVE );
	$res = $dbr->query( "
		select distinct t.`old_text` as page_text
		from `page` p
		inner join `revision` r on p.`page_latest`=r.`rev_id`
		inner join `text` t on r.`rev_text_id`=t.`old_id`
		where p.`page_title` = '".$tpl_name."'
		and p.`page_namespace` = 10
		limit 1
	" );
	$row = $dbr->fetchObject( $res );
	$tpl_text = $row->page_text;
	$dbr->freeResult( $res );
	
	#preg_match_all("/\{{3}(.+)[^\}{3}\{{3}]\}{3}/", $tpl_text, $matches);
	preg_match_all("/(?<=\{{3})[^\}]*[^\}]*/", $tpl_text, $matches);
	#print_r($matches);
	
	$params_array = array();
	foreach ($matches[0] as $params) {
		 $expl_params = explode("|", $params, 2);
		 $params_array[] = $expl_params[0];
	}
	$params_array = array_unique($params_array);
	
	$form_inputs = "";
	if (count($params_array)) {
		$form_inputs .= "<table class=\"standard\" style=\"width: auto\">
		<tr><td>Name or number</td><td>Value</td></tr>
		";
		foreach ($params_array as $param_name) {
			$param_value = "";
			if ($wgRequest->getVal("sender") == "findtpl") {
				$param_value = $wgRequest->getVal("form_".urlencode($param_name));
			}
			$form_inputs .= "<tr><td align=right>$param_name</td><td><input type=text name=\"form_".urlencode($param_name)."\" value=\"$param_value\" style=\"width:200px\"/></td></tr>";
		}
		$form_inputs .= "<tr><td colspan=2><center><input type=submit value=\"Find\"/></center></td></tr>
		</table>
		";
	}
	
	$output = "";
	$tt = Title::makeTitle( 10, $tpl_name );
	$output .= "Find template : <B><a href=\"".$tt->getFullURL()."\">".$tpl_name."</a></B><BR/>
Parameters:
<form method=\"POST\">
<input type=\"hidden\" name=\"formsent\" value=\"1\"/>
<input type=\"hidden\" name=\"action\" value=\"purge\"/>
<input type=\"hidden\" name=\"sender\" value=\"findtpl\"/>
$form_inputs
</form>
";
	
	if ($wgRequest->getVal("sender") == "findtpl") {
		$output .= "<br><b>Search results:</b>";
		$search_params = array();
		foreach ($wgRequest->getValues() as $key=>$value) {
			if (strpos($key, "form_") === 0) {
				if (strlen(strval($value)) > 0) {
					$search_params[urldecode(substr($key, 5))] = strval($value);
				}
			}
		}
		$SQLQuery = "
			select p.`page_title`, p.`page_namespace`, t.`old_text` as page_text
			from `page` p
			inner join `revision` r on p.`page_latest`=r.`rev_id`
			inner join `text` t on r.`rev_text_id`=t.`old_id`
			where p.`page_id` in 
			(select distinct `tl_from` from `templatelinks` where `tl_title` = '$tpl_name')
		";
		$res = $dbr->query( $SQLQuery );
		$search_res_output = '';
		while ( $row = $dbr->fetchObject( $res ) ) {
			$page_text = $row->page_text;
			$nt = Title::makeTitle( $row->page_namespace, $row->page_title );
			preg_match_all('/{{'.$tpl_name.'\|([^}}]*)}}/i', $page_text, $matches);
			foreach ($matches[1] as $tpl_key=>$tpl_value) {
				$arr_text = explode('|', $tpl_value);
				$i = 1;
				foreach ($arr_text as &$value) {
					$value = explode('=', $value, 2);
					if (isset($value[1])) {
						$found_params[$value[0]] = trim($value[1]);
					}
					else {
						$found_params[$i] = trim($value[0]);
						$i++;
					}
				}
				if (count($search_params)) {
					$result = array_uintersect_uassoc($search_params, $found_params, "SearchCompare", "strcasecmp");
					if ($result == $search_params) {
						$search_res_output .= "<br><a href=\"".$nt->getFullURL()."\">".$nt->getPrefixedText()."</a>";
						break;
					}
				}
				else {
					$search_res_output .= "<br><a href=\"".$nt->getFullURL()."\">".$nt->getPrefixedText()."</a>";
					break;
				}
			}
		}
		$dbr->freeResult( $res );
		$wgHooks['ParserAfterTidy'][] = array('fnFindTplPositioning', urlencode($tpl_name));
	}
	if (strlen($search_res_output)) {
		$output .= $search_res_output;
	}
	else if ($wgRequest->getInt("formsent")) {
		$output .= "<br/>Search result is empty.";
	}
	$tpl_name_enc = urlencode($tpl_name);
	$output = "<DIV id=\"FindTpl_$tpl_name_enc\" style=\"padding: 10px; boder: 1px solid black;\">".str_replace("\n", "", $output)."</DIV>";
	return $output;
}

function SearchCompare($v1,$v2) {
	$v1 = strtolower($v1);
	$v2 = strtolower($v2);
	if ((stripos($v2, $v1) === 0) ||(stripos($v2, $v1) > 0)){
		return 0;
    }
	else { return 1;
	}
	return -1;
}

function fnFindTplPositioning ( $tpl_name_enc, &$parser, &$text) {
	$label = "FindTpl_$tpl_name_enc";
	$text .= "<script language=\"Javascript\">document.getElementById(\"$label\").scrollIntoView(true);</script>";	
	return true;
}
 ?>