+/**
+ * Count the number of items on a user's watchlist
+ *
+ * @param $talk Include talk pages
+ * @return integer
+ */
+function wlCountItems( &$user, $talk = true ) {
+ $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
+
+ # Fetch the raw count
+ $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
+ $row = $dbr->fetchObject( $res );
+ $count = $row->count;
+ $dbr->freeResult( $res );
+
+ # Halve to remove talk pages if needed
+ if( !$talk )
+ $count = floor( $count / 2 );
+
+ return( $count );
+}
+
+/**
+ * Allow the user to clear their watchlist
+ *
+ * @param $out Output object
+ * @param $request Request object
+ * @param $par Parameters passed to the watchlist page
+ * @return bool True if it's been taken care of; false indicates the watchlist
+ * code needs to do something further
+ */
+function wlHandleClear( &$out, &$request, $par ) {
+ global $wgLang;
+
+ # Check this function has something to do
+ if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
+ global $wgUser;
+ $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
+ $count = wlCountItems( $wgUser );
+ if( $count > 0 ) {
+ # See if we're clearing or confirming
+ if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
+ # Clearing, so do it and report the result
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
+ $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
+ $out->returnToMain();
+ } else {
+ # Confirming, so show a form
+ $wlTitle = SpecialPage::getTitleFor( 'Watchlist' );
+ $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
+ $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
+ $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
+ $out->addHTML(
+ wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
+ wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
+ wfCloseElement( 'form' )
+ );
+ }
+ return( true );
+ } else {
+ # Nothing on the watchlist; nothing to do here
+ $out->addWikiText( wfMsg( 'nowatchlist' ) );
+ $out->returnToMain();
+ return( true );
+ }
+ } else {
+ return( false );
+ }
+}