SearchIndexController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
24 
25 /**
26  * SearchIndexController creates a Lucene index from the complete datastore.
27  *
28  * The controller supports the following actions:
29  *
30  * <div class="controller-action">
31  * <div> __Action__ _default_ </div>
32  * <div>
33  * Create the index.
34  * </div>
35  * </div>
36  *
37  * For additional actions and parameters see [BatchController actions](@ref BatchController).
38  *
39  * @author ingo herwig <ingo@wemove.com>
40  */
42 
43  private $_search = null;
44 
45  /**
46  * Constructor
47  * @param $session
48  * @param $persistenceFacade
49  * @param $permissionManager
50  * @param $actionMapper
51  * @param $localization
52  * @param $message
53  * @param $configuration
54  * @param $search
55  */
56  public function __construct(Session $session,
57  PersistenceFacade $persistenceFacade,
58  PermissionManager $permissionManager,
59  ActionMapper $actionMapper,
60  Localization $localization,
61  Message $message,
62  Configuration $configuration,
63  Search $search) {
64  parent::__construct($session, $persistenceFacade, $permissionManager,
65  $actionMapper, $localization, $message, $configuration);
66  $this->_search = $search;
67  }
68 
69  /**
70  * @see BatchController::getWorkPackage()
71  */
72  protected function getWorkPackage($number) {
73  if ($number == 0) {
74  if ($this->_search instanceof IndexedSearch) {
75  // get all types to index
76  $types = array();
77  $persistenceFacade = $this->getPersistenceFacade();
78  foreach ($persistenceFacade->getKnownTypes() as $type) {
79  $tpl = $persistenceFacade->create($type);
80  if ($this->_search->isSearchable($tpl)) {
81  $types[] = $type;
82  }
83  }
84  $this->_search->resetIndex();
85  return array('name' => $this->getMessage()->getText('Collect objects'),
86  'size' => 1, 'oids' => $types, 'callback' => 'collect');
87  }
88  else {
89  // no index to be updated
90  return null;
91  }
92  }
93  else {
94  return null;
95  }
96  }
97 
98  /**
99  * Collect all oids of the given types
100  * @param $types The types to process
101  * @note This is a callback method called on a matching work package @see BatchController::addWorkPackage()
102  */
103  protected function collect($types) {
104  $persistenceFacade = $this->getPersistenceFacade();
105  foreach ($types as $type) {
106  $oids = $persistenceFacade->getOIDs($type);
107  if (sizeof($oids) == 0) {
108  $oids = array(1);
109  }
110  $this->addWorkPackage($this->getMessage()->getText('Indexing %0%', array($type)),
111  10, $oids, 'index');
112  }
113  }
114 
115  /**
116  * Create the lucene index from the given objects
117  * @param $oids The oids to process
118  * @note This is a callback method called on a matching work package @see BatchController::addWorkPackage()
119  */
120  protected function index($oids) {
121  $persistenceFacade = $this->getPersistenceFacade();
122  foreach($oids as $oid) {
123  if (ObjectId::isValid($oid)) {
124  $obj = $persistenceFacade->load($oid);
125  $this->_search->addToIndex($obj);
126  }
127  }
128  $this->_search->commitIndex(false);
129 
130  if ($this->getStepNumber() == $this->getNumberOfSteps()) {
131  $this->addWorkPackage($this->getMessage()->getText('Optimizing index'),
132  1, array(0), 'optimize');
133  }
134  }
135 
136  /**
137  * Optimize the search index
138  * @param $oids The oids to process
139  * @note This is a callback method called on a matching work package @see BatchController::addWorkPackage()
140  */
141  protected function optimize($oids) {
142  $this->_search->optimizeIndex();
143  }
144  // PROTECTED REGION END
145 }
146 ?>
Localization defines the interface for storing localized entity instances and retrieving them back...
getMessage()
Get the Message instance.
Definition: Controller.php:254
addWorkPackage($name, $size, $oids, $callback, $args=null)
Add a work package to session.
__construct(Session $session, PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, ActionMapper $actionMapper, Localization $localization, Message $message, Configuration $configuration, Search $search)
Constructor.
SearchIndexController creates a Lucene index from the complete datastore.
getStepNumber()
Get the number of the current step (1..number of steps).
collect($types)
Collect all oids of the given types.
IndexedSearch implementations are used to search entity objects in a search index.
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
BatchController is used to process complex, longer running actions, that need to be divided into seve...
PermissionManager implementations are used to handle all authorization requests.
Implementations of Configuration give access to the application configuration.
Application controllers.
Definition: namespaces.php:3
static isValid($oid)
Check if a serialized ObjectId has a valid syntax, the type is known and if the number of primary key...
Definition: ObjectId.php:132
ActionMapper implementations are responsible for instantiating and executing Controllers based on the...
PersistenceFacade defines the interface for PersistenceFacade implementations.
Search implementations are used to search entity objects.
Definition: Search.php:21
getNumberOfSteps()
Get the number of steps to process.
index($oids)
Create the lucene index from the given objects.
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222