/**
* Called for AJAX watch/unwatch requests.
- * @param $pageID Integer ID of the page to be watched/unwatched
+ * @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, or '<err#>' on error (invalid XML in case we want to add HTML sometime)
+ * @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($pageID = "", $watch = "") {
- if(wfReadOnly())
- return '<err#>'; // redirect to action=(un)watch, which will display the database lock message
+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) || !is_numeric($pageID))
+ if('w' !== $watch && 'u' !== $watch) {
return '<err#>';
+ }
$watch = 'w' === $watch;
- $pageID = intval($pageID);
- $title = Title::newFromID($pageID);
- if(!$title)
+ $title = Title::newFromText($pagename);
+ if(!$title) {
+ // Invalid title
return '<err#>';
+ }
$article = new Article($title);
$watching = $title->userIsWatching();
$dbw->commit();
}
}
-
- return $watch ? '<w#>' : '<u#>';
+ if( $watch ) {
+ return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
+ } else {
+ return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
+ }
}
?>
// * ajax.js:
/*extern sajax_init_object, sajax_do_call */
// * wikibits.js:
- /*extern changeText, akeytt, hookEvent */
+ /*extern changeText, akeytt, hookEvent, alertMsg */
// These should have been initialized in the generated js
-/*extern wgAjaxWatch, wgArticleId */
+/*extern wgAjaxWatch, wgPageName */
if(typeof wgAjaxWatch === "undefined" || !wgAjaxWatch) {
var wgAjaxWatch = {
watchMsg: "Watch",
unwatchMsg: "Unwatch",
watchingMsg: "Watching...",
- unwatchingMsg: "Unwatching..."
+ unwatchingMsg: "Unwatching...",
};
}
wgAjaxWatch.watching = false; // currently watching page
wgAjaxWatch.inprogress = false; // ajax request in progress
wgAjaxWatch.timeoutID = null; // see wgAjaxWatch.ajaxCall
-wgAjaxWatch.watchLink1 = null; // "watch"/"unwatch" link
-wgAjaxWatch.watchLink2 = null; // second one, for (some?) non-Monobook-based
-wgAjaxWatch.oldHref = null; // url for action=watch/action=unwatch
+wgAjaxWatch.watchLinks = []; // "watch"/"unwatch" links
wgAjaxWatch.setLinkText = function(newText) {
- changeText(wgAjaxWatch.watchLink1, newText);
- if (wgAjaxWatch.watchLink2) {
- changeText(wgAjaxWatch.watchLink2, newText);
+ for (i = 0; i < wgAjaxWatch.watchLinks.length; i++) {
+ changeText(wgAjaxWatch.watchLinks[i], newText);
}
};
wgAjaxWatch.setLinkID = function(newId) {
- wgAjaxWatch.watchLink1.id = newId;
+ // We can only set the first one
+ wgAjaxWatch.watchLinks[0].setAttribute( 'id', newId );
akeytt(newId); // update tooltips for Monobook
};
+wgAjaxWatch.setHref = function( string ) {
+ for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
+ if( string == 'watch' ) {
+ wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
+ .replace( /&action=unwatch/, '&action=watch' );
+ } else if( string == 'unwatch' ) {
+ wgAjaxWatch.watchLinks[i].href = wgAjaxWatch.watchLinks[i].href
+ .replace( /&action=watch/, '&action=unwatch' );
+ }
+ }
+}
+
wgAjaxWatch.ajaxCall = function() {
- if(!wgAjaxWatch.supported || wgAjaxWatch.inprogress) {
- return;
+ if(!wgAjaxWatch.supported) {
+ return true;
+ } else if (wgAjaxWatch.inprogress) {
+ return false;
}
wgAjaxWatch.inprogress = true;
- wgAjaxWatch.setLinkText(wgAjaxWatch.watching ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
- sajax_do_call("wfAjaxWatch", [wgArticleId, (wgAjaxWatch.watching ? "u" : "w")], wgAjaxWatch.processResult);
+ wgAjaxWatch.setLinkText( wgAjaxWatch.watching
+ ? wgAjaxWatch.unwatchingMsg : wgAjaxWatch.watchingMsg);
+ sajax_do_call(
+ "wfAjaxWatch",
+ [wgPageName, (wgAjaxWatch.watching ? "u" : "w")],
+ wgAjaxWatch.processResult
+ );
// if the request isn't done in 10 seconds, allow user to try again
- wgAjaxWatch.timeoutID = window.setTimeout(function() { wgAjaxWatch.inprogress = false; }, 10000);
- return;
+ wgAjaxWatch.timeoutID = window.setTimeout(
+ function() { wgAjaxWatch.inprogress = false; },
+ 10000
+ );
+ return false;
};
wgAjaxWatch.processResult = function(request) {
return;
}
var response = request.responseText;
- if(response == "<err#>") {
- window.location.href = wgAjaxWatch.oldHref;
+ if( response.match(/^<err#>/) ) {
+ window.location.href = wgAjaxWatch.watchLink1.href;
return;
- } else if(response == "<w#>") {
+ } else if( response.match(/^<w#>/) ) {
wgAjaxWatch.watching = true;
wgAjaxWatch.setLinkText(wgAjaxWatch.unwatchMsg);
wgAjaxWatch.setLinkID("ca-unwatch");
- wgAjaxWatch.oldHref = wgAjaxWatch.oldHref.replace(/action=watch/, "action=unwatch");
- } else if(response == "<u#>") {
+ wgAjaxWatch.setHref( 'unwatch' );
+ } else if( response.match(/^<u#>/) ) {
wgAjaxWatch.watching = false;
wgAjaxWatch.setLinkText(wgAjaxWatch.watchMsg);
wgAjaxWatch.setLinkID("ca-watch");
- wgAjaxWatch.oldHref = wgAjaxWatch.oldHref.replace(/action=unwatch/, "action=watch");
+ wgAjaxWatch.setHref( 'watch' );
}
+ jsMsg( response.substr(4), 'watch' );
wgAjaxWatch.inprogress = false;
if(wgAjaxWatch.timeoutID) {
window.clearTimeout(wgAjaxWatch.timeoutID);
};
wgAjaxWatch.onLoad = function() {
+ // This document structure hardcoding sucks. We should make a class and
+ // toss all this out the window.
var el1 = document.getElementById("ca-unwatch");
var el2 = null;
if (!el1) {
// The id can be either for the parent (Monobook-based) or the element
// itself (non-Monobook)
- wgAjaxWatch.watchLink1 = el1.tagName.toLowerCase() == "a" ? el1 : el1.firstChild;
- wgAjaxWatch.watchLink2 = el2 ? el2 : null;
+ wgAjaxWatch.watchLinks.push( el1.tagName.toLowerCase() == "a"
+ ? el1 : el1.firstChild );
- wgAjaxWatch.oldHref = wgAjaxWatch.watchLink1.getAttribute("href");
- wgAjaxWatch.watchLink1.setAttribute("href", "javascript:wgAjaxWatch.ajaxCall()");
- if (wgAjaxWatch.watchLink2) {
- wgAjaxWatch.watchLink2.setAttribute("href", "javascript:wgAjaxWatch.ajaxCall()");
+ if( el2 ) {
+ wgAjaxWatch.watchLinks.push( el2 );
+ }
+
+ // I couldn't get for (watchLink in wgAjaxWatch.watchLinks) to work, if
+ // you can be my guest.
+ for( i = 0; i < wgAjaxWatch.watchLinks.length; i++ ) {
+ wgAjaxWatch.watchLinks[i].onclick = wgAjaxWatch.ajaxCall;
}
return;
};
var supportsAjax = request ? true : false;
delete request;
return supportsAjax;
-}
\ No newline at end of file
+}
// the original.
var ta;
if ( doId ) {
- ta = new Array;
- ta[doId] = window.ta[doId];
+ ta = [doId];
} else {
ta = window.ta;
}
* End of table sorting code
*/
+
+/**
+ * Add a cute little box at the top of the screen to inform the user of
+ * something, replacing any preexisting message.
+ *
+ * @param String message HTML to be put inside the right div
+ * @param String class Used in adding a class; should be different for each
+ * call to allow CSS/JS to hide different boxes. null = no class used.
+ * @return Boolean True on success, false on failure
+ */
+function jsMsg( message, class ) {
+ if ( !document.getElementById ) {
+ return false;
+ }
+ // We special-case skin structures provided by the software. Skins that
+ // choose to abandon or significantly modify our formatting can just define
+ // an mw-js-message div to start with.
+ var messageDiv = document.getElementById( 'mw-js-message' );
+ if ( !messageDiv ) {
+ messageDiv = document.createElement( 'div' );
+ if ( document.getElementById( 'column-content' )
+ && document.getElementById( 'content' ) ) {
+ // MonoBook, presumably
+ document.getElementById( 'content' ).insertBefore(
+ messageDiv,
+ document.getElementById( 'content' ).firstChild
+ );
+ } else if ( document.getElementById('content')
+ && document.getElementById( 'article' ) ) {
+ // Non-Monobook but still recognizable (old-style)
+ document.getElementById( 'article').insertBefore(
+ messageDiv,
+ document.getElementById( 'article' ).firstChild
+ );
+ } else {
+ return false;
+ }
+ }
+
+ messageDiv.setAttribute( 'id', 'mw-js-message' );
+ if( class ) {
+ messageDiv.setAttribute( 'class', 'mw-js-message-'+class );
+ }
+ messageDiv.innerHTML = message;
+ return true;
+}
+
function runOnloadHook() {
// don't run anything below this for non-dom browsers
if (doneOnloadHook || !(document.getElementById && document.getElementsByTagName)) {
// so the below should be redundant. It's there just in case.
hookEvent("load", runOnloadHook);
-hookEvent("load", mwSetupToolbar);
\ No newline at end of file
+hookEvent("load", mwSetupToolbar);