r77231 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77230‎ | r77231 | r77232 >
Date:15:40, 24 November 2010
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Moved switches in Special:Unusedimages and Special:Ancientpages to extract a unix timestamp from a fielf to DatabaseBase and related classes to avoid code duplication
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMssql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseOracle.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialAncientpages.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUnusedimages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -474,6 +474,9 @@
475475 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
476476 }
477477
 478+ public function unixTimestamp( $field ) {
 479+ return "UNIX_TIMESTAMP($field)";
 480+ }
478481
479482 /**
480483 * Determines if the last failure was due to a deadlock
Index: trunk/phase3/includes/db/DatabaseOracle.php
@@ -839,6 +839,10 @@
840840 return 'SELECT * ' . ( $all ? '':'/* UNION_UNIQUE */ ' ) . 'FROM (' . implode( $glue, $sqls ) . ')' ;
841841 }
842842
 843+ public function unixTimestamp( $field ) {
 844+ return "((trunc($field) - to_date('19700101','YYYYMMDD')) * 86400)";
 845+ }
 846+
843847 function wasDeadlock() {
844848 return $this->lastErrno() == 'OCI-00060';
845849 }
Index: trunk/phase3/includes/db/Database.php
@@ -1989,6 +1989,16 @@
19901990 }
19911991
19921992 /**
 1993+ * Convert a field to an unix timestamp
 1994+ *
 1995+ * @param $field String: field name
 1996+ * @return String: SQL statement
 1997+ */
 1998+ public function unixTimestamp( $field ) {
 1999+ return "EXTRACT(epoch FROM $field)";
 2000+ }
 2001+
 2002+ /**
19932003 * Determines if the last failure was due to a deadlock
19942004 * STUB
19952005 */
Index: trunk/phase3/includes/db/DatabaseMssql.php
@@ -787,6 +787,10 @@
788788 return false;
789789 }
790790
 791+ public function unixTimestamp( $field ) {
 792+ return "DATEDIFF(s,CONVERT(datetime,'1/1/1970'),$field)";
 793+ }
 794+
791795 /**
792796 * Begin a transaction, committing any previously open transaction
793797 */
Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -431,6 +431,10 @@
432432 return implode( $glue, $sqls );
433433 }
434434
 435+ public function unixTimestamp( $field ) {
 436+ return $field;
 437+ }
 438+
435439 function wasDeadlock() {
436440 return $this->lastErrno() == 5; // SQLITE_BUSY
437441 }
Index: trunk/phase3/includes/specials/SpecialUnusedimages.php
@@ -40,25 +40,11 @@
4141 function isSyndicated() { return false; }
4242
4343 function getSQL() {
44 - global $wgCountCategorizedImagesAsUsed, $wgDBtype;
 44+ global $wgCountCategorizedImagesAsUsed;
 45+
4546 $dbr = wfGetDB( DB_SLAVE );
4647
47 - switch ($wgDBtype) {
48 - case 'mysql':
49 - $epoch = 'UNIX_TIMESTAMP(img_timestamp)';
50 - break;
51 - case 'oracle':
52 - $epoch = '((trunc(img_timestamp) - to_date(\'19700101\',\'YYYYMMDD\')) * 86400)';
53 - break;
54 - case 'sqlite':
55 - $epoch = 'img_timestamp';
56 - break;
57 - case 'mssql':
58 - $epoch = 'DATEDIFF(s,CONVERT(datetime,\'1/1/1970\'),img_timestamp)';
59 - break;
60 - default:
61 - $epoch = 'EXTRACT(epoch FROM img_timestamp)';
62 - }
 48+ $epoch = $db->unixTimestamp( 'rev_timestamp' );
6349
6450 if ( $wgCountCategorizedImagesAsUsed ) {
6551 list( $page, $image, $imagelinks, $categorylinks ) = $dbr->tableNamesN( 'page', 'image', 'imagelinks', 'categorylinks' );
Index: trunk/phase3/includes/specials/SpecialAncientpages.php
@@ -39,32 +39,11 @@
4040 function isSyndicated() { return false; }
4141
4242 function getSQL() {
43 - global $wgDBtype;
4443 $db = wfGetDB( DB_SLAVE );
4544 $page = $db->tableName( 'page' );
4645 $revision = $db->tableName( 'revision' );
 46+ $epoch = $db->unixTimestamp( 'rev_timestamp' );
4747
48 - switch ($wgDBtype) {
49 - case 'mysql':
50 - $epoch = 'UNIX_TIMESTAMP(rev_timestamp)';
51 - break;
52 - case 'ibm_db2':
53 - // TODO implement proper conversion to a Unix epoch
54 - $epoch = 'rev_timestamp';
55 - break;
56 - case 'oracle':
57 - $epoch = '((trunc(rev_timestamp) - to_date(\'19700101\',\'YYYYMMDD\')) * 86400)';
58 - break;
59 - case 'sqlite':
60 - $epoch = 'rev_timestamp';
61 - break;
62 - case 'mssql':
63 - $epoch = 'DATEDIFF(s,CONVERT(datetime,\'1/1/1970\'),rev_timestamp)';
64 - break;
65 - default:
66 - $epoch = 'EXTRACT(epoch FROM rev_timestamp)';
67 - }
68 -
6948 return
7049 "SELECT 'Ancientpages' as type,
7150 page_namespace as namespace,

Follow-up revisions

RevisionCommit summaryAuthorDate
r77274Fix for r77231: correct variable name, was throwing fatal errorsialex09:38, 25 November 2010
r79345Remove now-unused SQL timestamp conversion functions added in r77231. They we...catrope16:29, 31 December 2010

Comments

#Comment by Raymond (talk | contribs)   08:08, 25 November 2010

Seen on translatewiki:

[24-Nov-2010 22:32:13] PHP Notice:  Undefined variable: db in /www/w/includes/specials/SpecialUnusedimages.php on line 47
[24-Nov-2010 22:32:13] PHP Fatal error:  Call to a member function unixTimestamp() on a non-object in /www/w/includes/specials/SpecialUnusedimages.php on line 47
[24-Nov-2010 23:35:35] PHP Fatal error:  Call to a member function format() on a non-object in /www/w/includes/GlobalFunctions.php on line 2055
[24-Nov-2010 23:35:36] PHP Fatal error:  Call to a member function format() on a non-object in /www/w/includes/GlobalFunctions.php on line 2055

To be fair I am not sure if the 3rd and 4th line belonge to this commit.

#Comment by IAlex (talk | contribs)   09:41, 25 November 2010

Two first corrected in 77274; I don't know from where come the two others.

#Comment by Catrope (talk | contribs)   15:58, 3 December 2010

There's a better approach to this in the querypage-work2 branch, which we hope to merge to trunk in the week before Christmas.

#Comment by Reedy (talk | contribs)   02:37, 6 January 2011

Marking resolved as this is superceeded by the querypage-work2 branch

Status & tagging log