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