2006-03-26 19:03:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
|
|
|
|
* @package MediaWiki
|
|
|
|
|
* @addtogroup Ajax
|
|
|
|
|
*/
|
|
|
|
|
|
2006-03-26 19:03:14 +00:00
|
|
|
if( !defined( 'MEDIAWIKI' ) )
|
2006-06-07 04:15:58 +00:00
|
|
|
die( 1 );
|
2006-03-26 19:03:14 +00:00
|
|
|
|
|
|
|
|
/**
|
2006-04-19 15:46:24 +00:00
|
|
|
* Function converts an Javascript escaped string back into a string with
|
|
|
|
|
* specified charset (default is UTF-8).
|
2006-03-26 19:03:14 +00:00
|
|
|
* Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $source String escaped with Javascript's escape() function
|
|
|
|
|
* @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
|
2006-03-26 19:03:14 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function js_unescape($source, $iconv_to = 'UTF-8') {
|
|
|
|
|
$decodedStr = '';
|
|
|
|
|
$pos = 0;
|
|
|
|
|
$len = strlen ($source);
|
|
|
|
|
while ($pos < $len) {
|
|
|
|
|
$charAt = substr ($source, $pos, 1);
|
|
|
|
|
if ($charAt == '%') {
|
|
|
|
|
$pos++;
|
|
|
|
|
$charAt = substr ($source, $pos, 1);
|
|
|
|
|
if ($charAt == 'u') {
|
|
|
|
|
// we got a unicode character
|
|
|
|
|
$pos++;
|
|
|
|
|
$unicodeHexVal = substr ($source, $pos, 4);
|
|
|
|
|
$unicode = hexdec ($unicodeHexVal);
|
|
|
|
|
$decodedStr .= code2utf($unicode);
|
|
|
|
|
$pos += 4;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// we have an escaped ascii character
|
|
|
|
|
$hexVal = substr ($source, $pos, 2);
|
|
|
|
|
$decodedStr .= chr (hexdec ($hexVal));
|
|
|
|
|
$pos += 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$decodedStr .= $charAt;
|
|
|
|
|
$pos++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($iconv_to != "UTF-8") {
|
|
|
|
|
$decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-03-26 19:03:14 +00:00
|
|
|
return $decodedStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function coverts number of utf char into that character.
|
|
|
|
|
* Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $num Integer
|
2006-03-26 19:03:14 +00:00
|
|
|
* @return utf8char
|
|
|
|
|
*/
|
|
|
|
|
function code2utf($num){
|
|
|
|
|
if ( $num<128 )
|
|
|
|
|
return chr($num);
|
|
|
|
|
if ( $num<2048 )
|
|
|
|
|
return chr(($num>>6)+192).chr(($num&63)+128);
|
|
|
|
|
if ( $num<65536 )
|
|
|
|
|
return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
|
|
|
|
|
if ( $num<2097152 )
|
|
|
|
|
return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wfSajaxSearch( $term ) {
|
2006-08-29 15:43:34 +00:00
|
|
|
global $wgContLang, $wgOut;
|
2006-03-26 19:03:14 +00:00
|
|
|
$limit = 16;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-11-08 07:12:03 +00:00
|
|
|
$l = new Linker;
|
2006-03-26 19:03:14 +00:00
|
|
|
|
|
|
|
|
$term = str_replace( ' ', '_', $wgContLang->ucfirst(
|
|
|
|
|
$wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
if ( strlen( str_replace( '_', '', $term ) )<3 )
|
|
|
|
|
return;
|
|
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = wfGetDB( DB_SLAVE );
|
2006-03-26 19:03:14 +00:00
|
|
|
$res = $db->select( 'page', 'page_title',
|
|
|
|
|
array( 'page_namespace' => 0,
|
|
|
|
|
"page_title LIKE '". $db->strencode( $term) ."%'" ),
|
|
|
|
|
"wfSajaxSearch",
|
|
|
|
|
array( 'LIMIT' => $limit+1 )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$r = "";
|
|
|
|
|
|
|
|
|
|
$i=0;
|
|
|
|
|
while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
|
|
|
|
|
$nt = Title::newFromDBkey( $row->page_title );
|
2006-11-08 07:12:03 +00:00
|
|
|
$r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
|
2006-03-26 19:03:14 +00:00
|
|
|
}
|
|
|
|
|
if ( $i > $limit ) {
|
2006-11-08 07:12:03 +00:00
|
|
|
$more = '<i>' . $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
|
2006-03-26 19:03:14 +00:00
|
|
|
wfMsg('moredotdotdot'),
|
|
|
|
|
"namespace=0&from=" . wfUrlEncode ( $term ) ) .
|
|
|
|
|
'</i>';
|
|
|
|
|
} else {
|
|
|
|
|
$more = '';
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 23:07:02 +00:00
|
|
|
$subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
|
2006-08-29 15:43:34 +00:00
|
|
|
$subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
|
2006-07-02 22:50:52 +00:00
|
|
|
|
2007-01-17 12:19:53 +00:00
|
|
|
$term = urlencode( $term );
|
2007-01-09 20:25:28 +00:00
|
|
|
$html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
|
2006-03-26 19:03:14 +00:00
|
|
|
. wfMsg( 'hideresults' ) . '</a></div>'
|
|
|
|
|
. '<h1 class="firstHeading">'.wfMsg('search')
|
2006-07-02 22:50:52 +00:00
|
|
|
. '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
|
2006-11-08 07:12:03 +00:00
|
|
|
. $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
|
2006-03-26 19:03:14 +00:00
|
|
|
wfMsg( 'searchcontaining', $term ),
|
|
|
|
|
"search=$term&fulltext=Search" )
|
2006-11-08 07:12:03 +00:00
|
|
|
. '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
|
2006-03-26 19:03:14 +00:00
|
|
|
wfMsg( 'searchnamed', $term ) ,
|
|
|
|
|
"search=$term&go=Go" )
|
|
|
|
|
. "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
|
|
|
|
|
. '<ul>' .$r .'</ul>'.$more;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$response = new AjaxResponse( $html );
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$response->setCacheDuration( 30*60 );
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
return $response;
|
2006-03-26 19:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
2006-12-26 23:53:34 +00:00
|
|
|
/**
|
|
|
|
|
* Called for AJAX watch/unwatch requests.
|
|
|
|
|
* @param $pageID Integer ID of the page to be watched/unwatched
|
|
|
|
|
* @param $watch String 'w' to watch, 'u' to unwatch
|
|
|
|
|
* @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)
|
|
|
|
|
*/
|
Testing AJAX watch/unwatch, with E_STRICT error_reporting:
Prevent :
* Strict Standards: Non-static method Title::newFromID() should not be called statically in includes/AjaxFunctions.php on line 147
[stopped AJAX watch/unwatch working for me, as I have errors being logged to the page output]
E_STRICT warnings on truncated GET input (e.g. http://192.168.0.64/wiki/index.php?action=ajax&rs=wfAjaxWatch&rsargs[]=1 , rather than http://192.168.0.64/wiki/index.php?action=ajax&rs=wfAjaxWatch&rsargs[]=1&rsargs[]=u ), prevent by specifying default $watch value of "" (which should result in an error '<err#>' response, which is probably fine).
* Strict Standards: Missing argument 2 for wfAjaxWatch() in includes/AjaxFunctions.php on line 138
* Strict Standards: Undefined variable: watch in includes/AjaxFunctions.php on line 142
E_STRICT warnings on another truncated GET input ( http://192.168.0.64/wiki/index.php?action=ajax&rs=wfAjaxWatch&rsargs= ), prevent by specifying default $pageID value of "" (which again should result in an error '<err#>' response because it's not numeric, which is probably fine).
* Strict Standards: Missing argument 1 for wfAjaxWatch() in includes/AjaxFunctions.php on line 138
E_STRICT warning on bad GET input - "rs" as array, not as a string - (e.g. http://192.168.0.64/wiki/index.php?action=ajax&rs[]= ), adding explicit cast to string:
* Strict Standards: htmlspecialchars() expects parameter 1 to be string, array given in includes/AjaxDispatcher.php on line 58
2007-01-09 07:05:34 +00:00
|
|
|
function wfAjaxWatch($pageID = "", $watch = "") {
|
2006-12-26 23:53:34 +00:00
|
|
|
if(wfReadOnly())
|
|
|
|
|
return '<err#>'; // redirect to action=(un)watch, which will display the database lock message
|
|
|
|
|
|
|
|
|
|
if(('w' !== $watch && 'u' !== $watch) || !is_numeric($pageID))
|
|
|
|
|
return '<err#>';
|
|
|
|
|
$watch = 'w' === $watch;
|
|
|
|
|
$pageID = intval($pageID);
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromID($pageID);
|
|
|
|
|
if(!$title)
|
|
|
|
|
return '<err#>';
|
|
|
|
|
$article = new Article($title);
|
|
|
|
|
$watching = $title->userIsWatching();
|
|
|
|
|
|
|
|
|
|
if($watch) {
|
|
|
|
|
if(!$watching) {
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB(DB_MASTER);
|
2006-12-26 23:53:34 +00:00
|
|
|
$dbw->begin();
|
|
|
|
|
$article->doWatch();
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if($watching) {
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB(DB_MASTER);
|
2006-12-26 23:53:34 +00:00
|
|
|
$dbw->begin();
|
|
|
|
|
$article->doUnwatch();
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $watch ? '<w#>' : '<u#>';
|
|
|
|
|
}
|
2006-03-26 19:03:14 +00:00
|
|
|
?>
|