从sablog转到typecho后,数据都导入了,但是由于程序上的原因,有很多附件没有显示出来。Sablog对附件的处理是这样的:如果文章里有ABCD四个附件,文章里引用了AB两个附件,CD没在文章里引用,那么CD会自动在文章末尾显示出来。typecho对附件的处理方法:文章里引用了附件才会显示在前台,如果文章里没有引用附件的话,那这个附件只能在后台看见,前台是看不见附件的。

为此傻猫写了一个小工具,本人对PHP不是很熟悉,所以采用了本人最熟悉的delphi编写了一个小软件,直接对数据库进行处理。下载地址:typechoAtt.rar

处理原理:对文章对应的附件进行检查,如果文章里未引用附件的话,自动在文章末尾添加附件链接,如果已经引用的话则不处理。因为文章和附件内容都存在typecho_contents表中,所以所有操作都在这个表中进行,不会对其它表进行处理。

另附该软件源代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, MemDS, DBAccess, MyAccess, StdCtrls,IdStrings, ComCtrls;

type
  TForm1 = class(TForm)
    MyConnection1: TMyConnection;
    myqry_select: TMyQuery;
    myqry_update: TMyQuery;
    Button1: TButton;
    Memo1: TMemo;
    myqry_att: TMyQuery;
    Label1: TLabel;
    edt_server: TEdit;
    Label2: TLabel;
    edt_username: TEdit;
    Label3: TLabel;
    edt_password: TEdit;
    Label4: TLabel;
    edt_port: TEdit;
    Label5: TLabel;
    edt_database: TEdit;
    Label6: TLabel;
    StatusBar1: TStatusBar;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function IsImage(filename:string):Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.IsImage(filename:string):Boolean;
var atttile:string;
begin
            Result:=False;
           
            atttile:= UpperCase(filename);
            if Pos('.JPG',atttile)>0 then
            begin
                Result:=True;
                exit;
            end;

            if Pos('.JPEG',atttile)>0 then
            begin
                Result:=True;
                exit;
            end;

            if Pos('.BMP',atttile)>0 then
            begin
                Result:=True;
                exit;
            end;

            if Pos('.GIF',atttile)>0 then
            begin
                Result:=True;
                exit;
            end;

            if Pos('.PNG',atttile)>0 then
            begin
                Result:=True;
                exit;
            end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j,mycount,attcount:Integer;
    sql,sqlatt,mycid,mytxt,atttitle,attid,attname,myatt,temp,l,r,updatesql:string;
begin
    try
        MyConnection1.Server:=Trim(edt_server.Text);
        MyConnection1.Username:=Trim(edt_username.Text);
        MyConnection1.Password:=Trim(edt_password.Text);
        MyConnection1.Database:=Trim(edt_database.Text);
        MyConnection1.Port:=StrToInt(edt_port.Text);
        MyConnection1.Connected:=True;
    except
        Application.MessageBox('链接数据库失败,请检查配置信息!', '提示',
          MB_OK + MB_ICONSTOP);
        exit; 
    end;

    if not MyConnection1.Connected then exit;

 

    sql:='select cid,text from typecho_contents ';
    try
        //查询文章
        myqry_select.Close;
        myqry_select.SQL.Clear;
        myqry_select.SQL.Add(sql);
        myqry_select.Open;
        mycount:=myqry_select.RecordCount;
        for i:=1 to mycount do
        begin
            Button1.Caption:= IntToStr(i)+'/'+inttostr(mycount);
            mycid:=Trim(myqry_select.fieldbyname('cid').AsString);
            mytxt:=Trim(myqry_select.fieldbyname('text').AsString);
           
                  //查询附件内容
                  sqlatt:='select cid,title,text FROM `typecho_contents`  WHERE `order` ='+mycid+' AND (`type` LIKE ''attachment'') LIMIT 0 , 100 ';
                  myqry_att.Close;
                  myqry_att.SQL.Clear;
                  myqry_att.SQL.Add(sqlatt);
                  myqry_att.Open;
                  attcount:=myqry_att.RecordCount;
                  if attcount>0 then
                  begin
                      for j:=1 to attcount do
                      begin
                            myatt:=Trim(myqry_att.fieldbyname('text').AsString);
                            atttitle:= Trim(myqry_att.fieldbyname('title').AsString);
                            attid:=  Trim(myqry_att.fieldbyname('cid').AsString);

                            temp:='';

                            //;s:4:"path";s:49:"/attachments/b778f0603cc8b4ff2271b858289b3780.jpg";s:4:"size";i:61355
                            //把附件名称拆分出来
                            SplitString(myatt,'"path";s:',l,r);
                            SplitString(r,'";s:4:"size";',l,r);
                            SplitString(l,'"',l,r);
                            attname:=r;

                            if Pos(attname,mytxt)=0 then
                            begin
                                //对附件进行处理,图片和程序处理方式不一样
                                  if IsImage(atttitle) then
                                  temp:='<br><a href="https://samool.com/attachment/'+attid+'/" title="' + atttitle + '"><img src="' + attname + '" alt="' + atttitle + '" /></a><br>'
                                  else
                                  temp:='<br><a href="https://samool.com/attachment/'+attid+'/" title="' + atttitle + '">' + atttitle + '</a><br>' ;

                                  Memo1.Lines.Add('cid='+mycid+' 文章《'+atttitle+'》已经更新!');
                                 //更新数据库
                                  if temp<>'' then
                                  begin
                                      updatesql:='update `typecho_contents` set text=concat(text,'''+temp+''') where cid='+mycid;
                                      myqry_update.Close;
                                      myqry_update.SQL.Clear;
                                      myqry_update.SQL.Add(updatesql);
                                      myqry_update.Execute;
                                  end; 
                            end;
                            myqry_att.FindNext;
                      end;

                  end;

              myqry_select.FindNext;

        end;
    except
        ShowMessage('err');
    end;

end;

end.

最后修改:2009 年 08 月 18 日
一分也是爱