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://sk2.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', 1 ); //AJAX search cache version
78 function wfSajaxSearch( $term ) {
79 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks, $wgMemc;
81 $sk = $wgUser->getSkin();
84 if( !wfRunHooks( 'SajaxSearch', array( $term, &$output ) ) ) {
85 $response = new AjaxResponse( $output );
86 $response->setCacheDuration( 30*60 );
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 );
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 );
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() ) ."%'" ),
112 array( 'LIMIT' => $limit+
1 )
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";
121 $more = '<i>' . $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
122 wfMsg('moredotdotdot'),
123 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
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 )
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>';
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>';
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" )
163 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_diplay ) . "</h2>"
164 . '<ul>' .$r .'</ul>' . $more;
167 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION
, 'html' => $html ), 30 * 60 );
169 $response = new AjaxResponse( $html );
170 $response->setCacheDuration( 30*60 );
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
182 function wfAjaxWatch($pagename = "", $watch = "") {
184 // redirect to action=(un)watch, which will display the database lock
189 if('w' !== $watch && 'u' !== $watch) {
192 $watch = 'w' === $watch;
194 $title = Title
::newFromDBkey($pagename);
199 $article = new Article($title);
200 $watching = $title->userIsWatching();
204 $dbw = wfGetDB(DB_MASTER
);
211 $dbw = wfGetDB(DB_MASTER
);
213 $article->doUnwatch();
218 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
220 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );