* (bug 12451) AJAX title normalization tweaks
[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 function wfSajaxSearch( $term ) {
77 global $wgContLang, $wgOut, $wgUser, $wgCapitalLinks;
78 $limit = 16;
79 $sk = $wgUser->getSkin();
80
81 $term = trim( $term );
82 $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
83 if ( $wgCapitalLinks )
84 $term = $wgContLang->ucfirst( $term );
85 $term_title = Title::newFromText( $term );
86
87 $r = $more = '';
88 $canSearch = true;
89 if( $term_title && $term_title->getNamespace() != NS_SPECIAL ) {
90 $db = wfGetDB( DB_SLAVE );
91 $res = $db->select( 'page', array( 'page_title', 'page_namespace' ),
92 array( 'page_namespace' => $term_title->getNamespace(),
93 "page_title LIKE '". $db->strencode( $term_title->getDBKey() ) ."%'" ),
94 "wfSajaxSearch",
95 array( 'LIMIT' => $limit+1 )
96 );
97
98 $i = 0;
99 while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
100 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
101 $r .= '<li>' . $sk->makeKnownLinkObj( $nt ) . "</li>\n";
102 }
103 if ( $i > $limit ) {
104 $more = '<i>' . $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
105 wfMsg('moredotdotdot'),
106 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
107 '</i>';
108 }
109 } else if( $term_title && $term_title->getNamespace() == NS_SPECIAL ) {
110 SpecialPage::initList();
111 SpecialPage::initAliasList();
112 $specialPages = array_merge(
113 array_keys( SpecialPage::$mList ),
114 array_keys( SpecialPage::$mAliases )
115 );
116
117 foreach( $specialPages as $page ) {
118 if( $wgContLang->uc( $page ) != $page && strpos( $page, $term_title->getText() ) === 0 ) {
119 $r .= '<li>' . $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, $page ) ) . '</li>';
120 }
121 }
122
123 $canSearch = false;
124 }
125
126 $valid = (bool) $term_title;
127 $term_url = urlencode( $term );
128 $term_diplay = htmlspecialchars( $valid ? $term_title->getFullText() : $term );
129 $subtitlemsg = ( $valid ? 'searchsubtitle' : 'searchsubtitleinvalid' );
130 $subtitle = wfMsgWikiHtml( $subtitlemsg, $term_diplay );
131 $html = '<div id="searchTargetHide"><a onclick="Searching_Hide_Results();">'
132 . wfMsgHtml( 'hideresults' ) . '</a></div>'
133 . '<h1 class="firstHeading">'.wfMsgHtml('search')
134 . '</h1><div id="contentSub">'. $subtitle . '</div>';
135 if( $canSearch ) {
136 $html .= '<ul><li>'
137 . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
138 wfMsgHtml( 'searchcontaining', $term_diplay ),
139 "search={$term_url}&fulltext=Search" )
140 . '</li><li>' . $sk->makeKnownLink( $wgContLang->specialPage( 'Search' ),
141 wfMsgHtml( 'searchnamed', $term_diplay ) ,
142 "search={$term_url}&go=Go" )
143 . "</li></ul>";
144 }
145 if( $r ) {
146 $html .= "<h2>" . wfMsgHtml( 'articletitles', $term_diplay ) . "</h2>"
147 . '<ul>' .$r .'</ul>' . $more;
148 }
149
150 $response = new AjaxResponse( $html );
151
152 $response->setCacheDuration( 30*60 );
153
154 return $response;
155 }
156
157 /**
158 * Called for AJAX watch/unwatch requests.
159 * @param $pagename Prefixed title string for page to watch/unwatch
160 * @param $watch String 'w' to watch, 'u' to unwatch
161 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
162 * respectively, followed by an HTML message to display in the alert box; or
163 * '<err#>' on error
164 */
165 function wfAjaxWatch($pagename = "", $watch = "") {
166 if(wfReadOnly()) {
167 // redirect to action=(un)watch, which will display the database lock
168 // message
169 return '<err#>';
170 }
171
172 if('w' !== $watch && 'u' !== $watch) {
173 return '<err#>';
174 }
175 $watch = 'w' === $watch;
176
177 $title = Title::newFromDBkey($pagename);
178 if(!$title) {
179 // Invalid title
180 return '<err#>';
181 }
182 $article = new Article($title);
183 $watching = $title->userIsWatching();
184
185 if($watch) {
186 if(!$watching) {
187 $dbw = wfGetDB(DB_MASTER);
188 $dbw->begin();
189 $article->doWatch();
190 $dbw->commit();
191 }
192 } else {
193 if($watching) {
194 $dbw = wfGetDB(DB_MASTER);
195 $dbw->begin();
196 $article->doUnwatch();
197 $dbw->commit();
198 }
199 }
200 if( $watch ) {
201 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
202 } else {
203 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
204 }
205 }
206