Fix output of wfSajaxSearch:
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2
3 /**
4 * @package MediaWiki
5 * @addtogroup Ajax
6 */
7
8 if( !defined( 'MEDIAWIKI' ) ) {
9 die( 1 );
10 }
11
12 /**
13 * Function converts an Javascript escaped string back into a string with
14 * specified charset (default is UTF-8).
15 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
16 *
17 * @param $source String escaped with Javascript's escape() function
18 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
19 * @return string
20 */
21 function js_unescape($source, $iconv_to = 'UTF-8') {
22 $decodedStr = '';
23 $pos = 0;
24 $len = strlen ($source);
25
26 while ($pos < $len) {
27 $charAt = substr ($source, $pos, 1);
28 if ($charAt == '%') {
29 $pos++;
30 $charAt = substr ($source, $pos, 1);
31 if ($charAt == 'u') {
32 // we got a unicode character
33 $pos++;
34 $unicodeHexVal = substr ($source, $pos, 4);
35 $unicode = hexdec ($unicodeHexVal);
36 $decodedStr .= code2utf($unicode);
37 $pos += 4;
38 } else {
39 // we have an escaped ascii character
40 $hexVal = substr ($source, $pos, 2);
41 $decodedStr .= chr (hexdec ($hexVal));
42 $pos += 2;
43 }
44 } else {
45 $decodedStr .= $charAt;
46 $pos++;
47 }
48 }
49
50 if ($iconv_to != "UTF-8") {
51 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
52 }
53
54 return $decodedStr;
55 }
56
57 /**
58 * Function coverts number of utf char into that character.
59 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
60 *
61 * @param $num Integer
62 * @return utf8char
63 */
64 function code2utf($num){
65 if ( $num<128 )
66 return chr($num);
67 if ( $num<2048 )
68 return chr(($num>>6)+192).chr(($num&63)+128);
69 if ( $num<65536 )
70 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
71 if ( $num<2097152 )
72 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
73 return '';
74 }
75
76 function wfSajaxSearch( $term ) {
77 global $wgContLang, $wgOut;
78 $limit = 16;
79
80 $l = new Linker;
81
82 $term = trim( $term );
83 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
84 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
85 ) );
86
87 if ( strlen( str_replace( '_', '', $term ) )<3 )
88 return;
89
90 $db = wfGetDB( DB_SLAVE );
91 $res = $db->select( 'page', 'page_title',
92 array( 'page_namespace' => 0,
93 "page_title LIKE '". $db->strencode( $term) ."%'" ),
94 "wfSajaxSearch",
95 array( 'LIMIT' => $limit+1 )
96 );
97
98 $r = "";
99
100 $i=0;
101 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
102 $nt = Title::newFromDBkey( $row->page_title );
103 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
104 }
105 if ( $i > $limit ) {
106 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
107 wfMsg('moredotdotdot'),
108 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
109 '</i>';
110 } else {
111 $more = '';
112 }
113
114 $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
115 $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
116
117 $term_url = urlencode( $term );
118 $term_diplay = htmlspecialchars( str_replace( '_', ' ', $term ) );
119 $html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
120 . wfMsgHtml( 'hideresults' ) . '</a></div>'
121 . '<h1 class="firstHeading">'.wfMsgHtml('search')
122 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
123 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
124 wfMsgHtml( 'searchcontaining', $term_diplay ),
125 "search={$term_url}&fulltext=Search" )
126 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
127 wfMsgHtml( 'searchnamed', $term_diplay ) ,
128 "search={$term_url}&go=Go" )
129 . "</li></ul><h2>" . wfMsgHtml( 'articletitles', $term_diplay ) . "</h2>"
130 . '<ul>' .$r .'</ul>'.$more;
131
132 $response = new AjaxResponse( $html );
133
134 $response->setCacheDuration( 30*60 );
135
136 return $response;
137 }
138
139 /**
140 * Called for AJAX watch/unwatch requests.
141 * @param $pagename Prefixed title string for page to watch/unwatch
142 * @param $watch String 'w' to watch, 'u' to unwatch
143 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
144 * respectively, followed by an HTML message to display in the alert box; or
145 * '<err#>' on error
146 */
147 function wfAjaxWatch($pagename = "", $watch = "") {
148 if(wfReadOnly()) {
149 // redirect to action=(un)watch, which will display the database lock
150 // message
151 return '<err#>';
152 }
153
154 if('w' !== $watch && 'u' !== $watch) {
155 return '<err#>';
156 }
157 $watch = 'w' === $watch;
158
159 $title = Title::newFromText($pagename);
160 if(!$title) {
161 // Invalid title
162 return '<err#>';
163 }
164 $article = new Article($title);
165 $watching = $title->userIsWatching();
166
167 if($watch) {
168 if(!$watching) {
169 $dbw = wfGetDB(DB_MASTER);
170 $dbw->begin();
171 $article->doWatch();
172 $dbw->commit();
173 }
174 } else {
175 if($watching) {
176 $dbw = wfGetDB(DB_MASTER);
177 $dbw->begin();
178 $article->doUnwatch();
179 $dbw->commit();
180 }
181 }
182 if( $watch ) {
183 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
184 } else {
185 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
186 }
187 }
188