Introduce SajaxSearch hook
[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 define( 'AJAX_SEARCH_VERSION', 1 ); //AJAX search cache version
77
78 function wfSajaxSearch( $term ) {
79 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks, $wgMemc;
80 $limit = 16;
81 $sk = $wgUser->getSkin();
82 $output = '';
83
84 if( !wfRunHooks( 'SajaxSearch', array( $term, &$output ) ) ) {
85 $response = new AjaxResponse( $output );
86 $response->setCacheDuration( 30*60 );
87 return $response;
88 }
89
90 $term = trim( $term );
91 $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
92 if ( $wgCapitalLinks )
93 $term = $wgContLang->ucfirst( $term );
94 $term_title = Title::newFromText( $term );
95
96 $memckey = $term_title ? wfMemcKey( 'ajaxsearch', md5( $term_title->getFullText() ) ) : wfMemcKey( 'ajaxsearch', md5( $term ) );
97 $cached = $wgMemc->get($memckey);
98 if( is_array( $cached ) && $cached['version'] == AJAX_SEARCH_VERSION ) {
99 $response = new AjaxResponse( $cached['html'] );
100 $response->setCacheDuration( 30*60 );
101 return $response;
102 }
103
104 $r = $more = '';
105 $canSearch = true;
106 if( $term_title && $term_title->getNamespace() != NS_SPECIAL ) {
107 $db = wfGetDB( DB_SLAVE );
108 $res = $db->select( 'page', array( 'page_title', 'page_namespace' ),
109 array( 'page_namespace' => $term_title->getNamespace(),
110 "page_title LIKE '". $db->strencode( $term_title->getDBkey() ) ."%'" ),
111 "wfSajaxSearch",
112 array( 'LIMIT' => $limit+1 )
113 );
114
115 $i = 0;
116 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
117 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
118 $r .= '<li>' . $sk->makeKnownLinkObj( $nt ) . "</li>\n";
119 }
120 if ( $i > $limit ) {
121 $more = '<i>' . $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
122 wfMsg('moredotdotdot'),
123 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
124 '</i>';
125 }
126 } else if( $term_title && $term_title->getNamespace() == NS_SPECIAL ) {
127 SpecialPage::initList();
128 SpecialPage::initAliasList();
129 $specialPages = array_merge(
130 array_keys( SpecialPage::$mList ),
131 array_keys( SpecialPage::$mAliases )
132 );
133
134 foreach( $specialPages as $page ) {
135 if( $wgContLang->uc( $page ) != $page && strpos( $page, $term_title->getText() ) === 0 ) {
136 $r .= '<li>' . $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, $page ) ) . '</li>';
137 }
138 }
139
140 $canSearch = false;
141 }
142
143 $valid = (bool) $term_title;
144 $term_url = urlencode( $term );
145 $term_diplay = htmlspecialchars( $valid ? $term_title->getFullText() : $term );
146 $subtitlemsg = ( $valid ? 'searchsubtitle' : 'searchsubtitleinvalid' );
147 $subtitle = wfMsgWikiHtml( $subtitlemsg, $term_diplay );
148 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
149 . wfMsgHtml( 'hideresults' ) . '</a></div>'
150 . '<h1 class="firstHeading">'.wfMsgHtml('search')
151 . '</h1><div id="contentSub">'. $subtitle . '</div>';
152 if( $canSearch ) {
153 $html .= '<ul><li>'
154 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
155 wfMsgHtml( 'searchcontaining', $term_diplay ),
156 "search={$term_url}&fulltext=Search" )
157 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
158 wfMsgHtml( 'searchnamed', $term_diplay ) ,
159 "search={$term_url}&go=Go" )
160 . "</li></ul>";
161 }
162 if( $r ) {
163 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_diplay ) . "</h2>"
164 . '<ul>' .$r .'</ul>' . $more;
165 }
166
167 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION, 'html' => $html ), 30 * 60 );
168
169 $response = new AjaxResponse( $html );
170 $response->setCacheDuration( 30*60 );
171 return $response;
172 }
173
174 /**
175 * Called for AJAX watch/unwatch requests.
176 * @param $pagename Prefixed title string for page to watch/unwatch
177 * @param $watch String 'w' to watch, 'u' to unwatch
178 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
179 * respectively, followed by an HTML message to display in the alert box; or
180 * '<err#>' on error
181 */
182 function wfAjaxWatch($pagename = "", $watch = "") {
183 if(wfReadOnly()) {
184 // redirect to action=(un)watch, which will display the database lock
185 // message
186 return '<err#>';
187 }
188
189 if('w' !== $watch && 'u' !== $watch) {
190 return '<err#>';
191 }
192 $watch = 'w' === $watch;
193
194 $title = Title::newFromDBkey($pagename);
195 if(!$title) {
196 // Invalid title
197 return '<err#>';
198 }
199 $article = new Article($title);
200 $watching = $title->userIsWatching();
201
202 if($watch) {
203 if(!$watching) {
204 $dbw = wfGetDB(DB_MASTER);
205 $dbw->begin();
206 $article->doWatch();
207 $dbw->commit();
208 }
209 } else {
210 if($watching) {
211 $dbw = wfGetDB(DB_MASTER);
212 $dbw->begin();
213 $article->doUnwatch();
214 $dbw->commit();
215 }
216 }
217 if( $watch ) {
218 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
219 } else {
220 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
221 }
222 }
223