[TASK] Remove function index
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / example / class.crawlerhook.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2001-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Index search crawler hook example
29 *
30 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
31 */
32
33
34
35 /**
36 * Index search crawler hook example
37 *
38 * @package TYPO3
39 * @subpackage tx_indexedsearch
40 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
41 */
42 class tx_indexedsearch_crawlerhook {
43
44 /**
45 * Function is called when an indexing session starts according to the time intervals set for the indexing configuration.
46 *
47 * @return string Return a text string for the first, initiating queue entry for the crawler.
48 */
49 function initMessage() {
50 return 'Start of Custom Example Indexing session!';
51 }
52
53 /**
54 * This will do two things:
55 * 1) Carry out actual indexing of content (one or more items)
56 * 2) Add one or more new entries into the crawlers queue so we are called again (another instance) for further indexing in the session (optional of course, if all indexing is done, we add no new entries)
57 *
58 * @param array Indexing Configuration Record (the record which holds the information that lead to this indexing session...)
59 * @param array Session data variable. Passed by reference. Changed content is saved and passed back upon next instance in the session.
60 * @param array Params array from the queue entry.
61 * @param object Grant Parent Object (from "crawler" extension)
62 * @param object Parent Object (from "indexed_search" extension)
63 * @return void
64 */
65 function indexOperation($cfgRec,&$session_data,$params,&$pObj) {
66
67 // Init session data array if not already:
68 if (!is_array($session_data)) {
69 $session_data = array(
70 'step' => 0
71 );
72 }
73
74 // Increase step counter (this is just an example of how the session data can be used - to track how many instances of indexing is left)
75 $session_data['step']++;
76
77
78 switch((int)$session_data['step']) {
79 case 1: // Indexing Example: Content accessed with GET parameters added to URL:
80
81 // Load indexer if not yet [DON'T CHANGE]:
82 $pObj->loadIndexerClass();
83
84 // Get rootline from the Indexing Record (needed because the indexer relates all search results to a position in the page tree!) [DON'T CHANGE]:
85 $rl = $pObj->getUidRootLineForClosestTemplate($cfgRec['pid']);
86
87 // Set up language uid, if any:
88 $sys_language_uid = 0;
89
90 // Set up 2 example items to index:
91 $exampleItems = array(
92 array(
93 'ID' => '123',
94 'title' => 'Title of Example 1',
95 'content' => 'Vestibulum leo turpis, fringilla sit amet, semper eget, vestibulum ut, arcu. Vestibulum mauris orci, vulputate quis, congue eget, nonummy'
96 ),
97 array(
98 'ID' => 'example2',
99 'title' => 'Title of Example 2',
100 'content' => 'Cras tortor turpis, vulputate non, accumsan a, pretium in, magna. Cras turpis turpis, pretium pulvinar, pretium vel, nonummy eu.'
101 )
102 );
103
104 // For each item, index it (this is what you might like to do in batches of like 100 items if all your content spans thousands of items!)
105 foreach($exampleItems as $item) {
106
107 // Prepare the GET variables array that must be added to the page URL in order to view result:
108 parse_str('&itemID='.rawurlencode($item['ID']), $GETparams);
109
110 // Prepare indexer (make instance, initialize it, set special features for indexing parameterized content - probably none of this should be changed by you) [DON'T CHANGE]:
111 $indexerObj = t3lib_div::makeInstance('tx_indexedsearch_indexer');
112 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl, $GETparams, FALSE);
113 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
114 $indexerObj->forceIndexing = TRUE;
115
116 // Indexing the content of the item (see tx_indexedsearch_indexer::backend_indexAsTYPO3Page() for options)
117 $indexerObj->backend_indexAsTYPO3Page(
118 $item['title'],
119 '',
120 '',
121 $item['content'],
122 $GLOBALS['LANG']->charSet, // Charset of content - MUST be set.
123 $item['tstamp'], // Last-modified date
124 $item['create_date'], // Created date
125 $item['ID']
126 );
127 }
128 break;
129 case 2: // Indexing Example: Content accessed directly in file system:
130
131 // Load indexer if not yet [DON'T CHANGE]:
132 $pObj->loadIndexerClass();
133
134 // Get rootline from the Indexing Record (needed because the indexer relates all search results to a position in the page tree!) [DON'T CHANGE]:
135 $rl = $pObj->getUidRootLineForClosestTemplate($cfgRec['pid']);
136
137 // Set up language uid, if any:
138 $sys_language_uid = 0;
139
140 // Prepare indexer (make instance, initialize it, set special features for indexing parameterized content - probably none of this should be changed by you) [DON'T CHANGE]:
141 $indexerObj = t3lib_div::makeInstance('tx_indexedsearch_indexer');
142 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl);
143 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
144 $indexerObj->hash['phash'] = -1; // To avoid phash_t3 being written to file sections (otherwise they are removed when page is reindexed!!!)
145
146 // Index document:
147 $indexerObj->indexRegularDocument('fileadmin/templates/index.html', TRUE);
148 break;
149 case 3: // Indexing Example: Content accessed on External URLs:
150
151 // Load indexer if not yet.
152 $pObj->loadIndexerClass();
153
154 // Index external URL:
155 $indexerObj = t3lib_div::makeInstance('tx_indexedsearch_indexer');
156 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl);
157 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
158 $indexerObj->hash['phash'] = -1; // To avoid phash_t3 being written to file sections (otherwise they are removed when page is reindexed!!!)
159
160 // Index external URL (HTML only):
161 $indexerObj->indexExternalUrl('http://www.google.com/');
162 break;
163 }
164
165 // Finally, set entry for next indexing instance (if all steps are not completed)
166 if ($session_data['step']<=3) {
167 $title = 'Step #'.$session_data['step'].' of 3'; // Just information field. Never mind that the field is called "url" - this is what will be shown in the "crawler" log. Could be a URL - or whatever else tells what that indexing instance will do.
168 $pObj->addQueueEntryForHook($cfgRec, $title);
169 }
170 }
171 }
172
173
174 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/example/class.crawlerhook.php'])) {
175 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/example/class.crawlerhook.php']);
176 }
177
178 ?>