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