Revert revert of setSingleton(), unrelated to broken, accidentally committed code...
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
index db0f109..63e218a 100644 (file)
  * @addtogroup SpecialPage
  */
 
+
+/**
+ * Start point
+ */
+function wfSpecialNewPages( $par, $specialPage ) {
+       $page = new NewPagesPage( $specialPage );
+       $page->execute( $par );
+}
+
 /**
  * implements Special:Newpages
  * @addtogroup SpecialPage
  */
 class NewPagesPage extends QueryPage {
 
-       var $namespace;
-       var $username = '';
+       protected $options = array();
+       protected $nondefaults = array();
+       protected $specialPage;
+
+       public function __construct( $specialPage=null ) {
+               $this->specialPage = $specialPage;
+       }
+
+       public function execute( $par ) {
+               global $wgRequest, $wgLang;
+
+               $shownavigation = is_object( $this->specialPage ) && !$this->specialPage->including();
+
+               $defaults = array(
+                       /* bool */ 'hideliu' => false,
+                       /* bool */ 'hidepatrolled' => false,
+                       /* bool */ 'hidebots' => false,
+                       /* text */ 'namespace' => "0",
+                       /* text */ 'username' => '',
+                       /* int  */ 'offset' => 0,
+                       /* int  */ 'limit' => 50,
+               );
+
+               $options = $defaults; 
+
+               if ( $par ) {
+                       $bits = preg_split( '/\s*,\s*/', trim( $par ) );
+                       foreach ( $bits as $bit ) {
+                               if ( 'shownav' == $bit )
+                                       $shownavigation = true;
+                               if ( 'hideliu' === $bit )
+                                       $options['hideliu'] = true;
+                               if ( 'hidepatrolled' == $bit )
+                                       $options['hidepatrolled'] = true;
+                               if ( 'hidebots' == $bit )
+                                       $options['hidebots'] = true;
+                               if ( is_numeric( $bit ) )
+                                       $options['limit'] = intval( $bit );
+
+                               $m = array();
+                               if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
+                                       $options['limit'] = intval($m[1]);
+                               if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
+                                       $options['offset'] = intval($m[1]);
+                               if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
+                                       $ns = $wgLang->getNsIndex( $m[1] );
+                                       if( $ns !== false ) {
+                                               $options['namespace'] = $ns;
+                                       }
+                               }
+                       }
+               }
 
-       function NewPagesPage( $namespace = NS_MAIN, $username = '' ) {
-               $this->namespace = $namespace;
-               $this->username = $username;
+               // Override all values from requests, if specified
+               foreach ( $defaults as $v => $t ) {
+                       if ( is_bool($t) ) {
+                               $options[$v] = $wgRequest->getBool( $v, $options[$v] );
+                       } elseif( is_int($t) ) {
+                               $options[$v] = $wgRequest->getInt( $v, $options[$v] );
+                       } elseif( is_string($t) ) {
+                               $options[$v] = $wgRequest->getText( $v, $options[$v] );
+                       }
+               }
+
+               // Validate limit and offset params
+               if ( $options['limit'] <= 0 ) {
+                       $options['limit'] = $defaults['limit'];
+               }
+
+               if ( $options['offset'] < 0 ) {
+                       $options['offset'] = $defaults['offset'];
+               }
+
+               $nondefaults = array();
+               foreach ( $options as $v => $t ) {
+                       if ( $v === 'offset' ) continue; # Reset offset if parameters change
+                       wfAppendToArrayIfNotDefault( $v, $t, $defaults, $nondefaults );
+               }
+
+               # bind to class
+               $this->options = $options;
+               $this->nondefaults = $nondefaults;
+
+               if ( !$this->doFeed( $wgRequest->getVal( 'feed' ), $options['limit'] ) ) {
+                       $this->doQuery( $options['offset'], $options['limit'], $shownavigation );
+               }
+       }
+
+       function linkParameters() {
+               $nondefaults = $this->nondefaults;
+               // QueryPage seems to handle limit and offset itself
+               if ( isset( $nondefaults['limit'] ) ) {
+                       unset($nondefaults['limit']);
+               }
+               return $nondefaults;
        }
 
        function getName() {
@@ -27,29 +125,41 @@ class NewPagesPage extends QueryPage {
                return false;
        }
 
-       function makeUserWhere( &$dbo ) {
-               $title = Title::makeTitleSafe( NS_USER, $this->username );
-               if( $title ) {
-                       return ' AND rc_user_text = ' . $dbo->addQuotes( $title->getText() );
+       function makeUserWhere( $db ) {
+               global $wgGroupPermissions;
+               $conds = array();
+               if ($this->options['hidepatrolled']) {
+                       $conds['rc_patrolled'] = 0;
+               }
+               if ($this->options['hidebots']) {
+                       $conds['rc_bot'] = 0;
+               }
+               if ($wgGroupPermissions['*']['createpage'] == true && $this->options['hideliu']) {
+                       $conds['rc_user'] = 0;
                } else {
-                       return '';
+                       $title = Title::makeTitleSafe( NS_USER, $this->options['username'] );
+                       if( $title ) {
+                               $conds['rc_user_text'] = $title->getText();
+                       }
                }
+               return $conds;
        }
 
-       private function makeNamespaceWhere() {
-               return $this->namespace !== 'all'
-                       ? ' AND rc_namespace = ' . intval( $this->namespace )
-                       : '';
-       }
 
        function getSQL() {
-               global $wgUser, $wgUseRCPatrol;
-               $usepatrol = ( $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) ) ? 1 : 0;
+               global $wgUser, $wgUseNPPatrol, $wgUseRCPatrol;
+               $usepatrol = ( $wgUseNPPatrol || $wgUseRCPatrol ) ? 1 : 0;
                $dbr = wfGetDB( DB_SLAVE );
                list( $recentchanges, $page ) = $dbr->tableNamesN( 'recentchanges', 'page' );
 
-               $nsfilter = $this->makeNamespaceWhere();
-               $uwhere = $this->makeUserWhere( $dbr );
+               $conds = array();
+               $conds['rc_new'] = 1;
+               if ( $this->options['namespace'] !== 'all' ) {
+                       $conds['rc_namespace'] = intval( $this->options['namespace'] );
+               }
+               $conds['page_is_redirect'] = 0;
+               $conds += $this->makeUserWhere( $dbr );
+               $condstext = $dbr->makeList( $conds, LIST_AND );
 
                # FIXME: text will break with compression
                return
@@ -68,23 +178,20 @@ class NewPagesPage extends QueryPage {
                                page_len as length,
                                page_latest as rev_id
                        FROM $recentchanges,$page
-                       WHERE rc_cur_id=page_id AND rc_new=1
-                       {$nsfilter}
-                       AND page_is_redirect = 0
-                       {$uwhere}";
+                       WHERE rc_cur_id=page_id AND $condstext";
        }
        
-       function preprocessResults( &$dbo, &$res ) {
+       function preprocessResults( $db, $res ) {
                # Do a batch existence check on the user and talk pages
                $linkBatch = new LinkBatch();
-               while( $row = $dbo->fetchObject( $res ) ) {
-                       $linkBatch->addObj( Title::makeTitleSafe( NS_USER, $row->user_text ) );
-                       $linkBatch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_text ) );
+               while( $row = $db->fetchObject( $res ) ) {
+                       $linkBatch->add( NS_USER, $row->user_text );
+                       $linkBatch->add( NS_USER_TALK, $row->user_text );
                }
                $linkBatch->execute();
                # Seek to start
-               if( $dbo->numRows( $res ) > 0 )
-                       $dbo->dataSeek( $res, 0 );
+               if( $db->numRows( $res ) > 0 )
+                       $db->dataSeek( $res, 0 );
        }
 
        /**
@@ -116,8 +223,10 @@ class NewPagesPage extends QueryPage {
         * @return bool
         */
        function patrollable( $result ) {
-               global $wgUser, $wgUseRCPatrol;
-               return $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) && !$result->patrolled;
+               global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
+               return ( $wgUseRCPatrol || $wgUseNPPatrol )
+                       && $wgUser->isAllowed( 'patrol' )
+                       && !$result->patrolled;
        }
 
        function feedItemDesc( $row ) {
@@ -138,75 +247,73 @@ class NewPagesPage extends QueryPage {
         * @return string
         */     
        function getPageHeader() {
-               global $wgScript;
+               global $wgScript, $wgContLang, $wgGroupPermissions, $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
+               $sk = $wgUser->getSkin();
+               $align = $wgContLang->isRTL() ? 'left' : 'right';
                $self = SpecialPage::getTitleFor( $this->getName() );
-               $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
-               $form .= Xml::hidden( 'title', $self->getPrefixedUrl() );
-               # Namespace selector
-               $form .= '<table><tr><td align="right">' . Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '</td>';
-               $form .= '<td>' . Xml::namespaceSelector( $this->namespace, 'all' ) . '</td></tr>';
-               # Username filter
-               $form .= '<tr><td align="right">' . Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) . '</td>';
-               $form .= '<td>' . Xml::input( 'username', 30, $this->username, array( 'id' => 'mw-np-username' ) ) . '</td></tr>';
-               
-               $form .= '<tr><td></td><td>' . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</td></tr></table>';
-               $form .= Xml::hidden( 'offset', $this->offset ) . Xml::hidden( 'limit', $this->limit ) . '</form>';
-               return $form;
-       }
-       
-       /**
-        * Link parameters
-        *
-        * @return array
-        */
-       function linkParameters() {
-               return( array( 'namespace' => $this->namespace, 'username' => $this->username ) );
-       }
-       
-}
 
-/**
- * constructor
- */
-function wfSpecialNewpages($par, $specialPage) {
-       global $wgRequest, $wgContLang;
-
-       list( $limit, $offset ) = wfCheckLimits();
-       $namespace = NS_MAIN;
-       $username = '';
-
-       if ( $par ) {
-               $bits = preg_split( '/\s*,\s*/', trim( $par ) );
-               foreach ( $bits as $bit ) {
-                       if ( 'shownav' == $bit )
-                               $shownavigation = true;
-                       if ( is_numeric( $bit ) )
-                               $limit = $bit;
-
-                       $m = array();
-                       if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
-                               $limit = intval($m[1]);
-                       if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
-                               $offset = intval($m[1]);
-                       if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
-                               $ns = $wgContLang->getNsIndex( $m[1] );
-                               if( $ns !== false ) {
-                                       $namespace = $ns;
-                               }
-                       }
+               // show/hide links
+               $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ));
+
+               $hidelinks = array();
+
+               if ( $wgGroupPermissions['*']['createpage'] === true ) {
+                       $hidelinks['hideliu'] = 'rcshowhideliu';
                }
-       } else {
-               if( $ns = $wgRequest->getText( 'namespace', NS_MAIN ) )
-                       $namespace = $ns;
-               if( $un = $wgRequest->getText( 'username' ) )
-                       $username = $un;
-       }
-       
-       if ( ! isset( $shownavigation ) )
-               $shownavigation = ! $specialPage->including();
+               if ( $wgUseNPPatrol || $wgUseRCPatrol ) {
+                       $hidelinks['hidepatrolled'] = 'rcshowhidepatr';
+               }
+               $hidelinks['hidebots'] = 'rcshowhidebots';
 
-       $npp = new NewPagesPage( $namespace, $username );
+               $links = array();
+               foreach ( $hidelinks as $key => $msg ) {
+                       $reversed = 1-$this->options[$key];
+                       $link = $sk->makeKnownLinkObj( $self, $showhide[$reversed],
+                               wfArrayToCGI( array( $key => $reversed ), $this->nondefaults )
+                       );
+                       $links[$key] = wfMsgHtml( $msg, $link );
+               }
+
+               $hl = implode( ' | ', $links );
+
+               // Store query values in hidden fields so that form submission doesn't lose them
+               $hidden = array();
+               foreach ( $this->nondefaults as $key => $value ) {
+                       if ( $key === 'namespace' ) continue;
+                       if ( $key === 'username' ) continue;
+                       $hidden[] = Xml::hidden( $key, $value );
+               }
+               $hidden = implode( "\n", $hidden );
 
-       if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
-               $npp->doQuery( $offset, $limit, $shownavigation );
+               $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
+                       Xml::hidden( 'title', $self->getPrefixedDBkey() ) .
+                       Xml::openElement( 'table' ) .
+                       "<tr>
+                               <td align=\"$align\">" .
+                                       Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
+                               "</td>
+                               <td>" .
+                                       Xml::namespaceSelector( $this->options['namespace'], 'all' ) .
+                               "</td>
+                       </tr>
+                       <tr>
+                               <td align=\"$align\">" .
+                                       Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
+                               "</td>
+                               <td>" .
+                                       Xml::input( 'username', 30, $this->options['username'], array( 'id' => 'mw-np-username' ) ) .
+                               "</td>
+                       </tr>
+                       <tr> <td></td>
+                               <td>" .
+                                       Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
+                               "</td>
+                       </tr>" .
+                       "<tr><td></td><td>" . $hl . "</td></tr>" .
+                       Xml::closeElement( 'table' ) .
+                       $hidden .
+                       Xml::closeElement( 'form' );
+               return $form;
+       }
+       
 }
\ No newline at end of file