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