Ajax based suggest feature for the search box
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2
3 if( !defined( 'MEDIAWIKI' ) )
4 die( -1 );
5
6 require_once('WebRequest.php');
7
8 /**
9 * Function converts an Javascript escaped string back into a string with specified charset (default is UTF-8).
10 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
11 *
12 * @param string $source escaped with Javascript's escape() function
13 * @param string $iconv_to destination character set will be used as second paramether in the iconv function. Default is UTF-8.
14 * @return string
15 */
16 function js_unescape($source, $iconv_to = 'UTF-8') {
17 $decodedStr = '';
18 $pos = 0;
19 $len = strlen ($source);
20 while ($pos < $len) {
21 $charAt = substr ($source, $pos, 1);
22 if ($charAt == '%') {
23 $pos++;
24 $charAt = substr ($source, $pos, 1);
25 if ($charAt == 'u') {
26 // we got a unicode character
27 $pos++;
28 $unicodeHexVal = substr ($source, $pos, 4);
29 $unicode = hexdec ($unicodeHexVal);
30 $decodedStr .= code2utf($unicode);
31 $pos += 4;
32 }
33 else {
34 // we have an escaped ascii character
35 $hexVal = substr ($source, $pos, 2);
36 $decodedStr .= chr (hexdec ($hexVal));
37 $pos += 2;
38 }
39 }
40 else {
41 $decodedStr .= $charAt;
42 $pos++;
43 }
44 }
45
46 if ($iconv_to != "UTF-8") {
47 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
48 }
49
50 return $decodedStr;
51 }
52
53 /**
54 * Function coverts number of utf char into that character.
55 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
56 *
57 * @param int $num
58 * @return utf8char
59 */
60 function code2utf($num){
61 if ( $num<128 )
62 return chr($num);
63 if ( $num<2048 )
64 return chr(($num>>6)+192).chr(($num&63)+128);
65 if ( $num<65536 )
66 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
67 if ( $num<2097152 )
68 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
69 return '';
70 }
71
72 class AjaxCachePolicy {
73 var $policy;
74
75 function AjaxCachePolicy( $policy = null ) {
76 $this->policy = $policy;
77 }
78
79 function setPolicy( $policy ) {
80 $this->policy = $policy;
81 }
82
83 function writeHeader() {
84 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
85 if ( is_null( $this->policy ) ) {
86 // Bust cache in the head
87 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
88 // always modified
89 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
90 header ("Pragma: no-cache"); // HTTP/1.0
91 } else {
92 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->policy ) . " GMT");
93 header ("Cache-Control: public,max-age={$this->policy}");
94 }
95 }
96 }
97
98
99 function wfSajaxSearch( $term ) {
100 global $wgContLang, $wgUser, $wgRequest, $wgAjaxCachePolicy;
101 $limit = 16;
102
103 $l = new Linker;
104
105 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
106 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
107 ) );
108
109 if ( strlen( str_replace( '_', '', $term ) )<3 )
110 return;
111
112 $wgAjaxCachePolicy->setPolicy( 30*60 );
113
114 $db =& wfGetDB( DB_SLAVE );
115 $res = $db->select( 'page', 'page_title',
116 array( 'page_namespace' => 0,
117 "page_title LIKE '". $db->strencode( $term) ."%'" ),
118 "wfSajaxSearch",
119 array( 'LIMIT' => $limit+1 )
120 );
121
122 $r = "";
123
124 $i=0;
125 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
126 $nt = Title::newFromDBkey( $row->page_title );
127 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
128 }
129 if ( $i > $limit ) {
130 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
131 wfMsg('moredotdotdot'),
132 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
133 '</i>';
134 } else {
135 $more = '';
136 }
137
138 $term = htmlspecialchars( $term );
139 return '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
140 . wfMsg( 'hideresults' ) . '</a></div>'
141 . '<h1 class="firstHeading">'.wfMsg('search')
142 . '</h1><div id="contentSub">'.wfMsg('searchquery', $term) . '</div><ul><li>'
143 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
144 wfMsg( 'searchcontaining', $term ),
145 "search=$term&fulltext=Search" )
146 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
147 wfMsg( 'searchnamed', $term ) ,
148 "search=$term&go=Go" )
149 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
150 . '<ul>' .$r .'</ul>'.$more;
151 }
152
153 ?>