7 if( !defined( 'MEDIAWIKI' ) ) {
12 * Function converts an Javascript escaped string back into a string with
13 * specified charset (default is UTF-8).
14 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
16 * @param $source String escaped with Javascript's escape() function
17 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
20 function js_unescape($source, $iconv_to = 'UTF-8') {
23 $len = strlen ($source);
26 $charAt = substr ($source, $pos, 1);
29 $charAt = substr ($source, $pos, 1);
31 // we got a unicode character
33 $unicodeHexVal = substr ($source, $pos, 4);
34 $unicode = hexdec ($unicodeHexVal);
35 $decodedStr .= code2utf($unicode);
38 // we have an escaped ascii character
39 $hexVal = substr ($source, $pos, 2);
40 $decodedStr .= chr (hexdec ($hexVal));
44 $decodedStr .= $charAt;
49 if ($iconv_to != "UTF-8") {
50 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
57 * Function coverts number of utf char into that character.
58 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
63 function code2utf($num){
67 return chr(($num>>6)+
192).chr(($num&63)+
128);
69 return chr(($num>>12)+
224).chr((($num>>6)&63)+
128).chr(($num&63)+
128);
71 return chr(($num>>18)+
240).chr((($num>>12)&63)+
128).chr((($num>>6)&63)+
128) .chr(($num&63)+
128);
75 define( 'AJAX_SEARCH_VERSION', 2 ); //AJAX search cache version
77 function wfSajaxSearch( $term ) {
78 global $wgContLang, $wgUser, $wgCapitalLinks, $wgMemc;
80 $sk = $wgUser->getSkin();
83 $term = trim( $term );
84 $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
85 if ( $wgCapitalLinks )
86 $term = $wgContLang->ucfirst( $term );
87 $term_title = Title
::newFromText( $term );
89 $memckey = $term_title ?
wfMemcKey( 'ajaxsearch', md5( $term_title->getFullText() ) ) : wfMemcKey( 'ajaxsearch', md5( $term ) );
90 $cached = $wgMemc->get($memckey);
91 if( is_array( $cached ) && $cached['version'] == AJAX_SEARCH_VERSION
) {
92 $response = new AjaxResponse( $cached['html'] );
93 $response->setCacheDuration( 30*60 );
100 $results = PrefixSearch
::titleSearch( $term, $limit +
1 );
101 foreach( array_slice( $results, 0, $limit ) as $titleText ) {
102 $r .= '<li>' . $sk->makeKnownLink( $titleText ) . "</li>\n";
105 // Hack to check for specials
107 $t = Title
::newFromText( $results[0] );
108 if( $t && $t->getNamespace() == NS_SPECIAL
) {
110 if( count( $results ) > $limit ) {
112 $sk->makeKnownLinkObj(
113 SpecialPage
::getTitleFor( 'Specialpages' ),
114 wfMsgHtml( 'moredotdotdot' ) ) .
118 if( count( $results ) > $limit ) {
120 $sk->makeKnownLinkObj(
121 SpecialPage
::getTitleFor( "Allpages", $term ),
122 wfMsgHtml( 'moredotdotdot' ) ) .
128 $valid = (bool) $term_title;
129 $term_url = urlencode( $term );
130 $term_normalized = $valid ?
$term_title->getFullText() : $term;
131 $term_display = htmlspecialchars( $term );
132 $subtitlemsg = ( $valid ?
'searchsubtitle' : 'searchsubtitleinvalid' );
133 $subtitle = wfMsgExt( $subtitlemsg, array( 'parse' ), wfEscapeWikiText( $term_normalized ) );
134 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
135 . wfMsgHtml( 'hideresults' ) . '</a></div>'
136 . '<h1 class="firstHeading">'.wfMsgHtml('search')
137 . '</h1><div id="contentSub">'. $subtitle . '</div>';
140 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
141 wfMsgHtml( 'searchcontaining', $term_display ),
142 "search={$term_url}&fulltext=Search" )
143 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
144 wfMsgHtml( 'searchnamed', $term_display ) ,
145 "search={$term_url}&go=Go" )
149 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_display ) . "</h2>"
150 . '<ul>' .$r .'</ul>' . $more;
153 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION
, 'html' => $html ), 30 * 60 );
155 $response = new AjaxResponse( $html );
156 $response->setCacheDuration( 30*60 );
161 * Called for AJAX watch/unwatch requests.
162 * @param $pagename Prefixed title string for page to watch/unwatch
163 * @param $watch String 'w' to watch, 'u' to unwatch
164 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
165 * respectively, followed by an HTML message to display in the alert box; or
168 function wfAjaxWatch($pagename = "", $watch = "") {
170 // redirect to action=(un)watch, which will display the database lock
175 if('w' !== $watch && 'u' !== $watch) {
178 $watch = 'w' === $watch;
180 $title = Title
::newFromDBkey($pagename);
185 $article = new Article($title);
186 $watching = $title->userIsWatching();
190 $dbw = wfGetDB(DB_MASTER
);
197 $dbw = wfGetDB(DB_MASTER
);
199 $article->doUnwatch();
204 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
206 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );