User:Bagariavivek/Code/SpecialPage/WatchGroup groupname

From mediawiki.org

Special:EditWatchGroup/groupname and Special:WatchGroup/groupname[edit]

Basic Code Template[edit]

SpecialWatchGroupPages/<groupname>[edit]

To view details of pages in a particular WatchGroup

<?php
/**
 * @licence GNU GPL v3+
 * @author vivekkumarbagaria <vivekee047@gmail.com>
 *
 * Function:
 * This SpecialPage will display all the pages of a <groupname>
 *
 * To Do
 * Have relevant hooks between the code
 */

class SpecialWatchgroupPages extends UnlistedSpecialPage {

	protected $user ;
	protected $request ;
	protected $output ;
	public function __construct( $page = 'WatchGroupPages' ) {
		parent::__construct( $page );
	}

	function execute( $par ) {

		/**
		 * Check if user is anonymous?
		 *  If User is Anon return with a msg displaying to login
		 */

		$this->user = $this->getUser() ;
		$this->output = $this->getOutput() ;

		if ( $this->user->isAnon() ) {
			SpecialWatchGroups::userIsAnon() ;
			return ;
		}

		$this->setHeaders() ;
		$this->outputHeader() ;


		$args = func_get_args();
		$groupname = $args[0] ; ;
		$groupExists = $this->validateGroupName( $groupname ) ;
		if ( is_null( $groupExists ) ) {
			$this->output->addHTML( "No such group exists" );
			return ;
		}
		$this->output->setPageTitle( $groupname ) ;
		$watchPages = $this->extractWatchPages( $this->user , $groupname ) ;

		$this->displayPages( $watchPages ) ;

		// To add feed links to the ATOM. This will give the list of pages of the particular group.
		//To add conditions on the type of page. For eg, bot-edited, minor edit.
	}

	public function displayPages( $watchPages ) {
		$output = "<ul>\n";
		foreach ( $watchPages as $page ) {
			$title = Title::newFromText( $page ) ;
			$output .= "<li>"
					. Linker::link( $title )
					. ' (' . Linker::link( $title->getTalkPage() )
					. ")</li>\n";
		}
		$output .= "</ul>\n";
		$this->output->addHTML( $output ) ;

	}
	public static function extractWatchPages( $user , $groupname ) {

		$list = array() ;
		$dbr = wfGetDB( DB_SLAVE, 'watchgroups' );
		// given namespace zero ,just  for testing. Should work for everynamespace
		$res = $dbr->select(
					'watchpages',
					 '*',
					array(
						'wp_user'		=>	$user->getId() ,
						'wp_groupname'	=>	$groupname ,
						'wp_namespace'	=>	0
					),
				 __METHOD__ );
		foreach ( $res as $row ) {
			// To check the validity
			$list[] = $row->wp_title ;
		}
		return $list ;
	}

	public function validateGroupName( $groupname ) {
		$dbr = wfGetDB( DB_SLAVE, 'watchgroups' );
		$res = $dbr->select(
				'watchgroups',
				'*',
				array(
					'wtg_user' 		=>	$this->user->getId(),
					'wtg_groupname'	=>	$groupname,
				),
				__METHOD__
			);
		foreach ( $res as $group ) {
			return $group->wtg_groupname ;
		}

		return null ;

	}
	public function getUserWatchPages( $group ) {

		$list = array() ;
		$dbr = wfGetDB( DB_MASTER ) ;
		$res = $dbr->select(
			'watchpages' ,
			'*' ,
			array(
				'wtg_user' => $this->getUser()->getId() ,
				'wtg_groupname' => $group
			) ,
			__METHOD__
		) ;
		foreach ( $res as $row ) {
		// Add the valid pages from $res to list
			$list[] = $row->wtg_title ;
		}

		return $list;
	}

	/**
	Other basic functions to be defined
		a)Check the validity of the title
		b)Get the time, when this article last edited.
	*/
}

SpecialEditWatchGroups/<groupname>[edit]

To edit a particular WatchGroup

<?php
/**
 * @licence GNU GPL v3+
 * @author vivekkumarbagaria <vivekee047@gmail.com>
 *
 * Function : To Edit the users WatchGroups
 *
 */
class SpecialEditWatchGroups extends SpecialPage {

	protected $output ;
	protected $user ;
	protected $request ;

	public function __construct() {
		parent::__construct( 'EditWatchGroups' );
	}

	public function execute( $mode ) {

		$this->user 	= $this->getUser() ;
		if ( $this->user->isAnon() ) {
			SpecialWatchGroups::userIsAnon() ;
			return ;
		}

		$this->request = $this->getRequest() ;
		$this->output = $this->getOutput() ;
		$this->setHeaders();
		$this->outputHeader();
		$list = SpecialWatchGroups::ExtractWatchGroup( $this->user );
		$this->CreateEditForm( $list ) ;
		$this->addViewSubtitle();
	}


	// This function is borrowed from SpecialEditWatchList
	public function CreateEditForm( $list ) {
		$titles = implode( $list, "\n" );
		$fields = array(
			'Titles' => array(
				'type' => 'textarea',
				'label-message' => 'watchlistedit-raw-titles',
				'default' => $titles,
			),
		);
		$form = new HTMLForm( $fields, $this->getContext() );
		$form->setTitle( $this->getTitle() );
		$form->setSubmitCallback( array( $this, 'submitRaw' ) );
		$form->show();
	}


	public function submitRaw( $data ) {
		$wanted = explode( "\n" , trim( $data['Titles'] ) );
		$current = SpecialWatchGroups::ExtractWatchGroup( $this->user ) ;
		if ( count( $wanted ) > 0 ) {
			$add = array_diff( $wanted, $current );
			$remove = array_diff( $current, $wanted );
			if ( count( $add ) > 0 ) {
				$this->addGroups( $add );
			}
			if ( count( $remove ) > 0 ) {
				$this->removeGroups( $remove );
			}
			$this->user->invalidateCache();

		} else {
			$this->clearWatchGroups();
			$this->getUser()->invalidateCache();
		}

		$this->output->addHTML( "Groups have been added and removed as you wished" ) ;
	}


	public function addGroups( $list ) {
		foreach ( $list as $group ) {
			SpecialWatchGroups::addNewGroup( $this->user , $group ) ;
		}
	}


	public function removeGroups( $list ) {
		foreach ( $list as $group ) {
			SpecialWatchGroups::removeGroup( $this->user, $group ) ;
		}
	}


	private function clearWatchGroups() {
		$dbw = wfGetDB( DB_MASTER );
		$dbw->delete(
			'watchgroups',
			array( 'wp_user' => $this->getUser()->getId() ),
			__METHOD__
		);
	}

	
	public function addViewSubtitle() {
		$subtitle = Linker::linkKnown(
				SpecialPage::getTitleFor( "WatchGroups" ), "ViewWatchGroup"  	);
		$this->output->addSubtitle( $subtitle ) ;
	}
}

SpecialDeletedWatchPages[edit]

To view the list of watch pages which have been removed from the WatchGroups.This is a provision to get back deleted pages.

<?php
    class SpecialDeletedWatchPages extends UnlistedSpecialPage {
 
        public function __construct(){
                parent::__construct( 'DeletedWatchPages' );
        }
 
        public function execute(){
        //Check if user is anonymous?
        //extractPages()
        
        //Add back pages to the group they belonged which the user checks
        }

        /*
        Executed on form submit 
        */
        public addBackPages(){

        //Check the pages which have been checked in the form.
        //Add those pages to the groups which they belonged.
        //Display the success message        

        }
        
        public extractPages(){

        //Query all the deleted pages from the database
        //Display the groupname , deletion details with the page

        }