1<?php
2if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
3	header("location: index.php");
4	exit;
5}
6
7/**
8 * Create links in tiki_links table for objects other than wiki pages,
9 * comments and forum posts.
10 *
11 * @param Installer $installer
12 */
13function upgrade_20171123_create_object_links_tiki($installer)
14{
15	include_once('tiki-setup.php');
16	$maxRecordsPerQuery = 100;
17
18	$create_links = function ($installer, $type, $objectId, $data) {
19		$parserlib = TikiLib::lib('parser');
20		$pages = $parserlib->get_pages($data);
21
22		$linkhandle = "objectlink:$type:$objectId";
23
24		foreach ($pages as $page) {
25			$installer->query('REPLACE INTO `tiki_links` (`fromPage`, `toPage`) values (?, ?)', [$linkhandle, substr($page, 0, 158)]);
26		}
27	};
28
29	/**
30	 * Blog posts
31	 */
32	$table = $installer->table('tiki_blog_posts');
33
34	$offset = 0;
35	do {
36		$items = $table->fetchAll([], [], $maxRecordsPerQuery, $offset);
37
38		foreach ($items as $item) {
39			$create_links($installer, 'post', $item['postId'], $item['data']);
40		}
41		$resultCount = count($items);
42		$offset += $maxRecordsPerQuery;
43	} while ($resultCount == $maxRecordsPerQuery);
44
45	/**
46	 * Articles
47	 */
48	$table = $installer->table('tiki_articles');
49
50	$offset = 0;
51	do {
52		$items = $table->fetchAll([], [], $maxRecordsPerQuery, $offset);
53
54		foreach ($items as $item) {
55			$data = $item['heading'] . "\n" . $item['body'];
56			$create_links($installer, 'article', $item['articleId'], $data);
57		}
58		$resultCount = count($items);
59		$offset += $maxRecordsPerQuery;
60	} while ($resultCount == $maxRecordsPerQuery);
61
62	/**
63	 * Calendar events
64	 */
65	$table = $installer->table('tiki_calendar_items');
66
67	$offset = 0;
68	do {
69		$items = $table->fetchAll([], [], $maxRecordsPerQuery, $offset);
70
71		foreach ($items as $item) {
72			$create_links($installer, 'calendar event', $item['calitemId'], $item['description']);
73		}
74		$resultCount = count($items);
75		$offset += $maxRecordsPerQuery;
76	} while ($resultCount == $maxRecordsPerQuery);
77
78	/**
79	 * Trackers
80	 */
81	$table = $installer->table('tiki_trackers');
82
83	$offset = 0;
84	do {
85		$items = $table->fetchAll([], [], $maxRecordsPerQuery, $offset);
86
87		foreach ($items as $item) {
88			if ($item['descriptionIsParsed'] == 'y') {
89				$create_links($installer, 'tracker', $item['trackerId'], $item['description']);
90			}
91		}
92		$resultCount = count($items);
93		$offset += $maxRecordsPerQuery;
94	} while ($resultCount == $maxRecordsPerQuery);
95
96	/**
97	 * Tracker fields
98	 */
99	$table = $installer->table('tiki_tracker_fields');
100
101	$offset = 0;
102	do {
103		$items = $table->fetchAll([], [], $maxRecordsPerQuery, $offset);
104
105		foreach ($items as $item) {
106			if ($item['descriptionIsParsed'] == 'y') {
107				$create_links($installer, 'trackerfield', $item['fieldId'], $item['description']);
108			}
109		}
110		$resultCount = count($items);
111		$offset += $maxRecordsPerQuery;
112	} while ($resultCount == $maxRecordsPerQuery);
113
114
115	/**
116	 * Tracker item fields
117	 */
118	$trackerFields = $installer->table('tiki_tracker_fields');
119	$itemFields = $installer->table('tiki_tracker_item_fields');
120	foreach ($trackerFields->fetchAll(['fieldId'], ['type' => 'a']) as $field) {
121		$fieldId = $field['fieldId'];
122
123		$offset = 0;
124		do {
125			$items = $itemFields->fetchAll([], ['fieldId' => (int)$fieldId], $maxRecordsPerQuery, $offset);
126
127			foreach ($items as $itemField) {
128				$objectId = sprintf("%d:%d", (int)$itemField['itemId'], $fieldId);
129				$create_links($installer, 'trackeritemfield', $objectId, $itemField['value']);
130			}
131			$resultCount = count($items);
132			$offset += $maxRecordsPerQuery;
133		} while ($resultCount == $maxRecordsPerQuery);
134	}
135
136	return true;
137}
138