r69311 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69310‎ | r69311 | r69312 >
Date:19:53, 13 July 2010
Author:yaron
Status:ok (Comments)
Tags:
Comment:
__APPROVEDREVS__ magic word added - sets "approvability"; hasSupportedNamespace() function replaced with pageIsApprovable()
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)
  • /trunk/extensions/ApprovedRevs/maintenance/approveAllPages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ApprovedRevs/maintenance/approveAllPages.php
@@ -55,7 +55,7 @@
5656 // some extensions, like Semantic Forms, need $wgTitle
5757 // set as well
5858 $wgTitle = $title;
59 - if ( ApprovedRevs::hasSupportedNamespace( $title ) &&
 59+ if ( ApprovedRevs::pageIsApprovable( $title ) &&
6060 ! ApprovedRevs::hasApprovedRevision( $title ) ) {
6161 ApprovedRevs::setApprovedRevID( $title, $page->page_latest );
6262 $this->output( wfTimestamp( TS_DB ) . ' Approved the last revision of page "' . $title->getFullText() . '".' );
Index: trunk/extensions/ApprovedRevs/ApprovedRevs_body.php
@@ -14,7 +14,7 @@
1515 * one.
1616 */
1717 public static function getApprovedRevID( $title ) {
18 - if ( ! self::hasSupportedNamespace( $title ) ) {
 18+ if ( ! self::pageIsApprovable( $title ) ) {
1919 return null;
2020 }
2121 $dbr = wfGetDB( DB_SLAVE );
@@ -45,12 +45,37 @@
4646 }
4747
4848 /**
49 - * Returns whether this page is in a namespace that the Approved Revs
50 - * extension doesn't support.
 49+ * Returns whether this page can be approved - either because it's in
 50+ * a supported namespace, or because it's been specially marked as
 51+ * approvable. Also stores the boolean answer as a field in the page
 52+ * object, to speed up processing if it's called more than once.
5153 */
52 - public static function hasSupportedNamespace( $title ) {
 54+ public static function pageIsApprovable( $title ) {
 55+ // if this function was already called for this page, the
 56+ // value should have been stored as a field in the $title object
 57+ if ( ! is_null( $title->isApprovable ) ) {
 58+ return $title->isApprovable;
 59+ }
 60+
 61+ // check the namespace
5362 global $egApprovedRevsNamespaces;
54 - return( in_array( $title->getNamespace(), $egApprovedRevsNamespaces ) );
 63+ if ( in_array( $title->getNamespace(), $egApprovedRevsNamespaces ) ) {
 64+ $title->isApprovable = true;
 65+ return true;
 66+ }
 67+
 68+ // it's not in an included namespace, so check for the page
 69+ // property - for some reason, calling the standard
 70+ // getProperty() function doesn't work, so we just do a DB
 71+ // query on the page_props table
 72+ $dbr = wfGetDB( DB_SLAVE );
 73+ extract( $dbr->tableNames( 'page', 'page_props' ) );
 74+ $sql = "SELECT COUNT(*) FROM $page_props WHERE pp_page = {$title->getArticleID()} AND pp_propname = 'approvedrevs' AND pp_value = 'y'";
 75+ $res = $dbr->query( $sql );
 76+ $row = $dbr->fetchRow( $res );
 77+ $isApprovable = ( $row[0] == '1' );
 78+ $title->isApprovable = $isApprovable;
 79+ return $isApprovable;
5580 }
5681
5782 /**
Index: trunk/extensions/ApprovedRevs/ApprovedRevs.php
@@ -47,6 +47,9 @@
4848 $wgHooks['UnknownAction'][] = 'ApprovedRevsHooks::unsetAsApproved';
4949 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'ApprovedRevsHooks::setTranscludedPageRev';
5050 $wgHooks['ArticleDeleteComplete'][] = 'ApprovedRevsHooks::deleteRevisionApproval';
 51+$wgHooks['MagicWordwgVariableIDs'][] = 'ApprovedRevsHooks::addMagicWordVariableIDs';
 52+$wgHooks['LanguageGetMagic'][] = 'ApprovedRevsHooks::addMagicWordLanguage';
 53+$wgHooks['ParserBeforeTidy'][] = 'ApprovedRevsHooks::handleMagicWords';
5154 $wgHooks['AdminLinks'][] = 'ApprovedRevsHooks::addToAdminLinks';
5255 $wgHooks['LoadExtensionSchemaUpdates'][] = 'ApprovedRevsHooks::describeDBSchema';
5356
Index: trunk/extensions/ApprovedRevs/ApprovedRevs.hooks.php
@@ -144,7 +144,7 @@
145145 */
146146 static function addApprovalLink( $historyPage, &$row , &$s ) {
147147 $title = $historyPage->getTitle();
148 - if ( ! ApprovedRevs::hasSupportedNamespace( $title ) ) {
 148+ if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
149149 return true;
150150 }
151151
@@ -188,7 +188,7 @@
189189 return true;
190190 }
191191 $title = $article->getTitle();
192 - if ( ! ApprovedRevs::hasSupportedNamespace( $title ) ) {
 192+ if ( ! ApprovedRevs::pageIsApprovable( $title ) ) {
193193 return true;
194194 }
195195 if ( ! $title->userCan( 'approverevisions' ) ) {
@@ -275,6 +275,38 @@
276276 }
277277
278278 /**
 279+ * Register magic-word variable IDs
 280+ */
 281+ static function addMagicWordVariableIDs( &$magicWordVariableIDs ) {
 282+ $magicWordVariableIDs[] = 'MAG_APPROVEDREVS';
 283+ return true;
 284+ }
 285+
 286+ /**
 287+ * Set the actual value of the magic words
 288+ */
 289+ static function addMagicWordLanguage( &$magicWords, $langCode ) {
 290+ switch( $langCode ) {
 291+ default:
 292+ $magicWords['MAG_APPROVEDREVS'] = array( 0, '__APPROVEDREVS__' );
 293+ }
 294+ return true;
 295+ }
 296+
 297+ /**
 298+ * Set values in the page_props table based on the presence of the
 299+ * 'APPROVEDREVS' magic word in a page
 300+ */
 301+ static function handleMagicWords( &$parser, &$text ) {
 302+ global $wgOut, $wgAction;
 303+ $mw_hide = MagicWord::get( 'MAG_APPROVEDREVS' );
 304+ if ( $mw_hide->matchAndRemove( $text ) ) {
 305+ $parser->mOutput->setProperty( 'approvedrevs', 'y' );
 306+ }
 307+ return true;
 308+ }
 309+
 310+ /**
279311 * Adds a link to 'Special:ApprovedPages' to the the page
280312 * 'Special:AdminLinks', defined by the Admin Links extension.
281313 */

Follow-up revisions

RevisionCommit summaryAuthorDate
r69323Better formatting for code, removed unneeded variables; r69311yaron01:27, 14 July 2010

Comments

#Comment by Jeroen De Dauw (talk | contribs)   00:46, 14 July 2010

"if ( ! is_null" should be "if ( !is_null" (no space after the !)

$wgAction is unused in handleMagicWords

Using $dbr->numRows() is better (more secure) then manually constructing a count query and executing with $dbr->query.

Status & tagging log