r73866 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73865‎ | r73866 | r73867 >
Date:03:36, 28 September 2010
Author:yaron
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/ApprovedRevs/ApprovedRevs.php (modified) (history)
  • /trunk/extensions/ApprovedRevs/ApprovedRevs_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ApprovedRevs/ApprovedRevs.php
@@ -9,7 +9,7 @@
1010 * @author Yaron Koren
1111 */
1212
13 -define( 'APPROVED_REVS_VERSION', '0.3.1' );
 13+define( 'APPROVED_REVS_VERSION', '0.4' );
1414
1515 // credits
1616 $wgExtensionCredits['other'][] = array(
@@ -24,6 +24,7 @@
2525 // global variables
2626 $egApprovedRevsIP = dirname( __FILE__ ) . '/';
2727 $egApprovedRevsNamespaces = array( NS_MAIN, NS_TEMPLATE, NS_HELP, NS_PROJECT );
 28+$egApprovedRevsBlankIfUnapproved = false;
2829
2930 // internationalization
3031 $wgExtensionMessagesFiles['ApprovedRevs'] = $egApprovedRevsIP . 'ApprovedRevs.i18n.php';
@@ -35,10 +36,15 @@
3637 $wgSpecialPages['ApprovedPages'] = 'ARApprovedPages';
3738 $wgAutoloadClasses['ARApprovedPages'] = $egApprovedRevsIP . 'AR_ApprovedPages.php';
3839 $wgSpecialPageGroups['ApprovedPages'] = 'pages';
 40+$wgSpecialPages['UnapprovedPages'] = 'ARUnapprovedPages';
 41+$wgAutoloadClasses['ARUnapprovedPages'] = $egApprovedRevsIP . 'AR_UnapprovedPages.php';
 42+$wgSpecialPageGroups['UnapprovedPages'] = 'pages';
3943
4044 // hooks
4145 $wgHooks['ParserBeforeInternalParse'][] = 'ApprovedRevsHooks::setApprovedRevForParsing';
 46+$wgHooks['ArticleSaveComplete'][] = 'ApprovedRevsHooks::setLatestAsApproved';
4247 $wgHooks['ArticleFromTitle'][] = 'ApprovedRevsHooks::showApprovedRevision';
 48+$wgHooks['ArticleAfterFetchContent'][] = 'ApprovedRevsHooks::showBlankIfUnapproved';
4349 $wgHooks['DisplayOldSubtitle'][] = 'ApprovedRevsHooks::setSubtitle';
4450 $wgHooks['SkinTemplateNavigation'][] = 'ApprovedRevsHooks::changeEditLink';
4551 $wgHooks['PageHistoryBeforeList'][] = 'ApprovedRevsHooks::storeApprovedRevisionForHistoryPage';
@@ -66,4 +72,5 @@
6773 $wgAvailableRights[] = 'viewlinktolatest';
6874 $wgGroupPermissions['*']['viewlinktolatest'] = true;
6975
 76+// page properties
7077 $wgPageProps['approvedrevs'] = 'Whether or not the page is approvable';
Index: trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php
@@ -1,7 +1,5 @@
22 <?php
33
4 -if ( ! defined( 'MEDIAWIKI' ) ) die();
5 -
64 /**
75 * Functions for the Approved Revs extension called by hooks in the MediaWiki
86 * code.
@@ -14,38 +12,74 @@
1513
1614 class ApprovedRevsHooks {
1715
 16+ /**
 17+ * If the page is being saved, set the text of the approved revision
 18+ * as the text to be parsed, for correct saving of categories,
 19+ * Semantic MediaWiki properties, etc.
 20+ */
1821 static public function setApprovedRevForParsing( &$parser, &$text, &$stripState ) {
1922 global $wgRequest;
2023 $action = $wgRequest->getVal( 'action' );
2124 if ( $action == 'submit' ) {
2225 $title = $parser->getTitle();
 26+ if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
 27+ return true;
 28+ }
 29+ // if this is someone with approval power editing the
 30+ // page, exit now, because this will become the
 31+ // approved revision anyway
 32+ if ( $title->userCan( 'approverevisions' ) ) {
 33+ return true;
 34+ }
2335 $approvedText = ApprovedRevs::getApprovedContent( $title );
2436 if ( !is_null( $approvedText ) ) {
2537 $text = $approvedText;
2638 }
 39+ // if there's no approved revision, and 'blank if
 40+ // unapproved' is set to true, set the text to blank
 41+ if ( is_null( $approvedText ) ) {
 42+ global $egApprovedRevsBlankIfUnapproved;
 43+ if ( $egApprovedRevsBlankIfUnapproved ) {
 44+ $text = '';
 45+ }
 46+ }
2747 }
2848 return true;
2949 }
3050
3151 /**
 52+ * If the user saving this page has approval power, automatically
 53+ * set this latest revision to be the approved one - don't bother
 54+ * logging the approval, though; the log is reserved for manual
 55+ * approvals.
 56+ */
 57+ static public function setLatestAsApproved( &$article ) {
 58+ $title = $article->getTitle();
 59+ if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
 60+ return true;
 61+ }
 62+ if ( $title->userCan( 'approverevisions' ) ) {
 63+ // the rev ID is actually passed in via the hook, but
 64+ // it's at the end of a very long set of parameters,
 65+ // so for the sake of sanity we'll just re-get it
 66+ // here instead
 67+ $latestRevisionID = $title->getLatestRevID();
 68+ // save approval without logging
 69+ ApprovedRevs::saveApprovedRevIDInDB( $title, $latestRevisionID );
 70+ }
 71+ return true;
 72+ }
 73+
 74+ /**
3275 * Return the approved revision of the page, if there is one, and if
3376 * the page is simply being viewed, and if no specific revision has
3477 * been requested.
3578 */
3679 static function showApprovedRevision( &$title, &$article ) {
37 - // if a revision ID is set, exit
38 - if ( $title->mArticleID > -1 ) {
 80+ if ( ! ApprovedRevs::isDefaultPageRequest() ) {
3981 return true;
4082 }
41 - // if it's any action other than viewing, exit
42 - global $wgRequest;
43 - if ( $wgRequest->getCheck( 'action' ) &&
44 - $wgRequest->getVal( 'action' ) != 'view' &&
45 - $wgRequest->getVal( 'action' ) != 'purge' &&
46 - $wgRequest->getVal( 'action' ) != 'render' ) {
47 - return true;
48 - }
49 -
 83+
5084 $revisionID = ApprovedRevs::getApprovedRevID( $title );
5185 if ( ! empty( $revisionID ) ) {
5286 $article = new Article( $title, $revisionID );
@@ -53,6 +87,24 @@
5488 return true;
5589 }
5690
 91+ public static function showBlankIfUnapproved( &$article, &$content ) {
 92+ if ( ! ApprovedRevs::isDefaultPageRequest() ) {
 93+ return true;
 94+ }
 95+
 96+ $title = $article->getTitle();
 97+ $revisionID = ApprovedRevs::getApprovedRevID( $title );
 98+ if ( empty( $revisionID ) ) {
 99+ global $egApprovedRevsBlankIfUnapproved;
 100+ if ( $egApprovedRevsBlankIfUnapproved ) {
 101+ $content = '';
 102+ global $wgOut;
 103+ $wgOut->setSubtitle( wfMsg( 'approvedrevs-blankpageshown' ) );
 104+ }
 105+ }
 106+ return true;
 107+ }
 108+
57109 /**
58110 * If user is viewing the page via its main URL, and what they're
59111 * seeing is the approved revision of the page, remove the standard
@@ -236,11 +288,20 @@
237289
238290 ApprovedRevs::unsetApproval( $title );
239291
 292+ // the message depends on whether the page should display
 293+ // a blank right now or not
 294+ global $egApprovedRevsBlankIfUnapproved;
 295+ if ( $egApprovedRevsBlankIfUnapproved ) {
 296+ $successMsg = wfMsg( 'approvedrevs-unapprovesuccess2' );
 297+ } else {
 298+ $successMsg = wfMsg( 'approvedrevs-unapprovesuccess' );
 299+ }
 300+
240301 global $wgOut;
241302 $wgOut->addHTML( ' ' . Xml::element(
242303 'div',
243304 array( 'class' => 'successbox' ),
244 - wfMsg( 'approvedrevs-unapprovesuccess' )
 305+ $successMsg
245306 ) . "\n" );
246307 $wgOut->addHTML( ' ' . Xml::element(
247308 'p',
@@ -266,7 +327,7 @@
267328 }
268329
269330 /**
270 - * Deletes the approval record in the database if the page itself is
 331+ * Delete the approval record in the database if the page itself is
271332 * deleted.
272333 */
273334 static function deleteRevisionApproval( &$article, &$user, $reason, $id ) {
@@ -306,7 +367,7 @@
307368 }
308369
309370 /**
310 - * Adds a link to 'Special:ApprovedPages' to the the page
 371+ * Add a link to 'Special:ApprovedPages' to the the page
311372 * 'Special:AdminLinks', defined by the Admin Links extension.
312373 */
313374 function addToAdminLinks( &$admin_links_tree ) {
@@ -317,6 +378,7 @@
318379 $general_section->addRow( $extensions_row );
319380 }
320381 $extensions_row->addItem( ALItem::newFromSpecialPage( 'ApprovedPages' ) );
 382+ $extensions_row->addItem( ALItem::newFromSpecialPage( 'UnapprovedPages' ) );
321383 return true;
322384 }
323385
@@ -326,6 +388,7 @@
327389 $dir = dirname( __FILE__ );
328390
329391 // DB updates
 392+ // For now, there's just a single SQL file for all DB types.
330393 //if ( $wgDBtype == 'mysql' ) {
331394 $wgExtNewTables[] = array( 'approved_revs', "$dir/ApprovedRevs.sql" );
332395 //}
Index: trunk/extensions/ApprovedRevs/ApprovedRevs_body.php
@@ -45,6 +45,33 @@
4646 }
4747
4848 /**
 49+ * Helper function - returns whether the user is currently requesting
 50+ * a page via the simple URL for it - not specfying a version number,
 51+ * not editing the page, etc.
 52+ */
 53+ public static function isDefaultPageRequest() {
 54+ // this first test seems to no longer work with MW 1.16
 55+ /*
 56+ if ( $title->mArticleID > -1 ) {
 57+ return true;
 58+ }
 59+ */
 60+ global $wgRequest;
 61+ if ( $wgRequest->getCheck( 'oldid' ) ) {
 62+ return false;
 63+ }
 64+ // check if it's an action other than viewing
 65+ global $wgRequest;
 66+ if ( $wgRequest->getCheck( 'action' ) &&
 67+ $wgRequest->getVal( 'action' ) != 'view' &&
 68+ $wgRequest->getVal( 'action' ) != 'purge' &&
 69+ $wgRequest->getVal( 'action' ) != 'render' ) {
 70+ return false;
 71+ }
 72+ return true;
 73+ }
 74+
 75+ /**
4976 * Returns whether this page can be approved - either because it's in
5077 * a supported namespace, or because it's been specially marked as
5178 * approvable. Also stores the boolean answer as a field in the page
@@ -82,13 +109,7 @@
83110 return $isApprovable;
84111 }
85112
86 - /**
87 - * Sets a certain revision as the approved one for this page in the
88 - * approved_revs DB table; calls a "links update" on this revision
89 - * so that category information can be stored correctly, as well as
90 - * info for extensions such as Semantic MediaWiki; and logs the action.
91 - */
92 - public static function setApprovedRevID( $title, $rev_id, $is_latest = false ) {
 113+ public static function saveApprovedRevIDInDB( $title, $rev_id ) {
93114 $dbr = wfGetDB( DB_MASTER );
94115 $page_id = $title->getArticleId();
95116 $old_rev_id = $dbr->selectField( 'approved_revs', 'rev_id', array( 'page_id' => $page_id ) );
@@ -97,7 +118,16 @@
98119 } else {
99120 $dbr->insert( 'approved_revs', array( 'page_id' => $page_id, 'rev_id' => $rev_id ) );
100121 }
 122+ }
101123
 124+ /**
 125+ * Sets a certain revision as the approved one for this page in the
 126+ * approved_revs DB table; calls a "links update" on this revision
 127+ * so that category information can be stored correctly, as well as
 128+ * info for extensions such as Semantic MediaWiki; and logs the action.
 129+ */
 130+ public static function setApprovedRevID( $title, $rev_id, $is_latest = false ) {
 131+ self::saveApprovedRevIDInDB( $title, $rev_id );
102132 // if the revision being approved is definitely the latest
103133 // one, there's no need to call the parser on it
104134 if ( !$is_latest ) {

Status & tagging log