8 if( !defined( 'MEDIAWIKI' ) ) {
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
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.
21 function js_unescape($source, $iconv_to = 'UTF-8') {
24 $len = strlen ($source);
27 $charAt = substr ($source, $pos, 1);
30 $charAt = substr ($source, $pos, 1);
32 // we got a unicode character
34 $unicodeHexVal = substr ($source, $pos, 4);
35 $unicode = hexdec ($unicodeHexVal);
36 $decodedStr .= code2utf($unicode);
39 // we have an escaped ascii character
40 $hexVal = substr ($source, $pos, 2);
41 $decodedStr .= chr (hexdec ($hexVal));
45 $decodedStr .= $charAt;
50 if ($iconv_to != "UTF-8") {
51 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
58 * Function coverts number of utf char into that character.
59 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
64 function code2utf($num){
68 return chr(($num>>6)+
192).chr(($num&63)+
128);
70 return chr(($num>>12)+
224).chr((($num>>6)&63)+
128).chr(($num&63)+
128);
72 return chr(($num>>18)+
240).chr((($num>>12)&63)+
128).chr((($num>>6)&63)+
128) .chr(($num&63)+
128);
76 define( 'AJAX_SEARCH_VERSION', 2 ); //AJAX search cache version
78 function wfSajaxSearch( $term ) {
79 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks, $wgMemc;
81 $sk = $wgUser->getSkin();
84 $term = trim( $term );
85 $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
86 if ( $wgCapitalLinks )
87 $term = $wgContLang->ucfirst( $term );
88 $term_title = Title
::newFromText( $term );
90 $memckey = $term_title ?
wfMemcKey( 'ajaxsearch', md5( $term_title->getFullText() ) ) : wfMemcKey( 'ajaxsearch', md5( $term ) );
91 $cached = $wgMemc->get($memckey);
92 if( is_array( $cached ) && $cached['version'] == AJAX_SEARCH_VERSION
) {
93 $response = new AjaxResponse( $cached['html'] );
94 $response->setCacheDuration( 30*60 );
101 $results = PrefixSearch
::titleSearch( $term, $limit +
1 );
102 foreach( array_slice( $results, 0, $limit ) as $titleText ) {
103 $r .= '<li>' . $sk->makeKnownLink( $titleText ) . "</li>\n";
106 // Hack to check for specials
108 $t = Title
::newFromText( $results[0] );
109 if( $t && $t->getNamespace() == NS_SPECIAL
) {
111 if( count( $results ) > $limit ) {
113 $sk->makeKnownLinkObj(
114 SpecialPage
::getTitleFor( 'Specialpages' ),
115 wfMsgHtml( 'moredotdotdot' ) ) .
119 if( count( $results ) > $limit ) {
121 $sk->makeKnownLinkObj(
122 SpecialPage
::getTitleFor( "Allpages", $term ),
123 wfMsgHtml( 'moredotdotdot' ) ) .
129 $valid = (bool) $term_title;
130 $term_url = urlencode( $term );
131 $term_normalized = $valid ?
$term_title->getFullText() : $term;
132 $term_display = htmlspecialchars( $term );
133 $subtitlemsg = ( $valid ?
'searchsubtitle' : 'searchsubtitleinvalid' );
134 $subtitle = wfMsgExt( $subtitlemsg, array( 'parse' ), wfEscapeWikiText( $term_normalized ) );
135 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
136 . wfMsgHtml( 'hideresults' ) . '</a></div>'
137 . '<h1 class="firstHeading">'.wfMsgHtml('search')
138 . '</h1><div id="contentSub">'. $subtitle . '</div>';
141 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
142 wfMsgHtml( 'searchcontaining', $term_display ),
143 "search={$term_url}&fulltext=Search" )
144 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
145 wfMsgHtml( 'searchnamed', $term_display ) ,
146 "search={$term_url}&go=Go" )
150 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_display ) . "</h2>"
151 . '<ul>' .$r .'</ul>' . $more;
154 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION
, 'html' => $html ), 30 * 60 );
156 $response = new AjaxResponse( $html );
157 $response->setCacheDuration( 30*60 );
162 * Called for AJAX watch/unwatch requests.
163 * @param $pagename Prefixed title string for page to watch/unwatch
164 * @param $watch String 'w' to watch, 'u' to unwatch
165 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
166 * respectively, followed by an HTML message to display in the alert box; or
169 function wfAjaxWatch($pagename = "", $watch = "") {
171 // redirect to action=(un)watch, which will display the database lock
176 if('w' !== $watch && 'u' !== $watch) {
179 $watch = 'w' === $watch;
181 $title = Title
::newFromDBkey($pagename);
186 $article = new Article($title);
187 $watching = $title->userIsWatching();
191 $dbw = wfGetDB(DB_MASTER
);
198 $dbw = wfGetDB(DB_MASTER
);
200 $article->doUnwatch();
205 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
207 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );