Pass the user as an argument to 'isValidPassword' hook callbacks; see docs/hooks...
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
index 9804a24..4fb76dc 100644 (file)
@@ -1,9 +1,13 @@
 <?php
 
-if( !defined( 'MEDIAWIKI' ) )
-        die( 1 );
+/** 
+ * @package MediaWiki
+ * @addtogroup Ajax
+ */
 
-require_once('WebRequest.php');
+if( !defined( 'MEDIAWIKI' ) ) {
+       die( 1 );
+}
 
 /**
  * Function converts an Javascript escaped string back into a string with
@@ -15,40 +19,39 @@ require_once('WebRequest.php');
  * @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);
-   }
-  
-   return $decodedStr;
+       $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);
+       }
+
+       return $decodedStr;
 }
 
 /**
@@ -70,72 +73,10 @@ function code2utf($num){
    return '';
 }
 
-class AjaxCachePolicy {
-       var $policy;
-       var $vary;
-
-       function AjaxCachePolicy( $policy = null, $vary = null ) {
-               $this->policy = $policy;
-               $this->vary = $vary;
-       }
-
-       function setPolicy( $policy ) {
-               $this->policy = $policy;
-       }
-
-       function setVary( $vary ) {
-               $this->vary = $vary;
-       }
-
-       function writeHeader() {
-               global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
-               
-               header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
-               
-               if ( $this->policy ) {
-                       
-                       # If squid caches are configured, tell them to cache the response, 
-                       # and tell the client to always check with the squid. Otherwise,
-                       # tell the client to use a cached copy, without a way to purge it.
-                       
-                       if( $wgUseSquid ) {
-                               
-                               # Expect explicite purge of the proxy cache, but require end user agents
-                               # to revalidate against the proxy on each visit.
-                               # Surrogate-Control controls our Squid, Cache-Control downstream caches
-                               
-                               if ( $wgUseESI ) {
-                                       header( 'Surrogate-Control: max-age='.$this->policy.', content="ESI/1.0"');
-                                       header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
-                               } else {
-                                       header( 'Cache-Control: s-maxage='.$this->policy.', must-revalidate, max-age=0' );
-                               }
-                               
-                       } else {
-                       
-                               # Let the client do the caching. Cache is not purged.
-                               header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->policy ) . " GMT");
-                               header ("Cache-Control: s-max-age={$this->policy},public,max-age={$this->policy}");
-                       }
-                       
-               } else {
-                       # always expired, always modified
-                       header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
-                       header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
-                       header ("Pragma: no-cache");                          // HTTP/1.0
-               }
-               
-               if ( $this->vary ) {
-                       header ( "Vary: " . $this->vary );
-               }
-       }
-}
-                       
-
 function wfSajaxSearch( $term ) {
-       global $wgContLang, $wgAjaxCachePolicy, $wgOut;
+       global $wgContLang, $wgOut;
        $limit = 16;
-       
+
        $l = new Linker;
 
        $term = str_replace( ' ', '_', $wgContLang->ucfirst( 
@@ -145,9 +86,7 @@ function wfSajaxSearch( $term ) {
        if ( strlen( str_replace( '_', '', $term ) )<3 )
                return;
 
-       $wgAjaxCachePolicy->setPolicy( 30*60 );
-
-       $db =& wfGetDB( DB_SLAVE );
+       $db = wfGetDB( DB_SLAVE );
        $res = $db->select( 'page', 'page_title',
                        array(  'page_namespace' => 0,
                                "page_title LIKE '". $db->strencode( $term) ."%'" ),
@@ -172,10 +111,10 @@ function wfSajaxSearch( $term ) {
        }
 
        $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
-       $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) );
+       $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
 
-       $term = htmlspecialchars( $term );
-       return '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">' 
+       $term = urlencode( $term );
+       $html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
                . wfMsg( 'hideresults' ) . '</a></div>'
                . '<h1 class="firstHeading">'.wfMsg('search')
                . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
@@ -187,6 +126,61 @@ function wfSajaxSearch( $term ) {
                                        "search=$term&go=Go" )
                . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
                . '<ul>' .$r .'</ul>'.$more;
+
+       $response = new AjaxResponse( $html );
+
+       $response->setCacheDuration( 30*60 );
+
+       return $response;
+}
+
+/**
+ * Called for AJAX watch/unwatch requests.
+ * @param $pagename Prefixed title string for page to watch/unwatch
+ * @param $watch String 'w' to watch, 'u' to unwatch
+ * @return String '<w#>' or '<u#>' on successful watch or unwatch, 
+ *   respectively, followed by an HTML message to display in the alert box; or
+ *   '<err#>' on error
+ */
+function wfAjaxWatch($pagename = "", $watch = "") {
+       if(wfReadOnly()) {
+               // redirect to action=(un)watch, which will display the database lock
+               // message
+               return '<err#>'; 
+       }
+
+       if('w' !== $watch && 'u' !== $watch) {
+               return '<err#>';
+       }
+       $watch = 'w' === $watch;
+
+       $title = Title::newFromText($pagename);
+       if(!$title) {
+               // Invalid title
+               return '<err#>';
+       }
+       $article = new Article($title);
+       $watching = $title->userIsWatching();
+
+       if($watch) {
+               if(!$watching) {
+                       $dbw = wfGetDB(DB_MASTER);
+                       $dbw->begin();
+                       $article->doWatch();
+                       $dbw->commit();
+               }
+       } else {
+               if($watching) {
+                       $dbw = wfGetDB(DB_MASTER);
+                       $dbw->begin();
+                       $article->doUnwatch();
+                       $dbw->commit();
+               }
+       }
+       if( $watch ) {
+               return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
+       } else {
+               return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
+       }
 }
 
-?>