-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallook.php
69 lines (61 loc) · 2.36 KB
/
Callook.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
<?php
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'Callook',
'author' => 'Ricky Elrod',
'url' => 'https://github.com/CodeBlock/mw-callook',
'description' => "This extension provides an easy way to reference ham radio operators.",
'version' => 1.0,
);
$wgHooks['ParserFirstCallInit'][] = 'CallookSetupParserFunction';
$wgExtensionMessagesFiles['Callook'] = dirname( __FILE__ ) . '/Callook.i18n.php';
function CallookSetupParserFunction( &$parser ) {
$parser->setFunctionHook( 'callsign', 'CallsignFunction' );
$parser->setFunctionHook( 'callsignlist', 'CallsignListFunction' );
$parser->setFunctionHook( 'callsignlink', 'CallsignLinkFunction' );
return true;
}
/** Helper function to get callook JSON response. */
function GetCallookJson( $callsign ) {
$json = @file_get_contents( 'http://callook.info/' . urlencode( $callsign ) . '/json' );
$json_decoded = json_decode( $json, true );
return $json_decoded;
}
/** Format a callsign into the following format:
Name Here (<link to callook profile here>)
*/
function FormatSimple( $callsign ) {
$json_decoded = GetCallookJson( $callsign );
if ( $json_decoded === NULL ) {
return 'Unable to look up callsign "' . $callsign . '".';
} else {
$name = ucwords( strtolower( htmlspecialchars( $json_decoded['name'] ) ) );
return $name . ' ([http://callook.info/' . urlencode( $callsign ) . ' ' .
strtoupper( $callsign ) . "])";
}
}
/** Format a callsign by displaying the person's name and callsign (which is
linked to their callook.info profile.).
*/
function CallsignFunction( $parser, $callsign = '' ) {
return array ( FormatSimple( $callsign ) );
}
/** Link a callsign to its callook.info profile. **/
function CallsignLinkFunction( $parser, $callsign = '' ) {
$output = '[http://callook.info/' . urlencode( $callsign ) . ' ' .
strtoupper( $callsign ) . ']';
return array ( $output );
}
/** Take a list of callsigns (comma-separated), and display their owner's name
and callsign (linked to callook.info) as a list element.
*/
function CallsignListFunction( $parser, $callsign = '' ) {
$callsigns = explode( ',', str_replace ( ' ', '', $callsign ) );
$output = '';
$formatted = array_map( 'FormatSimple', $callsigns );
sort( $formatted );
foreach ( $formatted as $i ) {
$output .= '* ' . $i . "\n";
}
return array ( $output );
}