"searchsubtitleinvalid" message for searches that are not valid titles
[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
76 function AjaxCachePolicy( $policy = null ) {
77 $this->policy = $policy;
78 }
79
80 function setPolicy( $policy ) {
81 $this->policy = $policy;
82 }
83
84 function writeHeader() {
85 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
86 if ( is_null( $this->policy ) ) {
87 // Bust cache in the head
88 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
89 // always modified
90 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
91 header ("Pragma: no-cache"); // HTTP/1.0
92 } else {
93 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->policy ) . " GMT");
94 header ("Cache-Control: s-max-age={$this->policy},public,max-age={$this->policy}");
95 }
96 }
97 }
98
99
100 function wfSajaxSearch( $term ) {
101 global $wgContLang, $wgAjaxCachePolicy, $wgOut;
102 $limit = 16;
103
104 $l = new Linker;
105
106 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
107 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
108 ) );
109
110 if ( strlen( str_replace( '_', '', $term ) )<3 )
111 return;
112
113 $wgAjaxCachePolicy->setPolicy( 30*60 );
114
115 $db =& wfGetDB( DB_SLAVE );
116 $res = $db->select( 'page', 'page_title',
117 array( 'page_namespace' => 0,
118 "page_title LIKE '". $db->strencode( $term) ."%'" ),
119 "wfSajaxSearch",
120 array( 'LIMIT' => $limit+1 )
121 );
122
123 $r = "";
124
125 $i=0;
126 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
127 $nt = Title::newFromDBkey( $row->page_title );
128 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
129 }
130 if ( $i > $limit ) {
131 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
132 wfMsg('moredotdotdot'),
133 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
134 '</i>';
135 } else {
136 $more = '';
137 }
138
139 $subtitlemsg = ( Title::newFromtext($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
140 $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) );
141
142 $term = htmlspecialchars( $term );
143 return '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
144 . wfMsg( 'hideresults' ) . '</a></div>'
145 . '<h1 class="firstHeading">'.wfMsg('search')
146 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
147 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
148 wfMsg( 'searchcontaining', $term ),
149 "search=$term&fulltext=Search" )
150 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
151 wfMsg( 'searchnamed', $term ) ,
152 "search=$term&go=Go" )
153 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
154 . '<ul>' .$r .'</ul>'.$more;
155 }
156
157 ?>