Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / AjaxFunctions.php
blob86f853dbd740ce598c5e6930726efaeb3bdeb814
1 <?php
3 /**
4 * @package MediaWiki
5 * @addtogroup Ajax
6 */
8 if( !defined( 'MEDIAWIKI' ) ) {
9 die( 1 );
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
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
21 function js_unescape($source, $iconv_to = 'UTF-8') {
22 $decodedStr = '';
23 $pos = 0;
24 $len = strlen ($source);
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;
44 } else {
45 $decodedStr .= $charAt;
46 $pos++;
50 if ($iconv_to != "UTF-8") {
51 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
54 return $decodedStr;
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
61 * @param $num Integer
62 * @return utf8char
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 '';
76 function wfSajaxSearch( $term ) {
77 global $wgContLang, $wgOut;
78 $limit = 16;
80 $l = new Linker;
82 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
83 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
84 ) );
86 if ( strlen( str_replace( '_', '', $term ) )<3 )
87 return;
89 $db = wfGetDB( DB_SLAVE );
90 $res = $db->select( 'page', 'page_title',
91 array( 'page_namespace' => 0,
92 "page_title LIKE '". $db->strencode( $term) ."%'" ),
93 "wfSajaxSearch",
94 array( 'LIMIT' => $limit+1 )
97 $r = "";
99 $i=0;
100 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
101 $nt = Title::newFromDBkey( $row->page_title );
102 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
104 if ( $i > $limit ) {
105 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
106 wfMsg('moredotdotdot'),
107 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
108 '</i>';
109 } else {
110 $more = '';
113 $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
114 $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
116 $term = urlencode( $term );
117 $html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
118 . wfMsg( 'hideresults' ) . '</a></div>'
119 . '<h1 class="firstHeading">'.wfMsg('search')
120 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
121 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
122 wfMsg( 'searchcontaining', $term ),
123 "search=$term&fulltext=Search" )
124 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
125 wfMsg( 'searchnamed', $term ) ,
126 "search=$term&go=Go" )
127 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
128 . '<ul>' .$r .'</ul>'.$more;
130 $response = new AjaxResponse( $html );
132 $response->setCacheDuration( 30*60 );
134 return $response;
138 * Called for AJAX watch/unwatch requests.
139 * @param $pageID Integer ID of the page to be watched/unwatched
140 * @param $watch String 'w' to watch, 'u' to unwatch
141 * @return String '<w#>' or '<u#>' on successful watch or unwatch, respectively, or '<err#>' on error (invalid XML in case we want to add HTML sometime)
143 function wfAjaxWatch($pageID = "", $watch = "") {
144 if(wfReadOnly())
145 return '<err#>'; // redirect to action=(un)watch, which will display the database lock message
147 if(('w' !== $watch && 'u' !== $watch) || !is_numeric($pageID))
148 return '<err#>';
149 $watch = 'w' === $watch;
150 $pageID = intval($pageID);
152 $title = Title::newFromID($pageID);
153 if(!$title)
154 return '<err#>';
155 $article = new Article($title);
156 $watching = $title->userIsWatching();
158 if($watch) {
159 if(!$watching) {
160 $dbw = wfGetDB(DB_MASTER);
161 $dbw->begin();
162 $article->doWatch();
163 $dbw->commit();
165 } else {
166 if($watching) {
167 $dbw = wfGetDB(DB_MASTER);
168 $dbw->begin();
169 $article->doUnwatch();
170 $dbw->commit();
174 return $watch ? '<w#>' : '<u#>';