[ Index ]
 

Code source de CMS made simple 1.0.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Search/ -> Search.module.php (source)

   1  <?php
   2  #CMS - CMS Made Simple
   3  #(c)2004 by Ted Kulp (wishy@users.sf.net)
   4  #This project's homepage is: http://cmsmadesimple.sf.net
   5  #
   6  #This program is free software; you can redistribute it and/or modify
   7  #it under the terms of the GNU General Public License as published by
   8  #the Free Software Foundation; either version 2 of the License, or
   9  #(at your option) any later version.
  10  #
  11  #This program is distributed in the hope that it will be useful,
  12  #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  #GNU General Public License for more details.
  15  #You should have received a copy of the GNU General Public License
  16  #along with this program; if not, write to the Free Software
  17  #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18  #
  19  #$Id: News.module.php 2114 2005-11-04 21:51:13Z wishy $
  20  
  21  include_once(dirname(__FILE__) . '/PorterStemmer.class.php');
  22  
  23  define( "NON_INDEXABLE_CONTENT", "<!-- pageAttribute: NotSearchable -->" );
  24  
  25  class SearchItemCollection
  26  {
  27    var $_ary;
  28    var $maxweight;
  29    
  30    function SearchItemCollection()
  31    {
  32      $this->_ary = array();
  33      $this->maxweight = 1;
  34    }
  35    
  36    function AddItem($title, $url, $txt, $weight = 1)
  37    {
  38      if( $txt == '' ) { $txt = $url; }
  39      $exists = false;
  40      
  41      foreach ($this->_ary as $oneitem)
  42        {
  43      if ($url == $oneitem->url)
  44        {
  45          $exists = true;
  46          break;
  47        }
  48        }
  49  
  50      if (!$exists)
  51        {
  52      $newitem = new StdClass();
  53      $newitem->url = $url;
  54      $newitem->urltxt = $txt;
  55      $newitem->title = $title;
  56      $newitem->intweight = intval($weight);
  57      if (intval($weight) > $this->maxweight)
  58        $this->maxweight = intval($weight);
  59      $this->_ary[] = $newitem;
  60        }
  61    }
  62      
  63    function CalculateWeights()
  64    {
  65      reset($this->_ary);
  66      while (list($key) = each($this->_ary))
  67        {
  68      $oneitem =& $this->_ary[$key];
  69      $oneitem->weight = intval(($oneitem->intweight / $this->maxweight) * 100);
  70        }
  71    }
  72  }
  73  
  74  class Search extends CMSModule
  75  {
  76    function GetName()
  77    {
  78      return 'Search';
  79    }
  80  
  81    function GetFriendlyName()
  82    {
  83      return $this->Lang('search');
  84    }
  85  
  86    function IsPluginModule()
  87    {
  88      return true;
  89    }
  90  
  91    function HasAdmin()
  92    {
  93      return true;
  94    }
  95  
  96    /*---------------------------------------------------------
  97     HandlesEvents()
  98     ---------------------------------------------------------*/
  99    function HandlesEvents ()
 100    {
 101      return true;
 102    }
 103  
 104    function GetVersion()
 105    {
 106      return '1.1';
 107    }
 108  
 109    function MinimumCMSVersion()
 110    {
 111      return '1.0';
 112    }
 113  
 114    function GetAdminDescription()
 115    {
 116      return $this->Lang('description');
 117    }
 118  
 119    function VisibleToAdminUser()
 120    {
 121      return true;
 122    }
 123  
 124    function SetParameters()
 125    {
 126      $this->CreateParameter('resultpage', 'null', $this->Lang('param_resultpage'));
 127      $this->CreateParameter('searchtext','null',$this->Lang('param_searchtext'));
 128      $this->CreateParameter('submit',$this->Lang('searchsubmit'),
 129                 $this->Lang('param_submit'));
 130    }
 131  
 132    function GetSearchHtmlTemplate()
 133    {
 134      return '{$startform}
 135  
 136  {$label}:&nbsp;{$inputbox}<input name="submit" value="{$submittext}" type="submit" />{$hidden}
 137  
 138  {$endform}';
 139    }
 140      
 141    function GetResultsHtmlTemplate()
 142    {
 143      return '<h3>{$searchresultsfor} &quot;{$phrase}&quot;</h3>
 144  {if $itemcount > 0}
 145      <ul>
 146      {foreach from=$results item=entry}
 147          <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)</li>
 148      {/foreach}
 149      </ul>
 150  
 151      <p>{$timetaken}: {$timetook}</p>
 152  {else}
 153      <p><strong>{$noresultsfound}</strong></p>
 154  {/if}';
 155    }
 156      
 157    function DefaultStopWords()
 158    {
 159      return 'i, me, my, myself, we, our, ours, ourselves, you, your, yours, 
 160  yourself, yourselves, he, him, his, himself, she, her, hers, 
 161  herself, it, its, itself, they, them, their, theirs, themselves, 
 162  what, which, who, whom, this, that, these, those, am, is, are, 
 163  was, were, be, been, being, have, has, had, having, do, does, 
 164  did, doing, a, an, the, and, but, if, or, because, as, until, 
 165  while, of, at, by, for, with, about, against, between, into, 
 166  through, during, before, after, above, below, to, from, up, down, 
 167  in, out, on, off, over, under, again, further, then, once, here, 
 168  there, when, where, why, how, all, any, both, each, few, more, 
 169  most, other, some, such, no, nor, not, only, own, same, so, 
 170  than, too, very';
 171    }
 172  
 173    function RemoveStopWordsFromArray($words)
 174    {
 175      $stop_words = preg_split("/[\s,]+/", $this->GetPreference('stopwords', $this->DefaultStopWords()));
 176      return array_diff($words, $stop_words);
 177    }
 178  
 179  	function StemPhrase($phrase)
 180      {
 181          // strip out smarty tags
 182          $phrase = preg_replace('/\{.*?\}/', '', $phrase);
 183  
 184          // strip out html and php stuff
 185          $phrase = strip_tags($phrase);
 186  
 187          // split into words
 188          // strtolower isn't friendly to other charsets
 189          $phrase = preg_replace("/([A-Z]+)/e",
 190                  "strtolower('\\1')",
 191                  $phrase);
 192          $words = preg_split('/[\s,!.()+-\/\\\\]+/', $phrase);
 193  
 194          // ignore stop words
 195          $words = $this->RemoveStopWordsFromArray($words);
 196  
 197          $stemmer = new PorterStemmer();
 198  
 199          // stem words
 200          $stemmed_words = array();
 201          $stem_pref = $this->GetPreference('usestemming', 'false');
 202          foreach ($words as $word)
 203          {
 204              //trim words get rid of wrapping quotes
 205              $word = trim($word, ' \'"');
 206  
 207              if (strlen($word) <= 0)
 208              {
 209                  continue;
 210              }
 211  
 212              if ($stem_pref == 'true')
 213                  $stemmed_words[] = $stemmer->stem($word, true);
 214              else
 215                  $stemmed_words[] = $word;
 216          }
 217  
 218          return $stemmed_words;
 219      }
 220  
 221    function AddWords($module = 'Search', $id = -1, $attr = '', $content = '', $expires = NULL)
 222    {
 223      $this->DeleteWords($module, $id, $attr);
 224      $db =& $this->GetDb();
 225          
 226      @$this->SendEvent('SearchItemAdded', array($module, $id, $attr, &$content, $expires));
 227          
 228      if ($content != "")
 229      {        
 230          //Clean up the content
 231          $stemmed_words = $this->StemPhrase($content);
 232          $words = array_count_values($stemmed_words);
 233          
 234          $q = "SELECT id FROM ".cms_db_prefix().'module_search_items WHERE module_name=?';
 235          if( $id != -1 )
 236          {
 237              $q .= " AND content_id=?";
 238              $parms[] = $id;
 239          }
 240          if( $attr != '' )
 241          {
 242              $q .= " AND extra_attr=?";
 243              $parms[] = $attr;
 244          }
 245          $dbresult = $db->Execute($q, $parms);
 246          
 247          if ($dbresult && $dbresult->RecordCount() > 0 && $row = $dbresult->FetchRow())
 248          {
 249              $itemid = $row['id'];
 250          }
 251          else
 252          {
 253              $itemid = $db->GenID(cms_db_prefix()."module_search_items_seq");
 254              $db->Execute('INSERT INTO '.cms_db_prefix().'module_search_items (id, module_name, content_id, extra_attr, expires) VALUES (?,?,?,?,?)', array($itemid, $module, $id, $attr, ($expires != NULL ? trim($db->DBTimeStamp($expires), "'") : NULL) ));
 255          }
 256          
 257          foreach ($words as $word=>$count)
 258          {
 259              $db->Execute('INSERT INTO '.cms_db_prefix().'module_search_index (item_id, word, count) VALUES (?,?,?)', array($itemid, $word, $count));
 260          }
 261        }
 262    }
 263  
 264    function DeleteWords($module = 'Search', $id = -1, $attr = '')
 265    {
 266      $db =& $this->GetDb();
 267      $parms = array( $module );
 268      $q = "DELETE FROM ".cms_db_prefix().'module_search_items WHERE module_name=?';
 269      if( $id != -1 )
 270      {
 271          $q .= " AND content_id=?";
 272          $parms[] = $id;
 273      }
 274      if( $attr != '' )
 275      {
 276          $q .= " AND extra_attr=?";
 277          $parms[] = $attr;
 278      }
 279      $db->Execute($q, $parms);
 280      $db->Execute('DELETE FROM '.cms_db_prefix().'module_search_index WHERE item_id NOT IN (SELECT id FROM '.cms_db_prefix().'module_search_items)');
 281      @$this->SendEvent('SearchItemDeleted', array($module, $id, $attr));
 282    }
 283      
 284    function DeleteAllWords()
 285    {
 286      $db =& $this->GetDb();
 287      $db->Execute('DELETE FROM '.cms_db_prefix().'module_search_index');
 288      $db->Execute('DELETE FROM '.cms_db_prefix().'module_search_items');
 289          
 290      @$this->SendEvent('SearchAllItemsDeleted');
 291    }
 292  
 293    function GetHelp($lang='en_US')
 294    {
 295      return $this->Lang('help');
 296    }
 297  
 298    function GetAuthor()
 299    {
 300      return 'Ted Kulp';
 301    }
 302  
 303    function GetAuthorEmail()
 304    {
 305      return 'ted@cmsmadesimple.org';
 306    }
 307  
 308    function GetChangeLog()
 309    {
 310      return $this->Lang('changelog');
 311    }
 312      
 313    function RegisterEvents()
 314    {
 315      $this->AddEventHandler( 'Core', 'ContentEditPost', false );
 316      $this->AddEventHandler( 'Core', 'ContentDeletePost', false );
 317      $this->AddEventHandler( 'Core', 'AddTemplatePost', false );
 318      $this->AddEventHandler( 'Core', 'EditTemplatePost', false );
 319      $this->AddEventHandler( 'Core', 'DeleteTemplatePost', false );
 320      $this->AddEventHandler( 'Core', 'AddGlobalContentPost', false );
 321      $this->AddEventHandler( 'Core', 'EditGlobalContentPost', false );
 322      $this->AddEventHandler( 'Core', 'DeleteGlobalContentPost', false );
 323      $this->AddEventHandler( 'Core', 'ModuleUninstalled', false );
 324    }
 325  
 326    function Reindex()
 327    {
 328      $this->DeleteAllWords();
 329          
 330  //     $allcontent =& ContentManager::GetAllContent(false);
 331  //     reset($allcontent);
 332  //     while (list($key) = each($allcontent))
 333  //       {
 334  //     $onecontent =& $allcontent[$key];
 335  //     if( $onecontent->Active() )
 336  //       {
 337  //         //$this->ContentEditPost($onecontent);
 338  //         $params = array('content' => &$onecontent);
 339  //         $this->DoEvent('Core', 'ContentEditPost', $params);
 340  //       }
 341  //       }
 342  
 343      global $gCms;
 344      $templateops =& $gCms->GetTemplateOperations();
 345      $alltemplates = $templateops->LoadTemplates();
 346      reset($alltemplates);
 347      while (list($key) = each($alltemplates))
 348        {
 349      $onetemplate =& $alltemplates[$key];
 350      //$this->EditTemplatePost($onetemplate);
 351      $params = array('template' => &$onetemplate,
 352                          'forceindexcontent'=>1);
 353      $this->DoEvent('Core', 'EditTemplatePost', $params);
 354        }
 355  
 356      $gcbops =& $gCms->GetGlobalContentOperations();
 357      $allblobs = $gcbops->LoadHtmlBlobs();
 358      reset($allblobs);
 359      while (list($key) = each($allblobs))
 360        {
 361      $oneblob =& $allblobs[$key];
 362      //$this->EditHtmlBlobPost($oneblob);
 363      $params = array('global_content' => &$oneblob);
 364      $this->DoEvent('Core', 'EditHtmlBlobPost', $params);
 365        }
 366          
 367      global $gCms;
 368      foreach($gCms->modules as $key=>$value)
 369        {
 370      if ($gCms->modules[$key]['installed'] == true &&
 371          $gCms->modules[$key]['active'] == true)
 372        {
 373          if (method_exists($gCms->modules[$key]['object'], 'SearchReindex'))
 374            {
 375          $gCms->modules[$key]['object']->SearchReindex($this);
 376            }
 377        }
 378        }
 379    }
 380  
 381    function GetEventDescription( $eventname )
 382    {
 383      return $this->lang('eventdesc-' . $eventname);
 384    }
 385  
 386    function GetEventHelp( $eventname )
 387    {
 388      return $this->lang('eventhelp-' . $eventname);
 389    }
 390      
 391    function DoEvent( $originator, $eventname, &$params )
 392    {
 393      if ($originator == 'Core')
 394        {
 395      switch ($eventname)
 396        {
 397        case 'ContentEditPost':
 398          $content =& $params['content'];
 399                      
 400              $db =& $this->GetDb();
 401              $q = "SELECT * FROM ".cms_db_prefix()."module_search_items WHERE
 402                    extra_attr = ? AND content_id = ? LIMIT 1";
 403              $dbresult =& $db->Execute( $q, array( 'template', $content->TemplateId() ));
 404              $template_indexed = ( $dbresult && ($dbresult->RecordCount() > 0) );
 405  
 406              if( !$template_indexed )
 407              {
 408                  $this->DeleteWords($this->GetName(), $content->Id(), 'content');
 409                  break;
 410              }
 411  
 412          //Only index content if it's active
 413          if ($content->Active()) {
 414  
 415            //Weight the title and menu text higher
 416            $text = str_repeat(' '.$content->Name(), 2) . ' ';
 417            $text .= str_repeat(' '.$content->MenuText(), 2) . ' ';
 418  
 419            $props = $content->Properties();
 420            foreach ($props->mPropertyValues as $k=>$v)
 421          {
 422            $text .= $v;
 423          }
 424  
 425            // here check for a string to see
 426            // if this content is indexable at all
 427            $non_indexable = strpos($text, NON_INDEXABLE_CONTENT);
 428            if (! $non_indexable)
 429          {
 430            $this->AddWords($this->GetName(), $content->Id(), 'content', $text);
 431          }
 432          }
 433          else
 434            {
 435          //Just in case the active flag was turned off
 436          $this->DeleteWords($this->GetName(), $content->Id(), 'content');
 437            }
 438                      
 439          break;
 440  
 441        case 'ContentDeletePost':
 442          $content =& $params['content'];
 443  
 444          $this->DeleteWords($this->GetName(), $content->Id(), 'content');
 445  
 446          break;
 447                      
 448        case 'AddTemplatePost':
 449          $template =& $params['template'];
 450                      
 451          if( $template->active != false )
 452            $this->AddWords($this->GetName(), $template->id, 'template', $template->content);
 453          else
 454            $this->DeleteWords($this->GetName(), $template->id, 'template');
 455                  
 456          break;
 457                      
 458        case 'EditTemplatePost':
 459          $template =& $params['template'];
 460  
 461          if( $template->active != false )
 462          {
 463              // here check for a string to see
 464              // if this content is indexable at all
 465              $non_indexable = strpos($template->content, NON_INDEXABLE_CONTENT);
 466  
 467              $db =& $this->GetDb();
 468  
 469              // check if the page was indexed already or not
 470              $q = "SELECT * FROM ".cms_db_prefix()."module_search_items WHERE content_id = ?
 471              AND extra_attr = ? LIMIT 1";
 472              $dbresult =& $db->Execute( $q, array( $template->id, 'template' ) );
 473              $was_indexed = ( $dbresult && ($dbresult->RecordCount() > 0) );
 474  
 475              // find all of the pages tied to a template
 476              $q = "SELECT content_id FROM ".cms_db_prefix()."content WHERE template_id = ?";
 477              $dbresult =& $db->Execute( $q, array( $template->id ) );
 478  
 479              if( ! $non_indexable )
 480              {
 481                  $this->AddWords($this->GetName(), $template->id, 'template', $template->content);
 482              }
 483              else
 484              {
 485                  $this->DeleteWords($this->GetName(), $template->id, 'template');
 486              }
 487  
 488              if( ($non_indexable && $was_indexed) )
 489              {
 490                  // we can't index the template, and it was indexed
 491                  // meaning we need to delete all indexes from
 492                  // the children.
 493                  $q2 = "DELETE FROM ".cms_db_prefix()."module_search_items WHERE
 494                  extra_attr = ? AND content_id  IN (";
 495                  $parms = array( 'content' );
 496  
 497                  // delete them all from the index
 498                  while( $dbresult && !$dbresult->EOF )
 499                  {
 500                      $q2 .= "?,";
 501                      $parms[] = $dbresult->fields['content_id'];
 502                      $dbresult->MoveNext();
 503                  }
 504                  $q2 = substr($q2,0,strlen($q2)-1);
 505                  $q2 .= ")";
 506  
 507                  $db->Execute( $q2, $parms );
 508                  
 509                  $db->Execute('DELETE FROM '.cms_db_prefix().'module_search_index WHERE item_id NOT IN (SELECT id FROM '.cms_db_prefix().'module_search_items)');
 510              }
 511              else 
 512              {
 513                  if (!$non_indexable && !$was_indexed)
 514                  { 
 515                      // The template is indexable, and was not indexed previously
 516                      // so we have to index it's children.
 517                      while( $dbresult && !$dbresult->EOF )
 518                      {
 519                          global $gCms;
 520                          $contentops =& $gCms->GetContentOperations();
 521                          $onecontent =& $contentops->LoadContentFromId($dbresult->fields['content_id']);
 522                          $parms = array('content'=>$onecontent);
 523                          $this->DoEvent('Core','ContentEditPost',$parms);
 524                          $dbresult->MoveNext();
 525                      }
 526                  }
 527              }
 528          }
 529          else
 530          {
 531              // template is inactive
 532              $this->DeleteWords($this->GetName(), $template->id, 'template');
 533          }
 534          break;
 535                      
 536        case 'DeleteTemplatePost':
 537          $template =& $params['template'];
 538  
 539          $this->DeleteWords($this->GetName(), $template->id, 'template');
 540  
 541          break;
 542                      
 543        case 'AddGlobalContentPost':
 544          $global_content =& $params['global_content'];
 545  
 546          $this->AddWords($this->GetName(), $global_content->id, 'global_content', $global_content->content);
 547  
 548          break;
 549                      
 550        case 'EditGlobalContentPost':
 551          $global_content =& $params['global_content'];
 552  
 553          $this->AddWords($this->GetName(), $global_content->id, 'global_content', $global_content->content);
 554  
 555          break;
 556                      
 557        case 'DeleteGlobalContentPost':
 558          $global_content =& $params['global_content'];
 559  
 560          $this->DeleteWords($this->GetName(), $global_content->id, 'global_content');
 561  
 562          break;
 563  
 564        case 'ModuleUninstalled':
 565          $module_name =& $params['name'];
 566  
 567          $this->DeleteWords($module_name);
 568  
 569          break;
 570        }
 571        }
 572    }
 573  }
 574  
 575  # vim:ts=4 sw=4 noet
 576  ?>


Généré le : Tue Apr 3 18:50:37 2007 par Balluche grâce à PHPXref 0.7