- $term = htmlspecialchars( $term );
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2
3 if( !defined( 'MEDIAWIKI' ) )
4 die( 1 );
5
6 /**
7 * Function converts an Javascript escaped string back into a string with
8 * specified charset (default is UTF-8).
9 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
10 *
11 * @param $source String escaped with Javascript's escape() function
12 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
13 * @return string
14 */
15 function js_unescape($source, $iconv_to = 'UTF-8') {
16 $decodedStr = '';
17 $pos = 0;
18 $len = strlen ($source);
19 while ($pos < $len) {
20 $charAt = substr ($source, $pos, 1);
21 if ($charAt == '%') {
22 $pos++;
23 $charAt = substr ($source, $pos, 1);
24 if ($charAt == 'u') {
25 // we got a unicode character
26 $pos++;
27 $unicodeHexVal = substr ($source, $pos, 4);
28 $unicode = hexdec ($unicodeHexVal);
29 $decodedStr .= code2utf($unicode);
30 $pos += 4;
31 }
32 else {
33 // we have an escaped ascii character
34 $hexVal = substr ($source, $pos, 2);
35 $decodedStr .= chr (hexdec ($hexVal));
36 $pos += 2;
37 }
38 }
39 else {
40 $decodedStr .= $charAt;
41 $pos++;
42 }
43 }
44
45 if ($iconv_to != "UTF-8") {
46 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
47 }
48
49 return $decodedStr;
50 }
51
52 /**
53 * Function coverts number of utf char into that character.
54 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
55 *
56 * @param $num Integer
57 * @return utf8char
58 */
59 function code2utf($num){
60 if ( $num<128 )
61 return chr($num);
62 if ( $num<2048 )
63 return chr(($num>>6)+192).chr(($num&63)+128);
64 if ( $num<65536 )
65 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
66 if ( $num<2097152 )
67 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
68 return '';
69 }
70
71 function wfSajaxSearch( $term ) {
72 global $wgContLang, $wgOut;
73 $limit = 16;
74
75 $l = new Linker;
76
77 $term = str_replace( ' ', '_', $wgContLang->ucfirst(
78 $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
79 ) );
80
81 if ( strlen( str_replace( '_', '', $term ) )<3 )
82 return;
83
84 $db =& wfGetDB( DB_SLAVE );
85 $res = $db->select( 'page', 'page_title',
86 array( 'page_namespace' => 0,
87 "page_title LIKE '". $db->strencode( $term) ."%'" ),
88 "wfSajaxSearch",
89 array( 'LIMIT' => $limit+1 )
90 );
91
92 $r = "";
93
94 $i=0;
95 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
96 $nt = Title::newFromDBkey( $row->page_title );
97 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
98 }
99 if ( $i > $limit ) {
100 $more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
101 wfMsg('moredotdotdot'),
102 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
103 '</i>';
104 } else {
105 $more = '';
106 }
107
108 $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
109 $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
110
111 $term = urlencode( $term );
112 $html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
113 . wfMsg( 'hideresults' ) . '</a></div>'
114 . '<h1 class="firstHeading">'.wfMsg('search')
115 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
116 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
117 wfMsg( 'searchcontaining', $term ),
118 "search=$term&fulltext=Search" )
119 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
120 wfMsg( 'searchnamed', $term ) ,
121 "search=$term&go=Go" )
122 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
123 . '<ul>' .$r .'</ul>'.$more;
124
125 $response = new AjaxResponse( $html );
126
127 $response->setCacheDuration( 30*60 );
128
129 return $response;
130 }
131
132 /**
133 * Called for AJAX watch/unwatch requests.
134 * @param $pageID Integer ID of the page to be watched/unwatched
135 * @param $watch String 'w' to watch, 'u' to unwatch
136 * @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)
137 */
138 function wfAjaxWatch($pageID = "", $watch = "") {
139 if(wfReadOnly())
140 return '<err#>'; // redirect to action=(un)watch, which will display the database lock message
141
142 if(('w' !== $watch && 'u' !== $watch) || !is_numeric($pageID))
143 return '<err#>';
144 $watch = 'w' === $watch;
145 $pageID = intval($pageID);
146
147 $title = Title::newFromID($pageID);
148 if(!$title)
149 return '<err#>';
150 $article = new Article($title);
151 $watching = $title->userIsWatching();
152
153 if($watch) {
154 if(!$watching) {
155 $dbw =& wfGetDB(DB_MASTER);
156 $dbw->begin();
157 $article->doWatch();
158 $dbw->commit();
159 }
160 } else {
161 if($watching) {
162 $dbw =& wfGetDB(DB_MASTER);
163 $dbw->begin();
164 $article->doUnwatch();
165 $dbw->commit();
166 }
167 }
168
169 return $watch ? '<w#>' : '<u#>';
170 }
171 ?>