-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinktypes.php
193 lines (182 loc) · 6.91 KB
/
linktypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
//////////////////////////////////////////////////////////////
//
// Copyright (C) Thomas Kock, Delmenhorst, 2008, 2009
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// http://www.gnu.org/copyleft/gpl.html
//
//////////////////////////////////////////////////////////////
if (!defined('MEDIAWIKI'))
die();
// Translate links
$wgHooks['InternalParseBeforeLinks'][] = 'fchw_ParseLinks';
// Each page save - store relations for current page
$wgHooks['LinksUpdateConstructed'][] = 'fchw_UpdateLinks';
// Delete page - delete relations
$wgHooks['ArticleDelete'][] = 'fchw_DeleteLinks';
// Move page - move relations
$wgHooks['TitleMoveComplete'][]='fchw_MoveLinks';
// Undelete page - recreate relations
$wgHooks['ArticleUndelete'][] = 'fchw_UndeleteLinks';
global $fchw;
$fchw['ParseLinks'] = 1;
// parse links when displaying page
function fchw_ParseLinks(&$parser, &$text) {
global $fchw;
if ($fchw['ParseLinks'] == 0)
return true;
$output = "";
$lastch = "";
$LinkStarted = false;
$Len = strlen($text);
for($i=0; $i<$Len; $i++) {
$ch = $text[$i];
if (($ch == "[") && ($lastch == "[")) {
$LinkStarted = true;
$LinkText = "";
}
if (($ch == "]") && ($lastch == "]")) {
$LinkStarted = false;
if (strpos($LinkText, "::") > 0) {
$Relation = substr($LinkText, 1, strpos($LinkText, "::")-1);
$LinkTmp = substr($LinkText, strpos($LinkText, "::")+2, -1);
$output .= "[$LinkTmp|$Relation::$LinkTmp]";
} else {
$output .= $LinkText;
}
}
if ($LinkStarted)
$LinkText .= $ch;
else
$output .= $ch;
$lastch = $ch;
}
$text = $output;
return true;
}
// each save - fill relations table for current page
function fchw_UpdateLinks($linksupdate) {
global $wgDBprefix;
global $fchw, $wgParser;
if ($fchw['ParseLinks'] == 0)
return true;
$fchw['ParseLinks'] = 0;
$options = new ParserOptions();
$title = Title::newFromID($linksupdate->mId);
if (($title === NULL)) continue;
$revision = Revision::newFromTitle($title);
if ($revision === NULL) continue;
$parserOutput = $wgParser->parse($revision->getText(), $title, $options, true, true, $revision->getID());
$linksupdate = new LinksUpdate($title, $parserOutput);
$linksupdate->doUpdate();
$fchw['Pages'] = fchw_GetPages();
$fchw['dbr'] = &wfGetDB(DB_SLAVE);
$fchw['table_relation'] = $fchw['dbr']->tableName('fchw_relation');
// <patch by Gerrit I.>
//$sql = "delete from ".$fchw['table_relation']." where from_title like '".$fchw['dbr']->strencode($linksupdate->mTitle->mPrefixedText)."'";
//$From_title = $linksupdate->mTitle->mPrefixedText;
$From_title = $linksupdate->mTitle->getPrefixedText();
if (strrpos($From_title, ":") > 0)
$From_title = substr($From_title, strpos($From_title, ":")+1);
$sql = "delete from ".$fchw['table_relation']." where from_title like '".$fchw['dbr']->strencode($From_title)."'";
// </patch by Gerrit I.>
$res = $fchw['dbr']->query($sql);
$cnt = 1;
foreach($linksupdate->mLinks as $Key2=>$Value2) {
foreach($Value2 as $Key=>$Value) {
fchw_SaveLink($linksupdate, $Key);
$cnt++;
}
}
$fchw['ParseLinks'] = 1;
return true;
}
// Delete links when deleting page
// added &$error for compatibility reasons with calling function.
function fchw_DeleteLinks(&$article, &$user, &$reason, &$error) {
global $fchw;
global $wgDBprefix;
$fchw['dbr'] = &wfGetDB(DB_SLAVE);
$fchw['table_relation'] = $fchw['dbr']->tableName('fchw_relation');
$sql = "delete from ".$fchw['table_relation']." where from_id = '".$article->getID()."'";
$res = $fchw['dbr']->query($sql);
return true;
}
// Move links when moving page
function fchw_MoveLinks(&$title, &$newtitle, &$user, $oldid, $newid) {
global $fchw;
global $wgDBprefix;
$fchw['dbr'] = &wfGetDB(DB_SLAVE);
$fchw['table_relation'] = $fchw['dbr']->tableName('fchw_relation');
$fchw['Pages'] = fchw_GetPages();
$fchw['dbr']->update($fchw['table_relation'],
array('from_title' => $newtitle),
array('from_title' => $title),
'fchw_MoveLinks');
$fchw['dbr']->update($fchw['table_relation'],
array('to_title' => $newtitle),
array('to_title' => $title),
'fchw_MoveLinks');
//die ("$title, $newtitle, $oldid, $newid");
return true;
}
// Undelete links when un-deleting page
function fchw_UndeleteLinks($title, $create) {
global $fchw;
global $wgDBprefix;
$fchw['dbr'] = &wfGetDB(DB_SLAVE);
$fchw['table_relation'] = $fchw['dbr']->tableName('fchw_relation');
$fchw['Pages'] = fchw_GetPages();
$fchw['dbr']->update($fchw['table_relation'],
array('to_id' => $fchw['Pages']["$title"]),
array('to_title' => $title),
'fchw_UndeleteLinks');
return true;
}
// Save link - save relation to database
function fchw_SaveLink($linksupdate, $Link) {
global $fchw;
if (!isset($fchw['RedirectedPages']))
$fchw['RedirectedPages'] = fchw_GetRedirectedPages();
if (strpos($Link, "::") > 0) {
$Relation = substr($Link, 0, strpos($Link, "::"));
$To_title = substr($Link, strpos($Link, "::")+2);
$To_id = 0;
//$From_title = $linksupdate->mTitle->mPrefixedText;
$From_title = $linksupdate->mTitle->getPrefixedText();
if (strrpos($From_title, ":") > 0)
$From_title = substr($From_title, strpos($From_title, ":")+1);
if (strrpos($To_title, ":") > 0)
$To_title = substr($To_title, strpos($To_title, ":")+1);
if (isset($fchw['Pages'][$To_title]))
$To_id = $fchw['Pages'][$To_title];
// REDIRECTED pages
if (isset($fchw['RedirectedPages'][$To_id])) {
$To_title = $fchw['RedirectedPages'][$To_id]['to_title'];
$To_id = $fchw['RedirectedPages'][$To_id]['to_id'];
}
//
$fchw['dbr']->insert($fchw['table_relation'],
array(
'from_id' => $linksupdate->mId,
'from_title' => $From_title,
'to_id' => $To_id,
'to_title' => $To_title,
'relation' => $Relation
), 'fchw_SaveLink');
}
return true;
}