Index: trunk/phase3/includes/Article.php |
— | — | @@ -997,25 +997,31 @@ |
998 | 998 | * when different from the currently set value. |
999 | 999 | * Giving 0 indicates the new page flag should |
1000 | 1000 | * be set on. |
| 1001 | + * @param bool $lastRevIsRedirect If given, will optimize adding and |
| 1002 | + * removing rows in redirect table. |
1001 | 1003 | * @return bool true on success, false on failure |
1002 | 1004 | * @private |
1003 | 1005 | */ |
1004 | | - function updateRevisionOn( &$dbw, $revision, $lastRevision = null ) { |
| 1006 | + function updateRevisionOn( &$dbw, $revision, $lastRevision = null, $lastRevIsRedirect = null ) { |
1005 | 1007 | wfProfileIn( __METHOD__ ); |
1006 | 1008 | |
| 1009 | + $text = $revision->getText(); |
| 1010 | + $rt = Title::newFromRedirect( $text ); |
| 1011 | + |
| 1012 | + $this->updateRedirectOn( $dbw, $rt, $lastRevIsRedirect ); |
| 1013 | + |
1007 | 1014 | $conditions = array( 'page_id' => $this->getId() ); |
1008 | 1015 | if( !is_null( $lastRevision ) ) { |
1009 | 1016 | # An extra check against threads stepping on each other |
1010 | 1017 | $conditions['page_latest'] = $lastRevision; |
1011 | 1018 | } |
1012 | 1019 | |
1013 | | - $text = $revision->getText(); |
1014 | 1020 | $dbw->update( 'page', |
1015 | 1021 | array( /* SET */ |
1016 | 1022 | 'page_latest' => $revision->getId(), |
1017 | 1023 | 'page_touched' => $dbw->timestamp(), |
1018 | 1024 | 'page_is_new' => ($lastRevision === 0) ? 1 : 0, |
1019 | | - 'page_is_redirect' => Article::isRedirect( $text ) ? 1 : 0, |
| 1025 | + 'page_is_redirect' => $rt !== NULL ? 1 : 0, |
1020 | 1026 | 'page_len' => strlen( $text ), |
1021 | 1027 | ), |
1022 | 1028 | $conditions, |
— | — | @@ -1026,6 +1032,56 @@ |
1027 | 1033 | } |
1028 | 1034 | |
1029 | 1035 | /** |
| 1036 | + * Add row to the redirect table if this is a redirect, remove otherwise. |
| 1037 | + * |
| 1038 | + * @param Database $dbw |
| 1039 | + * @param $redirectTitle a title object pointing to the redirect target, |
| 1040 | + * or NULL if this is not a redirect |
| 1041 | + * @param bool $lastRevIsRedirect If given, will optimize adding and |
| 1042 | + * removing rows in redirect table. |
| 1043 | + * @return bool true on success, false on failure |
| 1044 | + * @private |
| 1045 | + */ |
| 1046 | + function updateRedirectOn( &$dbw, $redirectTitle, $lastRevIsRedirect = null ) { |
| 1047 | + |
| 1048 | + // Always update redirects (target link might have changed) |
| 1049 | + // Update/Insert if we don't know if the last revision was a redirect or not |
| 1050 | + // Delete if changing from redirect to non-redirect |
| 1051 | + $isRedirect = !is_null($redirectTitle); |
| 1052 | + if ($isRedirect || is_null($lastRevIsRedirect) || $lastRevIsRedirect !== $isRedirect) { |
| 1053 | + |
| 1054 | + wfProfileIn( __METHOD__ ); |
| 1055 | + |
| 1056 | + $where = array('rd_from' => $this->getId()); |
| 1057 | + |
| 1058 | + if ($isRedirect) { |
| 1059 | + |
| 1060 | + // This title is a redirect, Add/Update row in the redirect table |
| 1061 | + $set = array( /* SET */ |
| 1062 | + 'rd_namespace' => $redirectTitle->getNamespace(), |
| 1063 | + 'rd_title' => $redirectTitle->getDBkey() |
| 1064 | + ); |
| 1065 | + |
| 1066 | + $dbw->update( 'redirect', $set, $where, __METHOD__ ); |
| 1067 | + |
| 1068 | + if ( $dbw->affectedRows() == 0 ) { |
| 1069 | + // Update failed, need to insert the row instead |
| 1070 | + $dbw->insert( 'redirect', array_merge($set, $where), __METHOD__ ); |
| 1071 | + } |
| 1072 | + } else { |
| 1073 | + |
| 1074 | + // This is not a redirect, remove row from redirect table |
| 1075 | + $dbw->delete( 'redirect', $where, __METHOD__); |
| 1076 | + } |
| 1077 | + |
| 1078 | + wfProfileOut( __METHOD__ ); |
| 1079 | + return ( $dbw->affectedRows() != 0 ); |
| 1080 | + } |
| 1081 | + |
| 1082 | + return true; |
| 1083 | + } |
| 1084 | + |
| 1085 | + /** |
1030 | 1086 | * If the given revision is newer than the currently set page_latest, |
1031 | 1087 | * update the page record. Otherwise, do nothing. |
1032 | 1088 | * |
— | — | @@ -1037,7 +1093,7 @@ |
1038 | 1094 | |
1039 | 1095 | $row = $dbw->selectRow( |
1040 | 1096 | array( 'revision', 'page' ), |
1041 | | - array( 'rev_id', 'rev_timestamp' ), |
| 1097 | + array( 'rev_id', 'rev_timestamp', 'page_is_redirect' ), |
1042 | 1098 | array( |
1043 | 1099 | 'page_id' => $this->getId(), |
1044 | 1100 | 'page_latest=rev_id' ), |
— | — | @@ -1048,12 +1104,14 @@ |
1049 | 1105 | return false; |
1050 | 1106 | } |
1051 | 1107 | $prev = $row->rev_id; |
| 1108 | + $lastRevIsRedirect = $row->page_is_redirect === '1'; |
1052 | 1109 | } else { |
1053 | 1110 | # No or missing previous revision; mark the page as new |
1054 | 1111 | $prev = 0; |
| 1112 | + $lastRevIsRedirect = null; |
1055 | 1113 | } |
1056 | 1114 | |
1057 | | - $ret = $this->updateRevisionOn( $dbw, $revision, $prev ); |
| 1115 | + $ret = $this->updateRevisionOn( $dbw, $revision, $prev, $lastRevIsRedirect ); |
1058 | 1116 | wfProfileOut( __METHOD__ ); |
1059 | 1117 | return $ret; |
1060 | 1118 | } |