r27301 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r27300‎ | r27301 | r27302 >
Date:19:06, 7 November 2007
Author:catrope
Status:old
Tags:
Comment:
APIEDIT BRANCH MERGE:
* Splitting deletion reason generation from Article::delete() to Article::generateReason()
* Make reason generation smarter, obsoleting Article::getLastNAuthors() (does any other function actually use it?)
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Article.php
@@ -1839,7 +1839,84 @@
18401840 }
18411841 return implode( ':', $bits );
18421842 }
 1843+
 1844+ /**
 1845+ * Auto-generates a deletion reason
 1846+ * @param bool &$hashistory Whether the page has a history
 1847+ */
 1848+ public function generateReason(&$hashistory)
 1849+ {
 1850+ global $wgContLang;
 1851+ $dbw = wfGetDB(DB_MASTER);
 1852+ // Get the last revision
 1853+ $rev = Revision::newFromTitle($this->mTitle);
 1854+ if(is_null($rev))
 1855+ return false;
 1856+ // Get the article's contents
 1857+ $contents = $rev->getText();
 1858+ $blank = false;
 1859+ // If the page is blank, use the text from the previous revision,
 1860+ // which can only be blank if there's a move/import/protect dummy revision involved
 1861+ if($contents == '')
 1862+ {
 1863+ $prev = $rev->getPrevious();
 1864+ if($prev)
 1865+ {
 1866+ $contents = $prev->getText();
 1867+ $blank = true;
 1868+ }
 1869+ }
18431870
 1871+ // Find out if there was only one contributor
 1872+ // Only scan the last 20 revisions
 1873+ $limit = 20;
 1874+ $res = $dbw->select('revision', 'rev_user_text', array('rev_page' => $this->getID()), __METHOD__,
 1875+ array('LIMIT' => $limit));
 1876+ if($res === false)
 1877+ // This page has no revisions, which is very weird
 1878+ return false;
 1879+ if($res->numRows() > 1)
 1880+ $hasHistory = true;
 1881+ else
 1882+ $hasHistory = false;
 1883+ $row = $dbw->fetchObject($res);
 1884+ $onlyAuthor = $row->rev_user_text;
 1885+ // Try to find a second contributor
 1886+ while(($row = $dbw->fetchObject($res)))
 1887+ if($row->rev_user_text != $onlyAuthor)
 1888+ {
 1889+ $onlyAuthor = false;
 1890+ break;
 1891+ }
 1892+ $dbw->freeResult($res);
 1893+
 1894+ // Generate the summary with a '$1' placeholder
 1895+ if($blank)
 1896+ // The current revision is blank and the one before is also
 1897+ // blank. It's just not our lucky day
 1898+ $reason = wfMsgForContent('exbeforeblank', '$1');
 1899+ else
 1900+ {
 1901+ if($onlyAuthor)
 1902+ $reason = wfMsgForContent('excontentauthor', '$1', $onlyAuthor);
 1903+ else
 1904+ $reason = wfMsgForContent('excontent', '$1');
 1905+ }
 1906+
 1907+ // Replace newlines with spaces to prevent uglyness
 1908+ $contents = preg_replace("/[\n\r]/", ' ', $contents);
 1909+ // Calculate the maximum amount of chars to get
 1910+ // Max content length = max comment length - length of the comment (excl. $1) - '...'
 1911+ $maxLength = 255 - (strlen($reason) - 2) - 3;
 1912+ $contents = $wgContLang->truncate($contents, $maxLength, '...');
 1913+ // Remove possible unfinished links
 1914+ $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents );
 1915+ // Now replace the '$1' placeholder
 1916+ $reason = str_replace( '$1', $contents, $reason );
 1917+ return $reason;
 1918+ }
 1919+
 1920+
18441921 /*
18451922 * UI entry point for page deletion
18461923 */
@@ -1881,88 +1958,16 @@
18821959 return;
18831960 }
18841961
1885 - # determine whether this page has earlier revisions
1886 - # and insert a warning if it does
1887 - $maxRevisions = 20;
1888 - $authors = $this->getLastNAuthors( $maxRevisions, $latest );
 1962+ // Generate deletion reason
 1963+ $hasHistory = false;
 1964+ $reason = $this->generateReason($hasHistory);
18891965
1890 - if( count( $authors ) > 1 && !$confirm ) {
 1966+ // If the page has a history, insert a warning
 1967+ if( $hasHistory && !$confirm ) {
18911968 $skin=$wgUser->getSkin();
18921969 $wgOut->addHTML( '<strong>' . wfMsg( 'historywarning' ) . ' ' . $skin->historyLink() . '</strong>' );
18931970 }
1894 -
1895 - # If a single user is responsible for all revisions, find out who they are
1896 - if ( count( $authors ) == $maxRevisions ) {
1897 - // Query bailed out, too many revisions to find out if they're all the same
1898 - $authorOfAll = false;
1899 - } else {
1900 - $authorOfAll = reset( $authors );
1901 - foreach ( $authors as $author ) {
1902 - if ( $authorOfAll != $author ) {
1903 - $authorOfAll = false;
1904 - break;
1905 - }
1906 - }
1907 - }
1908 - # Fetch article text
1909 - $rev = Revision::newFromTitle( $this->mTitle );
1910 -
1911 - if( !is_null( $rev ) ) {
1912 - # if this is a mini-text, we can paste part of it into the deletion reason
1913 - $text = $rev->getText();
1914 -
1915 - #if this is empty, an earlier revision may contain "useful" text
1916 - $blanked = false;
1917 - if( $text == '' ) {
1918 - $prev = $rev->getPrevious();
1919 - if( $prev ) {
1920 - $text = $prev->getText();
1921 - $blanked = true;
1922 - }
1923 - }
1924 -
1925 - $length = strlen( $text );
1926 -
1927 - # If the last revision is a blank revision (move, import or
1928 - # protection summary) and the one before it was blank
1929 - if( $length == 0 && $reason === '' ) {
1930 - $reason = wfMsgForContent( 'exblank' );
1931 - }
1932 -
1933 - if( $reason === '' ) {
1934 - if( !$blanked ) {
1935 - if( $authorOfAll === false ) {
1936 - $reason = wfMsgForContent( 'excontent', '$1' );
1937 - } else {
1938 - $reason = wfMsgForContent( 'excontentauthor', '$1', $authorOfAll );
1939 - }
1940 - } else {
1941 - $reason = wfMsgForContent( 'exbeforeblank', '$1' );
1942 - }
1943 -
1944 - # comment field=255, find the max length of the content from page
1945 - # Max content length is max comment length, minus length of the actual
1946 - # comment (except for the $1), and minus the possible ... chars
1947 - $maxLength = 255 - ( strlen( $reason ) - 2 ) - 3;
1948 - if( $maxLength < 0 ) {
1949 - $maxLength = 0;
1950 - }
1951 -
1952 - # let's strip out newlines
1953 - $text = preg_replace( "/[\n\r]/", '', $text );
1954 -
1955 - # Truncate to max length
1956 - global $wgContLang;
1957 - $text = $wgContLang->truncate( $text, $maxLength, '...' );
1958 -
1959 - # Remove possible unfinished links
1960 - $text = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $text );
1961 -
1962 - # Add to the reason field
1963 - $reason = str_replace( '$1', $text, $reason );
1964 - }
1965 - }
1966 -
 1971+
19671972 return $this->confirmDelete( '', $reason );
19681973 }
19691974

Status & tagging log