Watchlist grouping (not ready for review)
authorAaron Pramana <aaron@sociotopia.com>
Sun, 10 Jun 2012 06:22:26 +0000 (23:22 -0700)
committerAaron Pramana <aaron@sociotopia.com>
Fri, 15 Jun 2012 01:37:42 +0000 (18:37 -0700)
My first change for the watchlist grouping project. Some of my code was written
in a separate installation of MediaWiki and so I will be uploading
more changes in the coming days.

TODO:
*add database table creation SQL
*add/edit form message labels
*clean up the UI (a lot)
*more WatchlistGroup methods to come

Change-Id: I645b924074cb179959a45d390f11f314184de368

includes/AutoLoader.php
includes/User.php
includes/WatchedItem.php
includes/WatchlistGroup.php [new file with mode: 0644]
includes/specials/SpecialEditWatchlist.php

index d29c4e3..4d76f97 100644 (file)
@@ -258,6 +258,7 @@ $wgAutoloadLocalClasses = array(
        'ViewCountUpdate' => 'includes/ViewCountUpdate.php',
        'WantedQueryPage' => 'includes/QueryPage.php',
        'WatchedItem' => 'includes/WatchedItem.php',
+       'WatchlistGroup' => 'includes/WatchlistGroup.php',
        'WebRequest' => 'includes/WebRequest.php',
        'WebRequestUpload' => 'includes/WebRequest.php',
        'WebResponse' => 'includes/WebResponse.php',
index 01407b1..0d3d14d 100644 (file)
@@ -2627,8 +2627,8 @@ class User {
         * Watch an article.
         * @param $title Title of the article to look at
         */
-       public function addWatch( $title ) {
-               $wl = WatchedItem::fromUserTitle( $this, $title );
+       public function addWatch( $title, $group ) {
+               $wl = WatchedItem::fromUserTitle( $this, $title, $group );
                $wl->addWatch();
                $this->invalidateCache();
        }
index 932af16..cb27312 100644 (file)
@@ -27,7 +27,7 @@
  * @ingroup Watchlist
  */
 class WatchedItem {
-       var $mTitle, $mUser, $id, $ns, $ti;
+       var $mTitle, $mUser, $id, $gr, $ns, $ti;
        private $loaded = false, $watched, $timestamp;
 
        /**
@@ -36,7 +36,7 @@ class WatchedItem {
         * @param $title Title: the title we're going to (un)watch
         * @return WatchedItem object
         */
-       public static function fromUserTitle( $user, $title ) {
+       public static function fromUserTitle( $user, $title, $group = 0 ) {
                $wl = new WatchedItem;
                $wl->mUser = $user;
                $wl->mTitle = $title;
@@ -48,6 +48,8 @@ class WatchedItem {
                $wl->ns = $title->getNamespace();
 
                $wl->ti = $title->getDBkey();
+
+               $wl->gr = $group;
                return $wl;
        }
 
@@ -145,6 +147,7 @@ class WatchedItem {
                $dbw->insert( 'watchlist',
                  array(
                        'wl_user' => $this->id,
+                       'wl_group' => $this->gr,
                        'wl_namespace' => MWNamespace::getSubject($this->ns),
                        'wl_title' => $this->ti,
                        'wl_notificationtimestamp' => null
@@ -155,6 +158,7 @@ class WatchedItem {
                $dbw->insert( 'watchlist',
                  array(
                        'wl_user' => $this->id,
+                       'wl_group' => $this->gr,
                        'wl_namespace' => MWNamespace::getTalk($this->ns),
                        'wl_title' => $this->ti,
                        'wl_notificationtimestamp' => null
diff --git a/includes/WatchlistGroup.php b/includes/WatchlistGroup.php
new file mode 100644 (file)
index 0000000..0a58958
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Accessor and mutator for watchlist groups.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+class WatchlistGroup {
+       public static function getGroups( $context ){
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( 'watchlist_groups', array( 'wg_id', 'wg_name' ),
+                       array( 'wg_user' => $context->getUser()->getId()), __METHOD__ 
+               );
+               $groups = array();
+               foreach ( $res as $s ) {
+                       $groups[$s->wg_id] = $s->wg_name;
+               }
+               return $groups;
+       }
+
+       public static function getGroupFromUserTitle( $title, $context ){
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->selectRow( 'watchlist', array( 'wl_group' ),
+                       array( 'wl_title' => $title, 'wl_user' => $context->getUser()->getId()), __METHOD__ 
+               );
+               $gid = intval($res->wl_group);
+
+               if($gid == 0){
+                       $gname = 'None';
+               } else {
+                       $res = $dbr->selectRow( 'watchlist_groups', array( 'wg_name' ),
+                               array( 'wg_id' => $gid ), __METHOD__ 
+                       );
+                       $gname = $res->wg_name;
+               }
+               $group = array( $gname, $gid );
+               return $group;
+       }
+
+       public static function regroupTitles( $titles, $group, $context ){
+               $dbw = wfGetDB( DB_MASTER );
+               $res = $dbw->update( 'watchlist', array( 'wl_group' => $group ),
+                       array( 'wl_title' => $titles), __METHOD__ );
+               return $res;
+       }
+}
\ No newline at end of file
index 67f6d68..55a15a4 100644 (file)
@@ -275,7 +275,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
                $res = $dbr->select(
                        array( 'watchlist' ),
-                       array( 'wl_namespace',  'wl_title' ),
+                       array( 'wl_namespace', 'wl_group', 'wl_title' ),
                        array( 'wl_user' => $this->getUser()->getId() ),
                        __METHOD__,
                        array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
@@ -285,7 +285,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                foreach ( $res as $row ) {
                        $lb->add( $row->wl_namespace, $row->wl_title );
                        if ( !MWNamespace::isTalk( $row->wl_namespace ) ) {
-                               $titles[$row->wl_namespace][$row->wl_title] = 1;
+                               $titles[$row->wl_namespace][$row->wl_title] = intval($row->wl_group);
                        }
                }
 
@@ -436,6 +436,17 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
        public function submitNormal( $data ) {
                $removed = array();
 
+               // Regrouping submission
+               $group = intval( $data['group'] );
+               unset($data['group']);
+               if( $group > -1 ){
+                       foreach( $data as $titles ) {
+                               WatchlistGroup::regroupTitles( $titles, $group, $this->getContext() );
+                       }
+                       $this->successMessage = 'Titles were successfully regrouped!';
+                       return true;
+               }
+
                foreach( $data as $titles ) {
                        $this->unwatchTitles( $titles );
                        $removed = array_merge( $removed, $titles );
@@ -459,6 +470,8 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
        protected function getNormalForm(){
                global $wgContLang;
 
+               
+
                $fields = array();
                $count = 0;
 
@@ -501,6 +514,19 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        $this->toc = false;
                }
 
+               $groups = WatchlistGroup::getGroups( $this->getContext() );
+               foreach( $groups as &$g ){
+                       $g = 'Change group to "' . $g . '"';
+               }
+
+               $gsOptions = array_merge( array( 'Remove titles' => -1, 'Ungroup' => 0 ), array_flip( $groups ) );
+
+               $fields['group'] = array(
+                       'type' => 'select',
+                       'options' => $gsOptions,
+                       'label' => "Action:"
+               );
+
                $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
                $form->setTitle( $this->getTitle() );
                $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
@@ -540,9 +566,12 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        );
                }
 
+               $wgroup = WatchlistGroup::getGroupFromUserTitle( $title, $this->getContext() );
+               $wgrouplink = $wgroup[0];
+
                wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
 
-               return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
+               return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ") (Group: " . $wgrouplink . ")";
        }
 
        /**