http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73866

Revision: 73866
Author:   yaron
Date:     2010-09-28 03:36:15 +0000 (Tue, 28 Sep 2010)

Log Message:
-----------
Various changes and additions, mainly for three new features: (1) a new special 
page, 'UnapprovedPages', (2) a new global variable to allow unapproved pages to 
show up as blank, and (3) new edits by users with approval power now 
automatically get set as the approved version

Modified Paths:
--------------
    trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php
    trunk/extensions/ApprovedRevs/ApprovedRevs.php
    trunk/extensions/ApprovedRevs/ApprovedRevs_body.php

Modified: trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php
===================================================================
--- trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php        2010-09-28 
03:18:49 UTC (rev 73865)
+++ trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php        2010-09-28 
03:36:15 UTC (rev 73866)
@@ -1,7 +1,5 @@
 <?php
 
-if ( ! defined( 'MEDIAWIKI' ) ) die();
-
 /**
  * Functions for the Approved Revs extension called by hooks in the MediaWiki
  * code.
@@ -14,38 +12,74 @@
 
 class ApprovedRevsHooks {
 
+       /**
+        * If the page is being saved, set the text of the approved revision
+        * as the text to be parsed, for correct saving of categories,
+        * Semantic MediaWiki properties, etc.
+        */
        static public function setApprovedRevForParsing( &$parser, &$text, 
&$stripState ) {
                global $wgRequest;
                $action = $wgRequest->getVal( 'action' );
                if ( $action == 'submit' ) {
                        $title = $parser->getTitle();
+                       if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
+                               return true;
+                       }
+                       // if this is someone with approval power editing the
+                       // page, exit now, because this will become the
+                       // approved revision anyway
+                       if ( $title->userCan( 'approverevisions' ) ) {
+                               return true;
+                       }
                        $approvedText = ApprovedRevs::getApprovedContent( 
$title );
                        if ( !is_null( $approvedText ) ) {
                                $text = $approvedText;
                        }
+                       // if there's no approved revision, and 'blank if
+                       // unapproved' is set to true, set the text to blank
+                       if ( is_null( $approvedText ) ) {
+                               global $egApprovedRevsBlankIfUnapproved;
+                               if ( $egApprovedRevsBlankIfUnapproved ) {
+                                       $text = '';
+                               }
+                       }
                }
                return true;
        }
 
        /**
+        * If the user saving this page has approval power, automatically
+        * set this latest revision to be the approved one - don't bother
+        * logging the approval, though; the log is reserved for manual
+        * approvals.
+        */
+       static public function setLatestAsApproved( &$article ) {
+               $title = $article->getTitle();
+               if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
+                       return true;
+               }
+               if ( $title->userCan( 'approverevisions' ) ) {
+                       // the rev ID is actually passed in via the hook, but
+                       // it's at the end of a very long set of parameters,
+                       // so for the sake of sanity we'll just re-get it
+                       // here instead
+                       $latestRevisionID = $title->getLatestRevID();
+                       // save approval without logging
+                       ApprovedRevs::saveApprovedRevIDInDB( $title, 
$latestRevisionID );
+               }
+               return true;
+       }
+
+       /**
         * Return the approved revision of the page, if there is one, and if
         * the page is simply being viewed, and if no specific revision has
         * been requested.
         */
        static function showApprovedRevision( &$title, &$article ) {
-               // if a revision ID is set, exit
-               if ( $title->mArticleID > -1 ) {
+               if ( ! ApprovedRevs::isDefaultPageRequest() ) {
                        return true;
                }
-               // if it's any action other than viewing, exit
-               global $wgRequest;
-               if ( $wgRequest->getCheck( 'action' ) &&
-                       $wgRequest->getVal( 'action' ) != 'view' &&
-                       $wgRequest->getVal( 'action' ) != 'purge' &&
-                       $wgRequest->getVal( 'action' ) != 'render' ) {
-                               return true;
-               }
-       
+
                $revisionID = ApprovedRevs::getApprovedRevID( $title );
                if ( ! empty( $revisionID ) ) {
                        $article = new Article( $title, $revisionID );
@@ -53,6 +87,24 @@
                return true;
        }
 
+       public static function showBlankIfUnapproved( &$article, &$content ) {
+               if ( ! ApprovedRevs::isDefaultPageRequest() ) {
+                       return true;
+               }
+
+               $title = $article->getTitle();
+               $revisionID = ApprovedRevs::getApprovedRevID( $title );
+               if ( empty( $revisionID ) ) {
+                       global $egApprovedRevsBlankIfUnapproved;
+                       if ( $egApprovedRevsBlankIfUnapproved ) {
+                               $content = '';
+                               global $wgOut;
+                               $wgOut->setSubtitle( wfMsg( 
'approvedrevs-blankpageshown' ) );
+                       }
+               }
+               return true;
+       }
+
        /**
         * If user is viewing the page via its main URL, and what they're
         * seeing is the approved revision of the page, remove the standard
@@ -236,11 +288,20 @@
 
                ApprovedRevs::unsetApproval( $title );
 
+               // the message depends on whether the page should display
+               // a blank right now or not
+               global $egApprovedRevsBlankIfUnapproved;
+               if ( $egApprovedRevsBlankIfUnapproved ) {
+                       $successMsg = wfMsg( 'approvedrevs-unapprovesuccess2' );
+               } else {
+                       $successMsg = wfMsg( 'approvedrevs-unapprovesuccess' );
+               }
+
                global $wgOut;
                $wgOut->addHTML( '              ' . Xml::element(
                        'div',
                        array( 'class' => 'successbox' ),
-                       wfMsg( 'approvedrevs-unapprovesuccess' )
+                       $successMsg
                ) . "\n" );
                $wgOut->addHTML( '              ' . Xml::element(
                        'p',
@@ -266,7 +327,7 @@
        }
 
        /**
-        * Deletes the approval record in the database if the page itself is
+        * Delete the approval record in the database if the page itself is
         * deleted.
         */
        static function deleteRevisionApproval( &$article, &$user, $reason, $id 
) {
@@ -306,7 +367,7 @@
        }
 
        /**
-        * Adds a link to 'Special:ApprovedPages' to the the page
+        * Add a link to 'Special:ApprovedPages' to the the page
         * 'Special:AdminLinks', defined by the Admin Links extension.
         */
        function addToAdminLinks( &$admin_links_tree ) {
@@ -317,6 +378,7 @@
                        $general_section->addRow( $extensions_row );
                }
                $extensions_row->addItem( ALItem::newFromSpecialPage( 
'ApprovedPages' ) );
+               $extensions_row->addItem( ALItem::newFromSpecialPage( 
'UnapprovedPages' ) );
                return true;
        }
 
@@ -326,6 +388,7 @@
                $dir = dirname( __FILE__ );
 
                // DB updates
+               // For now, there's just a single SQL file for all DB types.
                //if ( $wgDBtype == 'mysql' ) {
                        $wgExtNewTables[] = array( 'approved_revs', 
"$dir/ApprovedRevs.sql" );
                //}

Modified: trunk/extensions/ApprovedRevs/ApprovedRevs.php
===================================================================
--- trunk/extensions/ApprovedRevs/ApprovedRevs.php      2010-09-28 03:18:49 UTC 
(rev 73865)
+++ trunk/extensions/ApprovedRevs/ApprovedRevs.php      2010-09-28 03:36:15 UTC 
(rev 73866)
@@ -9,7 +9,7 @@
  * @author Yaron Koren
  */
 
-define( 'APPROVED_REVS_VERSION', '0.3.1' );
+define( 'APPROVED_REVS_VERSION', '0.4' );
 
 // credits
 $wgExtensionCredits['other'][] = array(
@@ -24,6 +24,7 @@
 // global variables
 $egApprovedRevsIP = dirname( __FILE__ ) . '/';
 $egApprovedRevsNamespaces = array( NS_MAIN, NS_TEMPLATE, NS_HELP, NS_PROJECT );
+$egApprovedRevsBlankIfUnapproved = false;
 
 // internationalization
 $wgExtensionMessagesFiles['ApprovedRevs'] = $egApprovedRevsIP . 
'ApprovedRevs.i18n.php';
@@ -35,10 +36,15 @@
 $wgSpecialPages['ApprovedPages'] = 'ARApprovedPages';
 $wgAutoloadClasses['ARApprovedPages'] = $egApprovedRevsIP . 
'AR_ApprovedPages.php';
 $wgSpecialPageGroups['ApprovedPages'] = 'pages';
+$wgSpecialPages['UnapprovedPages'] = 'ARUnapprovedPages';
+$wgAutoloadClasses['ARUnapprovedPages'] = $egApprovedRevsIP . 
'AR_UnapprovedPages.php';
+$wgSpecialPageGroups['UnapprovedPages'] = 'pages';
 
 // hooks
 $wgHooks['ParserBeforeInternalParse'][] = 
'ApprovedRevsHooks::setApprovedRevForParsing';
+$wgHooks['ArticleSaveComplete'][] = 'ApprovedRevsHooks::setLatestAsApproved';
 $wgHooks['ArticleFromTitle'][] = 'ApprovedRevsHooks::showApprovedRevision';
+$wgHooks['ArticleAfterFetchContent'][] = 
'ApprovedRevsHooks::showBlankIfUnapproved';
 $wgHooks['DisplayOldSubtitle'][] = 'ApprovedRevsHooks::setSubtitle';
 $wgHooks['SkinTemplateNavigation'][] = 'ApprovedRevsHooks::changeEditLink';
 $wgHooks['PageHistoryBeforeList'][] = 
'ApprovedRevsHooks::storeApprovedRevisionForHistoryPage';
@@ -66,4 +72,5 @@
 $wgAvailableRights[] = 'viewlinktolatest';
 $wgGroupPermissions['*']['viewlinktolatest'] = true;
 
+// page properties
 $wgPageProps['approvedrevs'] = 'Whether or not the page is approvable';

Modified: trunk/extensions/ApprovedRevs/ApprovedRevs_body.php
===================================================================
--- trunk/extensions/ApprovedRevs/ApprovedRevs_body.php 2010-09-28 03:18:49 UTC 
(rev 73865)
+++ trunk/extensions/ApprovedRevs/ApprovedRevs_body.php 2010-09-28 03:36:15 UTC 
(rev 73866)
@@ -45,6 +45,33 @@
        }
 
        /**
+        * Helper function - returns whether the user is currently requesting
+        * a page via the simple URL for it - not specfying a version number,
+        * not editing the page, etc.
+        */
+       public static function isDefaultPageRequest() {
+               // this first test seems to no longer work with MW 1.16
+               /*
+               if ( $title->mArticleID > -1 ) {
+                       return true;
+               }
+               */
+               global $wgRequest;
+               if ( $wgRequest->getCheck( 'oldid' ) ) {
+                       return false;
+               }
+               // check if it's an action other than viewing
+               global $wgRequest;
+               if ( $wgRequest->getCheck( 'action' ) &&
+                       $wgRequest->getVal( 'action' ) != 'view' &&
+                       $wgRequest->getVal( 'action' ) != 'purge' &&
+                       $wgRequest->getVal( 'action' ) != 'render' ) {
+                               return false;
+               }
+               return true;
+       }
+
+       /**
         * Returns whether this page can be approved - either because it's in
         * a supported namespace, or because it's been specially marked as
         * approvable. Also stores the boolean answer as a field in the page
@@ -82,13 +109,7 @@
                return $isApprovable;
        }
 
-       /**
-        * Sets a certain revision as the approved one for this page in the
-        * approved_revs DB table; calls a "links update" on this revision
-        * so that category information can be stored correctly, as well as
-        * info for extensions such as Semantic MediaWiki; and logs the action.
-        */
-       public static function setApprovedRevID( $title, $rev_id, $is_latest = 
false ) {
+       public static function saveApprovedRevIDInDB( $title, $rev_id ) {
                $dbr = wfGetDB( DB_MASTER );
                $page_id = $title->getArticleId();
                $old_rev_id = $dbr->selectField( 'approved_revs', 'rev_id', 
array( 'page_id' => $page_id ) );
@@ -97,7 +118,16 @@
                } else {
                        $dbr->insert( 'approved_revs', array( 'page_id' => 
$page_id, 'rev_id' => $rev_id ) );
                }
+       }
 
+       /**
+        * Sets a certain revision as the approved one for this page in the
+        * approved_revs DB table; calls a "links update" on this revision
+        * so that category information can be stored correctly, as well as
+        * info for extensions such as Semantic MediaWiki; and logs the action.
+        */
+       public static function setApprovedRevID( $title, $rev_id, $is_latest = 
false ) {
+               self::saveApprovedRevIDInDB( $title, $rev_id );
                // if the revision being approved is definitely the latest
                // one, there's no need to call the parser on it
                if ( !$is_latest ) {



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to