[TASK] Remove superfluous parenthesis in sysexts
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / Example / CrawlerHook.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Example;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2001-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Index search crawler hook example
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * Index search crawler hook example
36 *
37 * @package TYPO3
38 * @subpackage tx_indexedsearch
39 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
40 */
41 class CrawlerHook {
42
43 /**
44 * Function is called when an indexing session starts according to the time intervals set for the indexing configuration.
45 *
46 * @return string Return a text string for the first, initiating queue entry for the crawler.
47 * @todo Define visibility
48 */
49 public 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 * @todo Define visibility
65 */
66 public function indexOperation($cfgRec, &$session_data, $params, &$pObj) {
67 // Init session data array if not already:
68 if (!is_array($session_data)) {
69 $session_data = array(
70 'step' => 0
71 );
72 }
73 // 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)
74 $session_data['step']++;
75 switch ((int) $session_data['step']) {
76 case 1:
77 // Indexing Example: Content accessed with GET parameters added to URL:
78 // Load indexer if not yet [DON'T CHANGE]:
79 $pObj->loadIndexerClass();
80 // Get rootline from the Indexing Record (needed because the indexer relates all search results to a position in the page tree!) [DON'T CHANGE]:
81 $rl = $pObj->getUidRootLineForClosestTemplate($cfgRec['pid']);
82 // Set up language uid, if any:
83 $sys_language_uid = 0;
84 // Set up 2 example items to index:
85 $exampleItems = array(
86 array(
87 'ID' => '123',
88 'title' => 'Title of Example 1',
89 'content' => 'Vestibulum leo turpis, fringilla sit amet, semper eget, vestibulum ut, arcu. Vestibulum mauris orci, vulputate quis, congue eget, nonummy'
90 ),
91 array(
92 'ID' => 'example2',
93 'title' => 'Title of Example 2',
94 'content' => 'Cras tortor turpis, vulputate non, accumsan a, pretium in, magna. Cras turpis turpis, pretium pulvinar, pretium vel, nonummy eu.'
95 )
96 );
97 // 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!)
98 foreach ($exampleItems as $item) {
99 // Prepare the GET variables array that must be added to the page URL in order to view result:
100 parse_str('&itemID=' . rawurlencode($item['ID']), $GETparams);
101 // 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]:
102 $indexerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_indexedsearch_indexer');
103 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl, $GETparams, FALSE);
104 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
105 $indexerObj->forceIndexing = TRUE;
106 // Indexing the content of the item (see tx_indexedsearch_indexer::backend_indexAsTYPO3Page() for options)
107 $indexerObj->backend_indexAsTYPO3Page($item['title'], '', '', $item['content'], $GLOBALS['LANG']->charSet, $item['tstamp'], $item['create_date'], $item['ID']);
108 }
109 break;
110 case 2:
111 // Indexing Example: Content accessed directly in file system:
112 // Load indexer if not yet [DON'T CHANGE]:
113 $pObj->loadIndexerClass();
114 // Get rootline from the Indexing Record (needed because the indexer relates all search results to a position in the page tree!) [DON'T CHANGE]:
115 $rl = $pObj->getUidRootLineForClosestTemplate($cfgRec['pid']);
116 // Set up language uid, if any:
117 $sys_language_uid = 0;
118 // 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]:
119 $indexerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_indexedsearch_indexer');
120 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl);
121 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
122 $indexerObj->hash['phash'] = -1;
123 // To avoid phash_t3 being written to file sections (otherwise they are removed when page is reindexed!!!)
124 // Index document:
125 $indexerObj->indexRegularDocument('fileadmin/templates/index.html', TRUE);
126 break;
127 case 3:
128 // Indexing Example: Content accessed on External URLs:
129 // Load indexer if not yet.
130 $pObj->loadIndexerClass();
131 // Index external URL:
132 $indexerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_indexedsearch_indexer');
133 $indexerObj->backend_initIndexer($cfgRec['pid'], 0, $sys_language_uid, '', $rl);
134 $indexerObj->backend_setFreeIndexUid($cfgRec['uid'], $cfgRec['set_id']);
135 $indexerObj->hash['phash'] = -1;
136 // To avoid phash_t3 being written to file sections (otherwise they are removed when page is reindexed!!!)
137 // Index external URL (HTML only):
138 $indexerObj->indexExternalUrl('http://www.google.com/');
139 break;
140 }
141 // Finally, set entry for next indexing instance (if all steps are not completed)
142 if ($session_data['step'] <= 3) {
143 $title = 'Step #' . $session_data['step'] . ' of 3';
144 // 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.
145 $pObj->addQueueEntryForHook($cfgRec, $title);
146 }
147 }
148
149 }
150
151
152 ?>