以前要修改MediaWiki的頁面輸出内容,都是通過修改頁面或者模闆的代碼來實現的,但最近在做手機版的時候,原始網站中有一些内容不是通過模闆來實現的,而是直接嵌入文本在頁面中,例如--~~~~這樣的簽名、時間,就不好通過修改模闆删除。
今天咨詢了同事,再仔細查看MediaWiki的相關PHP程序文件,終于是找到了修改的辦法和地方,可以修改includes/OutputPage.php這個程序:
/**
* Append $text to the body HTML
*
* @param $text String: HTML
*/
public function addHTML( $text ) {
//jamesqi 2012-5
$start=strpos($text,'<dl><dd>--<a href=');
$end=strpos($text,'(CST)
</dd></dl>');
if ($start>1 and $end>$start+1) {
$text=substr($text,0,$start).substr($text,$end+16);
}
//jamesqi 2012-5$this->mBodytext .= $text;
}
在頁面HTML源文件中找到需要删除的内容前後的特征字符串,然後很簡單就可以去掉這中間的簽名、時間信息。
另外,MediaWiki的分類頁中每頁200個條目的列表是分成了3列顯示的,這在手機屏幕上顯示也很擁擠了,我嘗試在includes/CategoryPage.php中修改一點地方就可以改為單列顯示:
/**
* Format a list of articles chunked by letter in a three-column
* list, ordered vertically.
*
* TODO: Take the headers into account when creating columns, so they're
* more visually equal.
*
* More distant TODO: Scrap this and use CSS columns, whenever IE finally
* supports those.
*
* @param $articles Array
* @param $articles_start_char Array
* @return String
* @private
*/
function columnList( $articles, $articles_start_char ) {
$columns = array_combine( $articles, $articles_start_char );
# Split into three columns
$columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
$ret = '<table width="100%"><tr valign="top"><td>';
$prevchar = null;
foreach ( $columns as $column ) {
$colContents = array();
# Kind of like array_flip() here, but we keep duplicates in an
# array instead of dropping them.
foreach ( $column as $article => $char ) {
if ( !isset( $colContents[$char] ) ) {
$colContents[$char] = array();
}
$colContents[$char][] = $article;
}
$first = true;
foreach ( $colContents as $char => $articles ) {
$ret .= '<h3>' . htmlspecialchars( $char );
if ( $first && $char === $prevchar ) {
# We're continuing a previous chunk at the top of a new
# column, so add " cont." after the letter.
$ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
}
$ret .= "</h3>\n";
$ret .= '<ul><li>';
$ret .= implode( "</li>\n<li>", $articles );
$ret .= '</li></ul>';
$first = false;
$prevchar = $char;
}
$ret .= "\n";//jamesqi 2012-5
// $ret .= "</td>\n<td>";
}$ret .= '</td></tr></table>';
return $ret;
}
可以看到,将最好幾句中分列的td标簽去掉後,這個表格就成為單行單列的了。
评论