(bug 7098) Add an option to disable/enable sending of HTTP ETag headers,
[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
10 * specified charset (default is UTF-8).
11 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
12 *
13 * @param $source String escaped with Javascript's escape() function
14 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
15 * @return string
16 */
17 function js_unescape($source, $iconv_to = 'UTF-8') {
18 $decodedStr = '';
19 $pos = 0;
20 $len = strlen ($source);
21 while ($pos < $len) {
22 $charAt = substr ($source, $pos, 1);
23 if ($charAt == '%') {
24 $pos++;
25 $charAt = substr ($source, $pos, 1);
26 if ($charAt == 'u') {
27 // we got a unicode character
28 $pos++;
29 $unicodeHexVal = substr ($source, $pos, 4);
30 $unicode = hexdec ($unicodeHexVal);
31 $decodedStr .= code2utf($unicode);
32 $pos += 4;
33 }
34 else {
35 // we have an escaped ascii character
36 $hexVal = substr ($source, $pos, 2);
37 $decodedStr .= chr (hexdec ($hexVal));
38 $pos += 2;
39 }
40 }
41 else {
42 $decodedStr .= $charAt;
43 $pos++;
44 }
45 }
46
47 if ($iconv_to != "UTF-8") {
48 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
49 }
50
51 return $decodedStr;
52 }
53
54 /**
55 * Function coverts number of utf char into that character.
56 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
57 *
58 * @param $num Integer
59 * @return utf8char
60 */
61 function code2utf($num){
62 if ( $num<128 )
63 return chr($num);
64 if ( $num<2048 )
65 return chr(($num>>6)+192).chr(($num&63)+128);
66 if ( $num<65536 )
67 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
68 if ( $num<2097152 )
69 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
70 return '';
71 }
72
73 class AjaxCachePolicy {
74 var $policy;
75 var $vary;
76
77 function AjaxCachePolicy( $policy = null, $vary = null ) {
78 $this->policy = $policy;
79 $this->vary = $vary;
80 }
81
82 function setPolicy( $policy ) {
83 $this->policy = $policy;
84 }
85
86 function setVary( $vary ) {
87 $this->vary = $vary;
88 }
89
90 function writeHeader() {
91 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
92
93 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
94
95 if ( $this->policy ) {
96
97 # If squid caches are configured, tell them to cache the response,
98 # and tell the client to always check with the squid. Otherwise,
99 # tell the client to use a cached copy, without a way to purge it.
100
101 if( $wgUseSquid ) {
102
103 # Expect explicite purge of the proxy cache, but require end user agents
104 # to revalidate against the proxy on each visit.
105 # Surrogate-Control controls our Squid, Cache-Control downstream caches
106
107 if ( $wgUseESI ) {
108 header( 'Surrogate-Control: max-age='.$this->policy.', content="ESI/1.0"');
109 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
110 } else {
111 header( 'Cache-Control: s-maxage='.$this->policy.', must-revalidate, max-age=0' );
112 }
113
114 } else {
115
116 # Let the client do the caching. Cache is not purged.
117 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->policy ) . " GMT");
118 header ("Cache-Control: s-max-age={$this->policy},public,max-age={$this->policy}");
119 }
120
121 } else {
122 # always expired, always modified
123 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
124 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
125 header ("Pragma: no-cache"); // HTTP/1.0
126 }
127
128 if ( $this->vary ) {
129 header ( "Vary: " . $this->vary );
130 }
131 }
132 }
133
134
135 function wfSajaxSearch( $term ) {
136 global $wgContLang, $wgAjaxCachePolicy, $wgOut;
137 $limit = 16;
138
139 $l = new Linker;
140
141 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
142 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
143 ) );
144
145 if ( strlen( str_replace( '_', '', $term ) )<3 )
146 return;
147
148 $wgAjaxCachePolicy->setPolicy( 30*60 );
149
150 $db =& wfGetDB( DB_SLAVE );
151 $res = $db->select( 'page', 'page_title',
152 array( 'page_namespace' => 0,
153 "page_title LIKE '". $db->strencode( $term) ."%'" ),
154 "wfSajaxSearch",
155 array( 'LIMIT' => $limit+1 )
156 );
157
158 $r = "";
159
160 $i=0;
161 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
162 $nt = Title::newFromDBkey( $row->page_title );
163 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
164 }
165 if ( $i > $limit ) {
166 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
167 wfMsg('moredotdotdot'),
168 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
169 '</i>';
170 } else {
171 $more = '';
172 }
173
174 $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
175 $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) );
176
177 $term = htmlspecialchars( $term );
178 return '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
179 . wfMsg( 'hideresults' ) . '</a></div>'
180 . '<h1 class="firstHeading">'.wfMsg('search')
181 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
182 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
183 wfMsg( 'searchcontaining', $term ),
184 "search=$term&fulltext=Search" )
185 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
186 wfMsg( 'searchnamed', $term ) ,
187 "search=$term&go=Go" )
188 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
189 . '<ul>' .$r .'</ul>'.$more;
190 }
191
192 ?>