eZ Publish  [4.0]
updatesearchindex.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <28-Nov-2002 12:45:40 bf>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 set_time_limit( 0 );
00031 
00032 //include_once( 'lib/ezutils/classes/ezcli.php' );
00033 //include_once( 'kernel/classes/ezscript.php' );
00034 
00035 require 'autoload.php';
00036 
00037 $cli = eZCLI::instance();
00038 $endl = $cli->endlineString();
00039 
00040 $script = eZScript::instance( array( 'description' => ( "eZ Publish search index updater.\n\n" .
00041                                                         "Goes trough all objects and reindexes the meta data to the search engine" .
00042                                                         "\n" .
00043                                                         "updatesearchindex.php"),
00044                                      'use-session' => true,
00045                                      'use-modules' => true,
00046                                      'use-extensions' => true ) );
00047 
00048 $script->startup();
00049 
00050 $options = $script->getOptions( "[db-host:][db-user:][db-password:][db-database:][db-type:|db-driver:][sql][clean]",
00051                                 "",
00052                                 array( 'db-host' => "Database host",
00053                                        'db-user' => "Database user",
00054                                        'db-password' => "Database password",
00055                                        'db-database' => "Database name",
00056                                        'db-driver' => "Database driver",
00057                                        'db-type' => "Database driver, alias for --db-driver",
00058                                        'sql' => "Display sql queries",
00059                                        'clean' =>  "Remove all search data before beginning indexing"
00060                                        ) );
00061 $script->initialize();
00062 
00063 $script->setIterationData( '.', '~' );
00064 
00065 $dbUser = $options['db-user'] ? $options['db-user'] : false;
00066 $dbPassword = $options['db-password'] ? $options['db-password'] : false;
00067 $dbHost = $options['db-host'] ? $options['db-host'] : false;
00068 $dbName = $options['db-database'] ? $options['db-database'] : false;
00069 $dbImpl = $options['db-driver'] ? $options['db-driver'] : false;
00070 $showSQL = $options['sql'] ? true : false;
00071 $siteAccess = $options['siteaccess'] ? $options['siteaccess'] : false;
00072 $cleanupSearch = $options['clean'] ? true : false;
00073 
00074 if ( $siteAccess )
00075 {
00076     changeSiteAccessSetting( $siteAccess );
00077 }
00078 
00079 function changeSiteAccessSetting( $siteAccess )
00080 {
00081     global $isQuiet;
00082     $cli = eZCLI::instance();
00083     if ( file_exists( 'settings/siteaccess/' . $siteAccess) )
00084     {
00085         if ( !$isQuiet )
00086             $cli->notice( "Using siteaccess $siteAccess for nice url update" );
00087     }
00088     else
00089     {
00090         if ( !$isQuiet )
00091             $cli->notice( "Siteaccess $siteAccess does not exist, using default siteaccess" );
00092     }
00093 }
00094 
00095 print( "Starting object re-indexing\n" );
00096 
00097 require_once( 'lib/ezutils/classes/ezexecution.php' );
00098 require_once( "lib/ezutils/classes/ezdebug.php" );
00099 //include_once( "kernel/classes/ezsearch.php" );
00100 
00101 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00102 
00103 $db = eZDB::instance();
00104 
00105 if ( $dbHost or $dbName or $dbUser or $dbImpl )
00106 {
00107     $params = array();
00108     if ( $dbHost !== false )
00109         $params['server'] = $dbHost;
00110     if ( $dbUser !== false )
00111     {
00112         $params['user'] = $dbUser;
00113         $params['password'] = '';
00114     }
00115     if ( $dbPassword !== false )
00116         $params['password'] = $dbPassword;
00117     if ( $dbName !== false )
00118         $params['database'] = $dbName;
00119     $db = eZDB::instance( $dbImpl, $params, true );
00120     eZDB::setInstance( $db );
00121 }
00122 
00123 $db->setIsSQLOutputEnabled( $showSQL );
00124 
00125 if ( $cleanupSearch )
00126 {
00127     print( "{eZSearchEngine: Cleaning up search data" );
00128     eZSearch::cleanup();
00129     print( "}$endl" );
00130 }
00131 
00132 $def = eZContentObject::definition();
00133 $conds = array(
00134     'status' => eZContentObject::STATUS_PUBLISHED
00135 );
00136 
00137 $count = eZPersistentObject::count( $def, $conds, 'id' );
00138 
00139 print( "Number of objects to index: $count $endl" );
00140 
00141 $length = 50;
00142 $limit = array( 'offset' => 0 , 'length' => $length );
00143 
00144 $fieldFilters = null;
00145 
00146 $script->resetIteration( $count );
00147 
00148 do
00149 {
00150     // clear in-memory object cache
00151     eZContentObject::clearCache();
00152 
00153     $objects = eZPersistentObject::fetchObjectList( $def, $fieldFilters, $conds, null, $limit );
00154 
00155     foreach ( $objects as $object )
00156     {
00157         if ( !$cleanupSearch )
00158         {
00159             eZSearch::removeObject( $object );
00160         }
00161         eZSearch::addObject( $object );
00162 
00163         $script->iterate( $cli, true );
00164     }
00165 
00166     $limit['offset'] += $length;
00167 
00168 } while ( count( $objects ) == $length );
00169 
00170 
00171 print( $endl . "done" . $endl );
00172 
00173 $script->shutdown();
00174 
00175 ?>