More profiling points for Linker::link(). And that's enough work on it for the moment.
[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, $wgUser, $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 $term_title = Title::newFromText( $term );
86
87 $memckey = $term_title ? wfMemcKey( 'ajaxsearch', md5( $term_title->getFullText() ) ) : wfMemcKey( 'ajaxsearch', md5( $term ) );
88 $cached = $wgMemc->get($memckey);
89 if( is_array( $cached ) && $cached['version'] == AJAX_SEARCH_VERSION ) {
90 $response = new AjaxResponse( $cached['html'] );
91 $response->setCacheDuration( 30*60 );
92 return $response;
93 }
94
95 $r = $more = '';
96 $canSearch = true;
97
98 $results = PrefixSearch::titleSearch( $term, $limit + 1 );
99 foreach( array_slice( $results, 0, $limit ) as $titleText ) {
100 $r .= '<li>' . $sk->makeKnownLink( $titleText ) . "</li>\n";
101 }
102
103 // Hack to check for specials
104 if( $results ) {
105 $t = Title::newFromText( $results[0] );
106 if( $t && $t->getNamespace() == NS_SPECIAL ) {
107 $canSearch = false;
108 if( count( $results ) > $limit ) {
109 $more = '<i>' .
110 $sk->makeKnownLinkObj(
111 SpecialPage::getTitleFor( 'Specialpages' ),
112 wfMsgHtml( 'moredotdotdot' ) ) .
113 '</i>';
114 }
115 } else {
116 if( count( $results ) > $limit ) {
117 $more = '<i>' .
118 $sk->makeKnownLinkObj(
119 SpecialPage::getTitleFor( "Allpages", $term ),
120 wfMsgHtml( 'moredotdotdot' ) ) .
121 '</i>';
122 }
123 }
124 }
125
126 $valid = (bool) $term_title;
127 $term_url = urlencode( $term );
128 $term_normalized = $valid ? $term_title->getFullText() : $term;
129 $term_display = htmlspecialchars( $term );
130 $subtitlemsg = ( $valid ? 'searchsubtitle' : 'searchsubtitleinvalid' );
131 $subtitle = wfMsgExt( $subtitlemsg, array( 'parse' ), wfEscapeWikiText( $term_normalized ) );
132 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
133 . wfMsgHtml( 'hideresults' ) . '</a></div>'
134 . '<h1 class="firstHeading">'.wfMsgHtml('search')
135 . '</h1><div id="contentSub">'. $subtitle . '</div>';
136 if( $canSearch ) {
137 $html .= '<ul><li>'
138 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
139 wfMsgHtml( 'searchcontaining', $term_display ),
140 "search={$term_url}&fulltext=Search" )
141 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
142 wfMsgHtml( 'searchnamed', $term_display ) ,
143 "search={$term_url}&go=Go" )
144 . "</li></ul>";
145 }
146 if( $r ) {
147 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_display ) . "</h2>"
148 . '<ul>' .$r .'</ul>' . $more;
149 }
150
151 $wgMemc->set( $memckey, array( 'version' => AJAX_SEARCH_VERSION, 'html' => $html ), 30 * 60 );
152
153 $response = new AjaxResponse( $html );
154 $response->setCacheDuration( 30*60 );
155 return $response;
156 }
157
158 /**
159 * Called for AJAX watch/unwatch requests.
160 * @param $pagename Prefixed title string for page to watch/unwatch
161 * @param $watch String 'w' to watch, 'u' to unwatch
162 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
163 * respectively, followed by an HTML message to display in the alert box; or
164 * '<err#>' on error
165 */
166 function wfAjaxWatch($pagename = "", $watch = "") {
167 if(wfReadOnly()) {
168 // redirect to action=(un)watch, which will display the database lock
169 // message
170 return '<err#>';
171 }
172
173 if('w' !== $watch && 'u' !== $watch) {
174 return '<err#>';
175 }
176 $watch = 'w' === $watch;
177
178 $title = Title::newFromDBkey($pagename);
179 if(!$title) {
180 // Invalid title
181 return '<err#>';
182 }
183 $article = new Article($title);
184 $watching = $title->userIsWatching();
185
186 if($watch) {
187 if(!$watching) {
188 $dbw = wfGetDB(DB_MASTER);
189 $dbw->begin();
190 $article->doWatch();
191 $dbw->commit();
192 }
193 } else {
194 if($watching) {
195 $dbw = wfGetDB(DB_MASTER);
196 $dbw->begin();
197 $article->doUnwatch();
198 $dbw->commit();
199 }
200 }
201 if( $watch ) {
202 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
203 } else {
204 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
205 }
206 }