From 03cdc155b7c6c730de6558b81e8f0e7622ad5a66 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Sat, 26 Mar 2011 19:18:39 +0000 Subject: [PATCH] Topple the last bastion of global-function-based special pages. Also fix HTMLCheckField to work with GET forms. --- includes/AutoLoader.php | 1 + includes/HTMLForm.php | 2 +- includes/SpecialPage.php | 2 +- includes/specials/SpecialNewimages.php | 308 +++++++++---------------- languages/messages/MessagesEn.php | 1 + maintenance/language/messages.inc | 1 + 6 files changed, 109 insertions(+), 206 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index dd819a14f0..85785d3ec6 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -666,6 +666,7 @@ $wgAutoloadLocalClasses = array( 'SpecialLog' => 'includes/specials/SpecialLog.php', 'SpecialMergeHistory' => 'includes/specials/SpecialMergeHistory.php', 'SpecialMostlinkedtemplates' => 'includes/specials/SpecialMostlinkedtemplates.php', + 'SpecialNewFiles' => 'includes/specials/SpecialNewimages.php', 'SpecialPreferences' => 'includes/specials/SpecialPreferences.php', 'SpecialPrefixindex' => 'includes/specials/SpecialPrefixindex.php', 'SpecialProtectedpages' => 'includes/specials/SpecialProtectedpages.php', diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 7af1f56d7c..377783b065 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -1264,7 +1264,7 @@ class HTMLCheckField extends HTMLFormField { } // GetCheck won't work like we want for checks. - if ( $request->getCheck( 'wpEditToken' ) ) { + if ( $request->getCheck( 'wpEditToken' ) || $this->mParent->getMethod() != 'post' ) { // XOR has the following truth table, which is what we want // INVERT VALUE | OUTPUT // true true | false diff --git a/includes/SpecialPage.php b/includes/SpecialPage.php index 6ab1fdf39e..89a015b8df 100644 --- a/includes/SpecialPage.php +++ b/includes/SpecialPage.php @@ -153,7 +153,7 @@ class SpecialPage { 'EditWatchlist' => 'SpecialEditWatchlist', # Recent changes and logs - 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ), + 'Newimages' => 'SpecialNewFiles', 'Log' => 'SpecialLog', 'Watchlist' => 'SpecialWatchlist', 'Newpages' => 'SpecialNewpages', diff --git a/includes/specials/SpecialNewimages.php b/includes/specials/SpecialNewimages.php index 6ee9e46f2c..aeefb4d346 100644 --- a/includes/specials/SpecialNewimages.php +++ b/includes/specials/SpecialNewimages.php @@ -20,244 +20,144 @@ * @file * @ingroup SpecialPage */ +class SpecialNewFiles extends IncludableSpecialPage { -/** - * @todo FIXME: this code is crap, should use Pager and Database::select(). - */ -function wfSpecialNewimages( $par, $specialPage ) { - global $wgUser, $wgOut, $wgLang, $wgRequest, $wgMiserMode; - - $wpIlMatch = $wgRequest->getText( 'wpIlMatch' ); - $dbr = wfGetDB( DB_SLAVE ); - $sk = $wgUser->getSkin(); - $shownav = !$specialPage->including(); - $hidebots = $wgRequest->getBool( 'hidebots' , 1 ); + public function __construct(){ + parent::__construct( 'Newimages' ); + } - $hidebotsql = ''; - if ( $hidebots ) { - # Make a list of group names which have the 'bot' flag set. - $botconds = array(); - foreach ( User::getGroupsWithPermission('bot') as $groupname ) { - $botconds[] = 'ug_group = ' . $dbr->addQuotes( $groupname ); - } + public function execute( $par ){ + global $wgOut, $wgLang; + $this->setHeaders(); - # If not bot groups, do not set $hidebotsql - if ( $botconds ) { - $isbotmember = $dbr->makeList( $botconds, LIST_OR ); + $pager = new NewFilesPager( $par ); - # This join, in conjunction with WHERE ug_group IS NULL, returns - # only those rows from IMAGE where the uploading user is not a mem- - # ber of a group which has the 'bot' permission set. - $ug = $dbr->tableName( 'user_groups' ); - $hidebotsql = " LEFT JOIN $ug ON img_user=ug_user AND ($isbotmember)"; - } + $form = $pager->getForm(); + $wgOut->addWikiMsg( 'newimages-text' ); + $form->prepareForm(); + $form->displayForm( '' ); + $wgOut->addHTML( $pager->getBody() . $pager->getNavigationBar() ); } +} - $image = $dbr->tableName( 'image' ); - $sql = "SELECT img_timestamp from $image"; - if ($hidebotsql) { - $sql .= "$hidebotsql WHERE ug_group IS NULL"; - } - $sql .= ' ORDER BY img_timestamp DESC'; - $sql = $dbr->limitResult($sql, 1, false); - $res = $dbr->query( $sql, __FUNCTION__ ); - $row = $dbr->fetchRow( $res ); - if( $row !== false ) { - $ts = $row[0]; - } else { - $ts = false; - } - - # If we were clever, we'd use this to cache. - $latestTimestamp = wfTimestamp( TS_MW, $ts ); +/** + * @ingroup SpecialPage Pager + */ +class NewFilesPager extends ReverseChronologicalPager { - # Hardcode this for now. - $limit = 48; - $parval = intval( $par ); - if ( $parval ) { - if ( $parval <= $limit && $parval > 0 ) { - $limit = $parval; - } - } + function __construct( $par = null ) { + global $wgRequest, $wgUser; - $where = array(); - $searchpar = array(); - if ( $wpIlMatch != '' && !$wgMiserMode) { - $nt = Title::newFromURL( $wpIlMatch ); - if( $nt ) { - $where[] = 'LOWER(img_name) ' . $dbr->buildLike( $dbr->anyString(), strtolower( $nt->getDBkey() ), $dbr->anyString() ); - $searchpar['wpIlMatch'] = $wpIlMatch; - } - } + $this->like = $wgRequest->getText( 'like' ); + $this->showbots = $wgRequest->getBool( 'showbots' , 0 ); + $this->skin = $wgUser->getSkin(); - $invertSort = false; - $until = $wgRequest->getVal( 'until' ); - if( $until ) { - $where[] = "img_timestamp < '" . $dbr->timestamp( $until ) . "'"; - } - $from = $wgRequest->getVal( 'from' ); - if( $from ) { - $where[] = "img_timestamp >= '" . $dbr->timestamp( $from ) . "'"; - $invertSort = true; + parent::__construct(); } - $sql = 'SELECT img_size, img_name, img_user, img_user_text,'. - "img_description,img_timestamp FROM $image"; - if( $hidebotsql ) { - $sql .= $hidebotsql; - $where[] = 'ug_group IS NULL'; - } - if( count( $where ) ) { - $sql .= ' WHERE ' . $dbr->makeList( $where, LIST_AND ); - } - $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' ); - $sql = $dbr->limitResult($sql, ( $limit + 1 ), false); - $res = $dbr->query( $sql, __FUNCTION__ ); + function getQueryInfo() { + global $wgMiserMode; + $conds = $jconds = array(); + $tables = array( 'image' ); - /** - * We have to flip things around to get the last N after a certain date - */ - $images = array(); - foreach ( $res as $s ) { - if( $invertSort ) { - array_unshift( $images, $s ); - } else { - array_push( $images, $s ); + if( !$this->showbots ) { + $tables[] = 'user_groups'; + $conds[] = 'ug_group IS NULL'; + $jconds['user_groups'] = array( + 'LEFT JOIN', + array( + 'ug_group' => User::getGroupsWithPermission( 'bot' ), + 'ug_user = img_user' + ) + ); } - } - $gallery = new ImageGallery(); - $firstTimestamp = null; - $lastTimestamp = null; - $shownImages = 0; - foreach( $images as $s ) { - $shownImages++; - if( $shownImages > $limit ) { - # One extra just to test for whether to show a page link; - # don't actually show it. - break; + if( !$wgMiserMode && $this->like !== null ){ + $dbr = wfGetDB( DB_SLAVE ); + $likeObj = Title::newFromURL( $this->like ); + if( $likeObj instanceof Title ){ + $like = $dbr->buildLike( $dbr->anyString(), strtolower( $likeObj->getDBkey() ), $dbr->anyString() ); + $conds[] = "LOWER(img_name) $like"; + } } - $name = $s->img_name; - $ut = $s->img_user_text; - - $nt = Title::newFromText( $name, NS_FILE ); - $ul = $sk->link( Title::makeTitle( NS_USER, $ut ), $ut ); - - $gallery->add( $nt, "$ul
\n".htmlspecialchars($wgLang->timeanddate( $s->img_timestamp, true ))."
\n" ); + $query = array( + 'tables' => $tables, + 'fields' => '*', + 'join_conds' => $jconds, + 'conds' => $conds + ); - $timestamp = wfTimestamp( TS_MW, $s->img_timestamp ); - if( empty( $firstTimestamp ) ) { - $firstTimestamp = $timestamp; - } - $lastTimestamp = $timestamp; + return $query; } - $titleObj = SpecialPage::getTitleFor( 'Newimages' ); - $action = $titleObj->getLocalURL( $hidebots ? '' : 'hidebots=0' ); - if ( $shownav && !$wgMiserMode ) { - $wgOut->addHTML( - Xml::openElement( 'form', array( 'action' => $action, 'method' => 'post', 'id' => 'imagesearch' ) ) . - Xml::fieldset( wfMsg( 'newimages-legend' ) ) . - Xml::inputLabel( wfMsg( 'newimages-label' ), 'wpIlMatch', 'wpIlMatch', 20, $wpIlMatch ) . ' ' . - Xml::submitButton( wfMsg( 'ilsubmit' ), array( 'name' => 'wpIlSubmit' ) ) . - Xml::closeElement( 'fieldset' ) . - Xml::closeElement( 'form' ) - ); + function getIndexField(){ + return 'img_timestamp'; } - $bydate = wfMsg( 'bydate' ); - $lt = $wgLang->formatNum( min( $shownImages, $limit ) ); - if ( $shownav ) { - $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate ); - $wgOut->addHTML( $text . "\n" ); + function getStartBody(){ + $this->gallery = new ImageGallery(); } - /** - * Paging controls... - */ - - # If we change bot visibility, this needs to be carried along. - if( !$hidebots ) { - $botpar = array( 'hidebots' => 0 ); - } else { - $botpar = array(); + function getEndBody(){ + return $this->gallery->toHTML(); } - $now = wfTimestampNow(); - $d = $wgLang->date( $now, true ); - $t = $wgLang->time( $now, true ); - $query = array_merge( - array( 'from' => $now ), - $botpar, - $searchpar - ); - - $dateLink = $sk->linkKnown( - $titleObj, - htmlspecialchars( wfMsg( 'sp-newimages-showfrom', $d, $t ) ), - array(), - $query - ); - $query = array_merge( - array( 'hidebots' => ( $hidebots ? 0 : 1 ) ), - $searchpar - ); + function formatRow( $row ) { + global $wgLang; - $showhide = $hidebots ? wfMsg( 'show' ) : wfMsg( 'hide' ); + $name = $row->img_name; + $user = User::newFromId( $row->img_user ); - $botLink = $sk->linkKnown( - $titleObj, - htmlspecialchars( wfMsg( 'showhidebots', $showhide ) ), - array(), - $query - ); + $title = Title::newFromText( $name, NS_FILE ); + $ul = $this->skin->link( $user->getUserpage(), $user->getName() ); - $opts = array( 'parsemag', 'escapenoentities' ); - $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) ); - if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) { - $query = array_merge( - array( 'from' => $firstTimestamp ), - $botpar, - $searchpar - ); - - $prevLink = $sk->linkKnown( - $titleObj, - $prevLink, - array(), - $query + $this->gallery->add( + $title, + "$ul
\n" + . htmlspecialchars( $wgLang->timeanddate( $row->img_timestamp, true ) ) + . "
\n" ); } - $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) ); - if( $invertSort || ( $shownImages > $limit && $lastTimestamp ) ) { - $query = array_merge( - array( 'until' => ( $lastTimestamp ? $lastTimestamp : "" ) ), - $botpar, - $searchpar - ); - - $nextLink = $sk->linkKnown( - $titleObj, - $nextLink, - array(), - $query + function getForm() { + global $wgRequest, $wgMiserMode; + + $fields = array( + 'like' => array( + 'type' => 'text', + 'label-message' => 'newimages-label', + 'name' => 'like', + ), + 'showbots' => array( + 'type' => 'check', + 'label' => wfMessage( 'showhidebots', wfMsg( 'show' ) ), + 'name' => 'showbots', + # 'default' => $wgRequest->getBool( 'showbots', 0 ), + ), + 'limit' => array( + 'type' => 'hidden', + 'default' => $wgRequest->getText( 'limit' ), + 'name' => 'limit', + ), + 'offset' => array( + 'type' => 'hidden', + 'default' => $wgRequest->getText( 'offset' ), + 'name' => 'offset', + ), ); - } - - $prevnext = '

' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'

'; + if( $wgMiserMode ){ + unset( $fields['like'] ); + } - if ($shownav) - $wgOut->addHTML( $prevnext ); + $form = new HTMLForm( $fields ); + $form->setTitle( $this->getTitle() ); + $form->setSubmitText( wfMsg( 'ilsubmit' ) ); + $form->setMethod( 'get' ); + $form->setWrapperLegend( wfMsg( 'newimages-legend' ) ); - if( count( $images ) ) { - $wgOut->addHTML( $gallery->toHTML() ); - if ($shownav) - $wgOut->addHTML( $prevnext ); - } else { - $wgOut->addWikiMsg( 'noimages' ); + return $form; } -} +} \ No newline at end of file diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index d93e3227d3..31cd3141c5 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -3663,6 +3663,7 @@ By executing it, your system may be compromised.", # Special:NewFiles 'newimages' => 'Gallery of new files', +'newimages-text' => 'Below is a list of recently-uploaded files.', 'imagelisttext' => "Below is a list of '''$1''' {{PLURAL:$1|file|files}} sorted $2.", 'newimages-summary' => 'This special page shows the last uploaded files.', 'newimages-legend' => 'Filter', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 385dcf4b1a..c140d3412f 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -2611,6 +2611,7 @@ $wgMessageStructure = array( ), 'newfiles' => array( 'newimages', + 'newimages-text', 'imagelisttext', 'newimages-summary', 'newimages-legend', -- 2.20.1