Extension:AlfrescoSearch
From MediaWiki.org
|
Release status: stable |
|
|---|---|
| Implementation | User interface, Search, Special page |
| Description | Alfresco Search is written by Sufyaan Kazi as a very simple means to search an Alfresco repository and retrieve content, while browsing a MediaWiki based wiki site. |
| Author(s) | SufyaanKTalk |
| Last Version | 0.04 (03/11/2009) |
| License | No license specified |
| Download | no link |
|
check usage (experimental) |
|
Contents |
[edit] What can this extension do?
Alfresco Search is written by Sufyaan Kazi as a very simple means to search an Alfresco repository while browsing a MediaWiki based wiki site. It creates a new SpecialPage in your Wiki that provides a mechanism to search for documents within an Alfresco repository. The returned results are hyperlinks to these documents in your Alfresco repository.
[edit] Usage
Alfresco Search can be used to search for documents in any Alfresco instance that has the PHP Library installed which basically provides a PHP API to the Alfresco OpenSearch.
[edit] Notes
This extension is experimental but it appears to be working fine on our MediaWiki installation. I had never written any PHP before this and so if anyone out there spots a better way of doing this, let me know. This code borrows heavily from the example QueryExecutor.php supplied with the Alfresco PHP library.
Also - at some point we downloaded and installed the Alfresco/MediaWiki integration which basically makes the MediaWiki store a "space" in Alfresco. We decided this was not a good way forward - every wiki page created thereafter lived in the Alfresco database introducing an unnecessary dependency between the two applications. Anyway, as a consequnce this code tries its best to filter out any documents within Alfresco which are actually MediaWiki pages. If you don't plan to install this extension then there please ignore this paragraph!
[edit] Download instructions
Create a new folder under your MediaWiki extensions directory called AlfrescoSearch. Please cut and paste the code found below and place it in $IP/extensions/AlfrescoSearch/ directory in new files with the names supplied, you should end up creating five files. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
[edit] Pre-Requisites
This software was tested with MediaWiki 1.15.1 on a Windows based MediaWiki installation. It may or may not work with earlier or later versions, but please test it. It should work on UNIX too, please amend the paths etc. as appropriate. We are running PHP 5.3 and Alfresco Community Edition 3.2.
It requires the following packages:
- Alfresco PHP library (http://wiki.alfresco.com/wiki/Alfresco_PHP_Library_Installation_Instructions)
- SimpleForms Extension for MediaWiki (http://www.mediawiki.org/wiki/Extension:Simple_Forms)
[edit] Installation
[edit] Install the PHP API Library
-
- Download and install the Alfresco PHP library locally on your server somewhere
- Edit your php.ini folder and update the include path to include the location of the php librabry as per the instructions here: (http://www.modwest.com/help/kb.phtml?qid=98&cat=5) e.g. http://www.modwest.com/help/kb.phtml?qid=98&cat=5
[edit] Install SimpleForms
- Download and install the SimpleForms extensions
[edit] Install AlfrescoSearch
- Edit the file AlfrescoConfig.php which you downloaded from below, change the hostname to match the host for your Alfresco installation.
(Note: localhost won't work even if MediaWiki and Alfresco are on the same server - otherwise when a user runs the search it will interpret this as localhost on their machine!). Also edit the username and password in this file. We created a mediawiki user in Alfresco to make things simpler and restrict what the user can do.
- Add the following to LocalSettings.php
-
require_once("$IP/extensions/AlfrescoSearch/AlfrescoSearch.php");
-
- and optionally add the following too if you want your Alfresco links to open in new windows (Note, this will affect all External links).
-
#External links behaviour (especially for Alfresco Search) -
$wgExternalLinkTarget = '_blank';
-
- Upload an Alfresco logo (25x24) to your wiki and call it AlfrescoLogo.png (or alternatively follow the instructions in AlfrescoConfig.php if you don't want an image.
Good luck :)
[edit] Testing
Open 'yourwikiURL'/index.php?title=Special:AlfrescoSearch, this should load the search page. Try it out. If this link doesn't work, at the very least you should a link 'Alfresco Search' when you go to 'Special Pages' for your wiki.
[edit] Code
[edit] AlfrescoConfig.php
<?php /** URL of Alfresco **/ $wgAlfrescoWSURL="hostname:8080/alfresco"; /** User to log in to Alfresco **/ $wgAlfrescoUser="mediawiki"; /** Password to log in to Alfresco **/ $wgAlfrescoPassword="mediawiki"; /** Name of Alfresco Logo - leave empty if you don't want one **/ /** No logo **/ //$wgAlfrescoLogoName=""; /** Logo **/ $wgAlfrescoLogoName="AlfrescoLogo.png";
[edit] AlfrescoSearch.alias.php
<?php $aliases = array(); /** English */ $aliases['en'] = array('AlfrescoSearch' => array( 'AlfrescoSearch' ),);
[edit] AlfrescoSearch.i18n.php
<?php $messages = array(); $messages['en'] = array( 'alfrescosearch' => 'Alfresco Search', 'alfrescosearch-desc' => "This page allows you to search Alfresco from within MediaWiki", );
[edit] AlfrescoSearch.php
<?php # Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly. if (!defined('MEDIAWIKI')) { echo <<<EOT To install my extension, put the following line in LocalSettings.php: require_once( "\$IP/extensions/AlfrescoSearch/AlfrescoSearch.php" ); EOT; exit( 1 ); } $wgExtensionCredits['specialpage'][] = array( 'name' => 'AlfrescoSearch', 'author' => 'Sufyaan Kazi', 'url' => 'http://www.mediawiki.org/wiki/User:SufyaanK', 'description' => 'Search Alfresco from MediaWiki', 'descriptionmsg' => 'Search Alfresco from MediaWiki', 'version' => '0.05', ); $dir = dirname(__FILE__) . '/'; $wgAutoloadClasses['AlfrescoSearch'] = $dir . 'AlfrescoSearch_body.php'; # Tell MediaWiki to load the extension body. $wgExtensionMessagesFiles['AlfrescoSearch'] = $dir . 'AlfrescoSearch.i18n.php'; $wgExtensionAliasesFiles['AlfrescoSearch'] = $dir . 'AlfrescoSearch.alias.php'; $wgSpecialPages['AlfrescoSearch'] = 'AlfrescoSearch'; # Let MediaWiki know about your new special page.
[edit] AlfrescoSearch_body.php
<?php #Version 0.05 class AlfrescoSearch extends SpecialPage { function __construct() { parent::__construct( 'AlfrescoSearch' ); wfLoadExtensionMessages('AlfrescoSearch'); } # # The method which will be executed by Mediawiki # function execute( $par ) { global $wgRequest, $wgOut; $this->setHeaders(); //Action for the form //$action = "index.php?title=Special:AlfrescoSearch"; //$action = "Special:AlfrescoSearch"; # Output /* * Have we come here because they pressed go? What are they searching on? */ #Get the Search Statement $statement = null; if (isset($_REQUEST['statement']) == true) { $statement = $_REQUEST['statement']; } //Show Search form $this->showSearchForm($wgOut,$statement); # Figure out whether we need to execute the query if($statement == null) { return; } # #There is a search statement in the sesion # $this->runSearch($wgOut,$statement); # #End of Execute method # } # # Get the parent name # function getParentNode($session, $spacesStore, $wgOut, $node){ //$wgOut->addWikiText("::::<nowiki>Looking for parent of ".$node." - ".$node->cm_name."</nowiki>"); if(is_null($node)) { return null; } if(is_null($node->parents)) { return null; } $tempNode = null; foreach ($node->parents as $chAssoc) { $tempNode = $chAssoc->parent; break; } //$wgOut->addWikiText(":::::<nowiki>Found ".$tempNode." - ".$tempNode->cm_name."</nowiki>"); return $tempNode; } # # Show the node breadcrumb # function getBreadcrumb($session, $spacesStore, $wgOut, $node){ //Get the parent of this node $parent = $this->getParentNode($session, $spacesStore, $wgOut, $node); //$breadcrumb=$node->cm_name; $breadcrumb=""; //build the list of parents of this parent do { $breadcrumb=$parent->cm_name."->".$breadcrumb; $parent = $this->getParentNode($session, $spacesStore, $wgOut, $parent); if( is_null($parent) || strcmp("Company Home",$parent->cm_name)== 0) { break; } //$wgOut->addWikiText("::::<nowiki>".$breadcrumb."</nowiki>"); } while(1); //put company home back $breadcrumb = "Company Home->".$breadcrumb; //remove the last "->" $breadcrumb = substr($breadcrumb,0,-2); //print out the breadcrumb $wgOut->addWikiText(":::<nowiki>".$breadcrumb."</nowiki>"); } # # Show the node properties # function showNodeProperties($session, $spacesStore, $wgOut, $node){ // Iterate over each property name and value foreach ($node->properties as $name=>$value) { if(is_string($value)) { $wgOut->addWikiText("::: <nowiki>".$name."->".$value."</nowiki>"); } else { $wgOut->addWikiText("::: <nowiki>URL->".$value->url."</nowiki>"); $wgOut->addWikiText("::: <nowiki>GuestURL->".$value->guestURL."</nowiki>"); $wgOut->addWikiText("::: <nowiki>Property->".$value->property."</nowiki>"); $wgOut->addWikiText("::: <nowiki>MimeType->".$value->mimetype."</nowiki>"); $wgOut->addWikiText("::: <nowiki>Encoding->".$value->encoding."</nowiki>"); $wgOut->addWikiText("::: <nowiki>Size->".$value->size."</nowiki>"); $wgOut->addWikiText("::: <nowiki>Node->".$value->node."</nowiki>"); $wgOut->addWikiText("::: <nowiki>Type->".$value->type."</nowiki>"); //VERY SLOW //$wgOut->addWikiText("::: <nowiki>Content->".$value->content."</nowiki>"); } } } # # Show the url # function showNodeURL($wgOut, $node){ // Iterate over each property name and value foreach ($node->properties as $name=>$value) { if(is_null($value) || is_string($value)) { continue; } $wgOut->addWikiText(":[".$value->url." ".$node->cm_name."]"); } } # # Show a link to the Alfresco article # function showArticleNodeNameAndLink($wgOut, $wgAlfrescoWSURL, $node,$wgAlfrescoLogoName){ if(strlen($wgAlfrescoLogoName) == 0) { //$wgOut->addWikiText(":[http://".$wgAlfrescoWSURL."/download/direct/workspace/SpacesStore/".$node->id."/".$node->cm_name." ".$node->cm_name."]"); $this->showNodeURL($wgOut, $node); } else { //$wgOut->addWikiText(":[[image:".$wgAlfrescoLogoName."|Alfresco Logo]] [http://".$wgAlfrescoWSURL."/download/direct/workspace/SpacesStore/".$node->id."/".$node->cm_name." ".$node->cm_name."]"); $this->showNodeURL($wgOut, $node); } } # # Show a link to the Alfresco Space # function showSpaceNodeNameAndLink($wgOut, $wgAlfrescoWSURL, $node,$wgAlfrescoLogoName){ if(strlen($wgAlfrescoLogoName) == 0) { $wgOut->addWikiText(":[http://".$wgAlfrescoWSURL."/n/browse/workspace/SpacesStore/".$node->id."/"." ".$node->cm_name."]"); } else { $wgOut->addWikiText(":[[image:".$wgAlfrescoLogoName."|Alfresco Logo]] [http://".$wgAlfrescoWSURL."/n/browse/workspace/SpacesStore/".$node->id."/"." ".$node->cm_name."]"); } //http://uklabvm13:8080/alfresco/n/browse/workspace/SpacesStore/82bc64ef-2ac3-46b1-b0c6-2bab71ff4f86 } # # Show a link to the Alfresco Node # function showNodeNameAndLink($wgOut, $wgAlfrescoWSURL, $node,$wgAlfrescoLogoName){ //Is it a space or an article? $NodeIsSpace = True; foreach ($node->properties as $name=>$value) { if(is_string($value)) { continue; } else { if(!isset($value->mimetype)) { $NodeIsSpace = False; break; } else { continue; } } } if($NodeIsSpace == True) { $this->showSpaceNodeNameAndLink($wgOut, $wgAlfrescoWSURL, $node,$wgAlfrescoLogoName); } else { $this->showArticleNodeNameAndLink($wgOut, $wgAlfrescoWSURL, $node,$wgAlfrescoLogoName); } } # # Print out Search Form # function showSearchForm($wgOut,$statement) { /* * Build the HTML Form for the search page */ //$wgOut->addHTML("<form action='".$action."' method='post' name='mainForm'>"); $wgOut->addHTML("<form action='' method='post' name='mainForm'>"); //Create the Main table for the page in wiki text $wgOut->addWikiText(" {| cellpadding=2 cellspacing=3 border=0 |- |Search for |{{#input:type=text|name=statement|value=".$statement."}} |{{#input:type=submit|value=Go}} |} "); //End the form $wgOut->addHTML("</form>"); } # # Print out Search Form # function runSearch($wgOut,$statement) { #Initalise the search results $titleNodes = null; $textNodes = null; #Include Alfresco PHP Libraries require_once('Alfresco/Service/Repository.php'); require_once('Alfresco/Service/Session.php'); require_once('Alfresco/Service/SpacesStore.php'); #Load settings to connect to Alfresco require_once("AlfrescoConfig.php"); # Start the Alfresco session $repository = new Repository("http://".$wgAlfrescoWSURL."/api"); $ticket = $repository->authenticate($wgAlfrescoUser , $wgAlfrescoPassword); $session = $repository->createSession($ticket); //Get a reference to the spaces store $spacesStore = new SpacesStore($session); # #Do the search in Alfresco # //search for articles with the phrase in the title $titleNodes = $session->query($spacesStore, "@cm\:name:\".$statement.\"."); //search for articles with the phrase in the text $textNodes = $session->query($spacesStore, "TEXT:\"$statement\"."); # # Print out Search Results # //Add a header for the repository searched $wgOut->addWikiText("Alfresco Search Results for <span style='color:red'>'".$statement."'</span> in Repository at http://".$wgAlfrescoWSURL); //Add a Header for Page Title Matches $wgOut->addWikiText("==Page Title Matches=="); # #Did we get any page title matches? # if ($titleNodes != null) { //Loop through the results foreach ($titleNodes as $titlenode) { $this->showNodeNameAndLink($wgOut,$wgAlfrescoWSURL,$titlenode,$wgAlfrescoLogoName); $this->getBreadcrumb($session,$spacesStore,$wgOut,$titlenode); //print out detailed properties //$this->showNodeProperties($session,$spacesStore,$wgOut,$titlenode); } } # #Did we get any page text matches? # //Add a header $wgOut->addWikiText("==Page Text Matches=="); //Did we get anypage text results? if ($textNodes != null) { //Loop through the results foreach ($textNodes as $textNode) { $this->showArticleNodeNameAndLink($wgOut,$wgAlfrescoWSURL,$textNode,$wgAlfrescoLogoName); $this->getBreadcrumb($session,$spacesStore,$wgOut,$textNode); //print out detailed properties //$this->showNodeProperties($session,$spacesStore,$wgOut,$textNode); } } else if ($statement != null) { $wgOut->addWikiText("No results found"); } } }