|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
$wgExtensionCredits['parserhook'][] = array( |
|
4
|
|
|
'name'=>'NRC - Recommendations', |
|
5
|
|
|
'url'=>'http://www.nrc.gc.ca', |
|
6
|
|
|
'author'=>'Luc Belliveau', |
|
7
|
|
|
'description'=>'Recommendation extension based on engines provided by the National Research Council.', |
|
8
|
|
|
'version'=>'0.0.1' |
|
9
|
|
|
); |
|
10
|
|
|
$wgHooks['OutputPageBeforeHTML'][] = 'RecommendationsInit'; |
|
11
|
|
|
|
|
12
|
|
|
function RecommendationsInit($article, &$text) { |
|
|
|
|
|
|
13
|
|
|
global $wgTitle, $wgUser; |
|
14
|
|
|
|
|
15
|
|
|
$loggedIn = $wgUser->mId != 0; |
|
16
|
|
|
$email = str_replace("'", "\\'", $wgUser->mEmail); |
|
17
|
|
|
if (strpos($email, "\n") !== false) return false; |
|
18
|
|
|
|
|
19
|
|
|
$articleId = $wgTitle->mArticleID; |
|
20
|
|
|
$context_obj1 = ''; |
|
21
|
|
|
if ($articleId == 1) { |
|
22
|
|
|
if ($loggedIn) { |
|
23
|
|
|
$context = 'article_c1'; |
|
24
|
|
|
} else { |
|
25
|
|
|
$context = 'article_c2'; |
|
26
|
|
|
} |
|
27
|
|
|
} else { |
|
28
|
|
|
$context_obj1 = $articleId; |
|
29
|
|
|
if ($loggedIn) { |
|
30
|
|
|
$context = 'article_c3'; |
|
31
|
|
|
} else { |
|
32
|
|
|
$context = 'article_c4'; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Automatically get the user's GCconnex GUID, solely based on email. |
|
38
|
|
|
* |
|
39
|
|
|
* TODO: This must be replaced by SSO. |
|
40
|
|
|
*/ |
|
41
|
|
|
$login_context = 'false'; |
|
42
|
|
|
if ($loggedIn && !isset($_SESSION['GCconnex_GUID'])) { |
|
43
|
|
|
$cx = stream_context_create([ |
|
44
|
|
|
'http'=> [ |
|
45
|
|
|
'proxy'=>'tcp://proxy.paz.nrc.gc.ca:8080', |
|
46
|
|
|
'request_fulluri' => true, |
|
47
|
|
|
], |
|
48
|
|
|
// 'https'=>['proxy'=>'proxy.paz.nrc.gc.ca:8080'], |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$searchUrl = "http://intranet.canada.ca/search-recherche"; |
|
52
|
|
|
$query = "query-recherche-eng.aspx?a=s&s=3&chk4=on"; |
|
53
|
|
|
$encodedEmail = urlencode($email); |
|
54
|
|
|
$data = file_get_contents("$searchUrl/$query&q=$encodedEmail", False, $cx); |
|
55
|
|
|
$re = '/https:\/\/gcconnex.gc.ca\/profile\/(.*?)"/'; |
|
56
|
|
|
if (preg_match($re, $data, $m) === 1) { |
|
57
|
|
|
$gcconnex_username = $m[1]; |
|
58
|
|
|
$profile_url = "https://gcconnex.gc.ca/profile/$gcconnex_username"; |
|
59
|
|
|
$profileData = file_get_contents($profile_url, False, $cx); |
|
60
|
|
|
$re = '/"guid":(.*?),/'; |
|
61
|
|
|
if (preg_match($re, $profileData, $pm)) { |
|
62
|
|
|
$_SESSION['GCconnex_GUID'] = $pm[1]; |
|
63
|
|
|
$login_context = 'true'; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($loggedIn) { |
|
69
|
|
|
$gcconnex_guid = str_replace("'", "\\'", $_SESSION['GCconnex_GUID']); |
|
70
|
|
|
if (strpos($gcconnex_guid, "\n") !== false) return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$script = <<<DOC |
|
74
|
|
|
<style> |
|
75
|
|
|
.nrcRecommendationContainer { |
|
76
|
|
|
overflow: hidden; |
|
77
|
|
|
padding-top: 10px; |
|
78
|
|
|
} |
|
79
|
|
|
.nrcRecommendationContainer:hover { |
|
80
|
|
|
overflow: visible; |
|
81
|
|
|
} |
|
82
|
|
|
.nrcRecommendationContainer div.fieldset-container { |
|
83
|
|
|
border: 1px solid #000; |
|
84
|
|
|
background-color: #eee; |
|
85
|
|
|
width: 500px; |
|
86
|
|
|
z-index: 1; |
|
87
|
|
|
position: relative; |
|
88
|
|
|
} |
|
89
|
|
|
</style> |
|
90
|
|
|
<link rel="stylesheet" type="text/css" href="/extensions/NRC_Recommendations/static/css/main.css"> |
|
91
|
|
|
<script type="text/javascript"> |
|
92
|
|
|
var NRC_context = { |
|
93
|
|
|
context: '$context', |
|
94
|
|
|
context_obj1: '$context_obj1', |
|
95
|
|
|
email: '$email', |
|
96
|
|
|
gcconnex_guid: '$gcconnex_guid', |
|
|
|
|
|
|
97
|
|
|
login_context: $login_context, |
|
98
|
|
|
} |
|
99
|
|
|
</script> |
|
100
|
|
|
<script src="/extensions/NRC_Recommendations/gcpedia.js"></script> |
|
101
|
|
|
DOC; |
|
102
|
|
|
|
|
103
|
|
|
$text = $text . "\n\n" .$script; |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: