Slowly merge in a few rev_deleted checks
[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://sk2.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_diplay = htmlspecialchars( $valid ? $term_title->getFullText() : $term );
132 $subtitlemsg = ( $valid ? 'searchsubtitle' : 'searchsubtitleinvalid' );
133 $subtitle = wfMsgWikiHtml( $subtitlemsg, $term_diplay );
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_diplay ),
142 "search={$term_url}&fulltext=Search" )
143 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
144 wfMsgHtml( 'searchnamed', $term_diplay ) ,
145 "search={$term_url}&go=Go" )
146 . "</li></ul>";
147 }
148 if( $r ) {
149 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_diplay ) . "</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 }
209