Add the other existing $skin.css/.js to the message files too to be consistent
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
6
7 if( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 /**
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
15 *
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.
18 * @return string
19 */
20 function js_unescape($source, $iconv_to = 'UTF-8') {
21 $decodedStr = '';
22 $pos = 0;
23 $len = strlen ($source);
24
25 while ($pos < $len) {
26 $charAt = substr ($source, $pos, 1);
27 if ($charAt == '%') {
28 $pos++;
29 $charAt = substr ($source, $pos, 1);
30 if ($charAt == 'u') {
31 // we got a unicode character
32 $pos++;
33 $unicodeHexVal = substr ($source, $pos, 4);
34 $unicode = hexdec ($unicodeHexVal);
35 $decodedStr .= code2utf($unicode);
36 $pos += 4;
37 } else {
38 // we have an escaped ascii character
39 $hexVal = substr ($source, $pos, 2);
40 $decodedStr .= chr (hexdec ($hexVal));
41 $pos += 2;
42 }
43 } else {
44 $decodedStr .= $charAt;
45 $pos++;
46 }
47 }
48
49 if ($iconv_to != "UTF-8") {
50 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
51 }
52
53 return $decodedStr;
54 }
55
56 /**
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
59 *
60 * @param $num Integer
61 * @return utf8char
62 */
63 function code2utf($num){
64 if ( $num<128 )
65 return chr($num);
66 if ( $num<2048 )
67 return chr(($num>>6)+192).chr(($num&63)+128);
68 if ( $num<65536 )
69 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
70 if ( $num<2097152 )
71 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
72 return '';
73 }
74
75 define( 'AJAX_SEARCH_VERSION', 2 ); //AJAX search cache version
76
77 function wfSajaxSearch( $term ) {
78 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks, $wgMemc;
79 $limit = 16;
80 $sk = $wgUser->getSkin();
81 $output = '';
82
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 );
88
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 );
94 return $response;
95 }
96
97 $r = $more = '';
98 $canSearch = true;
99
100 $results = PrefixSearch::titleSearch( $term, $limit + 1 );
101 foreach( array_slice( $results, 0, $limit ) as $titleText ) {
102 $r .= '<li>' . $sk->makeKnownLink( $titleText ) . "</li>\n";
103 }
104
105 // Hack to check for specials
106 if( $results ) {
107 $t = Title::newFromText( $results[0] );
108 if( $t && $t->getNamespace() == NS_SPECIAL ) {
109 $canSearch = false;
110 if( count( $results ) > $limit ) {
111 $more = '<i>' .
112 $sk->makeKnownLinkObj(
113 SpecialPage::getTitleFor( 'Specialpages' ),
114 wfMsgHtml( 'moredotdotdot' ) ) .
115 '</i>';
116 }
117 } else {
118 if( count( $results ) > $limit ) {
119 $more = '<i>' .
120 $sk->makeKnownLinkObj(
121 SpecialPage::getTitleFor( "Allpages", $term ),
122 wfMsgHtml( 'moredotdotdot' ) ) .
123 '</i>';
124 }
125 }
126 }
127
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>';
138 if( $canSearch ) {
139 $html .= '<ul><li>'
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" )
146 . "</li></ul>";
147 }
148 if( $r ) {
149 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_display ) . "</h2>"
150 . '<ul>' .$r .'</ul>' . $more;
151 }
152
153 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION, 'html' => $html ), 30 * 60 );
154
155 $response = new AjaxResponse( $html );
156 $response->setCacheDuration( 30*60 );
157 return $response;
158 }
159
160 /**
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
166 * '<err#>' on error
167 */
168 function wfAjaxWatch($pagename = "", $watch = "") {
169 if(wfReadOnly()) {
170 // redirect to action=(un)watch, which will display the database lock
171 // message
172 return '<err#>';
173 }
174
175 if('w' !== $watch && 'u' !== $watch) {
176 return '<err#>';
177 }
178 $watch = 'w' === $watch;
179
180 $title = Title::newFromDBkey($pagename);
181 if(!$title) {
182 // Invalid title
183 return '<err#>';
184 }
185 $article = new Article($title);
186 $watching = $title->userIsWatching();
187
188 if($watch) {
189 if(!$watching) {
190 $dbw = wfGetDB(DB_MASTER);
191 $dbw->begin();
192 $article->doWatch();
193 $dbw->commit();
194 }
195 } else {
196 if($watching) {
197 $dbw = wfGetDB(DB_MASTER);
198 $dbw->begin();
199 $article->doUnwatch();
200 $dbw->commit();
201 }
202 }
203 if( $watch ) {
204 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
205 } else {
206 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
207 }
208 }