6 if( !defined( 'MEDIAWIKI' ) ) {
11 * Function converts an Javascript escaped string back into a string with
12 * specified charset (default is UTF-8).
13 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
15 * @param $source String escaped with Javascript's escape() function
16 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
19 function js_unescape($source, $iconv_to = 'UTF-8') {
22 $len = strlen ($source);
25 $charAt = substr ($source, $pos, 1);
28 $charAt = substr ($source, $pos, 1);
30 // we got a unicode character
32 $unicodeHexVal = substr ($source, $pos, 4);
33 $unicode = hexdec ($unicodeHexVal);
34 $decodedStr .= code2utf($unicode);
37 // we have an escaped ascii character
38 $hexVal = substr ($source, $pos, 2);
39 $decodedStr .= chr (hexdec ($hexVal));
43 $decodedStr .= $charAt;
48 if ($iconv_to != "UTF-8") {
49 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
56 * Function coverts number of utf char into that character.
57 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
62 function code2utf($num){
66 return chr(($num>>6)+
192).chr(($num&63)+
128);
68 return chr(($num>>12)+
224).chr((($num>>6)&63)+
128).chr(($num&63)+
128);
70 return chr(($num>>18)+
240).chr((($num>>12)&63)+
128).chr((($num>>6)&63)+
128) .chr(($num&63)+
128);
74 define( 'AJAX_SEARCH_VERSION', 2 ); //AJAX search cache version
76 function wfSajaxSearch( $term ) {
77 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks, $wgMemc;
79 $sk = $wgUser->getSkin();
82 $term = trim( $term );
83 $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
84 if ( $wgCapitalLinks )
85 $term = $wgContLang->ucfirst( $term );
86 $term_title = Title
::newFromText( $term );
88 $memckey = $term_title ?
wfMemcKey( 'ajaxsearch', md5( $term_title->getFullText() ) ) : wfMemcKey( 'ajaxsearch', md5( $term ) );
89 $cached = $wgMemc->get($memckey);
90 if( is_array( $cached ) && $cached['version'] == AJAX_SEARCH_VERSION
) {
91 $response = new AjaxResponse( $cached['html'] );
92 $response->setCacheDuration( 30*60 );
99 $results = PrefixSearch
::titleSearch( $term, $limit +
1 );
100 foreach( array_slice( $results, 0, $limit ) as $titleText ) {
101 $r .= '<li>' . $sk->makeKnownLink( $titleText ) . "</li>\n";
104 // Hack to check for specials
106 $t = Title
::newFromText( $results[0] );
107 if( $t && $t->getNamespace() == NS_SPECIAL
) {
109 if( count( $results ) > $limit ) {
111 $sk->makeKnownLinkObj(
112 SpecialPage
::getTitleFor( 'Specialpages' ),
113 wfMsgHtml( 'moredotdotdot' ) ) .
117 if( count( $results ) > $limit ) {
119 $sk->makeKnownLinkObj(
120 SpecialPage
::getTitleFor( "Allpages", $term ),
121 wfMsgHtml( 'moredotdotdot' ) ) .
127 $valid = (bool) $term_title;
128 $term_url = urlencode( $term );
129 $term_normalized = $valid ?
$term_title->getFullText() : $term;
130 $term_display = htmlspecialchars( $term );
131 $subtitlemsg = ( $valid ?
'searchsubtitle' : 'searchsubtitleinvalid' );
132 $subtitle = wfMsgExt( $subtitlemsg, array( 'parse' ), wfEscapeWikiText( $term_normalized ) );
133 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
134 . wfMsgHtml( 'hideresults' ) . '</a></div>'
135 . '<h1 class="firstHeading">'.wfMsgHtml('search')
136 . '</h1><div id="contentSub">'. $subtitle . '</div>';
139 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
140 wfMsgHtml( 'searchcontaining', $term_display ),
141 "search={$term_url}&fulltext=Search" )
142 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
143 wfMsgHtml( 'searchnamed', $term_display ) ,
144 "search={$term_url}&go=Go" )
148 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_display ) . "</h2>"
149 . '<ul>' .$r .'</ul>' . $more;
152 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION
, 'html' => $html ), 30 * 60 );
154 $response = new AjaxResponse( $html );
155 $response->setCacheDuration( 30*60 );
160 * Called for AJAX watch/unwatch requests.
161 * @param $pagename Prefixed title string for page to watch/unwatch
162 * @param $watch String 'w' to watch, 'u' to unwatch
163 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
164 * respectively, followed by an HTML message to display in the alert box; or
167 function wfAjaxWatch($pagename = "", $watch = "") {
169 // redirect to action=(un)watch, which will display the database lock
174 if('w' !== $watch && 'u' !== $watch) {
177 $watch = 'w' === $watch;
179 $title = Title
::newFromDBkey($pagename);
184 $article = new Article($title);
185 $watching = $title->userIsWatching();
189 $dbw = wfGetDB(DB_MASTER
);
196 $dbw = wfGetDB(DB_MASTER
);
198 $article->doUnwatch();
203 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
205 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );