API: Requesting a token you aren't allowed to request no longer dies with an error...
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
index 3fd0eba..a9f58b2 100644 (file)
 <?php
 /**
- *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @file
+ * @ingroup SpecialPage
  */
 
+
+/**
+ * Start point
+ */
+function wfSpecialNewPages( $par, $sp ) {
+       $page = new NewPagesForm();
+       $page->execute( $par, $sp->including() );
+}
+
 /**
- *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * implements Special:Newpages
+ * @ingroup SpecialPage
  */
-class NewPagesPage extends QueryPage {
+class NewPagesForm {
 
-       var $namespace;
-       var $username = '';
+       // Stored objects
+       protected $opts, $title, $skin;
 
-       function NewPagesPage( $namespace = NS_MAIN, $username = '' ) {
-               $this->namespace = $namespace;
-               $this->username = $username;
-       }
+       // Some internal settings
+       protected $showNavigation = false;
+
+       protected function setup( $par ) {
+               global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
+
+               // Options
+               $opts = new FormOptions();
+               $this->opts = $opts; // bind
+               $opts->add( 'hideliu', false );
+               $opts->add( 'hidepatrolled', false );
+               $opts->add( 'hidebots', false );
+               $opts->add( 'limit', 50 );
+               $opts->add( 'offset', '' );
+               $opts->add( 'namespace', '0' );
+               $opts->add( 'username', '' );
+               $opts->add( 'feed', '' );
 
-       function getName() {
-               return 'Newpages';
+               // Set values
+               $opts->fetchValuesFromRequest( $wgRequest );
+               if ( $par ) $this->parseParams( $par );
+
+               // Validate
+               $opts->validateIntBounds( 'limit', 0, 5000 );
+               if( !$wgEnableNewpagesUserFilter ) {
+                       $opts->setValue( 'username', '' );
+               }
+
+               // Store some objects
+               $this->skin = $wgUser->getSkin();
+               $this->title = SpecialPage::getTitleFor( 'NewPages' );
        }
 
-       function isExpensive() {
-               # Indexed on RC, and will *not* work with querycache yet.
-               return false;
+       protected function parseParams( $par ) {
+               global $wgLang;
+               $bits = preg_split( '/\s*,\s*/', trim( $par ) );
+               foreach ( $bits as $bit ) {
+                       if ( 'shownav' == $bit )
+                               $this->showNavigation = true;
+                       if ( 'hideliu' === $bit )
+                               $this->opts->setValue( 'hideliu', true );
+                       if ( 'hidepatrolled' == $bit )
+                               $this->opts->setValue( 'hidepatrolled', true );
+                       if ( 'hidebots' == $bit )
+                               $this->opts->setValue( 'hidebots', true );
+                       if ( is_numeric( $bit ) )
+                               $this->opts->setValue( 'limit', intval( $bit ) );
+
+                       $m = array();
+                       if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
+                               $this->opts->setValue( 'limit', intval($m[1]) );
+                       // PG offsets not just digits!
+                       if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
+                               $this->opts->setValue( 'offset',  intval($m[1]) );
+                       if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
+                               $ns = $wgLang->getNsIndex( $m[1] );
+                               if( $ns !== false ) {
+                                       $this->opts->setValue( 'namespace',  $ns );
+                               }
+                       }
+               }
        }
 
-       function makeUserWhere( &$dbo ) {
-               $title = Title::makeTitleSafe( NS_USER, $this->username );
-               if( $title ) {
-                       return ' AND rc_user_text = ' . $dbo->addQuotes( $title->getText() );
+       /**
+        * Show a form for filtering namespace and username
+        *
+        * @param string $par
+        * @param bool $including true if the page is being included with {{Special:Newpages}}
+        * @return string
+        */
+       public function execute( $par, $including ) {
+               global $wgLang, $wgGroupPermissions, $wgUser, $wgOut;
+
+               $this->showNavigation = !$including; // Maybe changed in setup
+               $this->setup( $par );
+
+               if( !$including ) {
+                       // Settings
+                       $this->form();
+
+                       $this->setSyndicated();
+                       $feedType = $this->opts->getValue( 'feed' );
+                       if( $feedType ) {
+                               return $this->feed( $feedType, $options );
+                       }
+               }
+
+               $pager = new NewPagesPager( $this, $this->opts );
+               $pager->mLimit = $this->opts->getValue( 'limit' );
+               $pager->mOffset = $this->opts->getValue( 'offset' );
+
+               if( $pager->getNumRows() ) {
+                       $navigation = '';
+                       if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
+                       $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
                } else {
-                       return '';
-               }
-       }
-
-       function getSQL() {
-               global $wgUser, $wgUseRCPatrol;
-               $usepatrol = ( $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) ) ? 1 : 0;
-               $dbr =& wfGetDB( DB_SLAVE );
-               extract( $dbr->tableNames( 'recentchanges', 'page', 'text' ) );
-
-               $uwhere = $this->makeUserWhere( $dbr );
-
-               # FIXME: text will break with compression
-               return
-                       "SELECT 'Newpages' as type,
-                               rc_namespace AS namespace,
-                               rc_title AS title,
-                               rc_cur_id AS cur_id,
-                               rc_user AS user,
-                               rc_user_text AS user_text,
-                               rc_comment as comment,
-                               rc_timestamp AS timestamp,
-                               rc_timestamp AS value,
-                               '{$usepatrol}' as usepatrol,
-                               rc_patrolled AS patrolled,
-                               rc_id AS rcid,
-                               page_len as length,
-                               page_latest as rev_id
-                       FROM $recentchanges,$page
-                       WHERE rc_cur_id=page_id AND rc_new=1
-                       AND rc_namespace=" . $this->namespace . " AND page_is_redirect=0
-                       {$uwhere}";
-       }
-       
-       function preprocessResults( &$dbo, &$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 ) );
+                       $wgOut->addWikiMsg( 'specialpage-empty' );
                }
-               $linkBatch->execute();
-               # Seek to start
-               if( $dbo->numRows( $res ) > 0 )
-                       $dbo->dataSeek( $res, 0 );
+       }
+
+       protected function filterLinks() {
+               global $wgGroupPermissions, $wgUser;
+
+               // show/hide links
+               $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
+
+               // Option value -> message mapping
+               $filters = array(
+                       'hideliu' => 'rcshowhideliu',
+                       'hidepatrolled' => 'rcshowhidepatr',
+                       'hidebots' => 'rcshowhidebots'
+               );
+
+               // Disable some if needed
+               if ( $wgGroupPermissions['*']['createpage'] !== true )
+                       unset($filters['hideliu']);
+
+               if ( !$wgUser->useNPPatrol() )
+                       unset($filters['hidepatrolled']);
+
+               $links = array();
+               $changed = $this->opts->getChangedValues();
+               unset($changed['offset']); // Reset offset if query type changes
+
+               foreach ( $filters as $key => $msg ) {
+                       $onoff = 1 - $this->opts->getValue($key);
+                       $link = $this->skin->makeKnownLinkObj( $this->title, $showhide[$onoff],
+                               wfArrayToCGI( array( $key => $onoff ), $changed )
+                       );
+                       $links[$key] = wfMsgHtml( $msg, $link );
+               }
+
+               return implode( ' | ', $links );
+       }
+
+       protected function form() {
+               global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
+
+               // Consume values
+               $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
+               $namespace = $this->opts->consumeValue( 'namespace' );
+               $username = $this->opts->consumeValue( 'username' );
+
+               // Check username input validity
+               $ut = Title::makeTitleSafe( NS_USER, $username );
+               $userText = $ut ? $ut->getText() : '';
+
+               // Store query values in hidden fields so that form submission doesn't lose them
+               $hidden = array();
+               foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
+                       $hidden[] = Xml::hidden( $key, $value );
+               }
+               $hidden = implode( "\n", $hidden );
+
+               $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
+                       Xml::hidden( 'title', $this->title->getPrefixedDBkey() ) .
+                       Xml::fieldset( wfMsg( 'newpages' ) ) .
+                       Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
+                       "<tr>
+                               <td class='mw-label'>" .
+                                       Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
+                               "</td>
+                               <td class='mw-input'>" .
+                                       Xml::namespaceSelector( $namespace, 'all' ) .
+                               "</td>
+                       </tr>" .
+                       ($wgEnableNewpagesUserFilter ?
+                       "<tr>
+                               <td class='mw-label'>" .
+                                       Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
+                               "</td>
+                               <td class='mw-input'>" .
+                                       Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
+                               "</td>
+                       </tr>" : "" ) .
+                       "<tr> <td></td>
+                               <td class='mw-submit'>" .
+                                       Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
+                               "</td>
+                       </tr>" .
+                       "<tr>
+                               <td></td>
+                               <td class='mw-input'>" .
+                                       $this->filterLinks() .
+                               "</td>
+                       </tr>" .
+                       Xml::closeElement( 'table' ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       $hidden .
+                       Xml::closeElement( 'form' );
+
+               $wgOut->addHTML( $form );
+       }
+
+       protected function setSyndicated() {
+               global $wgOut;
+               $queryParams = array(
+                       'namespace' => $this->opts->getValue( 'namespace' ),
+                       'username' => $this->opts->getValue( 'username' )
+               );
+               $wgOut->setSyndicated( true );
+               $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
        }
 
        /**
@@ -88,19 +233,22 @@ class NewPagesPage extends QueryPage {
         * @param $result Result row
         * @return string
         */
-       function formatResult( $skin, $result ) {
-               global $wgLang, $wgContLang;
+       public function formatRow( $result ) {
+               global $wgLang, $wgContLang, $wgUser;
                $dm = $wgContLang->getDirMark();
 
-               $title = Title::makeTitleSafe( $result->namespace, $result->title );
-               $time = $wgLang->timeAndDate( $result->timestamp, true );
-               $plink = $skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rcid : '' );
-               $hist = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
-               $length = wfMsgHtml( 'nbytes', $wgLang->formatNum( htmlspecialchars( $result->length ) ) );
-               $ulink = $skin->userLink( $result->user, $result->user_text ) . $skin->userToolLinks( $result->user, $result->user_text );
-               $comment = $skin->commentBlock( $result->comment );
+               $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
+               $time = $wgLang->timeAndDate( $result->rc_timestamp, true );
+               $plink = $this->skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rc_id : '' );
+               $hist = $this->skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
+               $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
+                       $wgLang->formatNum( $result->length ) );
+               $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
+                       $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
+               $comment = $this->skin->commentBlock( $result->rc_comment );
+               $css = $this->patrollable( $result ) ? " class='not-patrolled'" : '';
 
-               return "{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}";
+               return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}</li>\n";
        }
 
        /**
@@ -109,94 +257,189 @@ class NewPagesPage extends QueryPage {
         * @param $result Result row
         * @return bool
         */
-       function patrollable( $result ) {
-               global $wgUser, $wgUseRCPatrol;
-               return $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) && !$result->patrolled;
-       }
-
-       function feedItemDesc( $row ) {
-               if( isset( $row->rev_id ) ) {
-                       $revision = Revision::newFromId( $row->rev_id );
-                       if( $revision ) {
-                               return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
-                                       htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
-                                       nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
+       protected function patrollable( $result ) {
+               global $wgUser;
+               return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
+       }
+
+       /**
+        * Output a subscription feed listing recent edits to this page.
+        * @param string $type
+        */
+       protected function feed( $type, $options ) {
+               require_once 'SpecialRecentchanges.php';
+
+               global $wgFeed, $wgFeedClasses;
+
+               if ( !$wgFeed ) {
+                       global $wgOut;
+                       $wgOut->addWikiMsg( 'feed-unavailable' );
+                       return;
+               }
+
+               if( !isset( $wgFeedClasses[$type] ) ) {
+                       global $wgOut;
+                       $wgOut->addWikiMsg( 'feed-invalid' );
+                       return;
+               }
+
+               $feed = new $wgFeedClasses[$type](
+                       $this->feedTitle(),
+                       wfMsg( 'tagline' ),
+                       $this->title->getFullUrl() );
+
+               $pager = new NewPagesPager( $this, $this->opts );
+               $limit = $this->opts->getValue( 'limit' );
+               global $wgFeedLimit;
+               if( $limit > $wgFeedLimit ) {
+                       $limit = $wgFeedLimit;
+               }
+               $pager->mLimit = $limit;
+
+               $feed->outHeader();
+               if( $pager->getNumRows() > 0 ) {
+                       while( $row = $pager->mResult->fetchObject() ) {
+                               $feed->outItem( $this->feedItem( $row ) );
                        }
                }
-               return parent::feedItemDesc( $row );
+               $feed->outFooter();
        }
-       
-       /**
-        * Show a form for filtering namespace and username
-        *
-        * @return string
-        */     
-       function getPageHeader() {
-               $self = Title::makeTitle( NS_SPECIAL, $this->getName() );
-               $form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
-               $form .= '<table><tr><td align="right">' . wfMsgHtml( 'namespace' ) . '</td>';
-               $form .= '<td>' . HtmlNamespaceSelector( $this->namespace ) . '</td><tr>';
-               $form .= '<tr><td align="right">' . wfMsgHtml( 'newpages-username' ) . '</td>';
-               $form .= '<td>' . wfInput( 'username', 30, $this->username ) . '</td></tr>';
-               $form .= '<tr><td></td><td>' . wfSubmitButton( wfMsg( 'allpagessubmit' ) ) . '</td></tr></table>';
-               $form .= wfHidden( 'offset', $this->offset ) . wfHidden( 'limit', $this->limit ) . '</form>';
-               return $form;
-       }
-       
+
+       protected function feedTitle() {
+               global $wgContLanguageCode, $wgSitename;
+               $page = SpecialPage::getPage( 'Newpages' );
+               $desc = $page->getDescription();
+               return "$wgSitename - $desc [$wgContLanguageCode]";
+       }
+
+       protected function feedItem( $row ) {
+               $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
+               if( $title ) {
+                       $date = $row->rc_timestamp;
+                       $comments = $this->stripComment( $row->rc_comment );
+
+                       return new FeedItem(
+                               $title->getPrefixedText(),
+                               $this->feedItemDesc( $row ),
+                               $title->getFullURL(),
+                               $date,
+                               $this->feedItemAuthor( $row ),
+                               $comments);
+               } else {
+                       return NULL;
+               }
+       }
+
        /**
-        * Link parameters
-        *
-        * @return array
+        * Quickie hack... strip out wikilinks to more legible form from the comment.
         */
-       function linkParameters() {
-               return( array( 'namespace' => $this->namespace, 'username' => $this->username ) );
+       protected function stripComment( $text ) {
+               return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
+       }
+
+       protected function feedItemAuthor( $row ) {
+               return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
+       }
+
+       protected function feedItemDesc( $row ) {
+               $revision = Revision::newFromId( $row->rev_id );
+               if( $revision ) {
+                       return '<p>' . htmlspecialchars( $revision->getUserText() ) . ': ' .
+                               htmlspecialchars( $revision->getComment() ) . 
+                               "</p>\n<hr />\n<div>" .
+                               nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
+               }
+               return '';
        }
-       
 }
 
 /**
- * constructor
+ * @ingroup SpecialPage Pager
  */
-function wfSpecialNewpages($par, $specialPage) {
-       global $wgRequest, $wgContLang;
+class NewPagesPager extends ReverseChronologicalPager {
+       // Stored opts
+       protected $opts, $mForm;
 
-       list( $limit, $offset ) = wfCheckLimits();
-       $namespace = NS_MAIN;
-       $username = '';
+       private $hideliu, $hidepatrolled, $hidebots, $namespace, $user, $spTitle;
 
-       if ( $par ) {
-               $bits = preg_split( '/\s*,\s*/', trim( $par ) );
-               foreach ( $bits as $bit ) {
-                       if ( 'shownav' == $bit )
-                               $shownavigation = true;
-                       if ( is_numeric( $bit ) )
-                               $limit = $bit;
+       function __construct( $form, FormOptions $opts ) {
+               parent::__construct();
+               $this->mForm = $form;
+               $this->opts = $opts;
+       }
 
-                       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;
-                               }
-                       }
+       function getTitle(){
+               static $title = null;
+               if ( $title === null )
+                       $title = SpecialPage::getTitleFor( 'Newpages' );
+               return $title;
+       }
+
+       function getQueryInfo() {
+               global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
+               $conds = array();
+               $conds['rc_new'] = 1;
+
+               $namespace = $this->opts->getValue( 'namespace' );
+               $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
+
+               $username = $this->opts->getValue( 'username' );
+               $user = Title::makeTitleSafe( NS_USER, $username );
+
+               if( $namespace !== false ) {
+                       $conds['rc_namespace'] = $namespace;
+                       $rcIndexes = array( 'new_name_timestamp' );
+               } else {
+                       $rcIndexes = array( 'rc_timestamp' );
                }
-       } else {
-               if( $ns = $wgRequest->getInt( 'namespace', 0 ) )
-                       $namespace = $ns;
-               if( $un = $wgRequest->getText( 'username' ) )
-                       $username = $un;
+               $conds[] = 'page_id = rc_cur_id';
+               $conds['page_is_redirect'] = 0;
+               # $wgEnableNewpagesUserFilter - temp WMF hack
+               if( $wgEnableNewpagesUserFilter && $user ) {
+                       $conds['rc_user_text'] = $user->getText();
+                       $rcIndexes = 'rc_user_text';
+               # If anons cannot make new pages, don't "exclude logged in users"!
+               } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
+                       $conds['rc_user'] = 0;
+               }
+               # If this user cannot see patrolled edits or they are off, don't do dumb queries!
+               if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
+                       $conds['rc_patrolled'] = 0;
+               }
+               if( $this->opts->getValue( 'hidebots' ) ) {
+                       $conds['rc_bot'] = 0;
+               }
+
+               return array(
+                       'tables' => array( 'recentchanges', 'page' ),
+                       'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
+                               rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id',
+                       'conds' => $conds,
+                       'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) )
+               );
        }
-       
-       if ( ! isset( $shownavigation ) )
-               $shownavigation = ! $specialPage->including();
 
-       $npp = new NewPagesPage( $namespace, $username );
+       function getIndexField() {
+               return 'rc_timestamp';
+       }
 
-       if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
-               $npp->doQuery( $offset, $limit, $shownavigation );
-}
+       function formatRow( $row ) {
+               return $this->mForm->formatRow( $row );
+       }
 
-?>
+       function getStartBody() {
+               # Do a batch existence check on pages
+               $linkBatch = new LinkBatch();
+               while( $row = $this->mResult->fetchObject() ) {
+                       $linkBatch->add( NS_USER, $row->rc_user_text );
+                       $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
+                       $linkBatch->add( $row->rc_namespace, $row->rc_title );
+               }
+               $linkBatch->execute();
+               return "<ul>";
+       }
+
+       function getEndBody() {
+               return "</ul>";
+       }
+}